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

#include <context.h>

Inheritance diagram for cookmem::MemContext< Arena, Logger, TreeType, T >:

Public Types

typedef MemContext< Arena, Logger, TreeType, T > MemCtx
 
typedef MemPool< Arena, Logger, TreeType, T > Pool
 
typedef Pool::size_type size_type
 
typedef Pool::difference_type difference_type
 
typedef Pool::value_type value_type
 
typedef Pool::pointer pointer
 
typedef Pool::const_pointer const_pointer
 

Public Member Functions

 MemContext (Arena &arena, Logger &logger, std::uint8_t paddingSize=DEFAULT_PADDING_SIZE)
 
 MemContext (MemCtx &other)
 
T * allocate (size_type size)
 
T * allocateAligned (size_type size, size_type alignment)
 
T * reallocate (T *ptr, size_type size)
 
T * callocate (size_type num, size_type size)
 
void deallocate (T *ptr, size_type size=0)
 
void validate ()
 
bool contains (T *ptr, bool checkUsed=false)
 
size_type getSize (T *ptr)
 
void releaseAll ()
 
void releaseAllToSystem ()
 
size_type getFootprintLimit () const
 
void setFootprintLimit (size_type footprintLimit)
 
size_type getFootprint () const
 
size_type getMaxFootprint () const
 
void setTrimEmptySegmentsInterval (size_type interval)
 
size_type getTrimEmptySegmentsInterval () const
 
void setImmediateReleaseThreshold (size_type threshold)
 
size_type getImmediateReleaseThreshold () 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)
 
Arena & getArena ()
 
Logger & getLogger ()
 
Pool & getPool ()
 

Detailed Description

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.

Member Typedef Documentation

◆ const_pointer

template<class Arena , class Logger , class TreeType = PtrRBTree, class T = void>
typedef Pool::const_pointer cookmem::MemContext< Arena, Logger, TreeType, T >::const_pointer

const pointer type

◆ difference_type

template<class Arena , class Logger , class TreeType = PtrRBTree, class T = void>
typedef Pool::difference_type cookmem::MemContext< Arena, Logger, TreeType, T >::difference_type

pointer difference type

◆ MemCtx

The type of this memory context.

◆ pointer

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

pointer type

◆ Pool

The type of the memory pool that does the actual work.

◆ size_type

size type

◆ value_type

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

value type

Constructor & Destructor Documentation

◆ MemContext() [1/2]

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

Constructor.

Parameters
arenathe arena to obtain memory segments from. It must outlive this context.
loggerthe logger for memory activities. It must outlive this context.
paddingSizethe number of padding bytes (0 to MAX_PADDING_SIZE) added after each allocation to detect out-of-bounds writes.

◆ MemContext() [2/2]

template<class Arena , class Logger , class TreeType = PtrRBTree, class T = void>
cookmem::MemContext< Arena, Logger, TreeType, T >::MemContext ( MemCtx &  other)
inline

Create a new, empty memory context that shares the arena and the logger of another context and uses the same padding size. No memory is copied.

Parameters
otherthe context whose arena and logger are shared.

Member Function Documentation

◆ allocate()

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

Allocate memory from the memory context.

Parameters
sizememory 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::MemContext< Arena, Logger, TreeType, T >::allocateAligned ( size_type  size,
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
sizememory 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::MemContext< 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::MemContext< 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::MemContext< 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.
sizemostly ignored. It is only used by the memory logger.

◆ getArena()

template<class Arena , class Logger , class TreeType = PtrRBTree, class T = void>
Arena & cookmem::MemContext< Arena, Logger, TreeType, T >::getArena ( )
inline

Get the underlying memory arena.

Returns
The underlying memory arena.

◆ getFootprint()

template<class Arena , class Logger , class TreeType = PtrRBTree, class T = void>
size_type cookmem::MemContext< 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::MemContext< 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::MemContext< 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.

◆ getLogger()

template<class Arena , class Logger , class TreeType = PtrRBTree, class T = void>
Logger & cookmem::MemContext< Arena, Logger, TreeType, T >::getLogger ( )
inline

Get the underlying logger.

Returns
The underlying logger.

◆ getMaxFootprint()

template<class Arena , class Logger , class TreeType = PtrRBTree, class T = void>
size_type cookmem::MemContext< 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::MemContext< 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::MemContext< Arena, Logger, TreeType, T >::getPaddingSize ( ) const
inline

Get the number of padding bytes checked after each allocation.

◆ getPool()

template<class Arena , class Logger , class TreeType = PtrRBTree, class T = void>
Pool & cookmem::MemContext< Arena, Logger, TreeType, T >::getPool ( )
inline

Get the underlying memory pool.

Returns
The underlying memory pool.

◆ getSize()

template<class Arena , class Logger , class TreeType = PtrRBTree, class T = void>
size_type cookmem::MemContext< Arena, Logger, TreeType, T >::getSize ( 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.

◆ getTrimEmptySegmentsInterval()

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

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

◆ isPadding()

template<class Arena , class Logger , class TreeType = PtrRBTree, class T = void>
bool cookmem::MemContext< 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::MemContext< 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.

◆ reallocate()

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

This function reallocate a memory.

Parameters
ptrthe current memory pointer.
sizethe 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::MemContext< Arena, Logger, TreeType, T >::releaseAll ( )
inline

Release all the memory segments held by this MemPool.

◆ releaseAllToSystem()

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

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.

◆ setFootprintLimit()

template<class Arena , class Logger , class TreeType = PtrRBTree, class T = void>
void cookmem::MemContext< 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::MemContext< 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.

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::MemContext< 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::MemContext< Arena, Logger, TreeType, T >::setStoringExactSize ( bool  b)
inline

Set whether or not we should be storing the exact user size in memory.

Parameters
bthe boolean choice

◆ setTrimEmptySegmentsInterval()

template<class Arena , class Logger , class TreeType = PtrRBTree, class T = void>
void cookmem::MemContext< 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::MemContext< Arena, Logger, TreeType, T >::validate ( )
inline

Validate the internal memory structures.


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