annotate win32-mingw/include/kj/arena.h @ 72:7b5216b54e42

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