On 2/28/23 06:27, Tankut Baris Aktemur via Gdb-patches wrote:
> This is a refactoring that converts the `init_register_cache` function
> to a method of the regcache struct. During this conversion, we also
> change the return type to void.
I think this is fine. However, with a bit more effort, I think
this could actually become a constructor, which would be nicer than
having to call an initialize function.
The only more "tricky" spot would be the use of regcache in
fast_tracepoint_ctx. I think you could replace the regcache_initted and
regcache fields with an std::optional. When instantiating the regcache,
you would emplace the optional, which would call the constructor at that
point. fast_tracepoint_ctx is only ever allocated as a local variable
in gdb_collect, so using a gdb::optional there should be fine.
Another improvement to try (for a subsequent patch) would be to pass an
array_view to the initialize method (or constructor), and assert that
the register size described by the tdesc matches the size of the
buffer array_view passed.
Simon
@@ -113,10 +113,9 @@ regcache_invalidate (void)
#endif
-struct regcache *
-init_register_cache (struct regcache *regcache,
- const struct target_desc *tdesc,
- unsigned char *regbuf)
+void
+regcache::initialize (const target_desc *tdesc,
+ unsigned char *regbuf)
{
if (regbuf == NULL)
{
@@ -125,13 +124,13 @@ init_register_cache (struct regcache *regcache,
created, in case there are registers the target never
fetches. This way they'll read as zero instead of
garbage. */
- regcache->tdesc = tdesc;
- regcache->registers
+ this->tdesc = tdesc;
+ this->registers
= (unsigned char *) xcalloc (1, tdesc->registers_size);
- regcache->registers_owned = 1;
- regcache->register_status
+ this->registers_owned = 1;
+ this->register_status
= (unsigned char *) xmalloc (tdesc->reg_defs.size ());
- memset ((void *) regcache->register_status, REG_UNAVAILABLE,
+ memset ((void *) this->register_status, REG_UNAVAILABLE,
tdesc->reg_defs.size ());
#else
gdb_assert_not_reached ("can't allocate memory from the heap");
@@ -139,17 +138,15 @@ init_register_cache (struct regcache *regcache,
}
else
{
- regcache->tdesc = tdesc;
- regcache->registers = regbuf;
- regcache->registers_owned = 0;
+ this->tdesc = tdesc;
+ this->registers = regbuf;
+ this->registers_owned = 0;
#ifndef IN_PROCESS_AGENT
- regcache->register_status = NULL;
+ this->register_status = nullptr;
#endif
}
- regcache->registers_valid = 0;
-
- return regcache;
+ this->registers_valid = 0;
}
#ifndef IN_PROCESS_AGENT
@@ -160,8 +157,9 @@ new_register_cache (const struct target_desc *tdesc)
struct regcache *regcache = new struct regcache;
gdb_assert (tdesc->registers_size != 0);
+ regcache->initialize (tdesc, nullptr);
- return init_register_cache (regcache, tdesc, NULL);
+ return regcache;
}
void
@@ -46,6 +46,9 @@ struct regcache : public reg_buffer_common
unsigned char *register_status = nullptr;
#endif
+ /* Init the regcache data. */
+ void initialize (const target_desc *tdesc, unsigned char *regbuf);
+
/* See gdbsupport/common-regcache.h. */
enum register_status get_register_status (int regnum) const override;
@@ -59,10 +62,6 @@ struct regcache : public reg_buffer_common
bool raw_compare (int regnum, const void *buf, int offset) const override;
};
-struct regcache *init_register_cache (struct regcache *regcache,
- const struct target_desc *tdesc,
- unsigned char *regbuf);
-
void regcache_cpy (struct regcache *dst, struct regcache *src);
/* Create a new register cache for INFERIOR. */
@@ -4706,7 +4706,7 @@ get_context_regcache (struct tracepoint_hit_ctx *ctx)
if (!fctx->regcache_initted)
{
fctx->regcache_initted = 1;
- init_register_cache (&fctx->regcache, ipa_tdesc, fctx->regspace);
+ fctx->regcache.initialize (ipa_tdesc, fctx->regspace);
supply_regblock (&fctx->regcache, NULL);
supply_fast_tracepoint_registers (&fctx->regcache, fctx->regs);
}
@@ -4721,7 +4721,7 @@ get_context_regcache (struct tracepoint_hit_ctx *ctx)
if (!sctx->regcache_initted)
{
sctx->regcache_initted = 1;
- init_register_cache (&sctx->regcache, ipa_tdesc, sctx->regspace);
+ sctx->regcache.initialize (ipa_tdesc, sctx->regspace);
supply_regblock (&sctx->regcache, NULL);
/* Pass down the tracepoint address, because REGS doesn't
include the PC, but we know what it must have been. */
@@ -4799,8 +4799,7 @@ do_action_at_tracepoint (struct tracepoint_hit_ctx *ctx,
/* Wrap the regblock in a register cache (in the stack, we
don't want to malloc here). */
- init_register_cache (&tregcache, context_regcache->tdesc,
- regspace + 1);
+ tregcache.initialize (context_regcache->tdesc, regspace + 1);
/* Copy the register data to the regblock. */
regcache_cpy (&tregcache, context_regcache);
@@ -5207,7 +5206,7 @@ traceframe_get_pc (struct traceframe *tframe)
if (dataptr == NULL)
return 0;
- init_register_cache (®cache, tdesc, dataptr);
+ regcache.initialize (tdesc, dataptr);
return regcache_read_pc (®cache);
}