Repository navigation
Expand file tree
/
Copy pathutils.h
More file actions
266 lines (235 loc) · 7.51 KB
/
Copy pathutils.h
File metadata and controls
266 lines (235 loc) · 7.51 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
// SPDX-License-Identifier: MIT
// Copyright (c) 2021 Michael Bäck <mhcoma@gmail.com>
#ifndef __CCTL_UTILS_H__
#define __CCTL_UTILS_H__
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "cctl.h"
// Memory Utilities
/// ```c
/// const void* cctl_get_memory_address(T data)
/// ```
/// Returns the memory address representing `data`.
///
/// For string pointer types (`cstring`, `u16cstring`, `u32cstring`), it returns the pointer value itself.
/// For value types, it returns the address of the variable.
#define cctl_get_memory_address(data) \
_Generic((data), \
cstring: (const void*) (uintptr_t)(data), \
u16cstring: (const void*) (uintptr_t)(data), \
u32cstring: (const void*) (uintptr_t)(data), \
default: (const void*) &(data) \
)
/// ```c
/// size_t cctl_get_byte_size(T data)
/// ```
/// Returns the byte size of `data`.
///
/// For null-terminated strings, calculates length in bytes including the null terminator.
/// For value types, returns `sizeof(data)`.
#define cctl_get_byte_size(data) \
_Generic((data), \
cstring: (data) ? strlen((const char*) (uintptr_t) (data)) + 1 : 0, \
u16cstring: cctl_calculate_byte_size((const void*) (uintptr_t) (data), sizeof(char16_t)), \
u32cstring: cctl_calculate_byte_size((const void*) (uintptr_t) (data), sizeof(char32_t)), \
default: sizeof(data) \
)
/// Calculates byte size for wide string buffers including the null terminator.
static inline size_t cctl_calculate_byte_size(const void* p_data, size_t char_size) {
if (!p_data) return 0;
if (char_size == sizeof(char16_t)) {
const char16_t* p_data_cursor = (const char16_t*) p_data;
size_t length = 0;
while (p_data_cursor[length] != 0) {
length++;
}
return (length + 1) * sizeof(char16_t);
}
if (char_size == sizeof(char32_t)) {
const char32_t* p_data_cursor = (const char32_t*) p_data;
size_t length = 0;
while (p_data_cursor[length] != 0) {
length++;
}
return (length + 1) * sizeof(char32_t);
}
return 0;
}
/// Compares two memory blocks byte-by-byte.
static inline int cctl_compare_memory(const void* p_addr_a, size_t a_size, const void* p_addr_b, size_t b_size) {
if (a_size != b_size) {
return a_size < b_size ? -1 : 1;
}
if (a_size == 0) {
return 0;
}
return memcmp(p_addr_a, p_addr_b, a_size);
}
/// Allocates a new buffer and copies memory from `p_src_addr`.
static inline void* cctl_copy_memory(const void* p_src_addr, size_t size) {
if (!p_src_addr || size == 0) {
return NULL;
}
void* p_dest_addr = malloc(size);
if (!p_dest_addr) {
return NULL;
}
memcpy(p_dest_addr, p_src_addr, size);
return p_dest_addr;
}
// Compare Functions
#define cctl_define_compare_func(T) \
static inline int cctl_join(cctl_compare, T)(T a, T b) { \
if (a < b) return -1; \
if (a > b) return 1; \
return 0; \
}
#define cctl_define_memory_compare_func(T) \
static inline int cctl_join(cctl_compare, T)(T a, T b) { \
return cctl_compare_memory( \
cctl_get_memory_address(a), cctl_get_byte_size(a), \
cctl_get_memory_address(b), cctl_get_byte_size(b) \
); \
}
cctl_define_compare_func(int8_t);
cctl_define_compare_func(uint8_t);
cctl_define_compare_func(int16_t);
cctl_define_compare_func(uint16_t);
cctl_define_compare_func(int32_t);
cctl_define_compare_func(uint32_t);
cctl_define_compare_func(int64_t);
cctl_define_compare_func(uint64_t);
cctl_define_compare_func(float);
cctl_define_compare_func(double);
static inline int cctl_compare_cstring(cstring a, cstring b) {
if (a == b) return 0;
if (!a) return -1;
if (!b) return 1;
return strcmp(a, b);
}
cctl_define_memory_compare_func(u16cstring);
cctl_define_memory_compare_func(u32cstring);
/// ```c
/// int (*cctl_get_default_compare_func(T))(T a, T b)
/// ```
/// Returns the default comparison function pointer for primitive types and string types.
///
/// Returns `NULL` if no default comparison function is registered for `T`.
#define cctl_get_default_compare_func(T) \
_Generic(*(T*) 0, \
cstring: cctl_compare_cstring, \
u16cstring: cctl_compare_u16cstring, \
u32cstring: cctl_compare_u32cstring, \
int8_t: cctl_compare_int8_t, \
uint8_t: cctl_compare_uint8_t, \
int16_t: cctl_compare_int16_t, \
uint16_t: cctl_compare_uint16_t, \
int32_t: cctl_compare_int32_t, \
uint32_t: cctl_compare_uint32_t, \
int64_t: cctl_compare_int64_t, \
uint64_t: cctl_compare_uint64_t, \
float: cctl_compare_float, \
double: cctl_compare_double, \
default: NULL \
)
// Copy Functions
#define cctl_define_memory_copy_func(T) \
static inline T cctl_join(cctl_copy, T)(T data) { \
return (T) (uintptr_t) cctl_copy_memory( \
cctl_get_memory_address(data), \
cctl_get_byte_size(data) \
); \
}
#define cctl_define_copy_func(T) \
static inline T cctl_join(cctl_copy, T)(T data) { \
return data; \
}
cctl_define_copy_func(int8_t);
cctl_define_copy_func(uint8_t);
cctl_define_copy_func(int16_t);
cctl_define_copy_func(uint16_t);
cctl_define_copy_func(int32_t);
cctl_define_copy_func(uint32_t);
cctl_define_copy_func(int64_t);
cctl_define_copy_func(uint64_t);
cctl_define_copy_func(float);
cctl_define_copy_func(double);
cctl_define_memory_copy_func(cstring);
cctl_define_memory_copy_func(u16cstring);
cctl_define_memory_copy_func(u32cstring);
/// ```c
/// T (*cctl_get_default_copy_func(T))(T data)
/// ```
/// Returns the default copy function pointer for primitive types and string types.
///
/// Performs deep duplication for string types (`cstring`, `u16cstring`, `u32cstring`).
/// Returns `NULL` if no default copy function is registered for `T`.
#define cctl_get_default_copy_func(T) \
_Generic(*(T*) 0, \
cstring: cctl_copy_cstring, \
u16cstring: cctl_copy_u16cstring, \
u32cstring: cctl_copy_u32cstring, \
int8_t: cctl_copy_int8_t, \
uint8_t: cctl_copy_uint8_t, \
int16_t: cctl_copy_int16_t, \
uint16_t: cctl_copy_uint16_t, \
int32_t: cctl_copy_int32_t, \
uint32_t: cctl_copy_uint32_t, \
int64_t: cctl_copy_int64_t, \
uint64_t: cctl_copy_uint64_t, \
float: cctl_copy_float, \
double: cctl_copy_double, \
default: NULL \
)
// Free Functions
#define cctl_define_memory_free_func(T) \
static inline void cctl_join(cctl_free, T)(T data) { \
if (data) { \
free((void*) (uintptr_t) (data)); \
} \
}
#define cctl_define_free_func(T) \
static inline void cctl_join(cctl_free, T)(T data) { \
(void) data; \
}
cctl_define_free_func(int8_t);
cctl_define_free_func(uint8_t);
cctl_define_free_func(int16_t);
cctl_define_free_func(uint16_t);
cctl_define_free_func(int32_t);
cctl_define_free_func(uint32_t);
cctl_define_free_func(int64_t);
cctl_define_free_func(uint64_t);
cctl_define_free_func(float);
cctl_define_free_func(double);
cctl_define_memory_free_func(cstring);
cctl_define_memory_free_func(u16cstring);
cctl_define_memory_free_func(u32cstring);
/// ```c
/// void (*cctl_get_default_free_func(T))(T data)
/// ```
/// Returns the default resource release function pointer for primitive types and string types.
///
/// Frees allocated memory for string types (`cstring`, `u16cstring`, `u32cstring`).
/// Returns `NULL` if no default free function is registered for `T`.
#define cctl_get_default_free_func(T) \
_Generic(*(T*) 0, \
cstring: cctl_free_cstring, \
u16cstring: cctl_free_u16cstring, \
u32cstring: cctl_free_u32cstring, \
int8_t: cctl_free_int8_t, \
uint8_t: cctl_free_uint8_t, \
int16_t: cctl_free_int16_t, \
uint16_t: cctl_free_uint16_t, \
int32_t: cctl_free_int32_t, \
uint32_t: cctl_free_uint32_t, \
int64_t: cctl_free_int64_t, \
uint64_t: cctl_free_uint64_t, \
float: cctl_free_float, \
double: cctl_free_double, \
default: NULL \
)
#endif