cannam@148
|
1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
|
cannam@148
|
2 // Licensed under the MIT License:
|
cannam@148
|
3 //
|
cannam@148
|
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
|
cannam@148
|
5 // of this software and associated documentation files (the "Software"), to deal
|
cannam@148
|
6 // in the Software without restriction, including without limitation the rights
|
cannam@148
|
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
cannam@148
|
8 // copies of the Software, and to permit persons to whom the Software is
|
cannam@148
|
9 // furnished to do so, subject to the following conditions:
|
cannam@148
|
10 //
|
cannam@148
|
11 // The above copyright notice and this permission notice shall be included in
|
cannam@148
|
12 // all copies or substantial portions of the Software.
|
cannam@148
|
13 //
|
cannam@148
|
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
cannam@148
|
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
cannam@148
|
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
cannam@148
|
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
cannam@148
|
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
cannam@148
|
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
cannam@148
|
20 // THE SOFTWARE.
|
cannam@148
|
21
|
cannam@148
|
22 #ifndef KJ_ARENA_H_
|
cannam@148
|
23 #define KJ_ARENA_H_
|
cannam@148
|
24
|
cannam@148
|
25 #if defined(__GNUC__) && !KJ_HEADER_WARNINGS
|
cannam@148
|
26 #pragma GCC system_header
|
cannam@148
|
27 #endif
|
cannam@148
|
28
|
cannam@148
|
29 #include "memory.h"
|
cannam@148
|
30 #include "array.h"
|
cannam@148
|
31 #include "string.h"
|
cannam@148
|
32
|
cannam@148
|
33 namespace kj {
|
cannam@148
|
34
|
cannam@148
|
35 class Arena {
|
cannam@148
|
36 // A class which allows several objects to be allocated in contiguous chunks of memory, then
|
cannam@148
|
37 // frees them all at once.
|
cannam@148
|
38 //
|
cannam@148
|
39 // Allocating from the same Arena in multiple threads concurrently is NOT safe, because making
|
cannam@148
|
40 // it safe would require atomic operations that would slow down allocation even when
|
cannam@148
|
41 // single-threaded. If you need to use arena allocation in a multithreaded context, consider
|
cannam@148
|
42 // allocating thread-local arenas.
|
cannam@148
|
43
|
cannam@148
|
44 public:
|
cannam@148
|
45 explicit Arena(size_t chunkSizeHint = 1024);
|
cannam@148
|
46 // Create an Arena. `chunkSizeHint` hints at where to start when allocating chunks, but is only
|
cannam@148
|
47 // a hint -- the Arena will, for example, allocate progressively larger chunks as time goes on,
|
cannam@148
|
48 // in order to reduce overall allocation overhead.
|
cannam@148
|
49
|
cannam@148
|
50 explicit Arena(ArrayPtr<byte> scratch);
|
cannam@148
|
51 // Allocates from the given scratch space first, only resorting to the heap when it runs out.
|
cannam@148
|
52
|
cannam@148
|
53 KJ_DISALLOW_COPY(Arena);
|
cannam@148
|
54 ~Arena() noexcept(false);
|
cannam@148
|
55
|
cannam@148
|
56 template <typename T, typename... Params>
|
cannam@148
|
57 T& allocate(Params&&... params);
|
cannam@148
|
58 template <typename T>
|
cannam@148
|
59 ArrayPtr<T> allocateArray(size_t size);
|
cannam@148
|
60 // Allocate an object or array of type T. If T has a non-trivial destructor, that destructor
|
cannam@148
|
61 // will be run during the Arena's destructor. Such destructors are run in opposite order of
|
cannam@148
|
62 // allocation. Note that these methods must maintain a list of destructors to call, which has
|
cannam@148
|
63 // overhead, but this overhead only applies if T has a non-trivial destructor.
|
cannam@148
|
64
|
cannam@148
|
65 template <typename T, typename... Params>
|
cannam@148
|
66 Own<T> allocateOwn(Params&&... params);
|
cannam@148
|
67 template <typename T>
|
cannam@148
|
68 Array<T> allocateOwnArray(size_t size);
|
cannam@148
|
69 template <typename T>
|
cannam@148
|
70 ArrayBuilder<T> allocateOwnArrayBuilder(size_t capacity);
|
cannam@148
|
71 // Allocate an object or array of type T. Destructors are executed when the returned Own<T>
|
cannam@148
|
72 // or Array<T> goes out-of-scope, which must happen before the Arena is destroyed. This variant
|
cannam@148
|
73 // is useful when you need to control when the destructor is called. This variant also avoids
|
cannam@148
|
74 // the need for the Arena itself to keep track of destructors to call later, which may make it
|
cannam@148
|
75 // slightly more efficient.
|
cannam@148
|
76
|
cannam@148
|
77 template <typename T>
|
cannam@148
|
78 inline T& copy(T&& value) { return allocate<Decay<T>>(kj::fwd<T>(value)); }
|
cannam@148
|
79 // Allocate a copy of the given value in the arena. This is just a shortcut for calling the
|
cannam@148
|
80 // type's copy (or move) constructor.
|
cannam@148
|
81
|
cannam@148
|
82 StringPtr copyString(StringPtr content);
|
cannam@148
|
83 // Make a copy of the given string inside the arena, and return a pointer to the copy.
|
cannam@148
|
84
|
cannam@148
|
85 private:
|
cannam@148
|
86 struct ChunkHeader {
|
cannam@148
|
87 ChunkHeader* next;
|
cannam@148
|
88 byte* pos; // first unallocated byte in this chunk
|
cannam@148
|
89 byte* end; // end of this chunk
|
cannam@148
|
90 };
|
cannam@148
|
91 struct ObjectHeader {
|
cannam@148
|
92 void (*destructor)(void*);
|
cannam@148
|
93 ObjectHeader* next;
|
cannam@148
|
94 };
|
cannam@148
|
95
|
cannam@148
|
96 size_t nextChunkSize;
|
cannam@148
|
97 ChunkHeader* chunkList = nullptr;
|
cannam@148
|
98 ObjectHeader* objectList = nullptr;
|
cannam@148
|
99
|
cannam@148
|
100 ChunkHeader* currentChunk = nullptr;
|
cannam@148
|
101
|
cannam@148
|
102 void cleanup();
|
cannam@148
|
103 // Run all destructors, leaving the above pointers null. If a destructor throws, the State is
|
cannam@148
|
104 // left in a consistent state, such that if cleanup() is called again, it will pick up where
|
cannam@148
|
105 // it left off.
|
cannam@148
|
106
|
cannam@148
|
107 void* allocateBytes(size_t amount, uint alignment, bool hasDisposer);
|
cannam@148
|
108 // Allocate the given number of bytes. `hasDisposer` must be true if `setDisposer()` may be
|
cannam@148
|
109 // called on this pointer later.
|
cannam@148
|
110
|
cannam@148
|
111 void* allocateBytesInternal(size_t amount, uint alignment);
|
cannam@148
|
112 // Try to allocate the given number of bytes without taking a lock. Fails if and only if there
|
cannam@148
|
113 // is no space left in the current chunk.
|
cannam@148
|
114
|
cannam@148
|
115 void setDestructor(void* ptr, void (*destructor)(void*));
|
cannam@148
|
116 // Schedule the given destructor to be executed when the Arena is destroyed. `ptr` must be a
|
cannam@148
|
117 // pointer previously returned by an `allocateBytes()` call for which `hasDisposer` was true.
|
cannam@148
|
118
|
cannam@148
|
119 template <typename T>
|
cannam@148
|
120 static void destroyArray(void* pointer) {
|
cannam@148
|
121 size_t elementCount = *reinterpret_cast<size_t*>(pointer);
|
cannam@148
|
122 constexpr size_t prefixSize = kj::max(alignof(T), sizeof(size_t));
|
cannam@148
|
123 DestructorOnlyArrayDisposer::instance.disposeImpl(
|
cannam@148
|
124 reinterpret_cast<byte*>(pointer) + prefixSize,
|
cannam@148
|
125 sizeof(T), elementCount, elementCount, &destroyObject<T>);
|
cannam@148
|
126 }
|
cannam@148
|
127
|
cannam@148
|
128 template <typename T>
|
cannam@148
|
129 static void destroyObject(void* pointer) {
|
cannam@148
|
130 dtor(*reinterpret_cast<T*>(pointer));
|
cannam@148
|
131 }
|
cannam@148
|
132 };
|
cannam@148
|
133
|
cannam@148
|
134 // =======================================================================================
|
cannam@148
|
135 // Inline implementation details
|
cannam@148
|
136
|
cannam@148
|
137 template <typename T, typename... Params>
|
cannam@148
|
138 T& Arena::allocate(Params&&... params) {
|
cannam@148
|
139 T& result = *reinterpret_cast<T*>(allocateBytes(
|
cannam@148
|
140 sizeof(T), alignof(T), !__has_trivial_destructor(T)));
|
cannam@148
|
141 if (!__has_trivial_constructor(T) || sizeof...(Params) > 0) {
|
cannam@148
|
142 ctor(result, kj::fwd<Params>(params)...);
|
cannam@148
|
143 }
|
cannam@148
|
144 if (!__has_trivial_destructor(T)) {
|
cannam@148
|
145 setDestructor(&result, &destroyObject<T>);
|
cannam@148
|
146 }
|
cannam@148
|
147 return result;
|
cannam@148
|
148 }
|
cannam@148
|
149
|
cannam@148
|
150 template <typename T>
|
cannam@148
|
151 ArrayPtr<T> Arena::allocateArray(size_t size) {
|
cannam@148
|
152 if (__has_trivial_destructor(T)) {
|
cannam@148
|
153 ArrayPtr<T> result =
|
cannam@148
|
154 arrayPtr(reinterpret_cast<T*>(allocateBytes(
|
cannam@148
|
155 sizeof(T) * size, alignof(T), false)), size);
|
cannam@148
|
156 if (!__has_trivial_constructor(T)) {
|
cannam@148
|
157 for (size_t i = 0; i < size; i++) {
|
cannam@148
|
158 ctor(result[i]);
|
cannam@148
|
159 }
|
cannam@148
|
160 }
|
cannam@148
|
161 return result;
|
cannam@148
|
162 } else {
|
cannam@148
|
163 // Allocate with a 64-bit prefix in which we store the array size.
|
cannam@148
|
164 constexpr size_t prefixSize = kj::max(alignof(T), sizeof(size_t));
|
cannam@148
|
165 void* base = allocateBytes(sizeof(T) * size + prefixSize, alignof(T), true);
|
cannam@148
|
166 size_t& tag = *reinterpret_cast<size_t*>(base);
|
cannam@148
|
167 ArrayPtr<T> result =
|
cannam@148
|
168 arrayPtr(reinterpret_cast<T*>(reinterpret_cast<byte*>(base) + prefixSize), size);
|
cannam@148
|
169 setDestructor(base, &destroyArray<T>);
|
cannam@148
|
170
|
cannam@148
|
171 if (__has_trivial_constructor(T)) {
|
cannam@148
|
172 tag = size;
|
cannam@148
|
173 } else {
|
cannam@148
|
174 // In case of constructor exceptions, we need the tag to end up storing the number of objects
|
cannam@148
|
175 // that were successfully constructed, so that they'll be properly destroyed.
|
cannam@148
|
176 tag = 0;
|
cannam@148
|
177 for (size_t i = 0; i < size; i++) {
|
cannam@148
|
178 ctor(result[i]);
|
cannam@148
|
179 tag = i + 1;
|
cannam@148
|
180 }
|
cannam@148
|
181 }
|
cannam@148
|
182 return result;
|
cannam@148
|
183 }
|
cannam@148
|
184 }
|
cannam@148
|
185
|
cannam@148
|
186 template <typename T, typename... Params>
|
cannam@148
|
187 Own<T> Arena::allocateOwn(Params&&... params) {
|
cannam@148
|
188 T& result = *reinterpret_cast<T*>(allocateBytes(sizeof(T), alignof(T), false));
|
cannam@148
|
189 if (!__has_trivial_constructor(T) || sizeof...(Params) > 0) {
|
cannam@148
|
190 ctor(result, kj::fwd<Params>(params)...);
|
cannam@148
|
191 }
|
cannam@148
|
192 return Own<T>(&result, DestructorOnlyDisposer<T>::instance);
|
cannam@148
|
193 }
|
cannam@148
|
194
|
cannam@148
|
195 template <typename T>
|
cannam@148
|
196 Array<T> Arena::allocateOwnArray(size_t size) {
|
cannam@148
|
197 ArrayBuilder<T> result = allocateOwnArrayBuilder<T>(size);
|
cannam@148
|
198 for (size_t i = 0; i < size; i++) {
|
cannam@148
|
199 result.add();
|
cannam@148
|
200 }
|
cannam@148
|
201 return result.finish();
|
cannam@148
|
202 }
|
cannam@148
|
203
|
cannam@148
|
204 template <typename T>
|
cannam@148
|
205 ArrayBuilder<T> Arena::allocateOwnArrayBuilder(size_t capacity) {
|
cannam@148
|
206 return ArrayBuilder<T>(
|
cannam@148
|
207 reinterpret_cast<T*>(allocateBytes(sizeof(T) * capacity, alignof(T), false)),
|
cannam@148
|
208 capacity, DestructorOnlyArrayDisposer::instance);
|
cannam@148
|
209 }
|
cannam@148
|
210
|
cannam@148
|
211 } // namespace kj
|
cannam@148
|
212
|
cannam@148
|
213 #endif // KJ_ARENA_H_
|