cmempool · Section 3
cmempool_destroy
NAME
cmempool_destroy - destroy a memory pool and set pointer to NULL
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/cmempool.h> cmempool_destroy(mp);
DESCRIPTION
Frees every resource of the memory pool. For a pool created from a preallocated buffer the macro frees only the pool struct, not the buffer itself.
PARAMETERS
mp Memory pool to destroy. The macro sets it to NULL afterward
NOTES
Calling the macro with a NULL pointer is safe For a preallocated pool, the buffer's lifetime is the caller's business The macro evaluates mp exactly once. It must be a modifiable lvalue, such as a variable or an element of an array, so with an expression that has a side effect, such as handles[i++], it destroys and clears one element.
The library can call the free function of a custom allocator after this macro returns, and even after main() returns. A pool that threads share keeps a small cache of entries for each thread. A destroy of the pool marks those caches dead but doesn't free them, because a cache belongs to the thread that holds it and another thread must not free it under that thread. Each cache is freed later, at one of three points: When the owning thread next has to get a cache for a pool that it doesn't already hold one for, when that thread exits, or at process exit or when dlclose(3) unloads the library, if that thread is the one that runs the library's unload handler. Each free calls the free function that the pool was created with. None of these points is guaranteed to come: A thread that keeps using the same few pools answers every request from the caches it already holds and never gets another one, so it can hold a dead cache for ever. A program that needs its allocator to see every free should join such threads.
Once dlclose(3) unloads the library the code that frees a cache is gone, so the cache of every other thread that is still running stays allocated for the rest of the process, while that thread itself exits cleanly. If their caches must go back to the allocator, join the threads that used a thread-safe pool before you unload the library.
So an allocator that is torn down at a known point must outlive that point. The library can call an allocator that draws from an arena after the end of main() has freed that arena, and the same goes for an allocator that draws from storage which the application's exit handler frees. An allocator built on malloc and free isn't affected, and neither is one built on storage that lives for the whole process. A pool created with single_threaded keeps no such cache and has no such tail.
WARNINGS
A pointer from the dynamic fallback that was never freed makes the destroy fail an assertion, so free every allocated entry before you destroy the pool.