From f5ef2656cb8c2ae7b5c0230fe89be6ff658c78d5 Mon Sep 17 00:00:00 2001 From: Emil-Jarosz Date: Sat, 12 Sep 2020 18:07:27 +0100 Subject: [PATCH 1/2] Reformat arena.hpp --- include/gp/allocator/arena.hpp | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/include/gp/allocator/arena.hpp b/include/gp/allocator/arena.hpp index 118a2bd..9881b4c 100644 --- a/include/gp/allocator/arena.hpp +++ b/include/gp/allocator/arena.hpp @@ -4,26 +4,24 @@ #include #include - - -namespace gp{ +namespace gp { template - class arena{ + class arena { page_allocator allocator; gp::buffer data; size_t last; size_t count; public: arena() - :last(0) - ,count(0) - ,data(gp::buffer(nullptr,nullptr)) + : last(0) + , count(0) + , data(gp::buffer(nullptr,nullptr)) {} arena(size_t sz) - :last(0) - ,count(0) - ,data(nullptr,nullptr) + : last(0) + , count(0) + , data(nullptr,nullptr) { if constexpr (gp::has_allocator_interface::value) { @@ -39,11 +37,10 @@ namespace gp{ } arena(char* pos,size_t sz) - :last(0) - ,count(0) - ,data(pos,pos+sz) - { - } + : last(0) + , count(0) + , data(pos,pos+sz) + {} void* allocate(size_t sz) { @@ -98,4 +95,4 @@ namespace gp{ } } }; -} \ No newline at end of file +} From 5d0b2b17048205c4bbca7e38d837c33d6136427a Mon Sep 17 00:00:00 2001 From: Emil-Jarosz Date: Sat, 12 Sep 2020 20:38:50 +0100 Subject: [PATCH 2/2] Make arena allocator align depending on size #3 --- include/gp/allocator/arena.hpp | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/include/gp/allocator/arena.hpp b/include/gp/allocator/arena.hpp index 9881b4c..d7aa4e8 100644 --- a/include/gp/allocator/arena.hpp +++ b/include/gp/allocator/arena.hpp @@ -3,23 +3,25 @@ #include "gp/buffer.hpp" #include #include +#include "gp/math/integer_math.hpp" +#include "gp/algorithm/min_max.hpp" namespace gp { - template + template class arena { page_allocator allocator; gp::buffer data; - size_t last; + size_t next; size_t count; public: arena() - : last(0) + : next(0) , count(0) , data(gp::buffer(nullptr,nullptr)) {} arena(size_t sz) - : last(0) + : next(0) , count(0) , data(nullptr,nullptr) { @@ -37,27 +39,22 @@ namespace gp { } arena(char* pos,size_t sz) - : last(0) + : next(0) , count(0) , data(pos,pos+sz) {} void* allocate(size_t sz) { - [[maybe_unused]] - size_t mod = 0; + size_t align = gp::min((1 << gp::math::log2(sz)), 16); + size_t padding = align - (reinterpret_cast(data.begin().data + next)) % align; - if constexpr (align != 1) - { - mod = align - ((intptr_t)data.begin())%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 { @@ -80,7 +77,7 @@ namespace gp { { i=0; } - last=0; + next=0; } return true; }