CookMem 1.6.0
A C++ memory context / allocator
Loading...
Searching...
No Matches
cookmem::MemPool< Arena, Logger, TreeType, T > Class Template Reference

#include <pool.h>

Public Types

typedef std::size_t size_type
typedef std::ptrdiff_t difference_type
typedef T value_type
typedef T * pointer
typedef const T * const_pointer

Public Member Functions

 MemPool (Arena &arena, Logger &logger, std::uint8_t paddingSize=DEFAULT_PADDING_SIZE)
 ~MemPool ()
void setFootprintLimit (size_type footprintLimit)
size_type getFootprintLimit () const
void setTrimEmptySegmentsInterval (size_type interval)
size_type getTrimEmptySegmentsInterval () const
void setImmediateReleaseThreshold (size_type threshold)
size_type getImmediateReleaseThreshold () const
T * allocate (size_type userSize)
T * allocateAligned (size_type userSize, size_type alignment)
T * reallocate (T *ptr, size_type newUserSize)
bool resizeInPlace (T *ptr, size_type newUserSize)
T * callocate (size_type num, size_type size)
void deallocate (T *ptr, size_type size=0)
bool contains (T *ptr, bool checkUsed=false)
void validate ()
size_type getUserSize (T *ptr)
template<class Visitor>
size_type forEachAllocation (Visitor visitor) const
void releaseAll ()
size_type getFootprint () const
size_type getMaxFootprint () const
bool isStoringExactSize () const
void setStoringExactSize (bool b)
bool isPadding () const
std::uint8_t getPaddingSize () const
std::uint8_t getPaddingByte () const
void setPaddingByte (std::uint8_t paddingByte)
void print () const

Static Public Member Functions

static unsigned int getTreeBinIndex (size_type chunkSize)

Detailed Description

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.

Member Typedef Documentation

◆ const_pointer

template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
typedef const T* cookmem::MemPool< Arena, Logger, TreeType, T >::const_pointer

const pointer type

◆ difference_type

template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
typedef std::ptrdiff_t cookmem::MemPool< Arena, Logger, TreeType, T >::difference_type

pointer difference type

◆ pointer

template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
typedef T* cookmem::MemPool< Arena, Logger, TreeType, T >::pointer

pointer type

◆ size_type

template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
typedef std::size_t cookmem::MemPool< Arena, Logger, TreeType, T >::size_type

size type

◆ value_type

template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
typedef T cookmem::MemPool< Arena, Logger, TreeType, T >::value_type

value type

Constructor & Destructor Documentation

◆ MemPool()

template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
cookmem::MemPool< Arena, Logger, TreeType, T >::MemPool ( Arena & arena,
Logger & logger,
std::uint8_t paddingSize = DEFAULT_PADDING_SIZE )
inline

Constructor

Parameters
arenaMemory arena used to allocate memory segments.
loggerLogging facility used to log memory allocation and deallocation.
paddingSizeThe number of bytes to pad after each allocation to detect out of bound access. 0 disables the padding check.

◆ ~MemPool()

template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
cookmem::MemPool< Arena, Logger, TreeType, T >::~MemPool ( )
inline

Destructor.

It releases all the memory segments used.

Member Function Documentation

◆ allocate()

template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
T * cookmem::MemPool< Arena, Logger, TreeType, T >::allocate ( size_type userSize)
inline

Allocate memory from the memory pool.

Parameters
userSizememory request size.
Returns
the memory region that is at least the request size. nullptr if the request cannot be satisfied.

◆ allocateAligned()

template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
T * cookmem::MemPool< Arena, Logger, TreeType, T >::allocateAligned ( size_type userSize,
size_type alignment )
inline

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
userSizememory request size.
alignmentthe alignment of the returned pointer. It must be a power of
  1. 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.

◆ callocate()

template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
T * cookmem::MemPool< Arena, Logger, TreeType, T >::callocate ( size_type num,
size_type size )
inline

Allocate n items of certain size. The memory allocated is zeroed.

Parameters
numnumber of elements
sizeelement size
Returns
memory allocated. nullptr if num * size overflows, or if the request cannot be satisfied.

◆ contains()

template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
bool cookmem::MemPool< Arena, Logger, TreeType, T >::contains ( T * ptr,
bool checkUsed = false )
inline

Check if a pointer is within the address space of segments owned by this MemPool.

Parameters
ptrmemory pointer
checkUsedcheck the pointer if it is used.
Returns
whether the memory address is in the space of segments owned by this MemPool. If checkUsed is true, the result is true only if the pointer is also marked as used.

◆ deallocate()

template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
void cookmem::MemPool< Arena, Logger, TreeType, T >::deallocate ( T * ptr,
size_type size = 0 )
inline

Free a piece of memory previously allocated by this memory pool.

Parameters
ptra piece of memory to be freed.
sizeignored.

◆ forEachAllocation()

template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
template<class Visitor>
size_type cookmem::MemPool< Arena, Logger, TreeType, T >::forEachAllocation ( Visitor visitor) const
inline

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
visitorthe 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.

◆ getFootprint()

template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
size_type cookmem::MemPool< Arena, Logger, TreeType, T >::getFootprint ( ) const
inline

Get the current memory footprint.

Returns
the current memory footprint.

◆ getFootprintLimit()

