summaryrefslogtreecommitdiff
path: root/src/mixed_arena.h
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2015-11-03 10:14:03 -0800
committerAlon Zakai <alonzakai@gmail.com>2015-11-03 10:14:03 -0800
commit3b0fc2374d042ba81e16ee09f57cdef9891be065 (patch)
tree0e2397e4c00f88b13614ae66c92c81a9c4778859 /src/mixed_arena.h
parenta18f879612876860429aa153336be293a9368310 (diff)
downloadbinaryen-3b0fc2374d042ba81e16ee09f57cdef9891be065.tar.gz
binaryen-3b0fc2374d042ba81e16ee09f57cdef9891be065.tar.bz2
binaryen-3b0fc2374d042ba81e16ee09f57cdef9891be065.zip
refactor arena code
Diffstat (limited to 'src/mixed_arena.h')
-rw-r--r--src/mixed_arena.h35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/mixed_arena.h b/src/mixed_arena.h
new file mode 100644
index 000000000..a92026580
--- /dev/null
+++ b/src/mixed_arena.h
@@ -0,0 +1,35 @@
+
+#include <vector>
+
+// Arena allocation for mixed-type data.
+struct MixedArena {
+ std::vector<char*> chunks;
+ int index; // in last chunk
+
+ template<class T>
+ T* alloc() {
+ const size_t CHUNK = 10000;
+ size_t currSize = (sizeof(T) + 7) & (-8); // same alignment as malloc TODO optimize?
+ assert(currSize < CHUNK);
+ if (chunks.size() == 0 || index + currSize >= CHUNK) {
+ chunks.push_back(new char[CHUNK]);
+ index = 0;
+ }
+ T* ret = (T*)(chunks.back() + index);
+ index += currSize;
+ new (ret) T();
+ return ret;
+ }
+
+ void clear() {
+ for (char* chunk : chunks) {
+ delete[] chunk;
+ }
+ chunks.clear();
+ }
+
+ ~MixedArena() {
+ clear();
+ }
+};
+