C Collections

cmempool · Section 3

cmempool_allocated_bytes

cmempool_allocated_bytes - report the memory a pool holds for its entries

C Collections (libccollections, -lccollections)

#include <ccollections/cmempool.h>
 
size_t cmempool_allocated_bytes(cmempool *mp);

Returns the size in bytes of the single block that mp holds for its
entries and for the state of each entry.
 
This is what the pool really costs, and cmempool_total_capacity(3)
does not report it on purpose. A thread-safe pool that is large enough
to keep a cache for each thread holds a reserve above the count it was
created with, so it uses more memory than its usable capacity suggests.
Use this call to fit a pool into a memory budget instead of repeating
the reserve arithmetic yourself.

mp                        Memory pool to query (must not be NULL)

Returns the size in bytes of the entry block of the pool

Only the entry block is counted. The handle itself is a small fixed
allocation, and each thread's cache is a separate allocation that the
library makes when that thread first uses the pool; the same reserve
bounds those caches. An entry served by the dynamic fallback is not
part of this block at all, and cmempool_dynamic_allocs_count(3)
accounts for those.
 
For a pool built by cmempool_create_from_preallocated_buffer_mp(3),
the call reports the part of the caller's buffer that the pool divided
into entries. Such a pool never gets a cache and has no reserve, so
the figure is exactly the element count times the stride plus one
status byte for each element, and the pool allocated none of it.
 
The value stays the same for the whole lifetime of the pool.
 
A NULL mp is a fatal error: The call prints a message and ends the
process with abort(), in every build.

Thread-safe when the pool was created with single_threaded = false.
The call takes the pool's lock, so it belongs on a cold path; don't
use it inside a hot loop.

cmempool_total_capacity(3), cmempool_used_count(3), cmempool_dynamic_allocs_count(3), crmempool_allocated_bytes(3), cmempool_create_mp(3)