make work as a standard allocator

v1
Brett 2023-12-18 20:55:27 -05:00
parent 825e2c71a4
commit 3638703242
2 changed files with 73 additions and 7 deletions

View File

@ -21,6 +21,7 @@
#include <cstring>
#include <array>
#include <optional>
#include <limits>
#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<class U>
struct rebind
{
typedef std::allocator<U> other;
};
private:
/**
* Stores a view to a region of memory that has been deallocated
@ -678,6 +690,33 @@ namespace blt
}
}
template<class U, class... Args>
inline void construct(U* p, Args&&... args)
{
::new((void*) p) U(std::forward<Args>(args)...);
}
template<class U>
inline void destroy(U* p)
{
p->~U();
}
inline size_t max_size() const
{
return std::numeric_limits<size_t>::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)

View File

@ -230,11 +230,38 @@ void test_allocations_1()
BLT_ERROR("Test (1) with size %d failed!", allocator_size);
}
template<size_t allocator_size = 20>
void test_allocations_2()
{
std::vector<int, blt::area_allocator<int, allocator_size>> 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<std::pair<fucked_type2*, size_t>> types;
area_allocator<fucked_type2, 20> int_test{};