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
14 struct AllocResult;
16 {
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
29 {
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
44 {
45 void* memory;
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
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
65 : size{ size }
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 {
98 }
99 else
100 {
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 {
123 _chunk_count = 0;
124 }
125
126} // namespace ice
#define ICE_ASSERT_CORE(expression)
Definition assert_core.hxx:43
Definition span.hxx:129
SPDX-License-Identifier: MIT.
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
Definition base.hxx:43
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
Definition mem.hxx:17
ice::ualign alignment
Definition mem.hxx:18
Definition mem.hxx:44
void * memory
Definition mem.hxx:45
ice::usize size
Definition mem.hxx:46
ice::ualign alignment
Definition mem.hxx:47
constexpr void finalize(ice::AllocResult result) const noexcept
Definition mem.hxx:108
constexpr auto include(T *&ptrref, ice::u64 count) noexcept
Definition mem.hxx:91
ice::u64 _chunk_count
Definition mem.hxx:31
ice::usize _chunk_offsets[14]
Definition mem.hxx:32
void ** _chunk_pointers[15]
Definition mem.hxx:33
constexpr ChunkedAllocRequest() noexcept=default
constexpr void reset() noexcept
Definition mem.hxx:120
ice::meminfo _request_meminfo
Definition mem.hxx:30
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