|
|
- #pragma once
- #include "buffer.hpp"
- #include "stdlib.h"
- #include "string.h"
-
- class arena{
- ksdk::buffer<char> data;
- size_t last;
- size_t count;
- public:
- arena()
- :last(0)
- ,count(0)
- ,data(ksdk::buffer<char>(nullptr,(size_t)0))
- {}
-
- arena(size_t sz)
- :last(0)
- ,count(0)
- ,data(ksdk::buffer<char>(nullptr,(size_t)0))
- {
- if(sz!=0)
- {
- auto v=malloc(sz);
- if(v!=nullptr)
- {
- data=ksdk::buffer<char>((char*)v,sz);
- }
- }
- }
-
- arena(char* pos,size_t sz)
- :last(0)
- ,count(0)
- ,data(ksdk::buffer<char>(pos,(size_t)sz))
- {
- }
-
- void* allocate(size_t sz)
- {
- auto ret=data.begin()+last;
- if(ret<data.end())
- {
- count++;
- last+=sz;
- return ret;
- }
- else
- {
- return nullptr;
- }
- }
-
- bool desallocate(void* ptr)
- {
- if(ptr>=data.begin() && ptr<data.end())
- {
- count--;
- if(count==0)
- {
- memset(data.begin(),data.size(),0);
- last=0;
- }
- return true;
- }
- return false;
- }
- };
|