[12/12] Use cleanup_function in regcache.c

Message ID 20190109033426.16062-13-tom@tromey.com
State New
Headers show
Series
  • remove some cleanups using a cleanup function
Related show

Commit Message

Tom Tromey Jan. 9, 2019, 3:34 a.m.
This removes the regcache_invalidator class in favor of a
cleanup_function.  This seems like an improvement (albeit a minor one)
because regcache_invalidator is only used in a single spot.

gdb/ChangeLog
2019-01-08  Tom Tromey  <tom@tromey.com>

	* regcache.c (class regcache_invalidator): Remove.
	(regcache::raw_write): Use cleanup_function.
---
 gdb/ChangeLog  |  5 +++++
 gdb/regcache.c | 41 ++++++++---------------------------------
 2 files changed, 13 insertions(+), 33 deletions(-)

-- 
2.17.2

Patch

diff --git a/gdb/regcache.c b/gdb/regcache.c
index c51ef771be..47339a0bce 100644
--- a/gdb/regcache.c
+++ b/gdb/regcache.c
@@ -28,6 +28,7 @@ 
 #include "reggroups.h"
 #include "observable.h"
 #include "regset.h"
+#include "common/cleanup-function.h"
 #include <forward_list>
 
 /*
@@ -219,37 +220,6 @@  reg_buffer::arch () const
   return m_descr->gdbarch;
 }
 
-/* Cleanup class for invalidating a register.  */
-
-class regcache_invalidator
-{
-public:
-
-  regcache_invalidator (struct regcache *regcache, int regnum)
-    : m_regcache (regcache),
-      m_regnum (regnum)
-  {
-  }
-
-  ~regcache_invalidator ()
-  {
-    if (m_regcache != nullptr)
-      m_regcache->invalidate (m_regnum);
-  }
-
-  DISABLE_COPY_AND_ASSIGN (regcache_invalidator);
-
-  void release ()
-  {
-    m_regcache = nullptr;
-  }
-
-private:
-
-  struct regcache *m_regcache;
-  int m_regnum;
-};
-
 /* Return  a pointer to register REGNUM's buffer cache.  */
 
 gdb_byte *
@@ -769,13 +739,18 @@  regcache::raw_write (int regnum, const gdb_byte *buf)
 
   /* Invalidate the register after it is written, in case of a
      failure.  */
-  regcache_invalidator invalidator (this, regnum);
+  auto do_invalidate
+    = [=] ()
+      {
+	this->invalidate (regnum);
+      };
+  cleanup_function invalidator (do_invalidate);
 
   target_store_registers (this, regnum);
 
   /* The target did not throw an error so we can discard invalidating
      the register.  */
-  invalidator.release ();
+  invalidator.cancel ();
 }
 
 void