[08/26] gdbserver: convert free_register_cache into a destructor of regcache

Message ID dfbc693a0b7edb8b1d51f4611a217332fa57a431.1677582744.git.tankut.baris.aktemur@intel.com
State New
Headers
Series gdbserver: refactor regcache and allow gradually populating |

Commit Message

Tankut Baris Aktemur Feb. 28, 2023, 11:28 a.m. UTC
  Convert the `free_register_cache` function into a destructor of the
regcache struct.  In one place, we completely remove the call to free
the regcache object by stack-allocating the object.

We also delete the copy constructor explicitly to prevent the risk of
copying regcaches and then double-freeing buffers when they are
destructed.
---
 gdbserver/gdbthread.h |  2 +-
 gdbserver/regcache.cc | 15 +++++----------
 gdbserver/regcache.h  |  9 +++++----
 gdbserver/server.cc   |  8 +++-----
 4 files changed, 14 insertions(+), 20 deletions(-)
  

Patch

diff --git a/gdbserver/gdbthread.h b/gdbserver/gdbthread.h
index 493e1dbf6cb..cfd81870af9 100644
--- a/gdbserver/gdbthread.h
+++ b/gdbserver/gdbthread.h
@@ -35,7 +35,7 @@  struct thread_info
 
   ~thread_info ()
   {
-    free_register_cache (this->regcache_data);
+    delete this->regcache_data;
   }
 
   /* The id of this thread.  */
diff --git a/gdbserver/regcache.cc b/gdbserver/regcache.cc
index be01df342bb..7b6337166ad 100644
--- a/gdbserver/regcache.cc
+++ b/gdbserver/regcache.cc
@@ -165,16 +165,11 @@  regcache::regcache (const target_desc *tdesc)
   initialize (tdesc, nullptr);
 }
 
-void
-free_register_cache (struct regcache *regcache)
+regcache::~regcache ()
 {
-  if (regcache)
-    {
-      if (regcache->registers_owned)
-	free (regcache->registers);
-      free (regcache->register_status);
-      delete regcache;
-    }
+  if (registers_owned)
+    free (registers);
+  free (register_status);
 }
 
 #endif
@@ -280,7 +275,7 @@  free_register_cache_thread (struct thread_info *thread)
   if (regcache != NULL)
     {
       regcache_invalidate_thread (thread);
-      free_register_cache (regcache);
+      delete regcache;
       set_thread_regcache_data (thread, NULL);
     }
 }
diff --git a/gdbserver/regcache.h b/gdbserver/regcache.h
index 32b3a8dccfc..614d5a2561f 100644
--- a/gdbserver/regcache.h
+++ b/gdbserver/regcache.h
@@ -51,6 +51,11 @@  struct regcache : public reg_buffer_common
   /* Constructors.  */
   regcache () = default;
   regcache (const target_desc *tdesc);
+  regcache (const regcache &rhs) = delete;
+  regcache &operator= (const regcache &rhs) = delete;
+
+  /* Deconstructor.  */
+  virtual ~regcache ();
 #endif
 
   /* Init the regcache data.  */
@@ -77,10 +82,6 @@  struct regcache : public reg_buffer_common
 
 regcache *get_thread_regcache (thread_info *thread, bool fetch = true);
 
-/* Release all memory associated with the register cache for INFERIOR.  */
-
-void free_register_cache (struct regcache *regcache);
-
 /* Invalidate cached registers for one thread.  */
 
 void regcache_invalidate_thread (struct thread_info *);
diff --git a/gdbserver/server.cc b/gdbserver/server.cc
index 4be12ec1c66..521f13c07d7 100644
--- a/gdbserver/server.cc
+++ b/gdbserver/server.cc
@@ -4237,15 +4237,13 @@  process_serial_event (void)
       require_running_or_break (cs.own_buf);
       if (cs.current_traceframe >= 0)
 	{
-	  struct regcache *regcache
-	    = new struct regcache (current_target_desc ());
+	  regcache a_regcache (current_target_desc ());
 
 	  if (fetch_traceframe_registers (cs.current_traceframe,
-					  regcache, -1) == 0)
-	    registers_to_string (regcache, cs.own_buf);
+					  &a_regcache, -1) == 0)
+	    registers_to_string (&a_regcache, cs.own_buf);
 	  else
 	    write_enn (cs.own_buf);
-	  free_register_cache (regcache);
 	}
       else
 	{