IceShard 1
A personal game engine project, with development focused on 2D/2.5D games.
Loading...
Searching...
No Matches
mem_allocator_stack.hxx
Go to the documentation of this file.
1
3
4#pragma once
6
7namespace ice
8{
9 template<ice::usize Capacity>
10 struct StackAllocator;
11
12 //using StackAllocator_256 = StackAllocator<256_B>;
15
16 template<ice::usize Capacity>
18 {
19 static constexpr ice::usize Constant_InternalCapacity = Capacity;
20
22 std::source_location = std::source_location::current()
23 ) noexcept;
24
26 ice::Allocator& backing_allocator,
27 std::source_location = std::source_location::current()
28 ) noexcept;
29
31 ice::Allocator& backing_allocator,
32 std::string_view name,
33 std::source_location = std::source_location::current()
34 ) noexcept;
35
36 inline void reset() noexcept;
37
38 protected:
39 inline auto do_allocate(ice::AllocRequest request) noexcept -> ice::AllocResult override;
40 inline void do_deallocate(void* memory) noexcept override;
41
42 private:
43 ice::Allocator* _backing_alloc;
44
45 ice::usize _static_usage;
46 char _static_buffer[Constant_InternalCapacity.value];
47 };
48
49 template<ice::usize Capacity>
50 inline StackAllocator<Capacity>::StackAllocator(
51 std::source_location src_loc
52 ) noexcept
53 : ice::Allocator{ src_loc }
54 , _backing_alloc{ nullptr }
55 , _static_usage{ 0_B }
56 {
57 }
58
59 template<ice::usize Capacity>
61 ice::Allocator& backing_allocator,
62 std::source_location src_loc
63 ) noexcept
64 : ice::Allocator{ src_loc, backing_allocator }
65 , _backing_alloc{ &backing_allocator }
66 , _static_usage{ 0_B }
67 {
68 }
69
70 template<ice::usize Capacity>
72 ice::Allocator& backing_allocator,
73 std::string_view name,
74 std::source_location src_loc
75 ) noexcept
76 : ice::Allocator{ src_loc, backing_allocator, name }
77 , _backing_alloc{ &backing_allocator }
78 , _static_usage{ 0_B }
79 {
80 }
81
82 template<ice::usize Capacity>
83 inline void StackAllocator<Capacity>::reset() noexcept
84 {
85 _static_usage = 0_B;
86 }
87
88 template<ice::usize Capacity>
90 {
91 ice::AllocResult result{
92 .memory = nullptr,
93 .size = 0_B,
94 .alignment = ice::ualign::invalid
95 };
96
97 ice::AlignResult<ice::usize> const aligned_usage = ice::align_to(_static_usage, request.alignment);
98 if ((aligned_usage.value + request.size) <= Capacity)
99 {
100 _static_usage = aligned_usage.value + request.size;
101
102 result.memory = ice::ptr_add(_static_buffer, aligned_usage.value);
103 result.size = request.size;
104 result.alignment = request.alignment;
105 }
106 else if (_backing_alloc != nullptr)
107 {
108 result = _backing_alloc->allocate(request);
109 }
110 return result;
111 }
112
113 template<ice::usize Capacity>
114 inline void StackAllocator<Capacity>::do_deallocate(void* pointer) noexcept
115 {
116 if ((_static_buffer + 0) <= pointer && ice::ptr_add(_static_buffer, _static_usage) > pointer)
117 {
118 ICE_ASSERT_CORE(true); // nothing to do here
119 }
120 else if (_backing_alloc != nullptr)
121 {
122 _backing_alloc->deallocate(pointer);
123 }
124 }
125
126} // namespace ice
#define ICE_ASSERT_CORE(expression)
Definition assert_core.hxx:43
SPDX-License-Identifier: MIT.
Definition array.hxx:12
StackAllocator< 1024_B > StackAllocator_1024
Definition mem_allocator_stack.hxx:13
auto ptr_add(void *pointer, ice::usize offset) noexcept -> void *
Definition mem_arithmetic.hxx:34
constexpr auto align_to(T value, ice::ualign alignment) noexcept -> ice::AlignResult< T >
Definition mem_align.hxx:41
StackAllocator< 2048_B > StackAllocator_2048
Definition mem_allocator_stack.hxx:14
ice::AllocatorBase< ice::build::is_debug||ice::build::is_develop > Allocator
Definition mem_types.hxx:25
@ invalid
Definition mem_size_types.hxx:40
Definition mem_align.hxx:13
T value
Definition mem_align.hxx:14
Definition mem.hxx:16
Definition mem.hxx:44
void * memory
Definition mem.hxx:45
ice::usize size
Definition mem.hxx:46
ice::ualign alignment
Definition mem.hxx:47
Definition mem_allocator_stack.hxx:18
StackAllocator(std::source_location=std::source_location::current()) noexcept
Definition mem_allocator_stack.hxx:50
static constexpr ice::usize Constant_InternalCapacity
Definition mem_allocator_stack.hxx:19
void do_deallocate(void *memory) noexcept override
auto do_allocate(ice::AllocRequest request) noexcept -> ice::AllocResult override
Represents a unsigned size value on the given platform.
Definition mem_size_types.hxx:26