diff --git a/include/gp/allocator/arena.hpp b/include/gp/allocator/arena.hpp index 118a2bd..d7aa4e8 100644 --- a/include/gp/allocator/arena.hpp +++ b/include/gp/allocator/arena.hpp @@ -3,27 +3,27 @@ #include "gp/buffer.hpp" #include #include +#include "gp/math/integer_math.hpp" +#include "gp/algorithm/min_max.hpp" - - -namespace gp{ - template - class arena{ +namespace gp { + template + class arena { page_allocator allocator; gp::buffer data; - size_t last; + size_t next; size_t count; public: arena() - :last(0) - ,count(0) - ,data(gp::buffer(nullptr,nullptr)) + : next(0) + , count(0) + , data(gp::buffer(nullptr,nullptr)) {} arena(size_t sz) - :last(0) - ,count(0) - ,data(nullptr,nullptr) + : next(0) + , count(0) + , data(nullptr,nullptr) { if constexpr (gp::has_allocator_interface::value) { @@ -39,28 +39,22 @@ namespace gp{ } arena(char* pos,size_t sz) - :last(0) - ,count(0) - ,data(pos,pos+sz) - { - } + : next(0) + , count(0) + , data(pos,pos+sz) + {} void* allocate(size_t sz) { - [[maybe_unused]] - size_t mod = 0; - - if constexpr (align != 1) - { - mod = align - ((intptr_t)data.begin())%align; - } + size_t align = gp::min((1 << gp::math::log2(sz)), 16); + size_t padding = align - (reinterpret_cast(data.begin().data + next)) % align; - auto ret=data.begin()+last+mod; + auto ret=data.begin()+padding+next; if(data.contains(ret)) { count++; - last+=sz+mod; - return &*(ret); + next+=padding+sz; + return ret.data; } else { @@ -83,7 +77,7 @@ namespace gp{ { i=0; } - last=0; + next=0; } return true; } @@ -98,4 +92,4 @@ namespace gp{ } } }; -} \ No newline at end of file +}