cannam@135: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors cannam@135: // Licensed under the MIT License: cannam@135: // cannam@135: // Permission is hereby granted, free of charge, to any person obtaining a copy cannam@135: // of this software and associated documentation files (the "Software"), to deal cannam@135: // in the Software without restriction, including without limitation the rights cannam@135: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell cannam@135: // copies of the Software, and to permit persons to whom the Software is cannam@135: // furnished to do so, subject to the following conditions: cannam@135: // cannam@135: // The above copyright notice and this permission notice shall be included in cannam@135: // all copies or substantial portions of the Software. cannam@135: // cannam@135: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR cannam@135: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, cannam@135: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE cannam@135: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER cannam@135: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, cannam@135: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN cannam@135: // THE SOFTWARE. cannam@135: cannam@135: #ifndef KJ_ARENA_H_ cannam@135: #define KJ_ARENA_H_ cannam@135: cannam@135: #if defined(__GNUC__) && !KJ_HEADER_WARNINGS cannam@135: #pragma GCC system_header cannam@135: #endif cannam@135: cannam@135: #include "memory.h" cannam@135: #include "array.h" cannam@135: #include "string.h" cannam@135: cannam@135: namespace kj { cannam@135: cannam@135: class Arena { cannam@135: // A class which allows several objects to be allocated in contiguous chunks of memory, then cannam@135: // frees them all at once. cannam@135: // cannam@135: // Allocating from the same Arena in multiple threads concurrently is NOT safe, because making cannam@135: // it safe would require atomic operations that would slow down allocation even when cannam@135: // single-threaded. If you need to use arena allocation in a multithreaded context, consider cannam@135: // allocating thread-local arenas. cannam@135: cannam@135: public: cannam@135: explicit Arena(size_t chunkSizeHint = 1024); cannam@135: // Create an Arena. `chunkSizeHint` hints at where to start when allocating chunks, but is only cannam@135: // a hint -- the Arena will, for example, allocate progressively larger chunks as time goes on, cannam@135: // in order to reduce overall allocation overhead. cannam@135: cannam@135: explicit Arena(ArrayPtr scratch); cannam@135: // Allocates from the given scratch space first, only resorting to the heap when it runs out. cannam@135: cannam@135: KJ_DISALLOW_COPY(Arena); cannam@135: ~Arena() noexcept(false); cannam@135: cannam@135: template cannam@135: T& allocate(Params&&... params); cannam@135: template cannam@135: ArrayPtr allocateArray(size_t size); cannam@135: // Allocate an object or array of type T. If T has a non-trivial destructor, that destructor cannam@135: // will be run during the Arena's destructor. Such destructors are run in opposite order of cannam@135: // allocation. Note that these methods must maintain a list of destructors to call, which has cannam@135: // overhead, but this overhead only applies if T has a non-trivial destructor. cannam@135: cannam@135: template cannam@135: Own allocateOwn(Params&&... params); cannam@135: template cannam@135: Array allocateOwnArray(size_t size); cannam@135: template cannam@135: ArrayBuilder allocateOwnArrayBuilder(size_t capacity); cannam@135: // Allocate an object or array of type T. Destructors are executed when the returned Own cannam@135: // or Array goes out-of-scope, which must happen before the Arena is destroyed. This variant cannam@135: // is useful when you need to control when the destructor is called. This variant also avoids cannam@135: // the need for the Arena itself to keep track of destructors to call later, which may make it cannam@135: // slightly more efficient. cannam@135: cannam@135: template cannam@135: inline T& copy(T&& value) { return allocate>(kj::fwd(value)); } cannam@135: // Allocate a copy of the given value in the arena. This is just a shortcut for calling the cannam@135: // type's copy (or move) constructor. cannam@135: cannam@135: StringPtr copyString(StringPtr content); cannam@135: // Make a copy of the given string inside the arena, and return a pointer to the copy. cannam@135: cannam@135: private: cannam@135: struct ChunkHeader { cannam@135: ChunkHeader* next; cannam@135: byte* pos; // first unallocated byte in this chunk cannam@135: byte* end; // end of this chunk cannam@135: }; cannam@135: struct ObjectHeader { cannam@135: void (*destructor)(void*); cannam@135: ObjectHeader* next; cannam@135: }; cannam@135: cannam@135: size_t nextChunkSize; cannam@135: ChunkHeader* chunkList = nullptr; cannam@135: ObjectHeader* objectList = nullptr; cannam@135: cannam@135: ChunkHeader* currentChunk = nullptr; cannam@135: cannam@135: void cleanup(); cannam@135: // Run all destructors, leaving the above pointers null. If a destructor throws, the State is cannam@135: // left in a consistent state, such that if cleanup() is called again, it will pick up where cannam@135: // it left off. cannam@135: cannam@135: void* allocateBytes(size_t amount, uint alignment, bool hasDisposer); cannam@135: // Allocate the given number of bytes. `hasDisposer` must be true if `setDisposer()` may be cannam@135: // called on this pointer later. cannam@135: cannam@135: void* allocateBytesInternal(size_t amount, uint alignment); cannam@135: // Try to allocate the given number of bytes without taking a lock. Fails if and only if there cannam@135: // is no space left in the current chunk. cannam@135: cannam@135: void setDestructor(void* ptr, void (*destructor)(void*)); cannam@135: // Schedule the given destructor to be executed when the Arena is destroyed. `ptr` must be a cannam@135: // pointer previously returned by an `allocateBytes()` call for which `hasDisposer` was true. cannam@135: cannam@135: template cannam@135: static void destroyArray(void* pointer) { cannam@135: size_t elementCount = *reinterpret_cast(pointer); cannam@135: constexpr size_t prefixSize = kj::max(alignof(T), sizeof(size_t)); cannam@135: DestructorOnlyArrayDisposer::instance.disposeImpl( cannam@135: reinterpret_cast(pointer) + prefixSize, cannam@135: sizeof(T), elementCount, elementCount, &destroyObject); cannam@135: } cannam@135: cannam@135: template cannam@135: static void destroyObject(void* pointer) { cannam@135: dtor(*reinterpret_cast(pointer)); cannam@135: } cannam@135: }; cannam@135: cannam@135: // ======================================================================================= cannam@135: // Inline implementation details cannam@135: cannam@135: template cannam@135: T& Arena::allocate(Params&&... params) { cannam@135: T& result = *reinterpret_cast(allocateBytes( cannam@135: sizeof(T), alignof(T), !__has_trivial_destructor(T))); cannam@135: if (!__has_trivial_constructor(T) || sizeof...(Params) > 0) { cannam@135: ctor(result, kj::fwd(params)...); cannam@135: } cannam@135: if (!__has_trivial_destructor(T)) { cannam@135: setDestructor(&result, &destroyObject); cannam@135: } cannam@135: return result; cannam@135: } cannam@135: cannam@135: template cannam@135: ArrayPtr Arena::allocateArray(size_t size) { cannam@135: if (__has_trivial_destructor(T)) { cannam@135: ArrayPtr result = cannam@135: arrayPtr(reinterpret_cast(allocateBytes( cannam@135: sizeof(T) * size, alignof(T), false)), size); cannam@135: if (!__has_trivial_constructor(T)) { cannam@135: for (size_t i = 0; i < size; i++) { cannam@135: ctor(result[i]); cannam@135: } cannam@135: } cannam@135: return result; cannam@135: } else { cannam@135: // Allocate with a 64-bit prefix in which we store the array size. cannam@135: constexpr size_t prefixSize = kj::max(alignof(T), sizeof(size_t)); cannam@135: void* base = allocateBytes(sizeof(T) * size + prefixSize, alignof(T), true); cannam@135: size_t& tag = *reinterpret_cast(base); cannam@135: ArrayPtr result = cannam@135: arrayPtr(reinterpret_cast(reinterpret_cast(base) + prefixSize), size); cannam@135: setDestructor(base, &destroyArray); cannam@135: cannam@135: if (__has_trivial_constructor(T)) { cannam@135: tag = size; cannam@135: } else { cannam@135: // In case of constructor exceptions, we need the tag to end up storing the number of objects cannam@135: // that were successfully constructed, so that they'll be properly destroyed. cannam@135: tag = 0; cannam@135: for (size_t i = 0; i < size; i++) { cannam@135: ctor(result[i]); cannam@135: tag = i + 1; cannam@135: } cannam@135: } cannam@135: return result; cannam@135: } cannam@135: } cannam@135: cannam@135: template cannam@135: Own Arena::allocateOwn(Params&&... params) { cannam@135: T& result = *reinterpret_cast(allocateBytes(sizeof(T), alignof(T), false)); cannam@135: if (!__has_trivial_constructor(T) || sizeof...(Params) > 0) { cannam@135: ctor(result, kj::fwd(params)...); cannam@135: } cannam@135: return Own(&result, DestructorOnlyDisposer::instance); cannam@135: } cannam@135: cannam@135: template cannam@135: Array Arena::allocateOwnArray(size_t size) { cannam@135: ArrayBuilder result = allocateOwnArrayBuilder(size); cannam@135: for (size_t i = 0; i < size; i++) { cannam@135: result.add(); cannam@135: } cannam@135: return result.finish(); cannam@135: } cannam@135: cannam@135: template cannam@135: ArrayBuilder Arena::allocateOwnArrayBuilder(size_t capacity) { cannam@135: return ArrayBuilder( cannam@135: reinterpret_cast(allocateBytes(sizeof(T) * capacity, alignof(T), false)), cannam@135: capacity, DestructorOnlyArrayDisposer::instance); cannam@135: } cannam@135: cannam@135: } // namespace kj cannam@135: cannam@135: #endif // KJ_ARENA_H_