bmv2
Designing your own switch target with bmv2
Loading...
Searching...
No Matches
short_alloc.h
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
29namespace detail {
30
31// See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56019
32// TODO(antonin): find a better way to do this?
33#if __GNUC__ == 4 && __GNUC_MINOR__ <= 8
34 using max_align_t = ::max_align_t;
35#else
36 using max_align_t = std::max_align_t;
37#endif
38
39template <std::size_t N, std::size_t alignment = alignof(max_align_t)>
40class arena
41{
42 alignas(alignment) char buf_[N];
43 char* ptr_;
44
45public:
46 ~arena() {ptr_ = nullptr;}
47 arena() noexcept : ptr_(buf_) {}
48 arena(const arena&) = delete;
49 arena& operator=(const arena&) = delete;
50
51 template <std::size_t ReqAlign> char* allocate(std::size_t n);
52 void deallocate(char* p, std::size_t n) noexcept;
53
54 static constexpr std::size_t size() noexcept {return N;}
55 std::size_t used() const noexcept {return static_cast<std::size_t>(ptr_ - buf_);}
56 void reset() noexcept {ptr_ = buf_;}
57
58private:
59 static
60 std::size_t
61 align_up(std::size_t n) noexcept
62 {return (n + (alignment-1)) & ~(alignment-1);}
63
64 bool
65 pointer_in_buffer(char* p) noexcept
66 {return buf_ <= p && p <= buf_ + N;}
67};
68
69template <std::size_t N, std::size_t alignment>
70template <std::size_t ReqAlign>
71char*
72arena<N, alignment>::allocate(std::size_t n)
73{
74 static_assert(ReqAlign <= alignment, "alignment is too small for this arena");
75 assert(pointer_in_buffer(ptr_) && "short_alloc has outlived arena");
76 auto const aligned_n = align_up(n);
77 if (static_cast<decltype(aligned_n)>(buf_ + N - ptr_) >= aligned_n)
78 {
79 char* r = ptr_;
80 ptr_ += aligned_n;
81 return r;
82 }
83
84 static_assert(alignment <= alignof(max_align_t), "you've chosen an "
85 "alignment that is larger than alignof(max_align_t), and "
86 "cannot be guaranteed by normal operator new");
87 return static_cast<char*>(::operator new(n));
88}
89
90template <std::size_t N, std::size_t alignment>
91void
92arena<N, alignment>::deallocate(char* p, std::size_t n) noexcept
93{
94 assert(pointer_in_buffer(ptr_) && "short_alloc has outlived arena");
95 if (pointer_in_buffer(p))
96 {
97 n = align_up(n);
98 if (p + n == ptr_)
99 ptr_ = p;
100 }
101 else
102 ::operator delete(p);
103}
104
105template <class T, std::size_t N, std::size_t Align = alignof(max_align_t)>
106class short_alloc
107{
108public:
109 using value_type = T;
110 static auto constexpr alignment = Align;
111 static auto constexpr size = N;
112 using arena_type = arena<size, alignment>;
113
114private:
115 arena_type& a_;
116
117public:
118 short_alloc(const short_alloc&) = default;
119 short_alloc& operator=(const short_alloc&) = delete;
120
121 short_alloc(arena_type& a) noexcept : a_(a)
122 {
123 static_assert(size % alignment == 0,
124 "size N needs to be a multiple of alignment Align");
125 }
126 template <class U>
127 short_alloc(const short_alloc<U, N, alignment>& a) noexcept
128 : a_(a.a_) {}
129
130 template <class _Up> struct rebind {using other = short_alloc<_Up, N, alignment>;};
131
132 T* allocate(std::size_t n)
133 {
134 return reinterpret_cast<T*>(a_.template allocate<alignof(T)>(n*sizeof(T)));
135 }
136 void deallocate(T* p, std::size_t n) noexcept
137 {
138 a_.deallocate(reinterpret_cast<char*>(p), n*sizeof(T));
139 }
140
141 template <class T1, std::size_t N1, std::size_t A1,
142 class U, std::size_t M, std::size_t A2>
143 friend
144 bool
145 operator==(const short_alloc<T1, N1, A1>& x, const short_alloc<U, M, A2>& y) noexcept;
146
147 template <class U, std::size_t M, std::size_t A> friend class short_alloc;
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
153operator==(const short_alloc<T, N, A1>& x, const short_alloc<U, M, A2>& y) noexcept
154{
155 return N == M && A1 == A2 && &x.a_ == &y.a_;
156}
157
158template <class T, std::size_t N, std::size_t A1, class U, std::size_t M, std::size_t A2>
159inline
160bool
161operator!=(const short_alloc<T, N, A1>& x, const short_alloc<U, M, A2>& y) noexcept
162{
163 return !(x == y);
164}
165
166} // namespace detail
167
168#endif // SHORT_ALLOC_H