annotate osx/include/kj/arena.h @ 169:223a55898ab9 tip default

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