diff options
author | Alon Zakai <alonzakai@gmail.com> | 2015-11-03 10:14:03 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2015-11-03 10:14:03 -0800 |
commit | 3b0fc2374d042ba81e16ee09f57cdef9891be065 (patch) | |
tree | 0e2397e4c00f88b13614ae66c92c81a9c4778859 /src/mixed_arena.h | |
parent | a18f879612876860429aa153336be293a9368310 (diff) | |
download | binaryen-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.h | 35 |
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(); + } +}; + |