From 36387032422d7ba5e2cadbd064860b4511ebb5de Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Mon, 18 Dec 2023 20:55:27 -0500 Subject: [PATCH] make work as a standard allocator --- include/blt/std/memory.h | 49 +++++++++++++++++++++++++++++++++++---- tests/src/memory_test.cpp | 31 +++++++++++++++++++++++-- 2 files changed, 73 insertions(+), 7 deletions(-) diff --git a/include/blt/std/memory.h b/include/blt/std/memory.h index 3f62a71..4b7ae75 100755 --- a/include/blt/std/memory.h +++ b/include/blt/std/memory.h @@ -21,6 +21,7 @@ #include #include #include +#include #if defined(__clang__) || defined(__llvm__) || defined(__GNUC__) || defined(__GNUG__) @@ -513,11 +514,22 @@ namespace blt class area_allocator { public: - typedef T value_type; - typedef T* pointer; - typedef const T* const_pointer; - typedef void* void_pointer; - typedef const void* const_void_pointer; + using type = T; + using value_type = type; + using pointer = type*; + using const_pointer = const type*; + using void_pointer = void*; + using const_void_pointer = const void*; + using reference = value_type&; + using const_reference = const value_type&; + using size_type = size_t; + using difference_type = size_t; + using propagate_on_container_move_assignment = std::false_type; + template + struct rebind + { + typedef std::allocator other; + }; private: /** * Stores a view to a region of memory that has been deallocated @@ -678,6 +690,33 @@ namespace blt } } + template + inline void construct(U* p, Args&&... args) + { + ::new((void*) p) U(std::forward(args)...); + } + + template + inline void destroy(U* p) + { + p->~U(); + } + + inline size_t max_size() const + { + return std::numeric_limits::max(); + } + + inline const_pointer address(const value_type& val) + { + return std::addressof(val); + } + + inline pointer address(value_type& val) + { + return std::addressof(val); + } + ~area_allocator() { for (auto*& blk : blocks) diff --git a/tests/src/memory_test.cpp b/tests/src/memory_test.cpp index 9551ee2..a555439 100644 --- a/tests/src/memory_test.cpp +++ b/tests/src/memory_test.cpp @@ -230,11 +230,38 @@ void test_allocations_1() BLT_ERROR("Test (1) with size %d failed!", allocator_size); } +template +void test_allocations_2() +{ + std::vector> vec; + for (size_t i = 0; i < allocator_size * 2; i++) + { + vec.push_back(10); + vec.push_back(42); + } + bool passed = true; + for (size_t i = 0; i < vec.size(); i += 2) + { + if (vec[i] != 10 && vec[i] != 42) + passed = false; + } + if (passed) + BLT_INFO("Test (2) with size %d passed!", allocator_size); + else + BLT_ERROR("Test (2) with size %d failed!", allocator_size); + blt::black_box(vec); +} + void blt::test::memory::test() { test_allocations_1(); - test_allocations_1<50>(); - test_allocations_1<4096>(); + test_allocations_1<1024 * 4>(); + test_allocations_1<1024 * 8>(); + test_allocations_1<1024 * 16>(); + test_allocations_2(); + test_allocations_2<1024 * 4>(); + test_allocations_2<1024 * 8>(); + test_allocations_2<1024 * 16>(); std::vector> types; area_allocator int_test{};