int main(void) { mempool_t *pool; void *e[4]; int nb_test = 0; int nb_success = 0; CHECK_NEQ(pool = mempool_new(16, 2), NULL); CHECK_NEQ(e[0] = mempool_alloc(pool), NULL); CHECK_NEQ(e[1] = mempool_alloc(pool), NULL); CHECK_NEQ(e[2] = mempool_alloc(pool), NULL); mempool_free(pool, e[1]); mempool_delete(pool); fprintf(stderr, "Nb test success %i/%i\n", nb_success, nb_test); if(nb_success != nb_test) return 1; return 0; }
/*-------------------------------------------------------------------------*/ void mempool_delete (Mempool pPool) /* Delete the pool <pPool>, all memory allocated from it, and all * depending pools. <pPool> is invalid after the function returns. */ { Memblock pBlock; Mempool pSubPool; assert(pPool != NULL); /* Free all memblocks */ pBlock = pPool->pBlocks; while (pBlock != NULL) { Memblock pThis = pBlock; pBlock = pBlock->pNext; xfree(pThis); } pBlock = pPool->pFree; while (pBlock != NULL) { Memblock pThis = pBlock; pBlock = pBlock->pNext; xfree(pThis); } /* If this pool is a dependee, delete it from the list */ if (pPool->pSuper != NULL) { Mempool pPrev, pNext; pPrev = pPool->pSuper; pNext = pPrev->pSubPools; if (pPrev->pSubPools == pPool) { pPrev->pSubPools = pPool->pNextSub; } else { for (; pNext != NULL && pNext != pPool ; pPrev = pNext, pNext = pNext->pNextSub ) /* SKIP */; if (pNext == pPool) pPrev->pNextSub = pPool->pNextSub; } } /* Delete all depending pools, but take care that those * pools don't start to scan our subpool list. */ for (pSubPool = pPool->pSubPools; pSubPool != NULL; ) { Mempool pThis = pSubPool; pSubPool = pSubPool->pNextSub; pThis->pSuper = NULL; /* ! */ mempool_delete(pThis); } /* Finally, deallocate this pool */ xfree(pPool); } /* mempool_delete() */