11 #include <boost/test/unit_test.hpp>
15 #include <unordered_map>
26 size_t expected_bytes_available = resource.ChunkSizeBytes();
30 void* block = resource.Allocate(8, 8);
31 expected_bytes_available -= 8;
35 resource.Deallocate(block, 8, 8);
40 void* b = resource.Allocate(8, 1);
41 BOOST_TEST(b == block);
45 resource.Deallocate(block, 8, 1);
51 b = resource.Allocate(8, 16);
52 BOOST_TEST(b != block);
58 resource.Deallocate(block, 8, 16);
64 block = resource.Allocate(16, 8);
69 resource.Deallocate(block, 16, 8);
76 void* p = resource.Allocate(0, 1);
80 resource.Deallocate(p, 0, 1);
91 uint8_t num_allocs = 200;
93 auto data = std::vector<Span<uint8_t>>();
96 for (uint8_t num_bytes = 0; num_bytes < num_allocs; ++num_bytes) {
97 uint8_t* bytes =
new (resource.Allocate(num_bytes, 1)) uint8_t[num_bytes];
98 BOOST_TEST(bytes !=
nullptr);
99 data.emplace_back(bytes, num_bytes);
102 std::fill(bytes, bytes + num_bytes, num_bytes);
107 for (
auto const& span : data) {
108 for (
auto x : span) {
109 BOOST_TEST(val == x);
111 std::destroy(span.data(), span.data() + span.size());
112 resource.Deallocate(span.data(), span.size(), 1);
121 struct PtrSizeAlignment {
129 std::vector<PtrSizeAlignment> ptr_size_alignment{};
130 for (
size_t i = 0; i < 1000; ++i) {
136 void* ptr = resource.Allocate(size, alignment);
137 BOOST_TEST(ptr !=
nullptr);
138 BOOST_TEST((
reinterpret_cast<uintptr_t
>(ptr) & (alignment - 1)) == 0);
139 ptr_size_alignment.push_back({ptr, size, alignment});
143 resource.Deallocate(x.ptr, x.bytes, x.alignment);
144 x = ptr_size_alignment.back();
145 ptr_size_alignment.pop_back();
150 for (
auto const& x : ptr_size_alignment) {
151 resource.Deallocate(x.ptr, x.bytes, x.alignment);
159 auto std_map = std::unordered_map<int, int>{};
161 using Map = std::unordered_map<int,
166 sizeof(std::pair<const int, int>) +
sizeof(
void*) * 4,
168 auto resource = Map::allocator_type::ResourceType(1024);
173 auto resource_map = Map{0, std::hash<int>{}, std::equal_to<int>{}, &resource};
178 for (
size_t i = 0; i < 10000; ++i) {
Forwards all allocations/deallocations to the PoolResource.
A memory resource similar to std::pmr::unsynchronized_pool_resource, but optimized for node-based con...
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.
BOOST_AUTO_TEST_SUITE_END()
static size_t DynamicUsage(const int8_t &v)
Dynamic memory usage for built-in types is zero.
BOOST_AUTO_TEST_CASE(basic_allocating)
static uint64_t InsecureRandRange(uint64_t range)