]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
Add a destructor function to OSet_Destroy() which can be called for each
authorNicholas Nethercote <njn@valgrind.org>
Thu, 22 Dec 2005 06:20:59 +0000 (06:20 +0000)
committerNicholas Nethercote <njn@valgrind.org>
Thu, 22 Dec 2005 06:20:59 +0000 (06:20 +0000)
node.

git-svn-id: svn://svn.valgrind.org/valgrind/trunk@5402

coregrind/m_oset.c
include/pub_tool_oset.h
memcheck/tests/oset_test.c

index 0730ac60ddc80fdcdf1fbfdd69dd36ffb2e74afd..810890f4d51c4e843a1452556430bf34d8d4f59f 100644 (file)
@@ -294,7 +294,7 @@ AvlTree* VG_(OSet_Create)(OffT _keyOff, OSetCmp_t _cmp,
 }
 
 // Destructor, frees up all memory held by remaining nodes.
-void VG_(OSet_Destroy)(AvlTree* t)
+void VG_(OSet_Destroy)(AvlTree* t, OSetNodeDestroy_t destroyNode)
 {
    AvlNode* n;
    Int i, sz = 0;
@@ -317,6 +317,7 @@ void VG_(OSet_Destroy)(AvlTree* t)
          if (n->right) stackPush(t, n->right, 1);
          break;
       case 3:
+         if (destroyNode) destroyNode(n);
          t->free(n);
          sz++;
          break;
index 9f1882082bd61c514811a9bf98cf0d1c0a79f5cf..05c8a6f569d57ed891b9b9e65629834c2ddea4dc 100644 (file)
 typedef struct _OSet     OSet;
 typedef struct _OSetNode OSetNode;
 
-typedef Int   (*OSetCmp_t)   ( void* key, void* elem );
-typedef void* (*OSetAlloc_t) ( SizeT szB );
-typedef void  (*OSetFree_t)  ( void* p );
+typedef Int   (*OSetCmp_t)         ( void* key, void* elem );
+typedef void* (*OSetAlloc_t)       ( SizeT szB );
+typedef void  (*OSetFree_t)        ( void* p );
+typedef void  (*OSetNodeDestroy_t) ( void* elem );
 
 /*--------------------------------------------------------------------*/
 /*--- Creating and destroying OSets and OSet members               ---*/
@@ -85,7 +86,9 @@ typedef void  (*OSetFree_t)  ( void* p );
 //   If cmp is NULL, keyOff must be zero.  This is checked.
 //
 // * Destroy: frees all nodes in the table, plus the memory used by
-//   the table itself.
+//   the table itself.  The passed-in function is called on each node first
+//   to allow the destruction of any attached resources;  if NULL it is not
+//   called.
 //
 // * AllocNode: Allocate and zero memory for a node to go into the OSet.
 //   Uses the alloc function given to VG_(OSet_Create)() to allocated a node
@@ -101,7 +104,7 @@ typedef void  (*OSetFree_t)  ( void* p );
 
 extern OSet* VG_(OSet_Create)    ( OffT keyOff, OSetCmp_t cmp,
                                    OSetAlloc_t alloc, OSetFree_t free );
-extern void  VG_(OSet_Destroy)   ( OSet* os );
+extern void  VG_(OSet_Destroy)   ( OSet* os, OSetNodeDestroy_t destroyNode );
 extern void* VG_(OSet_AllocNode) ( OSet* os, SizeT elemSize );
 extern void  VG_(OSet_FreeNode)  ( OSet* os, void* elem );
 
index 1b9ff1fcedcd0ed1bf140bfc61a9eb32dff6bf8c..6c06f2e91f73362d2e4a9abf18a3d2550213e192 100644 (file)
@@ -182,7 +182,7 @@ void example1(void)
    OSet_Print(oset1, "foo", intToStr);
 
    // Destroy the OSet
-   VG_(OSet_Destroy)(oset1);
+   VG_(OSet_Destroy)(oset1, NULL);
 }
 
 
@@ -353,7 +353,7 @@ void example2(void)
    }
 
    // Destroy the OSet
-   VG_(OSet_Destroy)(oset2);
+   VG_(OSet_Destroy)(oset2, NULL);
 }
 
 //-----------------------------------------------------------------------