annotate win64-msvc/include/kj/arena.h @ 59:cd4953afea46

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