Introduced the functions DRD_(malloclike_block)() and DRD_(freelike_block)().
git-svn-id: svn://svn.valgrind.org/valgrind/trunk@9711 a5019735-40e9-0310-863c-91ae7b9d1cf9
diff --git a/drd/drd_malloc_wrappers.c b/drd/drd_malloc_wrappers.c
index 62da58e..ff0381a 100644
--- a/drd/drd_malloc_wrappers.c
+++ b/drd/drd_malloc_wrappers.c
@@ -50,14 +50,14 @@
/* Local variables. */
-static StartUsingMem DRD_(s_start_using_mem_callback);
-static StopUsingMem DRD_(s_stop_using_mem_callback);
+static StartUsingMem s_start_using_mem_callback;
+static StopUsingMem s_stop_using_mem_callback;
/* Stats ... */
-static SizeT DRD_(s_cmalloc_n_mallocs) = 0;
-static SizeT DRD_(s_cmalloc_n_frees) = 0;
-static SizeT DRD_(s_cmalloc_bs_mallocd) = 0;
+static SizeT s_cmalloc_n_mallocs = 0;
+static SizeT s_cmalloc_n_frees = 0;
+static SizeT s_cmalloc_bs_mallocd = 0;
/* Record malloc'd blocks. */
-static VgHashTable DRD_(s_malloc_list) = NULL;
+static VgHashTable s_malloc_list = NULL;
/*------------------------------------------------------------*/
@@ -65,7 +65,7 @@
/*------------------------------------------------------------*/
/** Allocate its shadow chunk, put it on the appropriate list. */
-static DRD_Chunk* DRD_(create_chunk)(ThreadId tid, Addr p, SizeT size)
+static DRD_Chunk* create_chunk(ThreadId tid, Addr p, SizeT size)
{
DRD_Chunk* mc = VG_(malloc)("drd.malloc_wrappers.cDC.1",
sizeof(DRD_Chunk));
@@ -81,15 +81,11 @@
/*------------------------------------------------------------*/
/* Allocate memory and note change in memory available */
-static
-__inline__
-void* DRD_(new_block)(ThreadId tid,
- SizeT size, SizeT align,
- Bool is_zeroed)
+static void* new_block(ThreadId tid, SizeT size, SizeT align, Bool is_zeroed)
{
Addr p;
- DRD_(s_cmalloc_n_mallocs) ++;
+ s_cmalloc_n_mallocs ++;
// Allocate and zero
p = (Addr)VG_(cli_malloc)(align, size);
@@ -97,56 +93,79 @@
return NULL;
}
if (is_zeroed) VG_(memset)((void*)p, 0, size);
- DRD_(s_start_using_mem_callback)(p, p + size, 0/*ec_uniq*/);
- // Only update this stat if allocation succeeded.
- DRD_(s_cmalloc_bs_mallocd) += size;
-
- VG_(HT_add_node)(DRD_(s_malloc_list), DRD_(create_chunk)(tid, p, size));
+ DRD_(malloclike_block)(tid, p, size);
return (void*)p;
}
+/**
+ * Store information about a memory block that has been allocated by
+ * malloc() or a malloc() replacement.
+ */
+void DRD_(malloclike_block)(const ThreadId tid, const Addr p, const SizeT size)
+{
+ tl_assert(p);
+
+ s_start_using_mem_callback(p, p + size, 0/*ec_uniq*/);
+
+ // Only update this stat if allocation succeeded.
+ s_cmalloc_bs_mallocd += size;
+
+ VG_(HT_add_node)(s_malloc_list, create_chunk(tid, p, size));
+}
+
static void* DRD_(malloc)(ThreadId tid, SizeT n)
{
- return DRD_(new_block)(tid, n, VG_(clo_alignment), /*is_zeroed*/False);
+ return new_block(tid, n, VG_(clo_alignment), /*is_zeroed*/False);
}
static void* DRD_(memalign)(ThreadId tid, SizeT align, SizeT n)
{
- return DRD_(new_block)(tid, n, align, /*is_zeroed*/False);
+ return new_block(tid, n, align, /*is_zeroed*/False);
}
static void* DRD_(calloc)(ThreadId tid, SizeT nmemb, SizeT size1)
{
- return DRD_(new_block)(tid, nmemb*size1, VG_(clo_alignment),
+ return new_block(tid, nmemb*size1, VG_(clo_alignment),
/*is_zeroed*/True);
}
-static __inline__ void DRD_(handle_free)(ThreadId tid, Addr p)
+/**
+ * Remove the information that was stored by DRD_(malloclike_block)() about
+ * a memory block.
+ */
+Bool DRD_(freelike_block)(const ThreadId tid, const Addr p)
{
DRD_Chunk* mc;
- DRD_(s_cmalloc_n_frees)++;
+ tl_assert(p);
- mc = VG_(HT_remove)(DRD_(s_malloc_list), (UWord)p);
- if (mc == NULL)
- {
- tl_assert(0);
- }
- else
+ s_cmalloc_n_frees++;
+
+ mc = VG_(HT_remove)(s_malloc_list, (UWord)p);
+ if (mc)
{
tl_assert(p == mc->data);
if (mc->size > 0)
- DRD_(s_stop_using_mem_callback)(mc->data, mc->size);
- VG_(cli_free)((void*)p);
+ s_stop_using_mem_callback(mc->data, mc->size);
VG_(free)(mc);
+ return True;
}
+ return False;
+}
+
+static void handle_free(ThreadId tid, Addr p)
+{
+ if (DRD_(freelike_block)(tid, p))
+ VG_(cli_free)((void*)p);
+ else
+ tl_assert(False);
}
static void DRD_(free)(ThreadId tid, void* p)
{
- DRD_(handle_free)(tid, (Addr)p);
+ handle_free(tid, (Addr)p);
}
static void* DRD_(realloc)(ThreadId tid, void* p_old, SizeT new_size)
@@ -155,12 +174,12 @@
void* p_new;
SizeT old_size;
- DRD_(s_cmalloc_n_frees) ++;
- DRD_(s_cmalloc_n_mallocs) ++;
- DRD_(s_cmalloc_bs_mallocd) += new_size;
+ s_cmalloc_n_frees ++;
+ s_cmalloc_n_mallocs ++;
+ s_cmalloc_bs_mallocd += new_size;
/* Remove the old block */
- mc = VG_(HT_remove)(DRD_(s_malloc_list), (UWord)p_old);
+ mc = VG_(HT_remove)(s_malloc_list, (UWord)p_old);
if (mc == NULL) {
tl_assert(0);
return NULL;
@@ -178,7 +197,7 @@
else if (old_size > new_size)
{
/* new size is smaller */
- DRD_(s_stop_using_mem_callback)(mc->data + new_size, old_size);
+ s_stop_using_mem_callback(mc->data + new_size, old_size);
mc->size = new_size;
mc->where = VG_(record_ExeContext)(tid, 0);
p_new = p_old;
@@ -196,12 +215,12 @@
VG_(memcpy)((void*)a_new, p_old, mc->size);
/* Free old memory */
- DRD_(s_stop_using_mem_callback)(mc->data, mc->size);
+ s_stop_using_mem_callback(mc->data, mc->size);
VG_(free)(mc);
// Allocate a new chunk.
- mc = DRD_(create_chunk)(tid, a_new, new_size);
- DRD_(s_start_using_mem_callback)(a_new, a_new + new_size,
+ mc = create_chunk(tid, a_new, new_size);
+ s_start_using_mem_callback(a_new, a_new + new_size,
0/*ec_uniq*/);
}
else
@@ -217,14 +236,14 @@
// will have removed and then re-added mc unnecessarily. But that's ok
// because shrinking a block with realloc() is (presumably) much rarer
// than growing it, and this way simplifies the growing case.
- VG_(HT_add_node)(DRD_(s_malloc_list), mc);
+ VG_(HT_add_node)(s_malloc_list, mc);
return p_new;
}
static void* DRD_(__builtin_new)(ThreadId tid, SizeT n)
{
- void* const result = DRD_(new_block)(tid, n, VG_(clo_alignment),
+ void* const result = new_block(tid, n, VG_(clo_alignment),
/*is_zeroed*/False);
//VG_(message)(Vg_DebugMsg, "__builtin_new(%d, %d) = %p", tid, n, result);
return result;
@@ -233,22 +252,22 @@
static void DRD_(__builtin_delete)(ThreadId tid, void* p)
{
//VG_(message)(Vg_DebugMsg, "__builtin_delete(%d, %p)", tid, p);
- DRD_(handle_free)(tid, (Addr)p);
+ handle_free(tid, (Addr)p);
}
static void* DRD_(__builtin_vec_new)(ThreadId tid, SizeT n)
{
- return DRD_(new_block)(tid, n, VG_(clo_alignment), /*is_zeroed*/False);
+ return new_block(tid, n, VG_(clo_alignment), /*is_zeroed*/False);
}
static void DRD_(__builtin_vec_delete)(ThreadId tid, void* p)
{
- DRD_(handle_free)(tid, (Addr)p);
+ handle_free(tid, (Addr)p);
}
static SizeT DRD_(malloc_usable_size) ( ThreadId tid, void* p )
{
- DRD_Chunk *mc = VG_(HT_lookup)( DRD_(s_malloc_list), (UWord)p );
+ DRD_Chunk *mc = VG_(HT_lookup)( s_malloc_list, (UWord)p );
// There may be slop, but pretend there isn't because only the asked-for
// area will have been shadowed properly.
@@ -258,14 +277,14 @@
void DRD_(register_malloc_wrappers)(const StartUsingMem start_callback,
const StopUsingMem stop_callback)
{
- tl_assert(DRD_(s_malloc_list) == 0);
- DRD_(s_malloc_list) = VG_(HT_construct)("drd_malloc_list"); // a big prime
- tl_assert(DRD_(s_malloc_list) != 0);
+ tl_assert(s_malloc_list == 0);
+ s_malloc_list = VG_(HT_construct)("drd_malloc_list"); // a big prime
+ tl_assert(s_malloc_list != 0);
tl_assert(start_callback);
tl_assert(stop_callback);
- DRD_(s_start_using_mem_callback) = start_callback;
- DRD_(s_stop_using_mem_callback) = stop_callback;
+ s_start_using_mem_callback = start_callback;
+ s_stop_using_mem_callback = stop_callback;
VG_(needs_malloc_replacement)(DRD_(malloc),
DRD_(__builtin_new),
@@ -291,8 +310,8 @@
tl_assert(size);
tl_assert(where);
- VG_(HT_ResetIter)(DRD_(s_malloc_list));
- while ((mc = VG_(HT_Next)(DRD_(s_malloc_list))))
+ VG_(HT_ResetIter)(s_malloc_list);
+ while ((mc = VG_(HT_Next)(s_malloc_list)))
{
if (mc->data <= a && a < mc->data + mc->size)
{
@@ -321,8 +340,8 @@
return;
/* Count memory still in use. */
- VG_(HT_ResetIter)(DRD_(s_malloc_list));
- while ((mc = VG_(HT_Next)(DRD_(s_malloc_list))))
+ VG_(HT_ResetIter)(s_malloc_list);
+ while ((mc = VG_(HT_Next)(s_malloc_list)))
{
nblocks++;
nbytes += mc->size;
@@ -333,8 +352,8 @@
nbytes, nblocks);
VG_(message)(Vg_DebugMsg,
"malloc/free: %lu allocs, %lu frees, %lu bytes allocated.",
- DRD_(s_cmalloc_n_mallocs),
- DRD_(s_cmalloc_n_frees), DRD_(s_cmalloc_bs_mallocd));
+ s_cmalloc_n_mallocs,
+ s_cmalloc_n_frees, s_cmalloc_bs_mallocd);
if (VG_(clo_verbosity) > 1)
VG_(message)(Vg_DebugMsg, " ");
}