template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
size_type cookmem::MemPool< Arena, Logger, TreeType, T >::getFootprintLimit ( ) const
inline

Get the memory footprint limit.

Returns
the memory footprint limit

◆ getImmediateReleaseThreshold()

template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
size_type cookmem::MemPool< Arena, Logger, TreeType, T >::getImmediateReleaseThreshold ( ) const
inline

Get the size at which freed allocations release their memory right away.

Returns
the allocation size in bytes. 0 means disabled.

◆ getMaxFootprint()

template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
size_type cookmem::MemPool< Arena, Logger, TreeType, T >::getMaxFootprint ( ) const
inline

Get the maximum memory footprint.

Returns
the maximum memory footprint.

◆ getPaddingByte()

template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
std::uint8_t cookmem::MemPool< Arena, Logger, TreeType, T >::getPaddingByte ( ) const
inline

Get the padding byte.

Returns
The padding byte.

◆ getPaddingSize()

template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
std::uint8_t cookmem::MemPool< Arena, Logger, TreeType, T >::getPaddingSize ( ) const
inline

Get the number of padding bytes checked after each allocation.

◆ getTreeBinIndex()

template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
unsigned int cookmem::MemPool< Arena, Logger, TreeType, T >::getTreeBinIndex ( size_type chunkSize)
inlinestatic

Get the tree bin that holds free chunks of a given size. Chunks of 12 MB and more all go in the last bin.

Parameters
chunkSizethe chunk size, which must be at least the smallest large chunk size (256 bytes).
Returns
the tree bin index.

◆ getTrimEmptySegmentsInterval()

template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
size_type cookmem::MemPool< Arena, Logger, TreeType, T >::getTrimEmptySegmentsInterval ( ) const
inline

Get the number of deallocations between empty-segment trimming passes.

Returns
the number of deallocations between empty-segment trimming passes.

◆ getUserSize()

template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
size_type cookmem::MemPool< Arena, Logger, TreeType, T >::getUserSize ( T * ptr)
inline

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
ptrA pointer.
Returns
the allocation size. if the pointer is nullptr, the result is 0. if the pointer is not used, the result is 0.

◆ isPadding()

template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
bool cookmem::MemPool< Arena, Logger, TreeType, T >::isPadding ( ) const
inline

Check whether or not we are padding bytes at the end of the allocated memory.

Returns
whether or not we are padding bytes at the end of the allocated memory.

◆ isStoringExactSize()

template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
bool cookmem::MemPool< Arena, Logger, TreeType, T >::isStoringExactSize ( ) const
inline

Check whether or not we are storing the exact user size in the memory.

Returns
whether or not we are storing the exact user size in the memory.

◆ print()

template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
void cookmem::MemPool< Arena, Logger, TreeType, T >::print ( ) const
inline

Print the memory segments and chunks of the pool to stdout. This is for debugging.

◆ reallocate()

template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
T * cookmem::MemPool< Arena, Logger, TreeType, T >::reallocate ( T * ptr,
size_type newUserSize )
inline

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
ptrthe current memory pointer.
newUserSizethe new size
Returns
the reallocated pointer. If the new size cannot be satisfied, a nullptr is returned and the old pointer remains valid.

◆ releaseAll()

template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
void cookmem::MemPool< Arena, Logger, TreeType, T >::releaseAll ( )
inline

Release all the memory segments held by this MemPool.

◆ resizeInPlace()

template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
bool cookmem::MemPool< Arena, Logger, TreeType, T >::resizeInPlace ( T * ptr,
size_type newUserSize )
inline

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
ptrthe current memory pointer.
newUserSizethe 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.

◆ setFootprintLimit()

template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
void cookmem::MemPool< Arena, Logger, TreeType, T >::setFootprintLimit ( size_type footprintLimit)
inline

Set the maximum memory footprint limit. Allocations that would need new memory segments beyond this limit fail and return nullptr.

Parameters
footprintLimitthe maximum total size of the memory segments in bytes. 0 means no limit.

◆ setImmediateReleaseThreshold()

template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
void cookmem::MemPool< Arena, Logger, TreeType, T >::setImmediateReleaseThreshold ( size_type threshold)
inline

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
thresholdthe allocation size in bytes. The default is DEFAULT_IMMEDIATE_RELEASE_THRESHOLD. 0 disables the immediate release.

◆ setPaddingByte()

template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
void cookmem::MemPool< Arena, Logger, TreeType, T >::setPaddingByte ( std::uint8_t paddingByte)
inline

Set the padding byte.

Parameters
paddingByteThe padding byte.

◆ setStoringExactSize()

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
bthe boolean choice

◆ setTrimEmptySegmentsInterval()

template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
void cookmem::MemPool< Arena, Logger, TreeType, T >::setTrimEmptySegmentsInterval ( size_type interval)
inline

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
intervalthe number of deallocations between the passes. The default is DEFAULT_TRIM_EMPTY_SEGMENTS_INTERVAL. 1 (or 0) releases empty segments on every deallocation.

◆ validate()

template<class Arena, class Logger, class TreeType = PtrRBTree, class T = void>
void cookmem::MemPool< Arena, Logger, TreeType, T >::validate ( )
inline

Validate that all memory structures are correct.


The documentation for this class was generated from the following file: