#include <gtest/gtest.h>
namespace
{
{
public:
{
arena_bytes = 1024,
minimum_block_bytes = 16,
leaf_count = arena_bytes / minimum_block_bytes
};
struct statistics
{
};
buddy_memory_resource() = default;
buddy_memory_resource(const buddy_memory_resource&) = delete;
buddy_memory_resource&
operator=(
const buddy_memory_resource&) =
delete;
const statistics& stats() const noexcept
{
return stats_;
}
private:
enum class state : unsigned char
{
split,
allocated
};
static std::size_t block_size(std::size_t bytes, std::size_t alignment) noexcept
{
if (bytes == 0 || bytes > arena_bytes || alignment == 0 || (alignment & (alignment - 1U)) != 0 ||
alignment > alignof(std::max_align_t))
{
return 0;
}
std::size_t result = minimum_block_bytes;
while (result <
std::max(bytes, alignment))
{
result *= 2U;
}
return result;
}
static std::size_t node_at(std::size_t offset, std::size_t bytes) noexcept
{
return (arena_bytes / bytes - 1U) + offset / bytes;
}
std::size_t take(std::size_t node, std::size_t bytes, std::size_t wanted) noexcept
{
if (nodes_[node] == state::allocated || bytes < wanted)
{
return no_node;
}
if (bytes == wanted)
{
if (nodes_[node] != state::free)
{
return no_node;
}
nodes_[node] = state::allocated;
return node;
}
if (nodes_[node] == state::free)
{
nodes_[node] = state::split;
nodes_[2U * node + 1U] = state::free;
nodes_[2U * node + 2U] = state::free;
}
const auto left = take(2U * node + 1U, bytes / 2U, wanted);
return left != no_node ?
left : take(2U * node + 2U, bytes / 2U, wanted);
}
void add_reserved(std::size_t bytes) noexcept
{
stats_.reserved_bytes += bytes;
stats_.peak_reserved_bytes =
std::max(stats_.peak_reserved_bytes, stats_.reserved_bytes);
}
void* do_allocate(std::size_t bytes, std::size_t alignment) override
{
const auto block = block_size(bytes, alignment);
const auto node = block == 0 ? no_node : take(0, arena_bytes, block);
if (node == no_node)
{
#ifdefined(__cpp_exceptions)
throw std::bad_alloc();
#else
return nullptr;
#endif
}
++stats_.allocations;
add_reserved(block);
return arena_.data() + (node - (arena_bytes / block - 1U)) * block;
}
void do_deallocate(void* pointer, std::size_t bytes, std::size_t alignment) override
{
if (pointer == nullptr)
{
return;
}
const auto block = block_size(bytes, alignment);
const auto offset = static_cast<std::size_t>(static_cast<unsigned char*>(pointer) - arena_.data());
auto node = node_at(offset, block);
nodes_[node] = state::free;
while (node != 0)
{
const auto parent = (node - 1U) / 2U;
if (nodes_[2U * parent + 1U] != state::free || nodes_[2U * parent + 2U] != state::free)
{
break;
}
nodes_[parent] = state::free;
node = parent;
}
++stats_.deallocations;
stats_.reserved_bytes -= block;
}
void* do_reallocate(void* pointer, std::size_t old_bytes, std::size_t new_bytes, std::size_t alignment) override
{
++stats_.reallocations;
const auto wanted = block_size(new_bytes, alignment);
if (pointer == nullptr || wanted == 0)
{
return nullptr;
}
const auto old_block = block_size(old_bytes, alignment);
const auto offset = static_cast<std::size_t>(static_cast<unsigned char*>(pointer) - arena_.data());
auto node = node_at(offset, old_block);
auto candidate = node;
for (auto bytes = old_block; bytes < wanted; bytes *= 2U)
{
if (candidate == 0 || candidate % 2U == 0 || nodes_[candidate + 1U] != state::free)
{
return nullptr;
}
candidate = (candidate - 1U) / 2U;
}
auto current = old_block;
while (current < wanted)
{
nodes_[node] = state::free;
node = (node - 1U) / 2U;
nodes_[node] = state::allocated;
current *= 2U;
++stats_.growth_merges;
}
while (current > wanted)
{
nodes_[node] = state::split;
nodes_[2U * node + 2U] = state::free;
node = 2U * node + 1U;
nodes_[node] = state::allocated;
current /= 2U;
}
stats_.reserved_bytes -= old_block;
add_reserved(wanted);
++stats_.successful_reallocations;
return pointer;
}
bool do_is_equal(const memory_resource& rhs) const noexcept override
{
return this == &rhs;
}
std::size_t do_max_size() const noexcept override
{
return arena_bytes;
}
alignas(std::max_align_t) std::array<unsigned char, arena_bytes> arena_{};
std::array<state, 2U * leaf_count - 1U> nodes_{};
statistics stats_{};
};
template <typename Container>
void fill_in_stages(Container& values)
{
for (
std::size_t target = 16; target <= 256; target *= 2U)
{
values.reserve(target);
for (char value : values)
{
EXPECT_EQ(value, 'x');
}
while (values.size() < target)
{
values.push_back('x');
}
}
}
TEST(example_12_buddy_memory_resource, same_operations_as_vector)
{
buddy_memory_resource cetl_resource;
buddy_memory_resource vector_resource;
{
array values{allocator{&cetl_resource}};
fill_in_stages(values);
fill_in_stages(standard_values);
EXPECT_TRUE(
std::equal(values.begin(), values.end(), standard_values.begin()));
EXPECT_EQ(cetl_resource.stats().allocations, 1U);
EXPECT_EQ(cetl_resource.stats().deallocations, 0U);
EXPECT_EQ(cetl_resource.stats().growth_merges, 4U);
EXPECT_EQ(cetl_resource.stats().peak_reserved_bytes, 256U);
EXPECT_EQ(vector_resource.stats().reallocations, 0U);
EXPECT_GE(vector_resource.stats().allocations, 1U);
EXPECT_GE(vector_resource.stats().peak_reserved_bytes, 256U);
std::cout <<
"CETL/vector successful allocations: " << cetl_resource.stats().allocations <<
"/"
<< vector_resource.stats().allocations
<< "; peak arena bytes: " << cetl_resource.stats().peak_reserved_bytes << "/"
<< vector_resource.stats().peak_reserved_bytes << '\n';
}
EXPECT_EQ(cetl_resource.stats().reserved_bytes, 0U);
EXPECT_EQ(vector_resource.stats().reserved_bytes, 0U);
}
TEST(example_12_buddy_memory_resource, push_back_grows_without_payload_relocation)
{
buddy_memory_resource resource;
array values{allocator{&resource}};
values.push_back('x');
const auto initial_address =
reinterpret_cast<std::uintptr_t>(values.data());
{
values.push_back('x');
ASSERT_EQ(values.size(), i + 1U);
EXPECT_EQ(
reinterpret_cast<std::uintptr_t>(values.data()), initial_address);
}
EXPECT_EQ(resource.stats().allocations, 1U);
EXPECT_EQ(resource.stats().deallocations, 0U);
EXPECT_GT(resource.stats().growth_merges, 0U);
EXPECT_TRUE(
std::all_of(values.begin(), values.end(), [](
char value) { return value ==
'x'; }));
}
TEST(example_12_buddy_memory_resource, copy_assignment_combines_free_buddies)
{
buddy_memory_resource destination_resource;
buddy_memory_resource source_resource;
{
array destination{allocator{&destination_resource}};
array source{allocator{&source_resource}};
destination.resize(16, 'x');
source.resize(64, 'y');
const auto address =
reinterpret_cast<std::uintptr_t>(destination.data());
const auto before = destination_resource.stats();
destination = source;
EXPECT_EQ(
reinterpret_cast<std::uintptr_t>(destination.data()), address);
EXPECT_EQ(destination.capacity(), source.size());
EXPECT_EQ(destination, source);
EXPECT_EQ(source.size(), 64U);
EXPECT_TRUE(
std::all_of(source.begin(), source.end(), [](
char value) { return value ==
'y'; }));
EXPECT_EQ(destination_resource.stats().allocations, before.allocations);
EXPECT_EQ(destination_resource.stats().deallocations, before.deallocations);
EXPECT_EQ(destination_resource.stats().successful_reallocations, before.successful_reallocations + 1U);
EXPECT_EQ(destination_resource.stats().growth_merges, before.growth_merges + 2U);
EXPECT_EQ(destination_resource.stats().peak_reserved_bytes, 64U);
std::cout <<
"Copy assignment: two buddy merges, no replacement allocation, 64 peak destination bytes\n";
}
EXPECT_EQ(destination_resource.stats().reserved_bytes, 0U);
EXPECT_EQ(source_resource.stats().reserved_bytes, 0U);
}
TEST(example_12_buddy_memory_resource, copy_assignment_releases_before_fallback_allocation)
{
buddy_memory_resource destination_resource;
buddy_memory_resource source_resource;
{
array destination{allocator{&destination_resource}};
array source{allocator{&source_resource}};
destination.resize(16, 'x');
source.resize(64, 'y');
auto* blocker = destination_resource.allocate(16, 1);
const auto before = destination_resource.stats();
destination = source;
EXPECT_EQ(destination, source);
EXPECT_EQ(destination.capacity(), 64U);
EXPECT_EQ(source.size(), 64U);
EXPECT_EQ(destination_resource.stats().successful_reallocations, before.successful_reallocations);
EXPECT_EQ(destination_resource.stats().allocations, before.allocations + 1U);
EXPECT_EQ(destination_resource.stats().deallocations, before.deallocations + 1U);
EXPECT_EQ(destination_resource.stats().peak_reserved_bytes, 80U);
destination_resource.deallocate(blocker, 16, 1);
}
EXPECT_EQ(destination_resource.stats().reserved_bytes, 0U);
EXPECT_EQ(source_resource.stats().reserved_bytes, 0U);
}
TEST(example_12_buddy_memory_resource, blocked_growth_falls_back_in_the_container)
{
buddy_memory_resource resource;
array values{allocator{&resource}};
values.reserve(16);
values.resize(16, 'x');
auto* blocker = resource.allocate(16, 1);
values.reserve(32);
EXPECT_EQ(resource.stats().successful_reallocations, 0U);
EXPECT_EQ(resource.stats().allocations, 3U);
EXPECT_EQ(resource.stats().deallocations, 1U);
EXPECT_EQ(values.size(), 16U);
EXPECT_TRUE(
std::all_of(values.begin(), values.end(), [](
char value) { return value ==
'x'; }));
resource.deallocate(blocker, 16, 1);
}
TEST(example_12_buddy_memory_resource, failed_multilevel_merge_is_atomic)
{
buddy_memory_resource resource;
auto* first = resource.allocate(16, 1);
auto* buddy = resource.allocate(16, 1);
auto* blocker = resource.allocate(32, 1);
resource.deallocate(buddy, 16, 1);
EXPECT_EQ(resource.reallocate(first, 16, 64, 1), nullptr);
EXPECT_EQ(resource.stats().growth_merges, 0U);
EXPECT_EQ(resource.stats().reserved_bytes, 48U);
auto* still_free = resource.allocate(16, 1);
EXPECT_EQ(static_cast<unsigned char*>(still_free), static_cast<unsigned char*>(first) + 16);
resource.deallocate(still_free, 16, 1);
resource.deallocate(blocker, 32, 1);
resource.deallocate(first, 16, 1);
auto* whole = resource.allocate(buddy_memory_resource::arena_bytes);
resource.deallocate(whole, buddy_memory_resource::arena_bytes);
EXPECT_EQ(resource.stats().reserved_bytes, 0U);
}
TEST(example_12_buddy_memory_resource, upper_buddy_cannot_grow_downwards)
{
buddy_memory_resource resource;
auto* lower = resource.allocate(16, 1);
auto* upper = resource.allocate(16, 1);
resource.deallocate(lower, 16, 1);
EXPECT_EQ(resource.reallocate(upper, 16, 32, 1), nullptr);
resource.deallocate(upper, 16, 1);
}
TEST(example_12_buddy_memory_resource, shrink_releases_upper_buddies)
{
buddy_memory_resource resource;
auto* original = resource.allocate(64, 1);
auto* smaller = resource.reallocate(original, 64, 16, 1);
ASSERT_NE(smaller, nullptr);
EXPECT_EQ(resource.stats().reserved_bytes, 16U);
auto* buddy = resource.allocate(16, 1);
EXPECT_EQ(static_cast<unsigned char*>(buddy), static_cast<unsigned char*>(smaller) + 16);
resource.deallocate(buddy, 16, 1);
auto* restored = resource.reallocate(smaller, 16, 64, 1);
ASSERT_NE(restored, nullptr);
resource.deallocate(restored, 64, 1);
}
TEST(example_12_buddy_memory_resource, rounding_and_declined_requests_preserve_allocation)
{
buddy_memory_resource resource;
auto* p = resource.allocate(17);
EXPECT_EQ(resource.stats().reserved_bytes, 32U);
p = resource.reallocate(p, 17, 31);
ASSERT_NE(p, nullptr);
EXPECT_EQ(resource.stats().reserved_bytes, 32U);
EXPECT_EQ(resource.reallocate(p, 31, 0), nullptr);
EXPECT_EQ(resource.reallocate(p, 31, 32, 2U *
alignof(
std::max_align_t)),
nullptr);
resource.deallocate(p, 31);
EXPECT_EQ(resource.stats().reserved_bytes, 0U);
}
TEST(example_12_buddy_memory_resource, exhausted_arena_coalesces_after_scattered_frees)
{
buddy_memory_resource resource;
{
blocks[i] = resource.allocate(16, 1);
ASSERT_NE(blocks[i], nullptr);
std::fill_n(
static_cast<unsigned char*
>(blocks[i]), 16,
static_cast<unsigned char>(i));
}
#ifdefined(__cpp_exceptions)
#else
EXPECT_EQ(resource.allocate(16, 1), nullptr);
#endif
EXPECT_EQ(resource.stats().reserved_bytes, buddy_memory_resource::arena_bytes);
{
const auto* bytes = static_cast<const unsigned char*>(blocks[i]);
{
EXPECT_EQ(bytes[j], static_cast<unsigned char>(i));
}
}
{
resource.deallocate(blocks[(i * 17U) % blocks.size()], 16, 1);
}
auto* whole = resource.allocate(buddy_memory_resource::arena_bytes);
ASSERT_NE(whole, nullptr);
resource.deallocate(whole, buddy_memory_resource::arena_bytes);
EXPECT_EQ(resource.stats().reserved_bytes, 0U);
}
}
Interface to a class that manages memory resources.
Definition memory_resource.hpp:38
memory_resource & operator=(const memory_resource &rhs)=default
Implicitly defined assignment operator.
Adheres to the std::pmr::polymorphic_allocator specification.
Definition memory_resource.hpp:356
Minimal, generic container for storing Cyphal variable-length arrays.
Definition variable_length_array.hpp:1419
Defines the C++17 std::pmr::memory_resource abstract type and several other types within the std::pmr...
Includes cetl::variable_length_array type and non-member functions.