gdbserver: convert init_register_cache and new_register_cache into constructors
authorTankut Baris Aktemur <tankut.baris.aktemur@intel.com>
Wed, 29 Jan 2025 09:50:30 +0000 (10:50 +0100)
committerTankut Baris Aktemur <tankut.baris.aktemur@intel.com>
Wed, 29 Jan 2025 10:17:33 +0000 (11:17 +0100)
This is a refactoring that converts

  init_register_cache (struct regcache *regcache,
                       const struct target_desc *tdesc,
                       unsigned char *regbuf)

into the constructor

  regcache (const target_desc *tdesc, unsigned char *regbuf)

and converts

  new_register_cache (const struct target_desc *tdesc)

into the constructor

  regcache (const target_desc *tdesc)

Also use DISABLE_COPY_AND_ASSIGN for additional compile-time safety.

Tested by rebuilding gdbserver with '--enable-inprocess-agent=no' and
with '--enable-inprocess-agent=yes'.

Approved-By: Simon Marchi <simon.marchi@efficios.com>
gdbserver/regcache.cc
gdbserver/regcache.h
gdbserver/server.cc
gdbserver/tracepoint.cc

index 2c491d64e5ec1a54e6ca82cde4b29fa2f84ded57..557e38572a88d97d6652f55801a75a2cb3220ff8 100644 (file)
@@ -42,7 +42,7 @@ get_thread_regcache (thread_info *thread, bool fetch)
 
       gdb_assert (proc->tdesc != NULL);
 
-      regcache = new_register_cache (proc->tdesc);
+      regcache = new struct regcache (proc->tdesc);
       thread->set_regcache (regcache);
     }
 
@@ -112,55 +112,29 @@ regcache_invalidate (void)
 
 #endif
 
-struct regcache *
-init_register_cache (struct regcache *regcache,
-                    const struct target_desc *tdesc,
-                    unsigned char *regbuf)
+regcache::regcache (const target_desc *tdesc, unsigned char *regbuf)
+  : tdesc (tdesc), registers (regbuf)
 {
-  if (regbuf == NULL)
-    {
-#ifndef IN_PROCESS_AGENT
-      /* Make sure to zero-initialize the register cache when it is
-        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
-       = (unsigned char *) xcalloc (1, tdesc->registers_size);
-      regcache->registers_owned = true;
-      regcache->register_status
-       = (unsigned char *) xmalloc (tdesc->reg_defs.size ());
-      memset ((void *) regcache->register_status, REG_UNAVAILABLE,
-             tdesc->reg_defs.size ());
-#else
-      gdb_assert_not_reached ("can't allocate memory from the heap");
-#endif
-    }
-  else
-    {
-      regcache->tdesc = tdesc;
-      regcache->registers = regbuf;
-      regcache->registers_owned = false;
-#ifndef IN_PROCESS_AGENT
-      regcache->register_status = NULL;
-#endif
-    }
-
-  regcache->registers_fetched = false;
-
-  return regcache;
+  gdb_assert (regbuf != nullptr);
 }
 
 #ifndef IN_PROCESS_AGENT
 
-struct regcache *
-new_register_cache (const struct target_desc *tdesc)
+regcache::regcache (const target_desc *tdesc)
+  : tdesc (tdesc), registers_owned (true)
 {
-  struct regcache *regcache = new struct regcache;
-
   gdb_assert (tdesc->registers_size != 0);
 
-  return init_register_cache (regcache, tdesc, NULL);
+  /* Make sure to zero-initialize the register cache when it is
+     created, in case there are registers the target never
+     fetches.  This way they'll read as zero instead of
+     garbage.  */
+  this->registers
+    = (unsigned char *) xcalloc (1, tdesc->registers_size);
+  this->register_status
+    = (unsigned char *) xmalloc (tdesc->reg_defs.size ());
+  memset ((void *) this->register_status, REG_UNAVAILABLE,
+         tdesc->reg_defs.size ());
 }
 
 void
index 6fc04453a64e8edec8ad53ddb25ff4a5ede544c3..2fab5d01fc4fc3cbed7735151989d7ca98ec45c8 100644 (file)
@@ -45,8 +45,21 @@ struct regcache : public reg_buffer_common
 #ifndef IN_PROCESS_AGENT
   /* One of REG_UNAVAILABLE or REG_VALID.  */
   unsigned char *register_status = nullptr;
+
+  /* Construct a regcache using the register layout described by TDESC.
+
+     The regcache dynamically allocates its register buffer.  */
+  regcache (const target_desc *tdesc);
 #endif
 
+  /* Construct a regcache using the register layout described by TDESC
+     and REGBUF as the register buffer.
+
+     The regcache does *not* take ownership of the buffer.  */
+  regcache (const target_desc *tdesc, unsigned char *regbuf);
+
+  DISABLE_COPY_AND_ASSIGN (regcache);
+
   /* See gdbsupport/common-regcache.h.  */
   enum register_status get_register_status (int regnum) const override;
 
@@ -72,14 +85,6 @@ struct regcache : public reg_buffer_common
   void copy_from (regcache *src);
 };
 
