From: Florian Krohm Date: Sat, 13 Sep 2014 22:04:33 +0000 (+0000) Subject: Tidy up m_oset.c X-Git-Tag: svn/VALGRIND_3_11_0~1001 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2aa67544d6c27b2f5868609a6bb663e129343005;p=thirdparty%2Fvalgrind.git Tidy up m_oset.c - Document that the allocation function must ot return NULL. - As a conequence of the previous requirement the various Create and AllocNode functions cannot return NULL. Remove pointless asserts at call sites. - Remove documentation of undefined function CreateWithCmp. - Names of library functions (such as 'free') are reserved as a are names beginning with underscores. Don't use those. git-svn-id: svn://svn.valgrind.org/valgrind/trunk@14531 --- diff --git a/coregrind/m_debuginfo/readelf.c b/coregrind/m_debuginfo/readelf.c index 75fa469714..d3f646aabb 100644 --- a/coregrind/m_debuginfo/readelf.c +++ b/coregrind/m_debuginfo/readelf.c @@ -891,7 +891,6 @@ void read_elf_symtab__ppc64be_linux( (OSetCmp_t)cmp_TempSymKey, ML_(dinfo_zalloc), "di.respl.1", ML_(dinfo_free) ); - vg_assert(oset); /* Perhaps should start at i = 1; ELF docs suggest that entry 0 always denotes 'unknown symbol'. */ @@ -988,7 +987,6 @@ void read_elf_symtab__ppc64be_linux( /* A new (name,addr) key. Add and continue. */ elem = VG_(OSetGen_AllocNode)(oset, sizeof(TempSym)); - vg_assert(elem); elem->key = key; elem->tocptr = GET_TOCPTR_AVMA(sym_avmas_really); elem->size = sym_size; diff --git a/coregrind/m_debuginfo/storage.c b/coregrind/m_debuginfo/storage.c index 9c35c569ea..689a68f9bd 100644 --- a/coregrind/m_debuginfo/storage.c +++ b/coregrind/m_debuginfo/storage.c @@ -1047,7 +1047,6 @@ static void add_var_to_arange ( vg_assert(first->aMin <= first->aMax); /* create a new range */ nyu = VG_(OSetGen_AllocNode)( scope, sizeof(DiAddrRange) ); - vg_assert(nyu); nyu->aMin = aMin; nyu->aMax = tmp; vg_assert(nyu->aMin <= nyu->aMax); @@ -1077,7 +1076,6 @@ static void add_var_to_arange ( vg_assert(last->aMin <= last->aMax); /* create a new range */ nyu = VG_(OSetGen_AllocNode)( scope, sizeof(DiAddrRange) ); - vg_assert(nyu); nyu->aMin = tmp; nyu->aMax = aMax; vg_assert(nyu->aMin <= nyu->aMax); @@ -1260,7 +1258,6 @@ void ML_(addVar)( struct _DebugInfo* di, ML_(cmp_for_DiAddrRange_range), ML_(dinfo_zalloc), "di.storage.addVar.2", ML_(dinfo_free) ); - vg_assert(scope); if (0) VG_(printf)("create: scope = %p, adding at %ld\n", scope, VG_(sizeXA)(di->varinfo)); VG_(addToXA)( di->varinfo, &scope ); @@ -1270,7 +1267,6 @@ void ML_(addVar)( struct _DebugInfo* di, All of these invariants get checked both add_var_to_arange and after reading is complete, in canonicaliseVarInfo. */ nyu = VG_(OSetGen_AllocNode)( scope, sizeof(DiAddrRange) ); - vg_assert(nyu); nyu->aMin = (Addr)0; nyu->aMax = ~(Addr)0; nyu->vars = VG_(newXA)( ML_(dinfo_zalloc), "di.storage.addVar.3", diff --git a/coregrind/m_oset.c b/coregrind/m_oset.c index 979245c398..4499b6c68d 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; // allocator - const HChar* cc; // cc for allocator - OSetFree_t free; // deallocator + OSetAlloc_t alloc_fn; // allocator + const HChar* cc; // cost centre for allocator + OSetFree_t free_fn; // deallocator PoolAlloc* node_pa; // (optional) pool allocator for nodes. SizeT maxEltSize; // for node_pa, must be > 0. Otherwise unused. Word nElems; // number of elements in the tree @@ -285,9 +285,9 @@ 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, const HChar* _cc, - OSetFree_t _free) +AvlTree* VG_(OSetGen_Create)(PtrdiffT keyOff, OSetCmp_t cmp, + OSetAlloc_t alloc_fn, const HChar* cc, + OSetFree_t free_fn) { AvlTree* t; @@ -295,16 +295,16 @@ AvlTree* VG_(OSetGen_Create)(PtrdiffT _keyOff, OSetCmp_t _cmp, vg_assert(sizeof(AvlNode) == 3*sizeof(void*)); // Sanity check args - vg_assert(_alloc); - vg_assert(_free); - if (!_cmp) vg_assert(0 == _keyOff); // If no cmp, offset must be zero - - t = _alloc(_cc, sizeof(AvlTree)); - t->keyOff = _keyOff; - t->cmp = _cmp; - t->alloc = _alloc; - t->cc = _cc; - t->free = _free; + vg_assert(alloc_fn); + vg_assert(free_fn); + if (!cmp) vg_assert(0 == keyOff); // If no cmp, offset must be zero + + t = alloc_fn(cc, sizeof(AvlTree)); + t->keyOff = keyOff; + t->cmp = cmp; + t->alloc_fn = alloc_fn; + t->cc = cc; + t->free_fn = free_fn; t->node_pa = NULL; t->maxEltSize = 0; // Just in case it would be wrongly used. t->nElems = 0; @@ -314,27 +314,25 @@ AvlTree* VG_(OSetGen_Create)(PtrdiffT _keyOff, OSetCmp_t _cmp, return t; } -AvlTree* VG_(OSetGen_Create_With_Pool)(PtrdiffT _keyOff, OSetCmp_t _cmp, - OSetAlloc_t _alloc, const HChar* _cc, - OSetFree_t _free, - SizeT _poolSize, - SizeT _maxEltSize) +AvlTree* VG_(OSetGen_Create_With_Pool)(PtrdiffT keyOff, OSetCmp_t cmp, + OSetAlloc_t alloc_fn, const HChar* cc, + OSetFree_t free_fn, + SizeT poolSize, + SizeT maxEltSize) { AvlTree* t; - t = VG_(OSetGen_Create) (_keyOff, _cmp, - _alloc, _cc, - _free); + t = VG_(OSetGen_Create) (keyOff, cmp, alloc_fn, cc, free_fn); - vg_assert (_poolSize > 0); - vg_assert (_maxEltSize > 0); - t->maxEltSize = _maxEltSize; + vg_assert (poolSize > 0); + vg_assert (maxEltSize > 0); + t->maxEltSize = maxEltSize; t->node_pa = VG_(newPA)(sizeof(AvlNode) - + VG_ROUNDUP(_maxEltSize, sizeof(void*)), - _poolSize, - t->alloc, - _cc, - t->free); + + VG_ROUNDUP(maxEltSize, sizeof(void*)), + poolSize, + t->alloc_fn, + cc, + t->free_fn); VG_(addRefPA) (t->node_pa); return t; @@ -346,12 +344,12 @@ AvlTree* VG_(OSetGen_EmptyClone) (AvlTree* os) vg_assert(os); - t = os->alloc(os->cc, sizeof(AvlTree)); + t = os->alloc_fn(os->cc, sizeof(AvlTree)); t->keyOff = os->keyOff; t->cmp = os->cmp; - t->alloc = os->alloc; + t->alloc_fn = os->alloc_fn; t->cc = os->cc; - t->free = os->free; + t->free_fn = os->free_fn; t->node_pa = os->node_pa; if (t->node_pa) VG_(addRefPA) (t->node_pa); @@ -363,10 +361,10 @@ AvlTree* VG_(OSetGen_EmptyClone) (AvlTree* os) return t; } -AvlTree* VG_(OSetWord_Create)(OSetAlloc_t _alloc, const HChar* _cc, - OSetFree_t _free) +AvlTree* VG_(OSetWord_Create)(OSetAlloc_t alloc_fn, const HChar* cc, + OSetFree_t free_fn) { - return VG_(OSetGen_Create)(/*keyOff*/0, /*cmp*/NULL, _alloc, _cc, _free); + return VG_(OSetGen_Create)(/*keyOff*/0, /*cmp*/NULL, alloc_fn, cc, free_fn); } // Destructor, frees up all memory held by remaining nodes. @@ -407,7 +405,7 @@ void VG_(OSetGen_Destroy)(AvlTree* t) if (has_node_pa) VG_(freeEltPA) (t->node_pa, n); else - t->free(n); + t->free_fn(n); sz++; break; } @@ -416,7 +414,7 @@ void VG_(OSetGen_Destroy)(AvlTree* t) } /* Free the AvlTree itself. */ - t->free(t); + t->free_fn(t); } void VG_(OSetWord_Destroy)(AvlTree* t) @@ -434,7 +432,7 @@ void* VG_(OSetGen_AllocNode)(AvlTree* t, SizeT elemSize) vg_assert(elemSize <= t->maxEltSize); n = VG_(allocEltPA) (t->node_pa); } else { - n = t->alloc( t->cc, nodeSize ); + n = t->alloc_fn( t->cc, nodeSize ); } VG_(memset)(n, 0, nodeSize); n->magic = OSET_MAGIC; @@ -446,7 +444,7 @@ void VG_(OSetGen_FreeNode)(AvlTree* t, void* e) if (t->node_pa) VG_(freeEltPA) (t->node_pa, node_of_elem (e)); else - t->free( node_of_elem(e) ); + t->free_fn( node_of_elem(e) ); } /*--------------------------------------------------------------------*/ diff --git a/drd/drd_clientobj.c b/drd/drd_clientobj.c index 444d84a546..44234f9c28 100644 --- a/drd/drd_clientobj.c +++ b/drd/drd_clientobj.c @@ -60,7 +60,6 @@ void DRD_(clientobj_init)(void) tl_assert(s_clientobj_set == 0); s_clientobj_set = VG_(OSetGen_Create)(0, 0, VG_(malloc), "drd.clientobj.ci.1", VG_(free)); - tl_assert(s_clientobj_set); } /** diff --git a/include/pub_tool_oset.h b/include/pub_tool_oset.h index f696d64fec..def6fffc5c 100644 --- a/include/pub_tool_oset.h +++ b/include/pub_tool_oset.h @@ -89,24 +89,23 @@ typedef void (*OSetFree_t) ( void* p ); /*--- Creating and destroying OSets (UWord) ---*/ /*--------------------------------------------------------------------*/ -// * Create: allocates and initialises the OSet. Arguments: -// - alloc The allocation function used internally for allocating the -// OSet and all its nodes. +// * Create: allocates and initialises the OSet. Never returns NULL. +// Parameters: +// - alloc_fn The allocation function used internally for allocating the +// OSet and all its nodes. It must not return NULL (that is, +// if it returns it must have succeeded.) // - cc Cost centre string used by 'alloc'. -// - free The deallocation function used internally for freeing nodes +// - free_fn The deallocation function used internally for freeing nodes // called by VG_(OSetWord_Destroy)(). // -// * CreateWithCmp: like Create, but you specify your own comparison -// function. -// // * Destroy: frees all nodes in the table, plus the memory used by // 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. -extern OSet* VG_(OSetWord_Create) ( OSetAlloc_t alloc, const HChar* cc, - OSetFree_t _free ); -extern void VG_(OSetWord_Destroy) ( OSet* os ); +extern OSet* VG_(OSetWord_Create) ( OSetAlloc_t alloc_fn, const HChar* cc, + OSetFree_t free_fn ); +extern void VG_(OSetWord_Destroy) ( OSet* os ); /*--------------------------------------------------------------------*/ /*--- Operations on OSets (UWord) ---*/ @@ -160,17 +159,20 @@ extern Bool VG_(OSetWord_Next) ( OSet* os, /*OUT*/UWord* val ); /*--- Creating and destroying OSets and OSet members (Gen) ---*/ /*--------------------------------------------------------------------*/ -// * Create: allocates and initialises the OSet. Arguments: +// * Create: allocates and initialises the OSet. Never returns NULL. +// Parameters: // - keyOff The offset of the key within the element. // - cmp The comparison function between keys and elements, or NULL // if the OSet should use fast comparisons. -// - alloc The allocation function used for allocating the OSet itself; +// - alloc_fn The allocation function used for allocating the OSet itself; +// It must not return NULL (that is, if it returns it must +// have succeeded.) // If a pool allocator is used, it's called to allocate pool of // nodes. // If no pool allocator is used, it's called for each // invocation of VG_(OSetGen_AllocNode)(). // - cc Cost centre string used by 'alloc'. -// - free If no pool allocator is used, this is the deallocation +// - free_fn If no pool allocator is used, this is the deallocation // function used by VG_(OSetGen_FreeNode)() and // VG_(OSetGen_Destroy)(). // If a pool allocator is used, the memory used by the nodes is @@ -202,14 +204,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, const HChar* cc, - OSetFree_t _free); + OSetAlloc_t alloc_fn, const HChar* cc, + OSetFree_t free_fn); extern OSet* VG_(OSetGen_Create_With_Pool) ( PtrdiffT keyOff, OSetCmp_t cmp, - OSetAlloc_t alloc, + OSetAlloc_t alloc_fn, const HChar* cc, - OSetFree_t _free, + OSetFree_t free_fn, SizeT poolSize, SizeT maxEltSize); // Same as VG_(OSetGen_Create) but created OSet will use a pool allocator to diff --git a/memcheck/mc_main.c b/memcheck/mc_main.c index 69370eba24..5526833d21 100644 --- a/memcheck/mc_main.c +++ b/memcheck/mc_main.c @@ -578,7 +578,6 @@ static AuxMapEnt* find_or_alloc_in_auxmap ( Addr a ) a &= ~(Addr)0xFFFF; nyu = (AuxMapEnt*) VG_(OSetGen_AllocNode)( auxmap_L2, sizeof(AuxMapEnt) ); - tl_assert(nyu); nyu->base = a; nyu->sm = &sm_distinguished[SM_DIST_NOACCESS]; VG_(OSetGen_Insert)( auxmap_L2, nyu ); @@ -2414,7 +2413,6 @@ static void init_ocacheL2 ( void ) = VG_(OSetGen_Create)( offsetof(OCacheLine,tag), NULL, /* fast cmp */ ocacheL2_malloc, "mc.ioL2", ocacheL2_free); - tl_assert(ocacheL2); stats__ocacheL2_n_nodes = 0; } @@ -2450,7 +2448,6 @@ static void ocacheL2_add_line ( OCacheLine* line ) OCacheLine* copy; tl_assert(is_valid_oc_tag(line->tag)); copy = VG_(OSetGen_AllocNode)( ocacheL2, sizeof(OCacheLine) ); - tl_assert(copy); *copy = *line; stats__ocacheL2_refs++; VG_(OSetGen_Insert)( ocacheL2, copy );