annotate win32-mingw/include/kj/arena.h @ 55:284acf908dcd

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