C Collections

cmempool · Section 3

crmempool_create_mp

crmempool_create_mp - create a ranged memory pool

C Collections (libccollections, -lccollections)

#include <ccollections/cmempool.h>
 
crmempool *crmempool_create_mp(
    uint8_t smallest_size_power_of_two,
    uint8_t largest_size_power_of_two,
    uint8_t number_of_smallest_size_elems_power_of_two,
    crmempool_fallback_policy fb_policy,
    bool single_threaded,
    ccol_memmgmt_procs *mmgmt_procs,
    char **err);

Creates a ranged memory pool, which manages several fixed-size pools
of power-of-2 sizes and picks the right one for each allocation
request.

smallest_size_power_of_two              Exponent for smallest pool size
largest_size_power_of_two               Exponent for largest pool size
number_of_smallest_size_elems_power_of_two  Exponent for smallest pool count
fb_policy                               Fallback policy for an empty pool
single_threaded                         If true, take no lock
mmgmt_procs                             Custom memory management, or NULL
err                                     Optional pointer that receives an error string

Returns a pointer to the new ranged memory pool on success
Returns NULL on failure

Each sub-pool has half the elements of the size before it
The smallest size that the pool accepts is 16 bytes (power = 4)
Example: smallest=4 (16 bytes), largest=6 (64 bytes), count=8 (256 elems)
  gives three pools: 16 bytes (256 elems), 32 bytes (128) and 64 bytes
  (64)
Fallback policies:
  - crmempool_fallback_disabled: Tries each larger pool in turn, and returns
    NULL when all of them are empty too
  - crmempool_fallback_at_first_exhaustion: When the pool that fits the
    request has no free entry, that pool takes the memory from the
    heap, and the request never goes to a larger pool
  - crmempool_fallback_at_last_exhaustion: Tries each larger pool in turn,
    and goes to the heap only when every pool that can serve the
    request is empty
Each sub-pool calls the functions of mmgmt_procs for its dynamic
fallback entries, and for its own bookkeeping of them, in the middle of
an update of its own state and with its lock held when it has one.
Those functions must not call back into the same ranged pool: With a
lock the call can deadlock, and without one it corrupts that state. An
allocator built on a pool must draw from a different pool
 
Sizing for performance:
 
Each sub-pool has its own size, so the 8-element floor that
cmempool_create_mp(3) describes applies to each tier and not to the
pool as a whole. Since each tier holds half the elements of the tier
below it, the largest element sizes have the fewest entries and are the
first to drop under that floor. A tier with fewer than 8 elements takes
its lock on every allocation and every free.
 
A higher number_of_smallest_size_elems_power_of_two raises every tier
at once, so it is the simplest way to keep the tiers of large elements
above the floor. A narrower range between smallest_size_power_of_two
and largest_size_power_of_two does the same, because there are fewer
tiers to share the elements.
 
Every tier that gets a cache allocates a reserve of its own, so the
memory of a ranged pool can be up to half again the sum of its nominal
tier sizes, and proportionally less for a tier large enough for its
caches to reach their maximum depth. That memory is allocated when the
pool is created and nothing grows at run time.
crmempool_total_capacity() reports the nominal count of a tier,
and once that count runs out the request moves up to a larger tier or
falls back. The one exception is the one that
cmempool_total_capacity(3) describes: An entry leaves a thread's
cache with no lock held, so there is no count to consult, and
concurrent callers can hold a few entries more than a tier's nominal
count for a short time, but never more than that count plus the tier's
reserve.
 
The best range, tier count and single_threaded setting depend on the
sizes the program allocates, which no default can predict, so measure
instead of guessing. "make bench" in the source tree runs these pools
beside the system allocator on the current machine.

Thread-safe when single_threaded is false. Each sub-pool has its own
lock, so allocations of different size classes never contend with each
other, and inside a tier that is large enough to keep a cache for each
thread the common path takes no lock at all.
 
An entry in a thread's cache goes back to its tier when that thread
exits. A destroy of the pool doesn't give such an entry back. It frees
the whole block of each tier, those entries included, and cuts every
thread cache loose without reading it, because the thread that owns a
cache writes it with no synchronization and reading it from the
destroying thread would be a data race. The small allocation of the
cache itself is freed at the owning thread's next allocation from any
pool, when that thread exits, or at process exit, and the pool can
force none of these three. A program that needs its allocator to see
every free should join such threads before it destroys the pool.

crmempool_create(3), crmempool_create_from_preallocated_buffer_mp(3), crmempool_destroy(3), cmempool_create_mp(3), crmempool_total_capacity(3)