annotate win64-msvc/include/kj/arena.h @ 64:eccd51b72864

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