|
LibSWOC++ 1.5.14
Solid Wall of C++
|
#include <MemArena.h>

Classes | |
| struct | Block |
| Simple internal arena block of memory. Maintains the underlying memory. More... | |
Public Types | |
| using | unique_ptr = std::unique_ptr<self_type, void (*)(self_type *)> |
| using | BlockList = IntrusiveDList<Block::Linkage> |
| Intrusive list of blocks. | |
| using | const_iterator = BlockList::const_iterator |
| Constant element iteration. | |
| using | iterator = const_iterator |
| Element iteration. | |
Public Member Functions | |
| MemArena (size_t n=DEFAULT_BLOCK_SIZE) | |
| MemArena (MemSpan< void > static_block) | |
| MemArena (self_type const &that)=delete | |
| no copying | |
| MemArena (self_type &&that) noexcept | |
| Allow moving the arena. | |
| ~MemArena () | |
| Destructor. | |
| self_type & | operator= (self_type const &that)=delete |
| No copy assignment. | |
| self_type & | operator= (self_type &&that) noexcept |
| Move assignment. | |
| MemSpan< void > | alloc (size_t n, size_t align=DEFAULT_ALIGNMENT) |
| template<typename T> | |
| MemSpan< T > | alloc_span (size_t n) |
| template<typename T, typename... Args> | |
| T * | make (Args &&...args) |
| MemSpan< char > | localize (MemSpan< char const > s) |
| MemSpan< char > | localize (char const *s) |
| MemSpan< char > | localize_c (MemSpan< char const > s) |
| MemSpan< char > | localize_c (char const *s) |
| MemArena & | freeze (size_t n=0) |
| self_type & | thaw () |
| MemArena & | clear (size_t hint=0) |
| MemArena & | discard (MemSpan< void const > span) |
| MemArena & | discard (size_t hint=0) |
| size_t | size () const |
| size_t | remaining () const |
| template<typename T> | |
| MemSpan< T > | remnant_span (size_t n) |
| MemSpan< void > | remnant () |
| MemSpan< void > | remnant (size_t n, size_t align=DEFAULT_ALIGNMENT) |
| self_type & | require (size_t n, size_t align=DEFAULT_ALIGNMENT) |
| size_t | allocated_size () const |
| bool | contains (const void *ptr) const |
| size_t | reserved_size () const |
| const_iterator | begin () const |
| First active block. | |
| const_iterator | end () const |
| After Last active block. | |
| const_iterator | frozen_begin () const |
| First frozen block. | |
| const_iterator | frozen_end () const |
| After last frozen block. | |
Static Public Member Functions | |
| static self_type * | construct_self_contained (size_t n=DEFAULT_BLOCK_SIZE) |
Static Public Attributes | |
| static constexpr size_t | DEFAULT_ALIGNMENT {1} |
| Default memory alignment. | |
| static void(* | destroyer )(self_type *) = std::destroy_at<MemArena> |
Protected Types | |
| using | Page = Scalar<4096> |
| Size for rounding block sizes. | |
| using | QuarterPage = Scalar<Page::SCALE / 4> |
| Quarter page - unit for sub page sizes. | |
| using | Paragraph = Scalar<16> |
| Minimum unit of memory allocation. | |
Protected Member Functions | |
| Block * | make_block (size_t n) |
| void | destroy_frozen () |
| Clean up the frozen list. | |
| void | destroy_active () |
| Clean up the active list. | |
Protected Attributes | |
| size_t | _active_allocated = 0 |
| Total allocations in the active generation. | |
| size_t | _active_reserved = 0 |
| size_t | _frozen_allocated = 0 |
| Total allocations in the previous generation. This is only non-zero while the arena is frozen. | |
| size_t | _frozen_reserved = 0 |
| Total frozen reserved memory. | |
| size_t | _reserve_hint = 0 |
| BlockList | _frozen |
| Previous generation, frozen memory. | |
| BlockList | _active |
| Current generation. Allocate here. | |
| Block * | _static_block = nullptr |
| Static block, if any. | |
Static Protected Attributes | |
| static constexpr size_t | ALLOC_HEADER_SIZE = 16 |
| static constexpr size_t | DEFAULT_BLOCK_SIZE = Page::SCALE - Paragraph{round_up(ALLOC_HEADER_SIZE + sizeof(Block))} |
| Initial block size to allocate if not specified via API. | |
A memory arena.
The intended use is for allocating many small chunks of memory - few, large allocations are best handled through other mechanisms. The purpose is to amortize the cost of allocation of each chunk across larger internal allocations ("reserving memory"). In addition the allocated memory chunks are presumed to have similar lifetimes so all of the memory in the arena can be released when the arena is destroyed.
Definition at line 33 of file MemArena.h.
| using swoc::MemArena::BlockList = IntrusiveDList<Block::Linkage> |
Intrusive list of blocks.
Definition at line 178 of file MemArena.h.
| using swoc::MemArena::const_iterator = BlockList::const_iterator |
Constant element iteration.
Definition at line 447 of file MemArena.h.
Element iteration.
Definition at line 448 of file MemArena.h.
|
protected |
Size for rounding block sizes.
Definition at line 476 of file MemArena.h.
|
protected |
Minimum unit of memory allocation.
Definition at line 478 of file MemArena.h.
|
protected |
Quarter page - unit for sub page sizes.
Definition at line 477 of file MemArena.h.
| using swoc::MemArena::unique_ptr = std::unique_ptr<self_type, void (*)(self_type *)> |
Correct type for a unique pointer to an instance. Initialization is
To create the arena on demand
If the unique pointer is to be initialized with an arena, it should probably be a direct member isntead.
Definition at line 57 of file MemArena.h.
|
explicit |
Construct with reservation hint.
No memory is initially reserved, but when memory is needed this will be done so at least n bytes of available memory is reserved.
To pre-reserve call alloc(0), e.g.
| n | Minimum number of available bytes in the first internally reserved block. |
|
explicit |
Construct using static block.
| static_block | A block of memory that is non-deletable. |
static_block is used as the first block for allocation and is never deleted. This makes it possible to have an instance that allocates from stack memory and only allocates from the heap if the static block becomes full.
Definition at line 21 of file MemArena.cc.
|
noexcept |
Allow moving the arena.
Definition at line 37 of file MemArena.cc.
| swoc::MemArena::~MemArena | ( | ) |
Destructor.
Definition at line 256 of file MemArena.cc.
| MemSpan< void > swoc::MemArena::alloc | ( | size_t | n, |
| size_t | align = DEFAULT_ALIGNMENT ) |
Allocate n bytes of storage.
Returns a span of memory within the arena. alloc() is self expanding but DOES NOT self coalesce. This means that no matter the arena size, the caller will always be able to alloc() n bytes.
| n | number of bytes to allocate. |
| align | Required alignment, defaults to 1 (no alignment). Must be a power of 2. |
Definition at line 103 of file MemArena.cc.
| MemSpan< T > swoc::MemArena::alloc_span | ( | size_t | n | ) |
ALlocate a span of memory sufficient for n instance of T.
| T | Element type. |
| n | Number of instances. |
The instances are not initialized / constructed. This only allocates the memory. This is handy for types that don't need initialization, such as built in types like int.
The memory is aligned according to alignof(T).
| size_t swoc::MemArena::allocated_size | ( | ) | const |
| MemArena & swoc::MemArena::clear | ( | size_t | hint = 0 | ) |
Release all memory.
Empties the entire arena and deallocates all underlying memory. The hint for the next reserved block size will be n if n is not zero, otherwise it will be the sum of all allocations when this method was called.
| hint | Size hint for the next internal allocation. |
Definition at line 202 of file MemArena.cc.
|
static |
Make a self-contained instance.
| n | The initial memory size hint. |
Create an instance of MemArena that is stored in its own memory pool. The size hint n is adjusted to account for the space consumed by the MemArena instance. This instance will therefore always have done its initial internal memory allocation to provide space for itself.
This is most useful for smaller objects that need to strongly minimize their size when not allocating memory. In that context, this enables being able to have a memory pool as needed at the cost of a only single pointer in the instance.
MemArena itself will be in the frozen memory and must be moved to the fresh allocation.delete must not be called on the returned pointer. Instead the MemArena destructor must be explicitly called, which will clean up all of the allocated memory. See the documentation for further details. Definition at line 54 of file MemArena.cc.
| bool swoc::MemArena::contains | ( | const void * | ptr | ) | const |
Check if a the byte at ptr is in memory owned by this arena.
| ptr | Address of byte to check. |
true if the byte at ptr is in the arena, false if not. Definition at line 145 of file MemArena.cc.
|
protected |
Clean up the active list.
Definition at line 178 of file MemArena.cc.
|
protected |
Clean up the frozen list.
Definition at line 190 of file MemArena.cc.
| MemArena & swoc::MemArena::discard | ( | MemSpan< void const > | span | ) |
Best effort allocation discard.
The allocation is discard (become unallocated memory) if and only if it is at the end of a recent allocation block. If nothing has been allocated, this always works. Otherwise if later allocation exists, this method silently fails. This can work with multiple allocations in a stack - if all later allocations are discarded when this method is invoked it will succeed.
remnant | hint | Size hint for the next internal allocation. |
Definition at line 213 of file MemArena.cc.
| MemArena & swoc::MemArena::discard | ( | size_t | hint = 0 | ) |
Discard all allocations.
All active internal memory blocks are reset to be empty, discarding any allocations. These blocks will be re-used by subsequent allocations.
| hint | Size hint for the next internal allocation. |
Definition at line 243 of file MemArena.cc.
| MemArena & swoc::MemArena::freeze | ( | size_t | n = 0 | ) |
Freeze reserved memory.
All internal memory blocks are frozen and will not be involved in future allocations. Subsequent allocation will reserve new internal blocks. By default the first reserved block will be large enough to contain all frozen memory. If this is not correct a different target can be specified as n.
| n | Target number of available bytes in the next reserved internal block. |
*this Definition at line 118 of file MemArena.cc.
| MemSpan< char > swoc::MemArena::localize | ( | char const * | s | ) |
Copy the contents of a string view into the arena.
| s | Original string. |
| MemSpan< char > swoc::MemArena::localize | ( | MemSpan< char const > | s | ) |
Copy the contents of a string view into the arena.
| s | Original string. |
| MemSpan< char > swoc::MemArena::localize_c | ( | char const * | s | ) |
Copy the contents of a string view into the arena as a C string.
| s | Original string. |
A terminating nul character is added to the copy which is not included in the returned view. This enables using the string view as a C string.
| MemSpan< char > swoc::MemArena::localize_c | ( | MemSpan< char const > | s | ) |
Copy the contents of a string view into the arena as a C string.
| s | Original string. |
A terminating nul character is added to the copy which is not included in the returned view. This enables using the string view as a C string.
| T * swoc::MemArena::make | ( | Args &&... | args | ) |
Allocate and initialize a block of memory as an instance of T
The template type specifies the type to create and any arguments are forwarded to the constructor. Example:
Do not call delete an object created this way - that will attempt to free the memory and break. A destructor may be invoked explicitly but the point of this class is that no object in it needs to be deleted, the memory will all be reclaimed when the Arena is destroyed. In general it is a bad idea to make objects in the Arena that own memory that is not also in the Arena.
|
protected |
Internally allocates a new block of memory of size n bytes.
| n | Size of block to allocate. |
Definition at line 73 of file MemArena.cc.
Move assignment.
Definition at line 60 of file MemArena.cc.
| size_t swoc::MemArena::remaining | ( | ) | const |
| MemSpan< void > swoc::MemArena::remnant | ( | ) |
| MemSpan< void > swoc::MemArena::remnant | ( | size_t | n, |
| size_t | align = DEFAULT_ALIGNMENT ) |
Get an aligned remnant.
| n | Remnant size. |
| align | Memory alignment (default 1, must be power of 2). |
| MemSpan< T > swoc::MemArena::remnant_span | ( | size_t | n | ) |
Get aligned and sized remnant.
| T | Element type. |
| n | Number of instances of T |
This is guaranteed to be the same bytes as if alloc<T> was called. The returned span will always be the specified size, the remnant will be expanded as needed.
| MemArena & swoc::MemArena::require | ( | size_t | n, |
| size_t | align = DEFAULT_ALIGNMENT ) |
Require n bytes of contiguous memory to be available for allocation.
| n | Number of bytes. |
| align | Align requirement (default is 1, no alignment). |
This forces the remnant to be at least n bytes of contiguous memory. A subsequent alloc will use this space if the allocation size is at most the remnant size.
Definition at line 152 of file MemArena.cc.
| size_t swoc::MemArena::reserved_size | ( | ) | const |
Total memory footprint, including wasted space.
| size_t swoc::MemArena::size | ( | ) | const |
| MemArena & swoc::MemArena::thaw | ( | ) |
|
protected |
Current generation. Allocate here.
Definition at line 495 of file MemArena.h.
|
protected |
Total allocations in the active generation.
Definition at line 484 of file MemArena.h.
|
protected |
Total reserved memory (allocated from OS).
Definition at line 485 of file MemArena.h.
|
protected |
Previous generation, frozen memory.
Definition at line 494 of file MemArena.h.
|
protected |
Total allocations in the previous generation. This is only non-zero while the arena is frozen.
Definition at line 487 of file MemArena.h.
|
protected |
Total frozen reserved memory.
Definition at line 488 of file MemArena.h.
|
protected |
Minimum free space needed in the next allocated block. This is not zero iff reserve was called.
Definition at line 492 of file MemArena.h.
|
protected |
Static block, if any.
Definition at line 498 of file MemArena.h.
|
staticconstexprprotected |
Guess of overhead of malloc
Definition at line 480 of file MemArena.h.
|
staticconstexpr |
Default memory alignment.
Definition at line 41 of file MemArena.h.
|
staticconstexprprotected |
Initial block size to allocate if not specified via API.
Definition at line 482 of file MemArena.h.
Convenient alias for use with unique_ptr.
Definition at line 45 of file MemArena.h.