diff --git a/coregrind/m_oset.c b/coregrind/m_oset.c index 245a3974a..acb724eb8 100644 --- a/coregrind/m_oset.c +++ b/coregrind/m_oset.c @@ -377,15 +377,12 @@ void VG_(OSetGen_Destroy)(AvlTree* t) has_node_pa = t->node_pa != NULL; - if (has_node_pa) - VG_(releasePA) (t->node_pa); // decrement ref count. - - if (has_node_pa && VG_(nrRefPA) (t->node_pa) == 0) { - /* We are the only remaining user of this pool allocator. - => release (more efficiently) all the elements - by deleting the pool allocator */ - VG_(deletePA) (t->node_pa); - } else { + /* + * If we are the only remaining user of this pool allocator, release all + * the elements by deleting the pool allocator. That's more efficient than + * deleting tree nodes one by one. + */ + if (!has_node_pa || VG_(releasePA)(t->node_pa) > 0) { AvlNode* n = NULL; Int i = 0; Word sz = 0; diff --git a/coregrind/m_poolalloc.c b/coregrind/m_poolalloc.c index 88ac4519a..a236a0b68 100644 --- a/coregrind/m_poolalloc.c +++ b/coregrind/m_poolalloc.c @@ -137,14 +137,13 @@ void VG_(addRefPA) ( PoolAlloc* pa) pa->nrRef++; } -void VG_(releasePA) ( PoolAlloc* pa) +UWord VG_(releasePA)(PoolAlloc* pa) { + UWord nrRef; + vg_assert(pa->nrRef > 0); - pa->nrRef--; + nrRef = --pa->nrRef; + if (nrRef == 0) + VG_(deletePA)(pa); + return nrRef; } - -UWord VG_(nrRefPA) (PoolAlloc* pa) -{ - return pa->nrRef; -} - diff --git a/include/pub_tool_poolalloc.h b/include/pub_tool_poolalloc.h index bc197b07e..bc50daa1e 100644 --- a/include/pub_tool_poolalloc.h +++ b/include/pub_tool_poolalloc.h @@ -80,12 +80,10 @@ extern void VG_(freeEltPA) ( PoolAlloc* pa, void* p); // VG_(addRefPA) indicates there is a new reference to pa. extern void VG_(addRefPA) ( PoolAlloc* pa); -// VG_(releasePA) indicates a reference to pa has been released. -extern void VG_(releasePA) ( PoolAlloc* pa); - -// Returns the current nr of reference to pa. -// When this drops to 0, VG_(deletePA) can be called by the pa user. -extern UWord VG_(nrRefPA) (PoolAlloc* pa); +// VG_(releasePA) decrements the pa reference count and deletes the pa if that +// reference count has dropped to zero. Returns the new value of the reference +// count. +extern UWord VG_(releasePA) ( PoolAlloc* pa); #endif // __PUB_TOOL_POOLALLOC_