1 #ifndef DONUT_LINEAR_ALLOCATOR_HPP
2 #define DONUT_LINEAR_ALLOCATOR_HPP
21 : remainingMemoryBegin(initialMemory.data())
22 , remainingMemorySize(initialMemory.size())
23 , nextChunkSize(std::max(std::size_t{1024}, initialMemory.size() + initialMemory.size() / 2)) {}
25 void*
allocate(std::size_t size, std::size_t alignment) {
30 void* result = std::align(alignment, size, remainingMemoryBegin, remainingMemorySize);
33 const std::size_t newChunkSize = std::max(size, nextChunkSize);
34 const std::size_t newChunkAlignment = std::max(alignment,
alignof(std::max_align_t));
35 if (extraMemory.capacity() < 4) {
36 extraMemory.reserve(4);
38 AlignedHeapMemoryChunk& newChunk = extraMemory.emplace_back(newChunkSize, newChunkAlignment);
39 remainingMemoryBegin = newChunk.memory;
40 remainingMemorySize = newChunkSize;
41 nextChunkSize += nextChunkSize / 2;
42 result = remainingMemoryBegin;
44 remainingMemoryBegin =
static_cast<std::byte*
>(remainingMemoryBegin) + size;
45 remainingMemorySize -= size;
50 return remainingMemorySize;
54 struct AlignedHeapMemoryChunk {
56 std::size_t alignment;
58 AlignedHeapMemoryChunk(std::size_t size, std::size_t alignment)
59 : memory(operator new[](size, static_cast<std::align_val_t>(alignment)))
60 , alignment(alignment) {}
62 ~AlignedHeapMemoryChunk() {
63 operator delete[](memory,
static_cast<std::align_val_t
>(alignment));
66 AlignedHeapMemoryChunk(
const AlignedHeapMemoryChunk&) =
delete;
68 AlignedHeapMemoryChunk(AlignedHeapMemoryChunk&& other) noexcept
69 : memory(std::exchange(other.memory,
nullptr))
70 , alignment(std::exchange(other.alignment, 1)) {}
72 AlignedHeapMemoryChunk& operator=(
const AlignedHeapMemoryChunk&) =
delete;
74 AlignedHeapMemoryChunk& operator=(AlignedHeapMemoryChunk&& other) noexcept {
81 void* remainingMemoryBegin;
82 std::size_t remainingMemorySize;
83 std::size_t nextChunkSize;
84 std::vector<AlignedHeapMemoryChunk> extraMemory{};
93 : memoryResource(memoryResource) {
94 assert(memoryResource);
99 : memoryResource(other.memoryResource) {}
107 return static_cast<T*
>(memoryResource->
allocate(n *
sizeof(T),
alignof(T)));
112 template <
typename U>
114 return memoryResource == other.memoryResource;
118 template <
typename U>
Definition: LinearAllocator.hpp:88
bool operator==(const LinearAllocator< U > &other) const noexcept
Definition: LinearAllocator.hpp:113
void deallocate(T *, std::size_t) noexcept
Definition: LinearAllocator.hpp:110
LinearAllocator(LinearAllocator &&other) noexcept=default
LinearAllocator(const LinearAllocator< U > &other) noexcept
Definition: LinearAllocator.hpp:98
LinearAllocator & operator=(LinearAllocator &&other) noexcept=default
T * allocate(std::size_t n)
Definition: LinearAllocator.hpp:106
LinearAllocator & operator=(const LinearAllocator &other) noexcept=default
LinearAllocator(LinearMemoryResource *memoryResource) noexcept
Definition: LinearAllocator.hpp:92
T value_type
Definition: LinearAllocator.hpp:90
LinearAllocator(const LinearAllocator &other) noexcept=default
Definition: LinearAllocator.hpp:15
LinearMemoryResource(std::span< std::byte > initialMemory) noexcept
Definition: LinearAllocator.hpp:20
void * allocate(std::size_t size, std::size_t alignment)
Definition: LinearAllocator.hpp:25
LinearMemoryResource() noexcept
Definition: LinearAllocator.hpp:17
std::size_t getRemainingCapacity() const noexcept
Definition: LinearAllocator.hpp:49
void swap(Object &a, Object &b) noexcept
Definition: json.hpp:3980
Definition: Application.hpp:9