]> git.sesse.net Git - remoteglot-book/blobdiff - arena.cpp
Reduce binloader RAM requirements by over 40%.
[remoteglot-book] / arena.cpp
diff --git a/arena.cpp b/arena.cpp
new file mode 100644 (file)
index 0000000..048e6db
--- /dev/null
+++ b/arena.cpp
@@ -0,0 +1,33 @@
+#include <stdlib.h>
+#include <assert.h>
+#include "arena.h"
+
+Arena::Arena() : first(NULL) {}
+
+Arena::~Arena()
+{
+       Block *next;
+       for (Block *b = first; b != NULL; b = next) {
+               delete[] b->memory;
+
+               next = b->next;
+               delete b;
+       }
+}
+
+char *Arena::alloc(size_t bytes)
+{
+       assert(bytes < BLOCK_SIZE);  // Can fix, but we don't need to.
+
+       if (first == NULL || first->used + bytes > BLOCK_SIZE) {
+               Block *b = new Block;
+               b->memory = new char[BLOCK_SIZE];
+               b->used = 0;
+               b->next = first;
+               first = b;
+       }
+
+       char *ret = first->memory + first->used;
+       first->used += bytes;
+       return ret;
+}