diff options
author | Alon Zakai <alonzakai@gmail.com> | 2016-04-22 20:06:46 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2016-04-26 19:49:15 -0700 |
commit | c8a7755461d5e806bd734b75f441ac61c9963ee9 (patch) | |
tree | 24ed0234647ebdb7ba90904203a809f05f24cffe /src/mixed_arena.h | |
parent | 895e3e016d4875f70517411d195fbab5193784ea (diff) | |
download | binaryen-c8a7755461d5e806bd734b75f441ac61c9963ee9.tar.gz binaryen-c8a7755461d5e806bd734b75f441ac61c9963ee9.tar.bz2 binaryen-c8a7755461d5e806bd734b75f441ac61c9963ee9.zip |
refactor arena to allow allocating space directly
Diffstat (limited to 'src/mixed_arena.h')
-rw-r--r-- | src/mixed_arena.h | 21 |
1 files changed, 13 insertions, 8 deletions
diff --git a/src/mixed_arena.h b/src/mixed_arena.h index 16183286b..3258a2d31 100644 --- a/src/mixed_arena.h +++ b/src/mixed_arena.h @@ -73,8 +73,7 @@ struct MixedArena { next = nullptr; } - template<class T> - T* alloc() { + void* allocSpace(size_t size) { // the bump allocator data should not be modified by multiple threads at once. if (std::this_thread::get_id() != threadId) { // TODO use a fast double-checked locking pattern. @@ -87,17 +86,23 @@ struct MixedArena { curr->next = new MixedArena(); // will have our thread id } } - return curr->alloc<T>(); + return curr->allocSpace(size); } 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) { + size = (size + 7) & (-8); // same alignment as malloc TODO optimize? + assert(size < CHUNK); + if (chunks.size() == 0 || index + size >= CHUNK) { chunks.push_back(new char[CHUNK]); index = 0; } - T* ret = (T*)(chunks.back() + index); - index += currSize; + auto* ret = chunks.back() + index; + index += size; + return static_cast<void*>(ret); + } + + template<class T> + T* alloc() { + auto* ret = static_cast<T*>(allocSpace(sizeof(T))); new (ret) T(); return ret; } |