annotate win64-msvc/include/kj/arena.h @ 133:1ac99bfc383d

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