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.hxx
Go to the documentation of this file.
1
3
4
#pragma once
5
#include <
ice/mem_info.hxx
>
6
#include <
ice/mem_utils.hxx
>
7
#include <
ice/mem_memory.hxx
>
8
#include <malloc.h>
9
#include <new>
10
11
namespace
ice
12
{
13
14
struct
AllocResult
;
15
struct
AllocRequest
16
{
17
ice::usize
size
= 0_B;
18
ice::ualign
alignment
=
ice::ualign::b_default
;
19
20
constexpr
AllocRequest
() noexcept = default;
21
constexpr
AllocRequest
(
ice
::
usize
size
,
ice
::
ualign
alignment
=
ice
::
ualign
::
b_default
) noexcept;
22
constexpr
AllocRequest
(
ice
::
meminfo
memory_info) noexcept;
23
24
template<typename T> requires (std::is_pointer_v<T> == false)
25
constexpr
AllocRequest
(
ice
::
AlignResult
<T> align_result) noexcept;
26
};
27
28
struct
ChunkedAllocRequest
29
{
30
ice::meminfo
_request_meminfo
{ };
31
ice::u64
_chunk_count
= 0;
32
ice::usize
_chunk_offsets
[14];
33
void
**
_chunk_pointers
[15];
34
35
constexpr
explicit
ChunkedAllocRequest
() noexcept = default;
36
37
template<typename T>
38
constexpr auto
include
(T*& ptrref,
ice
::
u64
count
) noexcept;
39
constexpr
void
finalize
(
ice
::
AllocResult
result) const noexcept;
40
constexpr
void
reset
() noexcept;
41
};
42
43
struct
AllocResult
44
{
45
void
*
memory
;
46
ice::usize
size
;
47
ice::ualign
alignment
;
48
49
constexpr
operator
ice::Memory
() const noexcept;
50
};
51
52
auto
alloc
(
ice
::
usize
size
) noexcept ->
ice
::
AllocResult
;
53
void
release
(
void
* pointer) noexcept;
54
55
auto
alloc_aligned
(
ice
::
usize
size
,
ice
::
ualign
alignment
) noexcept ->
ice
::
AllocResult
;
56
void
release_aligned
(
void
* pointer) noexcept;
57
58
auto
memcpy
(
void
* dest,
void
const* source,
ice
::
usize
size
) noexcept ->
void
*;
59
auto
memcpy
(
void
* dest,
ice
::
Data
source) noexcept ->
void
*;
60
auto
memcpy
(
ice
::
Memory
memory,
ice
::
Data
data
) noexcept ->
ice
::
Memory
;
61
62
auto
memset
(
ice
::
Memory
memory,
ice
::
u8
value) noexcept ->
ice
::
Memory
;
63
64
constexpr
AllocRequest
::
AllocRequest
(
ice
::
usize
size
,
ice
::
ualign
alignment
) noexcept
65
:
size
{
size
}
66
,
alignment
{
alignment
}
67
{
68
}
69
70
constexpr
AllocRequest::AllocRequest
(
ice::meminfo
memory_info) noexcept
71
:
AllocRequest
{ memory_info.size, memory_info.alignment }
72
{
73
}
74
75
template
<
typename
T>
requires
(std::is_pointer_v<T> ==
false
)
76
constexpr
AllocRequest::AllocRequest
(
ice::AlignResult<T>
align_result) noexcept
77
:
AllocRequest
{ align_result.
value
, align_result.
alignment
}
78
{
79
}
80
81
constexpr
AllocResult::operator
ice::Memory
() const noexcept
82
{
83
return
ice::Memory
{
84
.location =
memory
,
85
.size =
size
,
86
.alignment =
alignment
87
};
88
}
89
90
template
<
typename
T>
91
inline
constexpr
auto
ChunkedAllocRequest::include
(T*& ptrref,
ice::u64
count
)
noexcept
92
{
93
ICE_ASSERT_CORE
(
_chunk_count
< 14);
// You sure you need that much? (Refactor if more are required)
94
95
if
(
_chunk_count
== 0)
96
{
97
_request_meminfo
=
ice::meminfo_of<T>
*
count
;
98
}
99
else
100
{
101
ice::usize
const
offset =
_request_meminfo
+=
ice::meminfo_of<T>
*
count
;
102
_chunk_offsets
[
_chunk_count
- 1] = offset;
103
}
104
_chunk_pointers
[
_chunk_count
] =
reinterpret_cast<
void
**
>
(ice::addressof(ptrref));
105
_chunk_count
+= 1;
106
}
107
108
inline
constexpr
void
ChunkedAllocRequest::finalize
(
ice::AllocResult
result)
const
noexcept
109
{
110
ICE_ASSERT_CORE
(result.size >=
_request_meminfo
.size);
111
ICE_ASSERT_CORE
(result.alignment >=
_request_meminfo
.alignment);
112
*
_chunk_pointers
[0] = result.memory;
// Assign the allocation result directly to the first pointer.
113
// Assign the other pointers according to the stored offsets
114
for
(
ice::u64
index = 1; index <
_chunk_count
; ++index)
115
{
116
*
_chunk_pointers
[index] =
ice::ptr_add
(result.memory,
_chunk_offsets
[index - 1]);
117
}
118
}
119
120
inline
constexpr
void
ChunkedAllocRequest::reset
() noexcept
121
{
122
_request_meminfo
=
ice::meminfo
{};
123
_chunk_count
= 0;
124
}
125
126
}
// namespace ice
ICE_ASSERT_CORE
#define ICE_ASSERT_CORE(expression)
Definition
assert_core.hxx:43
mem_info.hxx
mem_memory.hxx
mem_utils.hxx
ice::data
Definition
span.hxx:129
ice
SPDX-License-Identifier: MIT.
Definition
array.hxx:12
ice::memset
auto memset(ice::Memory memory, ice::u8 value) noexcept -> ice::Memory
ice::alloc
auto alloc(ice::usize size) noexcept -> ice::AllocResult
ice::u64
std::uint64_t u64
Definition
types.hxx:27
ice::release
void release(void *pointer) noexcept
ice::ptr_add
auto ptr_add(void *pointer, ice::usize offset) noexcept -> void *
Definition
mem_arithmetic.hxx:34
ice::count
constexpr auto count(T const (&)[Size]) noexcept -> ice::u32
Definition
base.hxx:43
ice::release_aligned
void release_aligned(void *pointer) noexcept
ice::alloc_aligned
auto alloc_aligned(ice::usize size, ice::ualign alignment) noexcept -> ice::AllocResult
ice::meminfo_of
constexpr ice::meminfo meminfo_of
Definition
mem_info.hxx:18
ice::ualign
ualign
Definition
mem_size_types.hxx:39
ice::ualign::b_default
@ b_default
Definition
mem_size_types.hxx:55
ice::u8
std::uint8_t u8
Definition
types.hxx:24
ice::memcpy
auto memcpy(void *dest, void const *source, ice::usize size) noexcept -> void *
ice::AlignResult
Definition
mem_align.hxx:13
ice::AlignResult::alignment
ice::ualign alignment
Definition
mem_align.hxx:16
ice::AlignResult::value
T value
Definition
mem_align.hxx:14
ice::AllocRequest::AllocRequest
constexpr AllocRequest() noexcept=default
ice::AllocRequest::size
ice::usize size
Definition
mem.hxx:17
ice::AllocRequest::alignment
ice::ualign alignment
Definition
mem.hxx:18
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::ChunkedAllocRequest::finalize
constexpr void finalize(ice::AllocResult result) const noexcept
Definition
mem.hxx:108
ice::ChunkedAllocRequest::include
constexpr auto include(T *&ptrref, ice::u64 count) noexcept
Definition
mem.hxx:91
ice::ChunkedAllocRequest::_chunk_count
ice::u64 _chunk_count
Definition
mem.hxx:31
ice::ChunkedAllocRequest::_chunk_offsets
ice::usize _chunk_offsets[14]
Definition
mem.hxx:32
ice::ChunkedAllocRequest::_chunk_pointers
void ** _chunk_pointers[15]
Definition
mem.hxx:33
ice::ChunkedAllocRequest::ChunkedAllocRequest
constexpr ChunkedAllocRequest() noexcept=default
ice::ChunkedAllocRequest::reset
constexpr void reset() noexcept
Definition
mem.hxx:120
ice::ChunkedAllocRequest::_request_meminfo
ice::meminfo _request_meminfo
Definition
mem.hxx:30
ice::Data
Definition
mem_data.hxx:17
ice::Memory
Definition
mem_memory.hxx:13
ice::meminfo
Definition
mem_size_types.hxx:59
ice::usize
Represents a unsigned size value on the given platform.
Definition
mem_size_types.hxx:26
Generated by
1.18.0