]> git.sesse.net Git - remoteglot-book/blob - arena.cpp
Small cleanup.
[remoteglot-book] / arena.cpp
1 #include <stdlib.h>
2 #include <assert.h>
3 #include "arena.h"
4
5 Arena::Arena() : first(NULL) {}
6
7 Arena::~Arena()
8 {
9         Block *next;
10         for (Block *b = first; b != NULL; b = next) {
11                 delete[] b->memory;
12
13                 next = b->next;
14                 delete b;
15         }
16 }
17
18 char *Arena::alloc(size_t bytes)
19 {
20         assert(bytes < BLOCK_SIZE);  // Can fix, but we don't need to.
21
22         if (first == NULL || first->used + bytes > BLOCK_SIZE) {
23                 Block *b = new Block;
24                 b->memory = new char[BLOCK_SIZE];
25                 b->used = 0;
26                 b->next = first;
27                 first = b;
28         }
29
30         char *ret = first->memory + first->used;
31         first->used += bytes;
32         return ret;
33 }