IceShard
1
A personal game engine project, with development focused on 2D/2.5D games.
Toggle main menu visibility
Loading...
Searching...
No Matches
memsys
public
ice
mem_allocator_stack.hxx
Go to the documentation of this file.
1
3
4
#pragma once
5
#include <
ice/mem_allocator.hxx
>
6
7
namespace
ice
8
{
9
template
<ice::usize Capacity>
10
struct
StackAllocator
;
11
12
//using StackAllocator_256 = StackAllocator<256_B>;
13
using
StackAllocator_1024
=
StackAllocator<1024_B>
;
14
using
StackAllocator_2048
=
StackAllocator<2048_B>
;
15
16
template
<ice::usize Capacity>
17
struct
StackAllocator
final :
ice::Allocator
18
{
19
static
constexpr
ice::usize
Constant_InternalCapacity
= Capacity;
20
21
inline
StackAllocator
(
22
std::source_location = std::source_location::current()
23
) noexcept;
24
25
inline
StackAllocator
(
26
ice
::
Allocator
& backing_allocator,
27
std::source_location = std::source_location::current()
28
) noexcept;
29
30
inline
StackAllocator
(
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>
60
inline
StackAllocator<Capacity>::StackAllocator
(
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>
71
inline
StackAllocator<Capacity>::StackAllocator
(
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>
89
inline
auto
StackAllocator<Capacity>::do_allocate
(
ice::AllocRequest
request)
noexcept
->
ice::AllocResult
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
ICE_ASSERT_CORE
#define ICE_ASSERT_CORE(expression)
Definition
assert_core.hxx:43
mem_allocator.hxx
ice
SPDX-License-Identifier: MIT.
Definition
array.hxx:12
ice::StackAllocator_1024
StackAllocator< 1024_B > StackAllocator_1024
Definition
mem_allocator_stack.hxx:13
ice::ptr_add
auto ptr_add(void *pointer, ice::usize offset) noexcept -> void *
Definition
mem_arithmetic.hxx:34
ice::align_to
constexpr auto align_to(T value, ice::ualign alignment) noexcept -> ice::AlignResult< T >
Definition
mem_align.hxx:41
ice::StackAllocator_2048
StackAllocator< 2048_B > StackAllocator_2048
Definition
mem_allocator_stack.hxx:14
ice::Allocator
ice::AllocatorBase< ice::build::is_debug||ice::build::is_develop > Allocator
Definition
mem_types.hxx:25
ice::ualign::invalid
@ invalid
Definition
mem_size_types.hxx:40
ice::AlignResult
Definition
mem_align.hxx:13
ice::AlignResult::value
T value
Definition
mem_align.hxx:14
ice::AllocRequest
Definition
mem.hxx:16
ice::AllocResult
Definition
mem.hxx:44
ice::AllocResult::memory
void * memory
Definition
mem.hxx:45
ice::AllocResult::size
ice::usize size
Definition
mem.hxx:46
ice::AllocResult::alignment
ice::ualign alignment
Definition
mem.hxx:47
ice::StackAllocator
Definition
mem_allocator_stack.hxx:18
ice::StackAllocator::StackAllocator
StackAllocator(std::source_location=std::source_location::current()) noexcept
Definition
mem_allocator_stack.hxx:50
ice::StackAllocator< 1024_B >::reset
void reset() noexcept
ice::StackAllocator< 1024_B >::Constant_InternalCapacity
static constexpr ice::usize Constant_InternalCapacity
Definition
mem_allocator_stack.hxx:19
ice::StackAllocator< 1024_B >::do_deallocate
void do_deallocate(void *memory) noexcept override
ice::StackAllocator< 1024_B >::do_allocate
auto do_allocate(ice::AllocRequest request) noexcept -> ice::AllocResult override
ice::usize
Represents a unsigned size value on the given platform.
Definition
mem_size_types.hxx:26
Generated by
1.18.0