|
From: Bart V. A. <bar...@gm...> - 2008-03-24 18:56:05
|
Hello,
VG_(OSetGen_Create)() has four arguments, where the last two are
pointers to an allocation and a deallocation function. These last two
functions are not only used to allocate and deallocate tree nodes, but
also for allocating and deallocating the OSet itself. I'd like to
build an OSet with tree nodes that store a pointer to a
reference-counted bitmap, with a deallocation function that does not
only deallocate the node memory but also decrements the reference
count. It would be inelegant to make the allocation and deallocation
functions try to figure out what kind of item (node or OSet) they are
allocating or deallocating. One solution it to change the OSet
implementation such that it always uses VG_(malloc) / VG_(free) for
the OSet, another solution is to add an extra OSet creation function
that accepts six arguments instead of four (where the two extra
arguments are pointers to the allocation and deallocation function for
the OSet itself).
The patch below implements the first option.
Comments are welcome.
Bart.
Index: coregrind/m_oset.c
===================================================================
--- coregrind/m_oset.c (.../trunk/coregrind/m_oset.c) (revision 7758)
+++ coregrind/m_oset.c (.../branches/DRDDEV/coregrind/m_oset.c) (working copy)
@@ -79,6 +79,7 @@
#include "pub_core_libcbase.h"
#include "pub_core_libcassert.h"
#include "pub_core_libcprint.h"
+#include "pub_core_mallocfree.h"
#include "pub_core_oset.h"
/*--------------------------------------------------------------------*/
@@ -294,7 +295,7 @@
vg_assert(_free);
if (!_cmp) vg_assert(0 == _keyOff); // If no cmp, offset must be zero
- t = _alloc(sizeof(AvlTree));
+ t = VG_(malloc)(sizeof(AvlTree));
t->keyOff = _keyOff;
t->cmp = _cmp;
t->alloc = _alloc;
@@ -344,7 +345,7 @@
vg_assert(sz == t->nElems);
/* Free the AvlTree itself. */
- t->free(t);
+ VG_(free)(t);
}
void VG_(OSetWord_Destroy)(AvlTree* t)
@@ -368,6 +369,11 @@
t->free( node_of_elem(e) );
}
+void* VG_(OSetGen_NodeToElem)(void* node)
+{
+ return elem_of_node(node);
+}
+
/*--------------------------------------------------------------------*/
/*--- Insertion ---*/
/*--------------------------------------------------------------------*/
Index: include/pub_tool_oset.h
===================================================================
--- include/pub_tool_oset.h (.../trunk/include) (revision 7758)
+++ include/pub_tool_oset.h (.../branches/DRDDEV/include) (working copy)
@@ -187,6 +187,7 @@
extern void VG_(OSetGen_Destroy) ( OSet* os );
extern void* VG_(OSetGen_AllocNode) ( OSet* os, SizeT elemSize );
extern void VG_(OSetGen_FreeNode) ( OSet* os, void* elem );
+extern void* VG_(OSetGen_NodeToElem)( void* node );
/*--------------------------------------------------------------------*/
/*--- Operations on OSets (Gen) ---*/
Index: memcheck/tests/oset_test.c
===================================================================
--- memcheck/tests/oset_test.c (.../trunk/memcheck) (revision 7758)
+++ memcheck/tests/oset_test.c (.../branches/DRDDEV/memcheck) (working copy)
@@ -28,6 +28,9 @@
#define vgPlain_memset memset
#define vgPlain_memcpy memcpy
+static void* VG_(malloc)( SizeT nbytes ) { return malloc(nbytes); }
+static void VG_(free) ( void* p ) { return free(p); }
+
#include "coregrind/m_oset.c"
#define NN 1000 // Size of OSets being created
|