From: Philippe Waroquiers Date: Fri, 18 Nov 2016 21:54:09 +0000 (+0000) Subject: Add typedef Alloc_Fn_t/Free_Fn_t in pub_tool_basics.h, use them everywhere X-Git-Tag: svn/VALGRIND_3_13_0~283 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=471900460d77e5ff48a6b97a2e3f59fd2a363aaf;p=thirdparty%2Fvalgrind.git Add typedef Alloc_Fn_t/Free_Fn_t in pub_tool_basics.h, use them everywhere git-svn-id: svn://svn.valgrind.org/valgrind/trunk@16138 --- diff --git a/coregrind/m_deduppoolalloc.c b/coregrind/m_deduppoolalloc.c index b8f10c72d6..9b367218fc 100644 --- a/coregrind/m_deduppoolalloc.c +++ b/coregrind/m_deduppoolalloc.c @@ -43,9 +43,9 @@ struct _DedupPoolAlloc { SizeT fixedSzb; /* If using VG_(allocFixedEltDedupPA), size of elements */ Bool strPA; /* True if this is a string dedup pool */ SizeT eltAlign; - void* (*alloc_fn)(const HChar*, SizeT); /* pool allocator */ + Alloc_Fn_t alloc_fn; /* pool allocator */ const HChar* cc; /* pool allocator's cost centre */ - void (*free_fn)(void*); /* pool allocator's deallocation function */ + Free_Fn_t free_fn; /* pool allocator's deallocation function */ /* XArray of void* (pointers to pools). The pools themselves. Each element is a pointer to a block of size at least PoolSzB bytes. The last block might be smaller due to a call to shrink_block. */ @@ -84,9 +84,9 @@ typedef DedupPoolAlloc* VG_(newDedupPA) ( SizeT poolSzB, SizeT eltAlign, - void* (*alloc_fn)(const HChar*, SizeT), + Alloc_Fn_t alloc_fn, const HChar* cc, - void (*free_fn)(void*) ) + Free_Fn_t free_fn ) { DedupPoolAlloc* ddpa; vg_assert(poolSzB >= eltAlign); diff --git a/coregrind/m_oset.c b/coregrind/m_oset.c index 3fd214f0f6..134fa43089 100644 --- a/coregrind/m_oset.c +++ b/coregrind/m_oset.c @@ -112,9 +112,9 @@ struct _OSetNode { struct _OSet { SizeT keyOff; // key offset OSetCmp_t cmp; // compare a key and an element, or NULL - OSetAlloc_t alloc_fn; // allocator + Alloc_Fn_t alloc_fn; // allocator const HChar* cc; // cost centre for allocator - OSetFree_t free_fn; // deallocator + Free_Fn_t free_fn; // deallocator PoolAlloc* node_pa; // (optional) pool allocator for nodes. SizeT maxEltSize; // for node_pa, must be > 0. Otherwise unused. UInt nElems; // number of elements in the tree @@ -286,8 +286,8 @@ static inline Bool stackPop(AvlTree* t, AvlNode** n, Int* i) // The underscores avoid GCC complaints about overshadowing global names. AvlTree* VG_(OSetGen_Create)(PtrdiffT keyOff, OSetCmp_t cmp, - OSetAlloc_t alloc_fn, const HChar* cc, - OSetFree_t free_fn) + Alloc_Fn_t alloc_fn, const HChar* cc, + Free_Fn_t free_fn) { AvlTree* t; @@ -315,8 +315,8 @@ AvlTree* VG_(OSetGen_Create)(PtrdiffT keyOff, OSetCmp_t cmp, } AvlTree* VG_(OSetGen_Create_With_Pool)(PtrdiffT keyOff, OSetCmp_t cmp, - OSetAlloc_t alloc_fn, const HChar* cc, - OSetFree_t free_fn, + Alloc_Fn_t alloc_fn, const HChar* cc, + Free_Fn_t free_fn, SizeT poolSize, SizeT maxEltSize) { @@ -361,8 +361,8 @@ AvlTree* VG_(OSetGen_EmptyClone) (const AvlTree* os) return t; } -AvlTree* VG_(OSetWord_Create)(OSetAlloc_t alloc_fn, const HChar* cc, - OSetFree_t free_fn) +AvlTree* VG_(OSetWord_Create)(Alloc_Fn_t alloc_fn, const HChar* cc, + Free_Fn_t free_fn) { return VG_(OSetGen_Create)(/*keyOff*/0, /*cmp*/NULL, alloc_fn, cc, free_fn); } diff --git a/coregrind/m_poolalloc.c b/coregrind/m_poolalloc.c index ed203ceeed..473962fe2e 100644 --- a/coregrind/m_poolalloc.c +++ b/coregrind/m_poolalloc.c @@ -36,9 +36,9 @@ struct _PoolAlloc { UWord nrRef; /* nr reference to this pool allocator */ UWord elemSzB; /* element size */ UWord nPerPool; /* # elems per pool */ - void* (*alloc_fn)(const HChar*, SizeT); /* pool allocator */ - const HChar* cc; /* pool allocator's cost centre */ - void (*free_fn)(void*); /* pool allocator's free-er */ + Alloc_Fn_t alloc_fn; /* pool allocator */ + const HChar* cc; /* pool allocator's cost centre */ + Free_Fn_t free_fn; /* pool allocator's free-er */ /* XArray of void* (pointers to pools). The pools themselves. Each element is a pointer to a block of size (elemSzB * nPerPool) bytes. */ @@ -50,9 +50,9 @@ struct _PoolAlloc { PoolAlloc* VG_(newPA) ( UWord elemSzB, UWord nPerPool, - void* (*alloc_fn)(const HChar*, SizeT), + Alloc_Fn_t alloc_fn, const HChar* cc, - void (*free_fn)(void*) ) + Free_Fn_t free_fn ) { PoolAlloc* pa; vg_assert(0 == (elemSzB % sizeof(UWord))); diff --git a/coregrind/m_rangemap.c b/coregrind/m_rangemap.c index b085d29681..37a6268313 100644 --- a/coregrind/m_rangemap.c +++ b/coregrind/m_rangemap.c @@ -48,9 +48,9 @@ typedef struct _RangeMap { - void* (*alloc_fn) ( const HChar*, SizeT ); /* alloc fn (nofail) */ + Alloc_Fn_t alloc_fn; /* alloc fn (nofail) */ const HChar* cc; /* cost centre for alloc */ - void (*free_fn) ( void* ); /* free fn */ + Free_Fn_t free_fn; /* free fn */ XArray* ranges; }; @@ -62,9 +62,9 @@ static void split_at ( /*MOD*/RangeMap* rm, UWord key ); static void show ( const RangeMap* rm ); -RangeMap* VG_(newRangeMap) ( void*(*alloc_fn)(const HChar*,SizeT), +RangeMap* VG_(newRangeMap) ( Alloc_Fn_t alloc_fn, const HChar* cc, - void(*free_fn)(void*), + Free_Fn_t free_fn, UWord initialVal ) { /* check user-supplied info .. */ diff --git a/coregrind/m_xarray.c b/coregrind/m_xarray.c index 3d48f1fca8..467e45a4c8 100644 --- a/coregrind/m_xarray.c +++ b/coregrind/m_xarray.c @@ -38,9 +38,9 @@ /* See pub_tool_xarray.h for details of what this is all about. */ struct _XArray { - void* (*alloc_fn) ( const HChar*, SizeT ); /* alloc fn (nofail) */ + Alloc_Fn_t alloc_fn; /* alloc fn (nofail) */ const HChar* cc; /* cost centre for alloc */ - void (*free_fn) ( void* ); /* free fn */ + Free_Fn_t free_fn; /* free fn */ Int (*cmpFn) ( const void*, const void* ); /* cmp fn (may be NULL) */ Word elemSzB; /* element size in bytes */ void* arr; /* pointer to elements */ @@ -50,9 +50,9 @@ struct _XArray { }; -XArray* VG_(newXA) ( void*(*alloc_fn)(const HChar*,SizeT), +XArray* VG_(newXA) ( Alloc_Fn_t alloc_fn, const HChar* cc, - void(*free_fn)(void*), + Free_Fn_t free_fn, Word elemSzB ) { XArray* xa; diff --git a/coregrind/m_xtree.c b/coregrind/m_xtree.c index 5da5986c57..275b8713b9 100644 --- a/coregrind/m_xtree.c +++ b/coregrind/m_xtree.c @@ -73,9 +73,9 @@ typedef struct _XT_shared { UWord nrRef; /* nr of XTrees referencing this shared memory. */ - void* (*alloc_fn)( const HChar*, SizeT ); /* alloc fn (nofail) */ + Alloc_Fn_t alloc_fn; /* alloc fn (nofail) */ const HChar* cc; /* cost centre for alloc */ - void (*free_fn)( void* ); /* free fn */ + Free_Fn_t free_fn; /* free fn */ /* The data associated to each ec is stored in 2 arrays: an xec array, shared between an xt and all its snapshots. @@ -102,7 +102,7 @@ typedef (with the index ecu/4). */ #define NO_OFFSET 0xffffffff -static XT_shared* new_XT_shared (void* (*alloc_fn)(const HChar*, SizeT), +static XT_shared* new_XT_shared (Alloc_Fn_t alloc_fn, const HChar* cc, void (*free_fn)(void*)) { @@ -208,9 +208,9 @@ static UWord release_XT_shared(XT_shared* shared) struct _XTree { - void* (*alloc_fn)( const HChar*, SizeT ); /* alloc fn (nofail) */ + Alloc_Fn_t alloc_fn; /* alloc fn (nofail) */ const HChar* cc; /* cost centre for alloc */ - void (*free_fn)( void* ); /* free fn */ + Free_Fn_t free_fn; /* free fn */ Word dataSzB; /* data size in bytes */ XT_init_data_t init_data_fn; XT_add_data_t add_data_fn; @@ -224,9 +224,9 @@ struct _XTree { }; -XTree* VG_(XT_create) ( void*(*alloc_fn)(const HChar*, SizeT), +XTree* VG_(XT_create) ( Alloc_Fn_t alloc_fn, const HChar* cc, - void(*free_fn) (void*), + Free_Fn_t free_fn, Word dataSzB, XT_init_data_t init_data_fn, XT_add_data_t add_data_fn, diff --git a/include/pub_tool_basics.h b/include/pub_tool_basics.h index 810e27ec8b..4ff9415755 100644 --- a/include/pub_tool_basics.h +++ b/include/pub_tool_basics.h @@ -129,6 +129,17 @@ typedef struct { UWord uw1; UWord uw2; } UWordPair; /* ThreadIds are simply indices into the VG_(threads)[] array. */ typedef UInt ThreadId; +/* Many data structures need to allocate and release memory. + The allocation/release functions must be provided by the caller. + The Alloc_Fn_t function must allocate a chunk of memory of size szB. + cc is the Cost Centre for this allocated memory. This constant string + is used to provide Valgrind's heap profiling, activated by + --profile-heap=no|yes. + The corresponding Free_Fn_t frees the memory chunk p. */ + +typedef void* (*Alloc_Fn_t) ( const HChar* cc, SizeT szB ); +typedef void (*Free_Fn_t) ( void* p ); + /* An abstraction of syscall return values. Linux/MIPS32 and Linux/MIPS64: When _isError == False, diff --git a/include/pub_tool_deduppoolalloc.h b/include/pub_tool_deduppoolalloc.h index 969c42cc98..ed8fb3142e 100644 --- a/include/pub_tool_deduppoolalloc.h +++ b/include/pub_tool_deduppoolalloc.h @@ -93,9 +93,9 @@ typedef struct _DedupPoolAlloc DedupPoolAlloc; This function never returns NULL. */ extern DedupPoolAlloc* VG_(newDedupPA) ( SizeT poolSzB, SizeT eltAlign, - void* (*alloc)(const HChar*, SizeT), + Alloc_Fn_t alloc_fn, const HChar* cc, - void (*free_fn)(void*) ); + Free_Fn_t free_fn ); /* Allocates or retrieve element from ddpa with eltSzB bytes to store elt. This function never returns NULL. diff --git a/include/pub_tool_oset.h b/include/pub_tool_oset.h index 0c97997204..95bed77df5 100644 --- a/include/pub_tool_oset.h +++ b/include/pub_tool_oset.h @@ -77,13 +77,8 @@ typedef struct _OSet OSet; -// - Cmp: returns -1, 0 or 1 if key is <, == or > elem. -// - Alloc: allocates a chunk of memory. -// - Free: frees a chunk of memory allocated with Alloc. - +// - OSetCmp_t: returns -1, 0 or 1 if key is <, == or > elem. typedef Word (*OSetCmp_t) ( const void* key, const void* elem ); -typedef void* (*OSetAlloc_t) ( const HChar* cc, SizeT szB ); -typedef void (*OSetFree_t) ( void* p ); /*--------------------------------------------------------------------*/ /*--- Creating and destroying OSets (UWord) ---*/ @@ -103,8 +98,8 @@ typedef void (*OSetFree_t) ( void* p ); // to allow the destruction of any attached resources; if NULL it is not // called. -extern OSet* VG_(OSetWord_Create) ( OSetAlloc_t alloc_fn, const HChar* cc, - OSetFree_t free_fn ); +extern OSet* VG_(OSetWord_Create) ( Alloc_Fn_t alloc_fn, const HChar* cc, + Free_Fn_t free_fn ); extern void VG_(OSetWord_Destroy) ( OSet* os ); /*--------------------------------------------------------------------*/ @@ -204,14 +199,14 @@ extern Bool VG_(OSetWord_Next) ( OSet* os, /*OUT*/UWord* val ); // lead to assertions in Valgrind's allocator. extern OSet* VG_(OSetGen_Create) ( PtrdiffT keyOff, OSetCmp_t cmp, - OSetAlloc_t alloc_fn, const HChar* cc, - OSetFree_t free_fn); + Alloc_Fn_t alloc_fn, const HChar* cc, + Free_Fn_t free_fn); extern OSet* VG_(OSetGen_Create_With_Pool) ( PtrdiffT keyOff, OSetCmp_t cmp, - OSetAlloc_t alloc_fn, + Alloc_Fn_t alloc_fn, const HChar* cc, - OSetFree_t free_fn, + Free_Fn_t free_fn, SizeT poolSize, SizeT maxEltSize); // Same as VG_(OSetGen_Create) but created OSet will use a pool allocator to diff --git a/include/pub_tool_poolalloc.h b/include/pub_tool_poolalloc.h index 549569fef8..35c9a47ef9 100644 --- a/include/pub_tool_poolalloc.h +++ b/include/pub_tool_poolalloc.h @@ -56,9 +56,9 @@ typedef struct _PoolAlloc PoolAlloc; This function never returns NULL. */ extern PoolAlloc* VG_(newPA) ( UWord elemSzB, UWord nPerPool, - void* (*alloc)(const HChar*, SizeT), + Alloc_Fn_t alloc_fn, const HChar* cc, - void (*free_fn)(void*) ); + Free_Fn_t free_fn ); /* Free all memory associated with a PoolAlloc. */ diff --git a/include/pub_tool_rangemap.h b/include/pub_tool_rangemap.h index 208b9dc732..1b98b8e48f 100644 --- a/include/pub_tool_rangemap.h +++ b/include/pub_tool_rangemap.h @@ -47,9 +47,9 @@ typedef struct _RangeMap RangeMap; succeeded.) The new array will contain a single range covering the entire key space, which will be bound to the value |initialVal|. This function never returns NULL. */ -RangeMap* VG_(newRangeMap) ( void*(*alloc_fn)(const HChar*,SizeT), +RangeMap* VG_(newRangeMap) ( Alloc_Fn_t alloc_fn, const HChar* cc, - void(*free_fn)(void*), + Free_Fn_t free_fn, UWord initialVal ); /* Free all memory associated with a RangeMap. */ diff --git a/include/pub_tool_xarray.h b/include/pub_tool_xarray.h index 0e91dc0082..9eb9b43cee 100644 --- a/include/pub_tool_xarray.h +++ b/include/pub_tool_xarray.h @@ -54,9 +54,9 @@ typedef Int (*XACmpFn_t)(const void *, const void *); for elements of the specified size. alloc_fn must not return NULL (that is, if it returns it must have succeeded.) This function never returns NULL. */ -extern XArray* VG_(newXA) ( void*(*alloc_fn)(const HChar*,SizeT), +extern XArray* VG_(newXA) ( Alloc_Fn_t alloc_fn, const HChar* cc, - void(*free_fn)(void*), + Free_Fn_t free_fn, Word elemSzB ); /* Free all memory associated with an XArray. */ diff --git a/include/pub_tool_xtree.h b/include/pub_tool_xtree.h index a976a38b8f..24566433d0 100644 --- a/include/pub_tool_xtree.h +++ b/include/pub_tool_xtree.h @@ -96,9 +96,9 @@ typedef void (*XT_filter_IPs_t) (Addr* ips, Int n_ips, alloc_fn must not return NULL (that is, if it returns it must have succeeded.). See respective typedef for *_fn arguments. */ -extern XTree* VG_(XT_create) ( void*(*alloc_fn)(const HChar*, SizeT), +extern XTree* VG_(XT_create) ( Alloc_Fn_t alloc_fn, const HChar* cc, - void(*free_fn) (void*), + Free_Fn_t free_fn, Word dataSzB, XT_init_data_t init_data_fn, XT_add_data_t add_data_fn,