-
-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathdata.hpp
More file actions
322 lines (276 loc) · 6.5 KB
/
Copy pathdata.hpp
File metadata and controls
322 lines (276 loc) · 6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/**
* @file liblava/core/data.hpp
* @brief Data wrapper
* @authors Lava Block OÜ and contributors
* @copyright Copyright (c) 2018-present, MIT License
*/
#pragma once
#include "liblava/core/types.hpp"
#include <string.h>
namespace lava {
/**
* @brief Align value up
* @param value Value to align
* @param align Target alignment
* @return auto Aligned value
*/
inline auto align_up(auto value,
auto align) {
return (value + align - 1) / align * align;
}
/**
* @brief Align a size
* @param size Site to align
* @param min Minimal alignment
* @return size_t Aligned size
*/
inline size_t align(size_t size,
size_t min = 0) {
if (min == 0)
return align_up(size, sizeof(void*));
return align_up((size + min - 1) & ~(min - 1), sizeof(void*));
}
/**
* @brief Get alignment of type
* @tparam T Type to align
* @param min Minimal alignment
* @return size_t Aligned size
*/
template <typename T>
inline size_t align(size_t min = 0) {
return align(sizeof(T), min);
}
/**
* @brief Allocate data
* @param size Size of data
* @param alignment Target alignment
* @return void* Allocated data
*/
inline void* alloc_data(size_t size,
size_t alignment = sizeof(c8)) {
#if _WIN32
return _aligned_malloc(size, alignment);
#else
if (size % alignment == 0) {
return aligned_alloc(alignment, size);
} else {
return aligned_alloc(alignment, ((size / alignment) + 1) * alignment);
}
#endif
}
/**
* @brief Free data
* @param data Data to free
*/
inline void free_data(void* data) {
#if _WIN32
_aligned_free(data);
#else
free(data);
#endif
}
/**
* @brief Reallocate data
* @param data Data to reallocate
* @param size Size of data
* @param alignment Target alignment
* @return void* Reallocated data
*/
inline void* realloc_data(void* data,
size_t size,
size_t alignment = sizeof(c8)) {
#if _WIN32
return _aligned_realloc(data, size, alignment);
#else
return realloc(data, align(size, alignment));
#endif
}
/**
* @brief Data wrapper
*/
struct data {
/// Reference to data wrapper
using ref = data const&;
/// Data pointer
using ptr = char*;
/// Const data pointer
using c_ptr = char const*;
/**
* @brief Cast to data pointer
* @param value Value to cast
* @return ptr Data pointer
*/
static inline ptr as_ptr(auto* value) {
return (ptr)value;
}
/**
* @brief Cast to const data pointer
* @param value Value to cast
* @return c_ptr Const data pointer
*/
static inline c_ptr as_c_ptr(auto* value) {
return (c_ptr)value;
}
/**
* @brief Data modes
*/
enum class mode : index {
alloc = 0,
no_alloc
};
/**
* @brief Construct a new data
*/
data() = default;
/**
* @brief Construct a new data
* @param addr Data pointer
* @param size Size of data
*/
data(auto* addr, size_t size)
: addr(as_ptr(addr)), size(size) {}
/**
* @brief Set and allocate data by length
* @param length Length of data
* @param mode Data mode
* @return Allocate was successful or failed (mode: alloc)
*/
bool set(size_t length,
mode mode = mode::alloc) {
size = length;
alignment = align<data::ptr>();
if (mode == mode::alloc)
return allocate();
return true;
}
/**
* @brief Allocate data
* @return Allocate was successful or failed
*/
bool allocate() {
addr = as_ptr(alloc_data(size, alignment));
return addr != nullptr;
}
/**
* @brief Deallocate data
*/
void deallocate() {
if (!addr)
return;
free_data(addr);
addr = nullptr;
}
/**
* @brief Pointer to end of data
* @return ptr Pointer to end
*/
ptr end() const {
return addr + size;
}
/// Pointer address
ptr addr = nullptr;
/// Size of data
size_t size = 0;
/// Data alignment
size_t alignment = 0;
};
/**
* @brief Data provider
*/
struct data_provider {
/**
* @brief Allocation function
*/
using alloc_func = std::function<data::ptr(size_t, size_t)>;
/// Called on allocation
alloc_func on_alloc;
/**
* @brief Free function
*/
using free_func = std::function<void()>;
/// Called on free
free_func on_free;
/**
* @brief Reallocation function
*/
using realloc_func = std::function<data::ptr(data::ptr, size_t, size_t)>;
/// Called on reallocation
realloc_func on_realloc;
};
/**
* @brief Const data wrapper
*/
struct c_data {
/// Reference to const data wrapper
using ref = c_data const&;
/**
* @brief Construct a new const data
*/
c_data() = default;
/**
* @brief Construct a new const data
* @param addr Pointer to data
* @param length Length of data
*/
c_data(void const* addr,
size_t length)
: addr(data::as_ptr(addr)), size(length) {}
/**
* @brief Construct a new const data from other data
* @param data Source data
*/
c_data(data::ref data)
: c_data(data.addr, data.size) {}
/// Const data pointer
data::c_ptr addr = nullptr;
/// Size of data
size_t size = 0;
};
/**
* @brief Unique data wrapper
*/
struct u_data : data {
/// Reference to unique data wrapper
using ref = u_data const&;
/**
* @brief Construct a new unique data
* @param length Length of data
* @param mode Data mode
*/
u_data(size_t length = 0,
data::mode mode = data::mode::alloc) {
if (length)
set(length, mode);
}
/**
* @brief Construct a new unique data from another data
* @param data Source data
*/
explicit u_data(data::ref data) {
addr = data.addr;
size = data.size;
alignment = data.alignment;
}
/**
* @brief Destroy the unique data
*/
~u_data() {
deallocate();
}
};
/**
* @brief Get next power of two
* @param x Source value
* @return size_t Next power of two
*/
inline size_t next_pow_2(size_t x) {
x--;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
x++;
return x;
}
} // namespace lava