Last active
February 24, 2020 04:44
-
-
Save BreadFish64/4291a0f2702b7bb3117d4f1a20f193bd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <array> | |
#include <cstdint> | |
template <typename T, std::size_t max_count> | |
class PoolAllocator { | |
using Uninitialized = std::array<unsigned char, sizeof(T)>; | |
std::array<Uninitialized, max_count> backing{}; | |
std::array<Uninitialized*, max_count> pointer_stack{}; | |
typename decltype(pointer_stack)::iterator stack_pointer = pointer_stack.begin(); | |
public: | |
PoolAllocator() { | |
for (std::size_t i = 0; i < max_count; ++i) { | |
pointer_stack[i] = &backing[i]; | |
} | |
} | |
template <typename U> | |
PoolAllocator(U&& other) : PoolAllocator{} {} | |
[[gnu::hot, gnu::flatten]] inline T* allocate(std::size_t n) { | |
return reinterpret_cast<T*>(*stack_pointer++); | |
} | |
[[gnu::hot, gnu::flatten]] inline void deallocate(T* ptr, std::size_t n) { | |
*--stack_pointer = reinterpret_cast<Uninitialized*>(ptr); | |
} | |
using value_type = T; | |
template <class U> | |
struct rebind { | |
using other = PoolAllocator<U, max_count>; | |
}; | |
}; | |
} // namespace Common |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment