Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion modules/strong_ptr.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public:
* @param p_alignment the desired alignment
* @return void* pointer to allocated space or nullptr if no space is
* available
* @throws std::bad_alloc if storage of the requested size and alignment
* cannot be obtained
*/
void* do_allocate(std::size_t p_bytes, std::size_t p_alignment)
{
Expand All @@ -80,7 +82,7 @@ public:
m_space -= p_bytes;
return result;
}
return nullptr;
throw std::bad_alloc();
};

/**
Expand Down
16 changes: 9 additions & 7 deletions tests/strong_ptr.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,8 @@ boost::ut::suite<"strong_ptr_only_test"> strong_ptr_only_test = []() {
};

"get_allocator_for_static_allocation"_test = [&] {
// Create a static object (using int to avoid affecting test_class instance count)
// Create a static object (using int to avoid affecting test_class instance
// count)
static int static_obj = 777;

// Create strong_ptr to static object using unsafe_assume_static_tag
Expand Down Expand Up @@ -456,7 +457,6 @@ boost::ut::suite<"monotonic_allocator_test"> monotonic_allocator_test = []() {

"max_buffer_test"_test = [&] {
auto allocator = monotonic_allocator<8>();

auto ptr1 =
allocator.allocate(sizeof(std::uint32_t), alignof(std::uint32_t));
auto int_ptr1 = static_cast<int*>(ptr1);
Expand All @@ -467,13 +467,15 @@ boost::ut::suite<"monotonic_allocator_test"> monotonic_allocator_test = []() {
auto int_ptr2 = static_cast<int*>(ptr2);
*int_ptr2 = 2;

[[maybe_unused]] auto ptr3 =
allocator.allocate(sizeof(std::uint32_t), alignof(std::uint32_t));
expect(that % 1 == *int_ptr1) << "Int assignment failed.\n";
expect(that % 2 == *int_ptr2) << "Int assignment failed.\n";
// TODO(#34): fix nullptr check on linux
// expect(that % nullptr == ptr3)
// << "Allocated memory out of bounds of buffer " << reinterpret_cast<std::intptr_t>(ptr3);

expect(throws<std::bad_alloc>([&] {
[[maybe_unused]] auto ptr3 =
allocator.allocate(sizeof(std::uint32_t), alignof(std::uint32_t));
}))
<< "Exception not thrown when bad alloc happens.\n";

allocator.deallocate(ptr1, sizeof(std::uint32_t));
allocator.deallocate(ptr2, sizeof(std::uint32_t));
};
Expand Down