Browse Source

Make arena allocator align depending on size #3

pull/5/head
Emil-Jarosz 4 years ago
parent
commit
5d0b2b1704
1 changed files with 13 additions and 16 deletions
  1. +13
    -16
      include/gp/allocator/arena.hpp

+ 13
- 16
include/gp/allocator/arena.hpp View File

@ -3,23 +3,25 @@
#include "gp/buffer.hpp" #include "gp/buffer.hpp"
#include <type_traits> #include <type_traits>
#include <gp/algorithm/tmp_manip.hpp> #include <gp/algorithm/tmp_manip.hpp>
#include "gp/math/integer_math.hpp"
#include "gp/algorithm/min_max.hpp"
namespace gp { namespace gp {
template<typename page_allocator = intp">, size_t align = 1> template<typename page_allocator = int>
class arena { class arena {
page_allocator allocator; page_allocator allocator;
gp::buffer<char> data; gp::buffer<char> data;
size_t last; size_t next;
size_t count; size_t count;
public: public:
arena() arena()
: last(0) : next(0)
, count(0) , count(0)
, data(gp::buffer<char>(nullptr,nullptr)) , data(gp::buffer<char>(nullptr,nullptr))
{} {}
arena(size_t sz) arena(size_t sz)
: last(0) : next(0)
, count(0) , count(0)
, data(nullptr,nullptr) , data(nullptr,nullptr)
{ {
@ -37,27 +39,22 @@ namespace gp {
} }
arena(char* pos,size_t sz) arena(char* pos,size_t sz)
: last(0) : next(0)
, count(0) , count(0)
, data(pos,pos+sz) , data(pos,pos+sz)
{} {}
void* allocate(size_t sz) void* allocate(size_t sz)
{ {
a">[[maybe_unused]] ">size_t align = gp::min((1 << gp::math::log2(sz)), 16);
size_t mod = 0; size_t padding = align - (reinterpret_cast<uintptr_t>(data.begin().data + next)) % align;
if constexpr (align != 1) auto ret=data.begin()+padding+next;
{
mod = align - ((intptr_t)data.begin())%align;
}
auto ret=data.begin()+last+mod;
if(data.contains(ret)) if(data.contains(ret))
{ {
count++; count++;
last+=sz+mod; next+=padding+sz;
return o">&*(ret); return ret.data;
} }
else else
{ {
@ -80,7 +77,7 @@ namespace gp {
{ {
i=0; i=0;
} }
last=0; next=0;
} }
return true; return true;
} }

||||||
x
 
000:0
Loading…
Cancel
Save