diff --git a/src/arena_allocator.hpp b/src/arena_allocator.hpp index a223313..41f462b 100644 --- a/src/arena_allocator.hpp +++ b/src/arena_allocator.hpp @@ -3,12 +3,14 @@ #include #include #include +#include #include #include #include #include #include #include +#include #include #include @@ -81,16 +83,25 @@ private: * @param size Size of the data area for this block * @param prev Pointer to the previous block (nullptr for first block) * @return Pointer to the newly created block - * @throws std::bad_alloc if memory allocation fails + * @note Prints error to stderr and calls std::abort() if memory allocation + * fails */ static Block *create(size_t size, Block *prev) { if (size > std::numeric_limits::max()) { - throw std::bad_alloc(); + std::fprintf( + stderr, + "ArenaAllocator: Block size %zu exceeds maximum uint32_t value\n", + size); + std::abort(); } void *memory = std::aligned_alloc( alignof(Block), align_up(sizeof(Block) + size, alignof(Block))); if (!memory) { - throw std::bad_alloc(); + std::fprintf( + stderr, + "ArenaAllocator: Failed to allocate memory block of size %zu\n", + size); + std::abort(); } size_t total_size = size + (prev ? prev->total_size : 0); size_t total_used = prev ? prev->total_used + prev->offset : 0; @@ -155,7 +166,8 @@ public: * @param size Number of bytes to allocate (0 returns nullptr) * @param alignment Required alignment (default: alignof(std::max_align_t)) * @return Pointer to allocated memory, or nullptr if size is 0 - * @throws std::bad_alloc if memory allocation fails + * @note Prints error to stderr and calls std::abort() if memory allocation + * fails * * ## Performance: * - O(1) amortized allocation time @@ -210,7 +222,8 @@ public: * `alignof(std::max_align_t)` * @return Pointer to the reallocated memory (may be the same as ptr or * different) - * @throws std::bad_alloc if memory allocation fails + * @note Prints error to stderr and calls std::abort() if memory allocation + * fails * * ## Behavior: * - If new_size == old_size, returns ptr unchanged @@ -236,12 +249,17 @@ public: * @param new_size Desired new size in number of T objects * @return Pointer to the reallocated memory (may be the same as ptr or * different) - * @throws std::bad_alloc if memory allocation fails or size overflow occurs + * @note Prints error to stderr and calls std::abort() if memory allocation + * fails or size overflow occurs */ template T *realloc(T *ptr, uint32_t old_size, uint32_t new_size) { if (size_t(new_size) * sizeof(T) > std::numeric_limits::max()) { - throw std::bad_alloc(); + std::fprintf(stderr, + "ArenaAllocator: Reallocation size overflow for type %s " + "(new_size=%u, sizeof(T)=%zu)\n", + typeid(T).name(), new_size, sizeof(T)); + std::abort(); } return static_cast(realloc_raw(ptr, old_size * sizeof(T), new_size * sizeof(T), alignof(T))); @@ -257,7 +275,8 @@ public: * @tparam Args Types of constructor arguments * @param args Arguments to forward to T's constructor * @return Pointer to the constructed object - * @throws std::bad_alloc if memory allocation fails + * @note Prints error to stderr and calls std::abort() if memory allocation + * fails * * ## Type Requirements: * T must be trivially destructible (std::is_trivially_destructible_v). @@ -293,7 +312,8 @@ public: * @param size Number of T objects to allocate space for * @return Pointer to allocated memory suitable for constructing an array of T * objects - * @throws std::bad_alloc if memory allocation fails + * @note Prints error to stderr and calls std::abort() if memory allocation + * fails * * ## Type Requirements: * T must be trivially destructible (std::is_trivially_destructible_v). @@ -315,7 +335,11 @@ public: return nullptr; } if (size_t(size) * sizeof(T) > std::numeric_limits::max()) { - throw std::bad_alloc(); + std::fprintf(stderr, + "ArenaAllocator: Allocation size overflow for type %s " + "(size=%u, sizeof(T)=%zu)\n", + typeid(T).name(), size, sizeof(T)); + std::abort(); } void *ptr = allocate_raw(sizeof(T) * size, alignof(T)); return static_cast(ptr); diff --git a/tests/test_commit_request.cpp b/tests/test_commit_request.cpp index 66d5801..45623a9 100644 --- a/tests/test_commit_request.cpp +++ b/tests/test_commit_request.cpp @@ -2,7 +2,6 @@ #include "../benchmarks/test_data.hpp" #include "parser_comparison.hpp" #include -#include /** * @brief Test helper that uses parser comparison to validate both parsers. @@ -479,4 +478,4 @@ TEST_CASE("Parser Comparison - Stress Tests") { std::string stress_json = weaseldb::test_data::generate_large_json(50); test_parser_comparison(stress_json, "Deep nesting with many operations"); } -} \ No newline at end of file +}