C Collections

cmempool · Section 3

crmempool_free_entry

crmempool_free_entry - free an entry back to the ranged pool

C Collections (libccollections, -lccollections)

#include <ccollections/cmempool.h>
 
crmempool_free_entry(rmp, entry);

Puts an allocated entry back into the sub-pool that owns it, and then
sets the caller's pointer to NULL.
 
The caller names the ranged pool, and the macro does not find it from
the entry. It does find out which sub-pool owns the entry from the
entry's address, so the caller never has to keep track of that. Free
an entry to the ranged pool it came from: Passing a different pool is
a caller error, and it is fatal.

rmp    Ranged pool the entry was allocated from
entry  Entry to free (set to NULL after the free)

The search for the owning sub-pool visits each tier at most once, and
  the free itself is O(1)
It works for an entry from any sub-pool of the ranged pool
It also works for an entry that the dynamic fallback allocated
Calling it with a NULL entry is safe. A NULL pool is a caller error, and
  it is fatal
It detects corruption with assertions
 
The owning sub-pool is found from the entry's address, and nothing is
read through an address that no sub-pool owns. An entry of the dynamic
fallback is found in the record of fallback entries that its sub-pool
keeps, and an entry of another pool, or a fallback entry that is
already free, ends in the assert. See cmempool_free_entry(3) and
crmempool_create_mp(3).
 
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-safe if the pool was created with single_threaded = false.
Each sub-pool has its own lock, so frees of different size classes
never contend with each other, and inside a tier that keeps a cache for
each thread the common path takes no lock at all. An entry that one
thread frees can be handed to another thread.

cmempool_free_entry(3), crmempool_alloc_entry(3), crmempool_create_mp(3)