-struct regcache *init_register_cache (struct regcache *regcache,
-                                     const struct target_desc *tdesc,
-                                     unsigned char *regbuf);
-
-/* Create a new register cache for INFERIOR.  */
-
-struct regcache *new_register_cache (const struct target_desc *tdesc);
-
 regcache *get_thread_regcache (thread_info *thread, bool fetch = true);
 
 /* Release all memory associated with the register cache for INFERIOR.  */
index 33ff847bc5ce87d0ed11f15531dccb6d40e26381..779af6d4e1107f00c75fd56d84c0d6c3ed76403f 100644 (file)
@@ -4678,7 +4678,7 @@ process_serial_event (void)
       if (cs.current_traceframe >= 0)
        {
          struct regcache *regcache
-           = new_register_cache (current_target_desc ());
+           = new struct regcache (current_target_desc ());
 
          if (fetch_traceframe_registers (cs.current_traceframe,
                                          regcache, -1) == 0)
index d1df12efd076698a6dde9c3b6dbfbaad5b2b6e55..0ed8e8f99948fb5047940a5369c27187152b6922 100644 (file)
@@ -1195,15 +1195,15 @@ struct fast_tracepoint_ctx : public tracepoint_hit_ctx
   {
     const struct target_desc *ipa_tdesc = get_ipa_tdesc (ipa_tdesc_idx);
 
-    if (!this->m_regcache_initted)
+    if (!this->m_regcache.has_value ())
       {
-       this->m_regcache_initted = 1;
-       init_register_cache (&this->m_regcache, ipa_tdesc, this->regspace);
-       supply_regblock (&this->m_regcache, nullptr);
-       supply_fast_tracepoint_registers (&this->m_regcache, this->regs);
+       this->m_regcache.emplace (ipa_tdesc, this->regspace);
+       supply_regblock (&this->m_regcache.value (), nullptr);
+       supply_fast_tracepoint_registers (&this->m_regcache.value (),
+                                         this->regs);
       }
 
-    return &this->m_regcache;
+    return &this->m_regcache.value ();
   }
 
   /* The buffer space M_REGCACHE uses.  We use a separate buffer
@@ -1221,8 +1221,7 @@ private:
 
   /* The regcache corresponding to the registers state at the time of
      the tracepoint hit.  Initialized lazily, from REGS.  */
-  struct regcache m_regcache;
-  int m_regcache_initted = 0;
+  std::optional<struct regcache> m_regcache;
 };
 
 #else
@@ -4430,7 +4429,6 @@ do_action_at_tracepoint (struct tracepoint_hit_ctx *ctx,
     case 'R':
       {
        unsigned char *regspace;
-       struct regcache tregcache;
        int regcache_size;
 
        trace_debug ("Want to collect registers");
@@ -4450,8 +4448,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);
+       regcache tregcache (context_regcache->tdesc, regspace + 1);
 
        /* Copy the register data to the regblock.  */
        tregcache.copy_from (context_regcache);
@@ -4836,7 +4833,6 @@ fetch_traceframe_registers (int tfnum, struct regcache *regcache, int regnum)
 static CORE_ADDR
 traceframe_get_pc (struct traceframe *tframe)
 {
-  struct regcache regcache;
   unsigned char *dataptr;
   const struct target_desc *tdesc = current_target_desc ();
 
@@ -4844,7 +4840,7 @@ traceframe_get_pc (struct traceframe *tframe)
   if (dataptr == NULL)
     return 0;
 
-  init_register_cache (&regcache, tdesc, dataptr);
+  regcache regcache (tdesc, dataptr);
   return regcache_read_pc (&regcache);
 }
 
This page took 0.049842 seconds and 5 git commands to generate.