5#ifndef BITCOIN_TEST_UTIL_POOLRESOURCETESTER_H
6#define BITCOIN_TEST_UTIL_POOLRESOURCETESTER_H
26 :
ptr(reinterpret_cast<uintptr_t>(p)),
size(
s)
43 template <std::
size_t MAX_BLOCK_SIZE_BYTES, std::
size_t ALIGN_BYTES>
46 auto sizes = std::vector<std::size_t>();
49 while (ptr !=
nullptr) {
53 sizes.push_back(size);
61 template <std::
size_t MAX_BLOCK_SIZE_BYTES, std::
size_t ALIGN_BYTES>
74 template <std::
size_t MAX_BLOCK_SIZE_BYTES, std::
size_t ALIGN_BYTES>
78 std::vector<PtrAndBytes> free_blocks;
79 for (std::size_t freelist_idx = 0; freelist_idx < resource.
m_free_lists.size(); ++freelist_idx) {
82 while (ptr !=
nullptr) {
83 free_blocks.emplace_back(ptr, bytes);
89 if (num_available_bytes > 0) {
94 std::vector<PtrAndBytes> chunks;
101 std::sort(free_blocks.begin(), free_blocks.end());
102 std::sort(chunks.begin(), chunks.end());
104 auto chunk_it = chunks.begin();
105 auto chunk_ptr_remaining = chunk_it->ptr;
106 auto chunk_size_remaining = chunk_it->size;
107 for (
const auto& free_block : free_blocks) {
108 if (chunk_size_remaining == 0) {
109 assert(chunk_it != chunks.end());
111 assert(chunk_it != chunks.end());
112 chunk_ptr_remaining = chunk_it->ptr;
113 chunk_size_remaining = chunk_it->size;
115 assert(free_block.ptr == chunk_ptr_remaining);
116 assert(free_block.size <= chunk_size_remaining);
118 chunk_ptr_remaining += free_block.size;
119 chunk_size_remaining -= free_block.size;
122 assert(chunk_ptr_remaining == chunk_it->ptr + chunk_it->size);
124 assert(chunk_it == chunks.end());
125 assert(chunk_size_remaining == 0);
A memory resource similar to std::pmr::unsynchronized_pool_resource, but optimized for node-based con...
size_t ChunkSizeBytes() const
Size in bytes to allocate per chunk, currently hardcoded to a fixed size.
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
Points to the beginning of available memory for carving out allocations.
std::byte * m_available_memory_end
Points to the end of available memory for carving out allocations.
static constexpr std::size_t ELEM_ALIGN_BYTES
Internal alignment value.
std::list< std::byte * > m_allocated_chunks
Contains all allocated pools of memory, used to free the data in the destructor.
Helper to get access to private parts of PoolResource.
static void CheckAllDataAccountedFor(const PoolResource< MAX_BLOCK_SIZE_BYTES, ALIGN_BYTES > &resource)
Once all blocks are given back to the resource, tests that the freelists are consistent:
static std::size_t AvailableMemoryFromChunk(const PoolResource< MAX_BLOCK_SIZE_BYTES, ALIGN_BYTES > &resource)
How many bytes are still available from the last allocated chunk.
static std::vector< std::size_t > FreeListSizes(const PoolResource< MAX_BLOCK_SIZE_BYTES, ALIGN_BYTES > &resource)
Extracts the number of elements per freelist.
PtrAndBytes(const void *p, std::size_t s)
friend bool operator<(PtrAndBytes const &a, PtrAndBytes const &b)
defines a sort ordering by the pointer value