annotate win32-mingw/include/kj/arena.h @ 163:5cc1366da2e9

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