template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
class cookmem::MemContext< Arena, Logger, TreeType, T >
A basic memory context that creates a MemPool based on the arena and logger references user passed in.
This class is mostly used in cases when arena or logger that do not have default constructors.
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
-
| size | 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 U, class... Args>
Allocate memory for an object of type U and construct it, like new.
The memory is aligned to alignof (U), including alignments larger than ALLOCATION_ALIGNMENT. Free the object with destroy (), or free all objects at once with releaseAll () or by destroying the context; those do not call destructors.
If the constructor throws, the memory is deallocated and the exception is passed on.
- Parameters
-
| args | the arguments passed to the constructor of U. |
- Returns
- the new object.
- Exceptions
-
| std::bad_alloc | if the memory cannot be allocated. Unlike allocate (), this function does not return nullptr. |
template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
template<class U>
Destroy an object created by create (), like delete: call its destructor, then deallocate its memory.
A pointer to a polymorphic base class works, as with delete, when the destructor is virtual.
- Parameters
-
| ptr | the object to destroy. Nothing is done if it is nullptr. |
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 context.
- 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 getSize (). |
- 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>
Release all the memory segments held by this MemPool, then the segments cached by the arena, so that the memory is returned to the system. Every pointer allocated from this context becomes invalid.
Unlike releaseAll (), this also empties the cache of a CachedArena, including the segments that other contexts sharing the arena could have reused.
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. |
| size | 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.
- 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>
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. |