cmempool · Section 3
cmempool_free_entry
NAME
cmempool_free_entry - free an entry back to the pool
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/cmempool.h> cmempool_free_entry(mp, entry);
DESCRIPTION
Puts an allocated entry back on the pool's free list and sets the caller's pointer to NULL. The caller names the pool, and the macro does not find it from the entry. Free an entry to the pool it came from: Passing a different pool is a caller error, and it is fatal. The pool decides from the address alone whether the entry is its own before it reads anything through it. An entry inside the pool's buffer is checked in the pool's status array. For any other address the pool looks in its record of the entries that its dynamic fallback handed out and that are not freed yet. An entry of another pool, an address the pool never handed out and a fallback entry that is already free all end in the assert, and none of them is read.
PARAMETERS
mp Pool that the entry came from entry Entry to free. The macro sets it to NULL afterward
NOTES
O(1) complexity Calling the macro with a NULL entry is safe. A NULL pool is a caller error, and it is fatal The macro detects corruption with assertions On a thread-safe pool the entry goes into a small cache of the calling thread, and the shared free list is reached only when that cache is full. A pool too small to keep a cache (fewer than 8 elements) takes the pool's lock on every call instead, and so does a thread that the pool has no cache left for; that is one lock for each call, as on a pool without a cache. cmempool_create_mp(3) explains how to size a pool. The cache makes no difference to the detection of corruption or of a double free: An entry in a cache is marked free, so a second free of it is caught exactly as it would be without the cache. The one exception is two threads that free the same entry at the same instant. The double-free check runs outside the lock, so one of the two threads can miss the other's mark. Such a call is a data race in the caller's program anyway, and a general purpose allocator with a thread cache has the same limit. The macro evaluates each argument exactly once. entry 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 entries[i++], it frees and clears one element.
THREAD SAFETY
Thread-safe if the pool was created with single_threaded = false. Concurrent callers don't contend on the common path, and an entry that one thread freed can be handed out to another thread.
SEE ALSO
cmempool_alloc_entry(3), cmempool_create_mp(3), crmempool_free_entry(3)