IceShard 1
A personal game engine project, with development focused on 2D/2.5D games.
Loading...
Searching...
No Matches
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
11namespace ice
12{
13
16 {
18 void* memory;
19
23
27
29 constexpr operator ice::Memory() const noexcept;
30 };
31
35 {
38
42
43 constexpr AllocRequest() noexcept = default;
44
46 constexpr AllocRequest(ice::usize size, ice::ualign alignment = ice::ualign::b_default) noexcept;
47
49 constexpr AllocRequest(ice::meminfo memory_info) noexcept;
50
52 template<typename T> requires (std::is_pointer_v<T> == false)
53 constexpr AllocRequest(ice::AlignResult<T> align_result) noexcept;
54 };
55
57 {
61 void** _chunk_pointers[15];
62
63 constexpr explicit ChunkedAllocRequest() noexcept = default;
64
65 template<typename T>
66 constexpr auto include(T*& ptrref, ice::u64 count) noexcept;
67 constexpr void finalize(ice::AllocResult result) const noexcept;
68 constexpr void reset() noexcept;
69 };
70
71 auto alloc(ice::usize size) noexcept -> ice::AllocResult;
72 void release(void* pointer) noexcept;
73
75 void release_aligned(void* pointer) noexcept;
76
77 auto memcpy(void* dest, void const* source, ice::usize size) noexcept -> void*;
78 auto memcpy(void* dest, ice::Data source) noexcept -> void*;
79 auto memcpy(ice::Memory memory, ice::Data data) noexcept -> ice::Memory;
80
81 auto memset(ice::Memory memory, ice::u8 value) noexcept -> ice::Memory;
82
84 : size{ size }
86 {
87 }
88
89 constexpr AllocRequest::AllocRequest(ice::meminfo memory_info) noexcept
90 : AllocRequest{ memory_info.size, memory_info.alignment }
91 {
92 }
93
94 template<typename T> requires (std::is_pointer_v<T> == false)
95 constexpr AllocRequest::AllocRequest(ice::AlignResult<T> align_result) noexcept
96 : AllocRequest{ align_result.value, align_result.alignment }
97 {
98 }
99
100 constexpr AllocResult::operator ice::Memory() const noexcept
101 {
102 return ice::Memory{
103 .location = memory,
104 .size = size,
105 .alignment = alignment
106 };
107 }
108
109 template<typename T>
110 inline constexpr auto ChunkedAllocRequest::include(T*& ptrref, ice::u64 count) noexcept
111 {
112 ICE_ASSERT_CORE(_chunk_count < 14); // You sure you need that much? (Refactor if more are required)
113
114 if (_chunk_count == 0)
115 {
117 }
118 else
119 {
121 _chunk_offsets[_chunk_count - 1] = offset;
122 }
123 _chunk_pointers[_chunk_count] = reinterpret_cast<void**>(ice::addressof(ptrref));
124 _chunk_count += 1;
125 }
126
127 inline constexpr void ChunkedAllocRequest::finalize(ice::AllocResult result) const noexcept
128 {
129 ICE_ASSERT_CORE(result.size >= _request_meminfo.size);
130 ICE_ASSERT_CORE(result.alignment >= _request_meminfo.alignment);
131 *_chunk_pointers[0] = result.memory; // Assign the allocation result directly to the first pointer.
132 // Assign the other pointers according to the stored offsets
133 for (ice::u64 index = 1; index < _chunk_count; ++index)
134 {
135 *_chunk_pointers[index] = ice::ptr_add(result.memory, _chunk_offsets[index - 1]);
136 }
137 }
138
139 inline constexpr void ChunkedAllocRequest::reset() noexcept
140 {
142 _chunk_count = 0;
143 }
144
145} // namespace ice
#define ICE_ASSERT_CORE(expression)
Definition assert_core.hxx:43
Definition span.hxx:129
Root namespace for all APIs part of the IceShard engine project.
Definition array.hxx:12
auto memset(ice::Memory memory, ice::u8 value) noexcept -> ice::Memory
auto alloc(ice::usize size) noexcept -> ice::AllocResult
std::uint64_t u64
Definition types.hxx:27
void release(void *pointer) noexcept
auto ptr_add(void *pointer, ice::usize offset) noexcept -> void *
Definition mem_arithmetic.hxx:34
constexpr auto count(T const (&)[Size]) noexcept -> ice::u32
Utility function to access number of elements of a typed C array.
Definition base.hxx:52
void release_aligned(void *pointer) noexcept
auto alloc_aligned(ice::usize size, ice::ualign alignment) noexcept -> ice::AllocResult
constexpr ice::meminfo meminfo_of
Definition mem_info.hxx:18
ualign
Definition mem_size_types.hxx:39
@ b_default
Definition mem_size_types.hxx:55
std::uint8_t u8
Definition types.hxx:24
auto memcpy(void *dest, void const *source, ice::usize size) noexcept -> void *
Definition mem_align.hxx:13
ice::ualign alignment
Definition mem_align.hxx:16
T value
Definition mem_align.hxx:14
constexpr AllocRequest() noexcept=default
ice::usize size
Number of bytes to allocate.
Definition mem.hxx:37
ice::ualign alignment
Alignment requested for this allocation.
Definition mem.hxx:41
The result of an allocation from an ice::Allocator object.
Definition mem.hxx:16
void * memory
A pointer holding the address of the allocated memory block.
Definition mem.hxx:18
ice::usize size
The final size of the allocated block.
Definition mem.hxx:22
ice::ualign alignment
The actual alignment of this allocation.
Definition mem.hxx:26
constexpr void finalize(ice::AllocResult result) const noexcept
Definition mem.hxx:127
constexpr auto include(T *&ptrref, ice::u64 count) noexcept
Definition mem.hxx:110
ice::u64 _chunk_count
Definition mem.hxx:59
ice::usize _chunk_offsets[14]
Definition mem.hxx:60
void ** _chunk_pointers[15]
Definition mem.hxx:61
constexpr ChunkedAllocRequest() noexcept=default
constexpr void reset() noexcept
Definition mem.hxx:139
ice::meminfo _request_meminfo
Definition mem.hxx:58
Definition mem_data.hxx:17
Definition mem_memory.hxx:13
Definition mem_size_types.hxx:59
Represents a unsigned size value on the given platform.
Definition mem_size_types.hxx:26