Patchwork [trans-mem] Adjust an alloc/free pair to new/delete in libitm

login
register
mail settings
Submitter Richard Henderson
Date June 16, 2010, 3:32 p.m.
Message ID <4C18EEA5.9090503@redhat.com>
Download mbox | patch
Permalink /patch/55897/
State New
Headers show

Comments

Richard Henderson - June 16, 2010, 3:32 p.m.
I noticed this while changing other things -- a bit of
incomplete transformation to C++.  I don't *think* it
really makes a difference, since gtm_transaction does
not have virtual methods.  But it seems cleaner to let
the language do its thing.


r~


        * alloc.cc (struct gtm_alloc_action): Move definition ...
        * libitm_i.h: ... here.
        (class gtm_transaction): Declare new and delete.
        * beginend.cc (alloc_tx): Reformulate as operator new.
        (free_tx): Reformulate as operator delete.
        * config/generic/tls.h (gtm_thread): Change free_tx member to void *.

Patch

diff --git a/libitm/alloc.cc b/libitm/alloc.cc
index d0f63f8..5c70144 100644
--- a/libitm/alloc.cc
+++ b/libitm/alloc.cc
@@ -26,13 +26,6 @@ 
 
 namespace GTM HIDDEN {
 
-struct gtm_alloc_action
-{
-  void (*free_fn)(void *);
-  bool allocated;
-};
-
-
 void
 gtm_transaction::record_allocation (void *ptr, void (*free_fn)(void *))
 {
diff --git a/libitm/beginend.cc b/libitm/beginend.cc
index 64a5bbc..f50503e 100644
--- a/libitm/beginend.cc
+++ b/libitm/beginend.cc
@@ -40,21 +40,23 @@  static _ITM_transactionId_t global_tid;
 
 /* Allocate a transaction structure.  Reuse an old one if possible.  */
 
-static gtm_transaction *
-alloc_tx (void)
+void *
+GTM::gtm_transaction::operator new (size_t s)
 {
-  gtm_transaction *tx;
   gtm_thread *thr = gtm_thr ();
+  void *tx;
+
+  assert(s == sizeof(gtm_transaction));
 
   if (thr->free_tx_count == 0)
-    tx = static_cast<gtm_transaction *>(xmalloc (sizeof (gtm_transaction)));
+    tx = xmalloc (sizeof (gtm_transaction));
   else
     {
       thr->free_tx_count--;
       tx = thr->free_tx[thr->free_tx_idx];
       thr->free_tx_idx = (thr->free_tx_idx + 1) % gtm_thread::MAX_FREE_TX;
     }
-  memset (tx, 0, sizeof (*tx));
+  memset (tx, 0, sizeof (gtm_transaction));
 
   return tx;
 }
@@ -64,8 +66,8 @@  alloc_tx (void)
    as the jmpbuf is used immediately after calling this function.  Thus
    the requirement that this queue be per-thread.  */
 
-static void
-free_tx (gtm_transaction *tx)
+void
+GTM::gtm_transaction::operator delete(void *tx)
 {
   gtm_thread *thr = gtm_thr ();
   unsigned idx
@@ -92,7 +94,7 @@  GTM::gtm_transaction::begin_transaction (uint32_t prop, const gtm_jmpbuf *jb)
 
   setup_gtm_thr ();
 
-  tx = alloc_tx ();
+  tx = new gtm_transaction;
 
   tx->prop = prop;
   tx->prev = gtm_tx();
@@ -185,7 +187,7 @@  _ITM_abortTransaction (_ITM_abortReason reason)
     gtm_transaction::serial_lock.read_unlock ();
 
   set_gtm_tx (tx->prev);
-  free_tx (tx);
+  delete tx;
 
   GTM_longjmp (&tx->jb, a_abortTransaction | a_restoreLiveVariables, tx->prop);
 }
@@ -211,7 +213,7 @@  GTM::gtm_transaction::trycommit_and_finalize ()
     {
       gtm_disp()->fini ();
       set_gtm_tx (this->prev);
-      free_tx (this);
+      delete this;
       if (this->state & gtm_transaction::STATE_SERIAL)
 	gtm_transaction::serial_lock.write_unlock ();
       else
diff --git a/libitm/config/generic/tls.h b/libitm/config/generic/tls.h
index d6c2bc8..d1504e2 100644
--- a/libitm/config/generic/tls.h
+++ b/libitm/config/generic/tls.h
@@ -47,7 +47,7 @@  struct gtm_thread
   static const unsigned MAX_FREE_TX = 8;
 
   // A queue of free gtm_transaction structs.
-  gtm_transaction *free_tx[MAX_FREE_TX];
+  void *free_tx[MAX_FREE_TX];
   unsigned free_tx_idx, free_tx_count;
 
   // The value returned by _ITM_getThreadnum to identify this thread.
diff --git a/libitm/libitm_i.h b/libitm/libitm_i.h
index 66451e7..48b9bb3 100644
--- a/libitm/libitm_i.h
+++ b/libitm/libitm_i.h
@@ -149,8 +149,13 @@  enum gtm_restart_reason
   NUM_RESTARTS
 };
 
-// This type is private to alloc.c.
-struct gtm_alloc_action;
+// This type is private to alloc.c, but needs to be defined so that
+// the template used inside gtm_transaction can instantiate.
+struct gtm_alloc_action
+{
+  void (*free_fn)(void *);
+  bool allocated;
+};
 
 // This type is private to local.c.
 struct gtm_local_undo;
@@ -228,6 +233,9 @@  struct gtm_transaction
   bool trycommit_and_finalize ();
   void restart (gtm_restart_reason) ITM_NORETURN;
 
+  static void *operator new(size_t);
+  static void operator delete(void *);
+
   // Invoked from assembly language, thus the "asm" specifier on
   // the name, avoiding complex name mangling.
   static uint32_t begin_transaction(uint32_t, const gtm_jmpbuf *)