memory_pool.h¶
A per-thread pool for the buffers the library allocates and frees repeatedly.
A buffer released with mempool_free() is kept by the releasing thread and handed out again for a request of the same size. What the pool retains over all threads is bounded by its capacity. Buffers below 4 KiB, and every buffer in a build with AddressSanitizer, bypass the pool.
Allocation failures exit the process, as in alloc.h.
Note
Thread-safe. A forked child starts with an empty pool.
Defines
-
MEMPOOL_CAPACITY_AUTO¶
A capacity that retains at most the most the pool ever had handed out at once.
-
MEMPOOL_CAPACITY_DEFAULT¶
The default capacity:
VFHE_MEMPOOL_CAPACITYin bytes when set, otherwise MEMPOOL_CAPACITY_AUTO.
Functions
-
void *mempool_aligned_malloc(size_t bytes)¶
Allocates uninitialised memory aligned to 64 bytes, from the pool when it holds a buffer of this size.
Warning
Exits the process with
EXIT_FAILUREif the allocation is refused.- Parameters:
bytes – [in] Bytes to allocate.
- Returns:
Memory to release with mempool_free(), or with plain
free.
-
void mempool_free(void *ptr, size_t bytes)¶
Releases a buffer to the calling thread’s pool.
Zeroes it first when mempool_wipe_on_release() says so. A buffer the capacity leaves no room for goes back to the C library.
- Parameters:
ptr – [in] Memory from mempool_aligned_malloc() or safe_aligned_malloc(), or NULL, which does nothing.
bytes – [in] The size it was allocated with.
-
void mempool_free_and_wipe(void *ptr, size_t bytes)¶
Releases a buffer as mempool_free() does, always zeroing it first.
For buffers known to hold secrets.
- Parameters:
ptr – [in] As for mempool_free().
bytes – [in] As for mempool_free().
-
void mempool_set_wipe_on_release(int enabled)¶
Sets whether every buffer released to the pool is zeroed first.
- Parameters:
enabled – [in] 1 to zero, 0 not to, or a negative value to restore the default: on when
VFHE_MEMPOOL_WIPE_ON_RELEASEis1, off otherwise.
-
int mempool_wipe_on_release(void)¶
Returns whether every buffer released to the pool is zeroed first.
- Returns:
1 or 0.
-
void *mempool_aligned_malloc_with_size_header(size_t bytes)¶
Allocates as mempool_aligned_malloc() does, recording the size ahead of the buffer.
For an owner that cannot keep the size, such as a cffi free callback.
Warning
Exits the process with
EXIT_FAILUREif the allocation is refused.- Parameters:
bytes – [in] Bytes to allocate.
- Returns:
Memory to release with mempool_free_with_size_header() only.
-
void mempool_free_with_size_header(void *ptr)¶
Releases a buffer from mempool_aligned_malloc_with_size_header().
- Parameters:
ptr – [in] The buffer, or NULL, which does nothing.
-
void mempool_release_all(void)¶
Returns every buffer retained, on every thread, to the C library.
-
void mempool_set_capacity(uint64_t bytes)¶
Sets the most the pool retains over all threads.
Lowering it below what is retained releases everything.
- Parameters:
bytes – [in] A number of bytes (0 retains nothing), MEMPOOL_CAPACITY_AUTO or MEMPOOL_CAPACITY_DEFAULT.
-
uint64_t mempool_capacity(void)¶
Returns the capacity setting.
- Returns:
A number of bytes, or MEMPOOL_CAPACITY_AUTO.
-
void mempool_statistics(MempoolStatistics *out)¶
Reads what the pool holds and how it has served requests.
- Parameters:
out – [out] Filled in.
-
struct MempoolStatistics¶
- #include <memory_pool.h>
What the pool holds and how it has served requests.
retained_bytes,outstanding_bytes,hitsandmissesare exact. Threads track what they hand out locally and share it in steps of a few MiB, sopeak_outstanding_bytes, which the automatic capacity follows, is exact while one thread uses the pool and may otherwise miss a short-lived peak by up to a few MiB per thread.Public Members
-
uint64_t retained_bytes¶
In free lists, over all threads.
-
uint64_t outstanding_bytes¶
Handed out, not yet released to the pool.
-
uint64_t peak_outstanding_bytes¶
The highest
outstanding_bytesso far.
-
uint64_t retention_limit_bytes¶
What the capacity allows right now.
-
uint64_t hits¶
Requests served from a free list.
-
uint64_t misses¶
Requests passed to the C library.
-
uint64_t retained_bytes¶