GameApi 28
Library used by GameApi Builder tool
Loading...
Searching...
No Matches
short_alloc.hpp
Go to the documentation of this file.
1#ifndef SHORT_ALLOC_H
2#define SHORT_ALLOC_H
3
4// The MIT License (MIT)
5//
6// Copyright (c) 2015 Howard Hinnant
7//
8// Permission is hereby granted, free of charge, to any person obtaining a copy
9// of this software and associated documentation files (the "Software"), to deal
10// in the Software without restriction, including without limitation the rights
11// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12// copies of the Software, and to permit persons to whom the Software is
13// furnished to do so, subject to the following conditions:
14//
15// The above copyright notice and this permission notice shall be included in all
16// copies or substantial portions of the Software.
17//
18// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24// SOFTWARE.
25
26#include <cstddef>
27#include <cassert>
28
29template <std::size_t N, std::size_t alignment = alignof(std::max_align_t)>
30class arena
31{
32 alignas(alignment) char buf_[N];
33 char* ptr_;
34
35public:
36 ~arena() {ptr_ = nullptr;}
37 arena() noexcept : ptr_(buf_) {}
38 arena(const arena&) = delete;
39 arena& operator=(const arena&) = delete;
40
41 template <std::size_t ReqAlign> char* allocate(std::size_t n);
42 void deallocate(char* p, std::size_t n) noexcept;
43
44 static constexpr std::size_t size() noexcept {return N;}
45 std::size_t used() const noexcept {return static_cast<std::size_t>(ptr_ - buf_);}
46 void reset() noexcept {ptr_ = buf_;}
47
48private:
49 static
50 std::size_t
51 align_up(std::size_t n) noexcept
52 {return (n + (alignment-1)) & ~(alignment-1);}
53
54 bool
55 pointer_in_buffer(char* p) noexcept
56 {return buf_ <= p && p <= buf_ + N;}
57};
58
59template <std::size_t N, std::size_t alignment>
60template <std::size_t ReqAlign>
61char*
63{
64 static_assert(ReqAlign <= alignment, "alignment is too small for this arena");
65 assert(pointer_in_buffer(ptr_) && "short_alloc has outlived arena");
66 auto const aligned_n = align_up(n);
67 if (static_cast<decltype(aligned_n)>(buf_ + N - ptr_) >= aligned_n)
68 {
69 char* r = ptr_;
70 ptr_ += aligned_n;
71 return r;
72 }
73
74 static_assert(alignment <= alignof(std::max_align_t), "you've chosen an "
75 "alignment that is larger than alignof(std::max_align_t), and "
76 "cannot be guaranteed by normal operator new");
77 return static_cast<char*>(::operator new(n));
78}
79
80template <std::size_t N, std::size_t alignment>
81void
82arena<N, alignment>::deallocate(char* p, std::size_t n) noexcept
83{
84 assert(pointer_in_buffer(ptr_) && "short_alloc has outlived arena");
85 if (pointer_in_buffer(p))
86 {
87 n = align_up(n);
88 if (p + n == ptr_) {
89 ptr_ = p;
90 }
91 }
92 else {
93 ::operator delete(p);
94 }
95}
96
97template <class T, std::size_t N, std::size_t Align = alignof(std::max_align_t)>
99{
100public:
101 using value_type = T;
102 static auto constexpr alignment = Align;
103 static auto constexpr size = N;
105
106private:
107 arena_type& a_;
108
109public:
110 short_alloc(const short_alloc&) = default;
112
113 explicit short_alloc(arena_type& a) noexcept : a_(a)
114 {
115 static_assert(size % alignment == 0,
116 "size N needs to be a multiple of alignment Align");
117 }
118 template <class U>
119 explicit short_alloc(const short_alloc<U, N, alignment>& a) noexcept
120 : a_(a.a_) {}
121
122 template <class _Up> struct rebind {using other = short_alloc<_Up, N, alignment>;};
123
124 T* allocate(std::size_t n)
125 {
126 return reinterpret_cast<T*>(a_.template allocate<alignof(T)>(n*sizeof(T)));
127 }
128 void deallocate(T* p, std::size_t n) noexcept
129 {
130 a_.deallocate(reinterpret_cast<char*>(p), n*sizeof(T));
131 }
132
133 template <class T1, std::size_t N1, std::size_t A1,
134 class U, std::size_t M, std::size_t A2>
135 friend
136 bool
138
139 template <class U, std::size_t M, std::size_t A> friend class short_alloc;
140};
141
142template <class T, std::size_t N, std::size_t A1, class U, std::size_t M, std::size_t A2>
143inline
144bool
146{
147 return N == M && A1 == A2 && &x.a_ == &y.a_;
148}
149
150template <class T, std::size_t N, std::size_t A1, class U, std::size_t M, std::size_t A2>
151inline
152bool
154{
155 return !(x == y);
156}
157
158#endif // SHORT_ALLOC_HPP
159
Definition short_alloc.hpp:31
arena(const arena &)=delete
char * allocate(std::size_t n)
Definition short_alloc.hpp:62
void deallocate(char *p, std::size_t n) noexcept
Definition short_alloc.hpp:82
void reset() noexcept
Definition short_alloc.hpp:46
~arena()
Definition short_alloc.hpp:36
std::size_t used() const noexcept
Definition short_alloc.hpp:45
static constexpr std::size_t size() noexcept
Definition short_alloc.hpp:44
arena & operator=(const arena &)=delete
arena() noexcept
Definition short_alloc.hpp:37
Definition short_alloc.hpp:99
short_alloc(arena_type &a) noexcept
Definition short_alloc.hpp:113
short_alloc(const short_alloc< U, N, alignment > &a) noexcept
Definition short_alloc.hpp:119
static auto constexpr size
Definition short_alloc.hpp:103
short_alloc & operator=(const short_alloc &)=delete
T value_type
Definition short_alloc.hpp:101
arena< size, alignment > arena_type
Definition short_alloc.hpp:104
T * allocate(std::size_t n)
Definition short_alloc.hpp:124
void deallocate(T *p, std::size_t n) noexcept
Definition short_alloc.hpp:128
friend bool operator==(const short_alloc< T1, N1, A1 > &x, const short_alloc< U, M, A2 > &y) noexcept
static auto constexpr alignment
Definition short_alloc.hpp:102
short_alloc(const short_alloc &)=default
friend class short_alloc
Definition short_alloc.hpp:139
bool operator==(const short_alloc< T, N, A1 > &x, const short_alloc< U, M, A2 > &y) noexcept
Definition short_alloc.hpp:145
bool operator!=(const short_alloc< T, N, A1 > &x, const short_alloc< U, M, A2 > &y) noexcept
Definition short_alloc.hpp:153
Definition GameApi.h:18
Definition GameApi.h:42
Definition short_alloc.hpp:122
short_alloc< _Up, N, alignment > other
Definition short_alloc.hpp:122