From 14d9f8ebf119d0dfd7493aef3d3340d5cbc368b0 Mon Sep 17 00:00:00 2001 From: subhransu mohanty Date: Fri, 31 Aug 2018 14:37:33 +0900 Subject: [PATCH] lottie/vector: add a local arena allocator. Change-Id: I7f76ab73d457ee9f4e9b5e35c1303c63c14efcea --- src/vector/vstackallocator.h | 134 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 src/vector/vstackallocator.h diff --git a/src/vector/vstackallocator.h b/src/vector/vstackallocator.h new file mode 100644 index 0000000..ce5c7b5 --- /dev/null +++ b/src/vector/vstackallocator.h @@ -0,0 +1,134 @@ +#ifndef VSTACK_ALLOCATOR_H +#define VSTACK_ALLOCATOR_H + +#include +#include + +template +class arena +{ + alignas(alignment) char buf_[N]; + char* ptr_; + +public: + ~arena() {ptr_ = nullptr;} + arena() noexcept : ptr_(buf_) {} + arena(const arena&) = delete; + arena& operator=(const arena&) = delete; + + template char* allocate(std::size_t n); + void deallocate(char* p, std::size_t n) noexcept; + + static constexpr std::size_t size() noexcept {return N;} + std::size_t used() const noexcept {return static_cast(ptr_ - buf_);} + void reset() noexcept {ptr_ = buf_;} + +private: + static + std::size_t + align_up(std::size_t n) noexcept + {return (n + (alignment-1)) & ~(alignment-1);} + + bool + pointer_in_buffer(char* p) noexcept + {return buf_ <= p && p <= buf_ + N;} +}; + +template +template +char* +arena::allocate(std::size_t n) +{ + static_assert(ReqAlign <= alignment, "alignment is too small for this arena"); + assert(pointer_in_buffer(ptr_) && "stack_alloc has outlived arena"); + auto const aligned_n = align_up(n); + if (static_cast(buf_ + N - ptr_) >= aligned_n) + { + char* r = ptr_; + ptr_ += aligned_n; + return r; + } + + static_assert(alignment <= alignof(std::max_align_t), "you've chosen an " + "alignment that is larger than alignof(std::max_align_t), and " + "cannot be guaranteed by normal operator new"); + return static_cast(::operator new(n)); +} + +template +void +arena::deallocate(char* p, std::size_t n) noexcept +{ + assert(pointer_in_buffer(ptr_) && "stack_alloc has outlived arena"); + if (pointer_in_buffer(p)) + { + n = align_up(n); + if (p + n == ptr_) + ptr_ = p; + } + else + ::operator delete(p); +} + +template +class stack_alloc +{ +public: + using value_type = T; + static auto constexpr alignment = Align; + static auto constexpr size = N; + using arena_type = arena; + +private: + arena_type& a_; + +public: + stack_alloc(const stack_alloc&) = default; + stack_alloc& operator=(const stack_alloc&) = delete; + + stack_alloc(arena_type& a) noexcept : a_(a) + { + static_assert(size % alignment == 0, + "size N needs to be a multiple of alignment Align"); + } + template + stack_alloc(const stack_alloc& a) noexcept + : a_(a.a_) {} + + template struct rebind {using other = stack_alloc<_Up, N, alignment>;}; + + T* allocate(std::size_t n) + { + return reinterpret_cast(a_.template allocate(n*sizeof(T))); + } + void deallocate(T* p, std::size_t n) noexcept + { + a_.deallocate(reinterpret_cast(p), n*sizeof(T)); + } + + template + friend + bool + operator==(const stack_alloc& x, const stack_alloc& y) noexcept; + + template friend class stack_alloc; +}; + +template +inline +bool +operator==(const stack_alloc& x, const stack_alloc& y) noexcept +{ + return N == M && A1 == A2 && &x.a_ == &y.a_; +} + +template +inline +bool +operator!=(const stack_alloc& x, const stack_alloc& y) noexcept +{ + return !(x == y); +} + +#endif // VSTACK_ALLOCATOR_H -- 2.7.4