IceShard 1
A personal game engine project, with development focused on 2D/2.5D games.
Loading...
Searching...
No Matches
mem_buffer.hxx
Go to the documentation of this file.
1
3
4#pragma once
5#include <ice/mem_memory.hxx>
7#include <ice/mem_data.hxx>
8
9namespace ice
10{
11
12 struct Buffer
13 {
16 ice::meminfo used{ .size = 0_B, .alignment = ice::ualign::b_default };
17 };
18
19 namespace buffer
20 {
21
22 inline void set_capacity(ice::Buffer& buffer, ice::usize new_capacity) noexcept;
23 inline void grow(ice::Buffer& buffer, ice::usize min_capacity) noexcept;
25
26 inline auto size(ice::Buffer const& buffer) noexcept -> ice::usize;
27 inline auto capacity(ice::Buffer const& buffer) noexcept -> ice::usize;
28 inline auto required_capacity(ice::Buffer const& buffer, ice::meminfo meminfo) noexcept -> ice::usize;
29 inline auto space(ice::Buffer const& buffer) noexcept -> ice::usize;
30 inline bool empty(ice::Buffer const& buffer) noexcept;
31 inline bool has_space(ice::Buffer const& buffer, ice::meminfo meminfo) noexcept;
32 inline auto data_view(ice::Buffer const& buffer) noexcept -> ice::Data;
33
34 } // namespace buffer
35
36 namespace buffer
37 {
38
39 inline void set_capacity(ice::Buffer& buffer, ice::usize new_capacity) noexcept
40 {
41 if (buffer.memory.size == new_capacity)
42 {
43 return;
44 }
45
46 ice::Memory new_data{ };
47 if (new_capacity > 0_B)
48 {
49 new_data = buffer.alloc->allocate({ new_capacity, ice::ualign::b_default });
50 ice::memcpy(new_data, ice::data_view(buffer.memory));
51 }
52
53 buffer.alloc->deallocate(buffer.memory);
54 buffer.memory = new_data;
55 buffer.used.size = ice::min(new_capacity, buffer.used.size);
56 }
57
58 inline void grow(ice::Buffer& buffer, ice::usize min_capacity) noexcept
59 {
60 ice::usize const new_size = buffer.memory.size * 2 + 256_B;
61 set_capacity(buffer, ice::max(min_capacity, new_size));
62 }
63
65 {
67 if (req_capacity >= ice::buffer::capacity(buffer))
68 {
69 ice::buffer::grow(buffer, req_capacity);
70 }
71
72 ice::usize const offset = buffer.used += meminfo;
73 return Memory{
74 .location = ice::ptr_add(buffer.memory.location, offset),
75 .size = meminfo.size,
76 .alignment = meminfo.alignment
77 };
78 }
79
80
81 inline auto size(ice::Buffer const& buffer) noexcept -> ice::usize
82 {
83 return buffer.used.size;
84 }
85
86 inline auto capacity(ice::Buffer const& buffer) noexcept -> ice::usize
87 {
88 return buffer.memory.size;
89 }
90
92 {
93 ice::meminfo tempinfo = buffer.used;
94 tempinfo += meminfo;
95 return tempinfo.size;
96 }
97
98 inline auto space(ice::Buffer const& buffer) noexcept -> ice::usize
99 {
100 return { buffer.memory.size.value - buffer.used.size.value };
101 }
102
103 inline bool empty(ice::Buffer const& buffer) noexcept
104 {
105 return buffer.used.size == 0_B;
106 }
107
112
113 inline auto memory_pointer(ice::Buffer const& buffer) noexcept
114 {
115 return buffer.memory.location;
116 }
117
118 inline auto data_view(ice::Buffer const& buffer) noexcept -> ice::Data
119 {
120 return ice::Data{
121 .location = buffer.memory.location,
122 .size = ice::buffer::size(buffer),
123 .alignment = buffer.memory.alignment,
124 };
125 }
126
127 } // namespace buffer
128
129} // namespace ice
Definition mem_buffer.hxx:20
auto size(ice::Buffer const &buffer) noexcept -> ice::usize
Definition mem_buffer.hxx:81
auto append_reserve(ice::Buffer &buffer, ice::meminfo meminfo) noexcept -> ice::Memory
Definition mem_buffer.hxx:64
void set_capacity(ice::Buffer &buffer, ice::usize new_capacity) noexcept
Definition mem_buffer.hxx:39
auto space(ice::Buffer const &buffer) noexcept -> ice::usize
Definition mem_buffer.hxx:98
auto memory_pointer(ice::Buffer const &buffer) noexcept
Definition mem_buffer.hxx:113
bool empty(ice::Buffer const &buffer) noexcept
Definition mem_buffer.hxx:103
bool has_space(ice::Buffer const &buffer, ice::meminfo meminfo) noexcept
Definition mem_buffer.hxx:108
auto data_view(ice::Buffer const &buffer) noexcept -> ice::Data
Definition mem_buffer.hxx:118
void grow(ice::Buffer &buffer, ice::usize min_capacity) noexcept
Definition mem_buffer.hxx:58
auto capacity(ice::Buffer const &buffer) noexcept -> ice::usize
Definition mem_buffer.hxx:86
auto required_capacity(ice::Buffer const &buffer, ice::meminfo meminfo) noexcept -> ice::usize
Definition mem_buffer.hxx:91
constexpr auto max(arr_t< Size, T > left, arr_t< Size, U > right) noexcept -> arr_t< Size, T >
Definition array_operations.hxx:49
constexpr auto min(arr_t< Size, T > left, arr_t< Size, U > right) noexcept -> arr_t< Size, T >
Definition array_operations.hxx:60
SPDX-License-Identifier: MIT.
Definition array.hxx:12
auto data_view(ice::Array< Type, Logic > const &arr) noexcept -> ice::Data=delete
auto ptr_add(void *pointer, ice::usize offset) noexcept -> void *
Definition mem_arithmetic.hxx:34
ice::AllocatorBase< ice::build::is_debug||ice::build::is_develop > Allocator
Definition mem_types.hxx:25
@ b_default
Definition mem_size_types.hxx:55
auto memcpy(void *dest, void const *source, ice::usize size) noexcept -> void *
Definition mem_buffer.hxx:13
ice::meminfo used
Definition mem_buffer.hxx:16
ice::Memory memory
Definition mem_buffer.hxx:15
ice::Allocator * alloc
Definition mem_buffer.hxx:14
Definition mem_data.hxx:17
Definition mem_memory.hxx:13
Definition mem_size_types.hxx:59
ice::ualign alignment
Definition mem_size_types.hxx:61
ice::usize size
Definition mem_size_types.hxx:60
Represents a unsigned size value on the given platform.
Definition mem_size_types.hxx:26