5#ifndef BITCOIN_TEST_UTIL_POOLRESOURCETESTER_H
6#define BITCOIN_TEST_UTIL_POOLRESOURCETESTER_H
27 :
ptr(reinterpret_cast<uintptr_t>(p)),
size(
s)
44 template <std::
size_t MAX_BLOCK_SIZE_BYTES, std::
size_t ALIGN_BYTES>
47 auto sizes = std::vector<std::size_t>();
50 while (ptr !=
nullptr) {
52 const auto* ptr_ = ptr;
57 sizes.push_back(size);
65 template <std::
size_t MAX_BLOCK_SIZE_BYTES, std::
size_t ALIGN_BYTES>
78 template <std::
size_t MAX_BLOCK_SIZE_BYTES, std::
size_t ALIGN_BYTES>
82 std::vector<PtrAndBytes> free_blocks;
83 for (std::size_t freelist_idx = 0; freelist_idx < resource.
m_free_lists.size(); ++freelist_idx) {
86 while (ptr !=
nullptr) {
87 free_blocks.emplace_back(ptr, bytes);
88 const auto* ptr_ = ptr;
96 if (num_available_bytes > 0) {
101 std::vector<PtrAndBytes> chunks;
108 std::sort(free_blocks.begin(), free_blocks.end());
109 std::sort(chunks.begin(), chunks.end());
111 auto chunk_it = chunks.begin();
112 auto chunk_ptr_remaining = chunk_it->ptr;
113 auto chunk_size_remaining = chunk_it->size;
114 for (
const auto& free_block : free_blocks) {
115 if (chunk_size_remaining == 0) {
116 assert(chunk_it != chunks.end());
118 assert(chunk_it != chunks.end());
119 chunk_ptr_remaining = chunk_it->ptr;
120 chunk_size_remaining = chunk_it->size;
122 assert(free_block.ptr == chunk_ptr_remaining);
123 assert(free_block.size <= chunk_size_remaining);
125 chunk_ptr_remaining += free_block.size;
126 chunk_size_remaining -= free_block.size;
129 assert(chunk_ptr_remaining == chunk_it->ptr + chunk_it->size);
131 assert(chunk_it == chunks.end());
132 assert(chunk_size_remaining == 0);
#define ASAN_POISON_MEMORY_REGION(addr, size)
#define ASAN_UNPOISON_MEMORY_REGION(addr, size)
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.
In-place linked list of the allocations, used for the 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