annotate osx/include/kj/arena.h @ 140:59a8758c56b1

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