diff --git a/modules/strong_ptr.cppm b/modules/strong_ptr.cppm index 7b73766..951245e 100644 --- a/modules/strong_ptr.cppm +++ b/modules/strong_ptr.cppm @@ -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) { @@ -80,7 +82,7 @@ public: m_space -= p_bytes; return result; } - return nullptr; + throw std::bad_alloc(); }; /** diff --git a/tests/strong_ptr.test.cpp b/tests/strong_ptr.test.cpp index 575f960..8662ebe 100644 --- a/tests/strong_ptr.test.cpp +++ b/tests/strong_ptr.test.cpp @@ -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 @@ -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(ptr1); @@ -467,13 +467,15 @@ boost::ut::suite<"monotonic_allocator_test"> monotonic_allocator_test = []() { auto int_ptr2 = static_cast(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(ptr3); + + expect(throws([&] { + [[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)); };