template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
class cookmem::MemPool< Arena, Logger, TreeType, T >
A memory allocator.
Note that this class only provides the algorithm necessary to deal with small memory chunks. The actual ability to get memory segment (such as mmap memory pages) is deferred to memory arena.
template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
Allocate memory with an alignment larger than ALLOCATION_ALIGNMENT.
The memory is freed with deallocate () like any other allocation. reallocate () does not keep the alignment if it moves the memory.
- Parameters
-
| userSize | memory request size. |
| alignment | the alignment of the returned pointer. It must be a power of
- An alignment up to ALLOCATION_ALIGNMENT is the same as allocate ().
|
- Returns
- the memory region that is at least the request size and is aligned to the alignment. nullptr if the alignment is not a power of 2, or if the request cannot be satisfied.
template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
template<class Visitor>
Call a visitor function for every allocation that has not been freed, like inspect_all in dlmalloc. This can be used to find memory leaks.
The allocations are visited segment by segment, in address order within each segment. The visitor function must not allocate, free or resize memory of this pool.
- Parameters
-
| visitor | the visitor function: a function, lambda or function object called as visitor (ptr, size) for each allocation, where ptr is the pointer returned to the user and size is the allocation size, as returned by getUserSize (). |
- Returns
- the number of allocations visited.
template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
Gets the allocated size for a pointer.
If we are storing the exact size of the pointer, then the result can match the exact size of user's allocation.
Otherwise, the result is the aligned size.
- Parameters
-
- Returns
- the allocation size. if the pointer is nullptr, the result is 0. if the pointer is not used, the result is 0.
template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
This function reallocate a memory.
The memory is resized in place when possible (see resizeInPlace ()). Otherwise new memory is allocated, the data is copied, and the old memory is freed.
- Parameters
-
| ptr | the current memory pointer. |
| newUserSize | the new size |
- Returns
- the reallocated pointer. If the new size cannot be satisfied, a nullptr is returned and the old pointer remains valid.
template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
Resize memory without moving it, like realloc_in_place in dlmalloc.
Shrinking always succeeds, and the unused tail is freed. Growing succeeds if the memory after the allocation is free and large enough.
- Parameters
-
| ptr | the current memory pointer. |
| newUserSize | the new size |
- Returns
- true if the memory now has the new size at the same address. false if it could not be resized in place, or if ptr is nullptr; the memory is then unchanged.
template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
Set the size at which freed allocations release their memory right away.
A large allocation usually gets a segment of its own. When an allocation at least this large is freed and its segment becomes entirely free, the segment is released to the arena immediately, instead of at the next empty-segment trimming pass. This is similar to the mmap threshold of dlmalloc.
- Parameters
-
| threshold | the allocation size in bytes. The default is DEFAULT_IMMEDIATE_RELEASE_THRESHOLD. 0 disables the immediate release. |
template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
| void cookmem::MemPool< Arena, Logger, TreeType, T >::setStoringExactSize |
( |
bool | b | ) |
|
|
inline |
Set whether or not we should be storing the exact user size in memory.
It can only be changed if the current padding is false.
- Parameters
-
template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
Set the number of deallocations between empty-segment trimming passes.
Memory segments that become entirely free are kept for later allocations. After every interval deallocations, the segments that are entirely free are released to the arena. A smaller interval keeps the footprint smaller, but releases and obtains segments more often.
- Parameters
-
| interval | the number of deallocations between the passes. The default is DEFAULT_TRIM_EMPTY_SEGMENTS_INTERVAL. 1 (or 0) releases empty segments on every deallocation. |