diff --git a/library/include/er/stack_alloc.h b/library/include/er/stack_alloc.h index ea188e1..d7d77c4 100644 --- a/library/include/er/stack_alloc.h +++ b/library/include/er/stack_alloc.h @@ -1,67 +1,38 @@ -#include -#include #include +#include namespace er { template class StackAlloc { -public: - using value_type = T; - using pointer = T*; - using reference = T&; - using const_reference = const T&; - using size_type = size_t; - using difference_type = ptrdiff_t; -} - - pointer allocate(size_type n) { - if (sizeof(T) * n > size) { - return static_cast(::operator new(n * sizeof(T))); - } else { - return reinterpret_cast(_stack); - } - } - - void deallocate(pointer p, size_type n) { - if (sizeof(T) * n > size) { - ::operator delete(p); - } else { - // do nothing, stack memory is automatically freed - } - } - - template - void construct(U* p, Args&&... args) { - new(p) U(std::forward(args)...); + public: + using value_type = T; + using size_type = size_t; + using difference_type = ptrdiff_t; + + T* allocate(size_type n) { + if (sizeof(T) * n > size) { + return static_cast(::operator new(n * sizeof(T))); } + return reinterpret_cast(_stack_mem); + } - template - void destroy(U* p) { - p->~U(); + void deallocate(T* p, size_type n) { + if (sizeof(T) * n > size) { + ::operator delete(p); } - - size_type max_size() const noexcept { - return std::max(1, size / sizeof(T)); - } - - pointer address(reference x) const noexcept { - return std::addressof(x); - } - - const T* address(const_reference x) const noexcept { - return std::addressof(x); - } - - StackAlloc() = default; - StackAlloc(const StackAlloc&) = default; - StackAlloc& operator=(const StackAlloc&) = default; - StackAlloc(StackAlloc&&) = default; - StackAlloc& operator=(StackAlloc&&) = default; - ~StackAlloc() = default; - -private: - alignas(T) char _stack[size]; + // else do nothing, stack memory is automatically freed + } + + StackAlloc() = default; + StackAlloc(const StackAlloc&) = default; + StackAlloc& operator=(const StackAlloc&) = default; + StackAlloc(StackAlloc&&) noexcept = default; + StackAlloc& operator=(StackAlloc&&) noexcept = default; + ~StackAlloc() = default; + + private: + alignas(T) uint8_t _stack_mem[size]; }; } // namespace er