[PATCH v2 05/11] gdbserver: convert free_register_cache into a destructor of regcache

Tankut Baris Aktemur
2024-12-30T10:56:24Z

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.
---
gdbserver/gdbthread.h | 2 +-
gdbserver/regcache.cc | 15 +++++----------
gdbserver/regcache.h | 7 +++----
gdbserver/server.cc | 8 +++-----
4 files changed, 12 insertions(+), 20 deletions(-)

diff --git a/gdbserver/gdbthread.h b/gdbserver/gdbthread.h
index d7b5bc3e1538bde8eafd0f3253706924d7330a21..d3459582a1fa503ad2c4a0d5bb448e920c36b2e3 100644
--- a/gdbserver/gdbthread.h
+++ b/gdbserver/gdbthread.h
@@ -33,7 +33,7 @@ struct thread_info : public intrusive_list_node<thread_info>

~thread_info ()
{
- free_register_cache (m_regcache);
+ delete m_regcache;
}

/* Return the process owning this thread. */
diff --git a/gdbserver/regcache.cc b/gdbserver/regcache.cc
index 11957af351d476d47eb4b9eacb810ac2f6aded40..e33c0c650dbcb7c3d28930d5bc4099932e13ec13 100644
--- a/gdbserver/regcache.cc
+++ b/gdbserver/regcache.cc
@@ -146,16 +146,11 @@ regcache::regcache (const target_desc *tdesc)
tdesc->reg_defs.size ());
}

-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
@@ -250,7 +245,7 @@ free_register_cache_thread (thread_info *thread)
if (regcache != NULL)
{
regcache_invalidate_thread (thread);
- free_register_cache (regcache);
+ delete regcache;
thread->set_regcache (nullptr);
}
}
diff --git a/gdbserver/regcache.h b/gdbserver/regcache.h
index ea6c26a37a07bf6d336d6bdf327d543fae0b6370..b371b5a2da6fcc5df5dc0037f5fb980a92ff9aca 100644
--- a/gdbserver/regcache.h
+++ b/gdbserver/regcache.h
@@ -48,6 +48,9 @@ struct regcache : public reg_buffer_common

/* Constructors. */
regcache (const target_desc *tdesc);
+
+ /* Destructor. */
+ ~regcache ();
#endif
regcache (const target_desc *tdesc, unsigned char *regbuf);

@@ -74,10 +77,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 (thread_info *);
diff --git a/gdbserver/server.cc b/gdbserver/server.cc
index a4d654de00adf014e28b0ce022ba8a5886ee77ce..509e7e2ffb8c67c18fac011c485bbd366a8d0370 100644
--- a/gdbserver/server.cc
+++ b/gdbserver/server.cc
@@ -4703,15 +4703,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
{

--
2.34.1

Intel Deutschland GmbH
Registered Address: Am Campeon 10, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de
Managing Directors: Sean Fennelly, Jeffrey Schneiderman, Tiffany Doon Silva