Bitcoin ABC 0.30.5
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. More... | |
PoolResource () | |
Construct a new Pool Resource object, defaults to 2^18=262144 chunk size. More... | |
PoolResource (const PoolResource &)=delete | |
Disable copy & move semantics, these are not supported for the resource. More... | |
PoolResource & | operator= (const PoolResource &)=delete |
PoolResource (PoolResource &&)=delete | |
PoolResource & | operator= (PoolResource &&)=delete |
~PoolResource () | |
Deallocates all memory allocated associated with the memory resource. More... | |
void * | Allocate (std::size_t bytes, std::size_t alignment) |
Allocates a block of bytes. More... | |
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. More... | |
std::size_t | NumAllocatedChunks () const |
Number of allocated chunks. More... | |
size_t | ChunkSizeBytes () const |
Size in bytes to allocate per chunk, currently hardcoded to a fixed size. More... | |
Private Member Functions | |
void | PlacementAddToList (void *p, ListNode *&node) |
Replaces node with placement constructed ListNode that points to the previous node. More... | |
void | AllocateChunk () |
Allocate one full memory chunk which will be used to carve out allocations. More... | |
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. More... | |
static constexpr bool | IsFreeListUsable (std::size_t bytes, std::size_t alignment) |
True when it is possible to make use of the freelist. More... | |
Private Attributes | |
const size_t | m_chunk_size_bytes |
Size in bytes to allocate per chunk. More... | |
std::list< std::byte * > | m_allocated_chunks {} |
Contains all allocated pools of memory, used to free the data in the destructor. More... | |
std::array< ListNode *, MAX_BLOCK_SIZE_BYTES/ELEM_ALIGN_BYTES+1 > | m_free_lists {} |
Single linked lists of all data that came from deallocating. More... | |
std::byte * | m_available_memory_it = nullptr |
Points to the beginning of available memory for carving out allocations. More... | |
std::byte * | m_available_memory_end = nullptr |
Points to the end of available memory for carving out allocations. More... | |
Static Private Attributes | |
static constexpr std::size_t | ELEM_ALIGN_BYTES |
Internal alignment value. More... | |
Friends | |
class | PoolResourceTester |
Access to internals for testing purpose only. More... | |
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 |
|
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 |
|
friend |
|
staticconstexprprivate |
|
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 |