Bitcoin ABC 0.26.3
P2P Digital Currency
|
A memory resource similar to std::pmr::unsynchronized_pool_resource, but optimized for node-based containers. More...
#include <pool.h>
Classes | |
struct | ListNode |
In-place linked list of the allocations, used for the freelist. More... | |
Public Member Functions | |
PoolResource (std::size_t chunk_size_bytes) | |
Construct a new PoolResource object which allocates the first chunk. | |
PoolResource () | |
Construct a new Pool Resource object, defaults to 2^18=262144 chunk size. | |
PoolResource (const PoolResource &)=delete | |
Disable copy & move semantics, these are not supported for the resource. | |
PoolResource & | operator= (const PoolResource &)=delete |
PoolResource (PoolResource &&)=delete | |
PoolResource & | operator= (PoolResource &&)=delete |
~PoolResource () | |
Deallocates all memory allocated associated with the memory resource. | |
void * | Allocate (std::size_t bytes, std::size_t alignment) |
Allocates a block of bytes. | |
void | Deallocate (void *p, std::size_t bytes, std::size_t alignment) noexcept |
Returns a block to the freelists, or deletes the block when it did not come from the chunks. | |
std::size_t | NumAllocatedChunks () const |
Number of allocated chunks. | |
size_t | ChunkSizeBytes () const |
Size in bytes to allocate per chunk, currently hardcoded to a fixed size. | |
Private Member Functions | |
void | PlacementAddToList (void *p, ListNode *&node) |
Replaces node with placement constructed ListNode that points to the previous node. | |
void | AllocateChunk () |
Allocate one full memory chunk which will be used to carve out allocations. | |
Static Private Member Functions | |
static constexpr std::size_t | NumElemAlignBytes (std::size_t bytes) |
How many multiple of ELEM_ALIGN_BYTES are necessary to fit bytes. | |
static constexpr bool | IsFreeListUsable (std::size_t bytes, std::size_t alignment) |
True when it is possible to make use of the freelist. | |
Private Attributes | |
const size_t | m_chunk_size_bytes |
Size in bytes to allocate per chunk. | |
std::list< std::byte * > | m_allocated_chunks {} |
Contains all allocated pools of memory, used to free the data in the destructor. | |
std::array< ListNode *, MAX_BLOCK_SIZE_BYTES/ELEM_ALIGN_BYTES+1 > | m_free_lists {} |
Single linked lists of all data that came from deallocating. | |
std::byte * | m_available_memory_it = nullptr |
Points to the beginning of available memory for carving out allocations. | |
std::byte * | m_available_memory_end = nullptr |
Points to the end of available memory for carving out allocations. | |
Static Private Attributes | |
static constexpr std::size_t | ELEM_ALIGN_BYTES |
Internal alignment value. | |
Friends | |
class | PoolResourceTester |
Access to internals for testing purpose only. | |
A memory resource similar to std::pmr::unsynchronized_pool_resource, but optimized for node-based containers.
It has the following properties:
PoolResource is not thread-safe. It is intended to be used by PoolAllocator.
MAX_BLOCK_SIZE_BYTES | Maximum size to allocate with the pool. If larger sizes are requested, allocation falls back to new(). |
ALIGN_BYTES | Required alignment for the allocations. |
An example: If you create a PoolResource<128, 8>(262144) and perform a bunch of allocations and deallocate 2 blocks with size 8 bytes, and 3 blocks with size 16, the members will look like this:
m_free_lists m_allocated_chunks ┌───┐ ┌───┐ ┌──────────----—──────┐ │ │ blocks │ ├─►│ 262144 B │ │ │ ┌─────┐ ┌─────┐ └─┬─┘ └──────────----—──────┘ │ 1 ├─►│ 8 B ├─►│ 8 B │ │ │ │ └─────┘ └─────┘ : │ │ │ │ │ ┌─────┐ ┌─────┐ ┌─────┐ ▼ │ 2 ├─►│16 B ├─►│16 B ├─►│16 B │ ┌───┐ ┌───────────────────────┐ │ │ └─────┘ └─────┘ └─────┘ │ ├─►│ ▲ │ ▲ │ │ └───┘ └──────┬────────────────┘ │ │ . │ │ m_available_memory_end │ . │ m_available_memory_it │ . │ │ │ │ │ │16 │ └───┘
Here m_free_lists[1] holds the 2 blocks of size 8 bytes, and m_free_lists[2] holds the 3 blocks of size 16. The blocks came from the data stored in the m_allocated_chunks list. Each chunk has bytes 262144. The last chunk has still some memory available for the blocks, and when m_available_memory_it is at the end, a new chunk will be allocated and added to the list.
|
inlineexplicit |
Construct a new PoolResource object which allocates the first chunk.
chunk_size_bytes will be rounded up to next multiple of ELEM_ALIGN_BYTES.
Definition at line 195 of file pool.h.
|
inline |
|
delete |
Disable copy & move semantics, these are not supported for the resource.
|
delete |
|
inline |
|
inline |
Allocates a block of bytes.
If possible the freelist is used, otherwise allocation is forwarded to operator new().
Definition at line 230 of file pool.h.
|
inlineprivate |
Allocate one full memory chunk which will be used to carve out allocations.
Also puts any leftover bytes into the freelist.
Precondition: leftover bytes are either 0 or few enough to fit into a place in the freelist
Definition at line 167 of file pool.h.
|
inline |
|
inlinenoexcept |
|
inlinestaticconstexprprivate |
|
inline |
|
inlinestaticconstexprprivate |
|
delete |
|
delete |
|
inlineprivate |
|
staticconstexprprivate |
Internal alignment value.
The larger of the requested ALIGN_BYTES and alignof(FreeList).
|
private |
|
private |
Points to the end of available memory for carving out allocations.
That member variable is redundant, and is always equal to m_allocated_chunks.back() + m_chunk_size_bytes
whenever it is accessed, but m_available_memory_end
caches this for clarity and efficiency.
|
private |
|
private |
|
private |