1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
#include "objstack.hpp"
namespace acommon {
using namespace std;
void ObjStack::setup_chunk()
{
bottom = first_free->data;
align_bottom(min_align);
top = (byte *)first_free + chunk_size;
align_top(min_align);
}
ObjStack::ObjStack(size_t chunk_s, size_t align)
: chunk_size(chunk_s), min_align(align), temp_end(0)
{
first_free = first = (Node *)malloc(chunk_size);
first->next = 0;
reserve = 0;
setup_chunk();
}
ObjStack::~ObjStack()
{
while (first) {
Node * tmp = first->next;
free(first);
first = tmp;
}
trim();
}
size_t ObjStack::calc_size()
{
size_t size = 0;
for (Node * p = first; p; p = p->next)
size += chunk_size;
return size;
}
void ObjStack::new_chunk()
{
if (reserve) {
first_free->next = reserve;
reserve = reserve->next;
first_free = first_free->next;
first_free->next = 0;
} else {
first_free->next = (Node *)malloc(chunk_size);
first_free = first_free->next;
}
first_free->next = 0;
setup_chunk();
}
void ObjStack::reset()
{
assert(first_free->next == 0);
if (first->next) {
first_free->next = reserve;
reserve = first->next;
first->next = 0;
}
first_free = first;
setup_chunk();
}
void ObjStack::trim()
{
while (reserve) {
Node * tmp = reserve->next;
free(reserve);
reserve = tmp;
}
}
}
|