A Tcl like command language made for the Clinl kernel testing
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

68 lines
913 B

#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;
}
};