1 /* Copyright (C) 2021 Wildfire Games.
3 * Permission is hereby granted, free of charge, to any person obtaining
4 * a copy of this software and associated documentation files (the
5 * "Software"), to deal in the Software without restriction, including
6 * without limitation the rights to use, copy, modify, merge, publish,
7 * distribute, sublicense, and/or sell copies of the Software, and to
8 * permit persons to whom the Software is furnished to do so, subject to
9 * the following conditions:
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 #ifndef INCLUDED_ALLOCATORS_DYNAMIC_ARENA
24 #define INCLUDED_ALLOCATORS_DYNAMIC_ARENA
29 #include <new> // bad_alloc
33 namespace Allocators
{
36 * 'Blind' memory allocator. Characteristics:
37 * - grows dynamically, and linearily, by BLOCK_SIZE
38 * - no reallocations, pointers remain valid
39 * - can allocate objects up to BLOCK_SIZE in size
40 * - can handle any aligment (up to BLOCK_SIZE)
41 * - Doesn't de-allocate unless cleared or destroyed.
43 template<size_t BLOCK_SIZE
>
52 m_Data
= static_cast<uint8_t*>(
53 std::malloc(BLOCK_SIZE
* sizeof(uint8_t)));
56 debug_warn("DynamicArena failed to allocate chunk");
57 throw std::bad_alloc();
63 std::free(static_cast<void*>(m_Data
));
66 Block(const Block
& other
) = delete;
67 Block
& operator=(const Block
& other
) = delete;
70 : m_Size(other
.m_Size
), m_Data(other
.m_Data
)
73 other
.m_Data
= nullptr;
76 Block
& operator=(Block
&& other
)
81 Block
tmp(std::move(other
));
87 friend void swap(Block
& lhs
, Block
& rhs
)
91 swap(lhs
.m_Size
, rhs
.m_Size
);
92 swap(lhs
.m_Data
, rhs
.m_Data
);
95 bool Available(size_t n
, size_t alignment
) const
97 return n
+ ROUND_UP(m_Size
, alignment
) <= BLOCK_SIZE
;
100 uint8_t* Allocate(size_t n
, size_t alignment
)
102 m_Size
= ROUND_UP(m_Size
, alignment
);
103 uint8_t* ptr
= &m_Data
[m_Size
];
110 uint8_t* m_Data
= nullptr;
113 NONCOPYABLE(DynamicArena
);
117 m_Blocks
.reserve(16);
121 void AllocateNewBlock()
123 m_Blocks
.emplace_back();
126 void* allocate(size_t n
, const void*, size_t alignment
)
128 // Safely handle zero-sized allocations (happens with GCC STL - see ticket #909).
134 debug_warn("DynamicArena cannot allocate more than chunk size");
135 throw std::bad_alloc();
138 if (!m_Blocks
.back().Available(n
, alignment
))
141 return reinterpret_cast<void*>(m_Blocks
.back().Allocate(n
, alignment
));
144 void deallocate(void* UNUSED(p
), size_t UNUSED(n
))
156 std::vector
<Block
> m_Blocks
;
159 } // namespace Allocators
161 #endif // INCLUDED_ALLOCATORS_DYNAMIC_ARENA