-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.c
557 lines (432 loc) · 10.1 KB
/
utilities.c
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/*
* (C) by marco-a
*/
#define UTILITIES_EXTERN
#define VOID_POINTER_TO_SIZE_T(pointer, index) (*((size_t *)pointer + index))
#define VOID_POINTER_TO_UINT_T(pointer, index) (*((uint_t *)pointer + index))
#include "utilities.h"
/*
* Memory allocation helpers
*/
/*
* @_mem_get_static_size
* Returns static size of memory.
*/
size_t _mem_get_static_size(void *mem)
{
if (mem == nil) {
return 0;
}
mem -= sizeof(size_t);
if (mem == nil) {
return 0;
}
return VOID_POINTER_TO_SIZE_T(mem, 0);
}
/*
* @_mem_get_num_elements
* Returns number of elements.
*/
size_t _mem_get_num_elements(void *mem)
{
if (mem == nil) {
return 0;
}
mem -= UTILITIES_MEM_OFFSET;
if (mem == nil) {
return 0;
}
return VOID_POINTER_TO_SIZE_T(mem, 0);
}
/*
* @_mem_get_element_size
* Returns size of one element.
*/
size_t _mem_get_element_size(void *mem)
{
if (mem == nil) {
return 0;
}
mem -= UTILITIES_MEM_OFFSET;
if (((size_t *)mem + 1) == nil) {
return 0;
}
return VOID_POINTER_TO_SIZE_T(mem, 1);
}
/*
* @_mem_get_relative_size
* Returns size of allocated memory.
*/
size_t _mem_get_relative_size(void *mem)
{
return _mem_get_num_elements(mem) * _mem_get_element_size(mem);
}
/*
* @_mem_get_absolute_size
* Returns size of allocated memory.
*/
size_t _mem_get_absolute_size(void *mem)
{
return _mem_get_relative_size(mem) + UTILITIES_MEM_OFFSET;
}
/*
* @_mem_get_datatype
* Returns datatype of allocated memory.
*/
const char *_mem_get_datatype(void *mem)
{
if (mem == nil) {
return nil;
}
#ifndef UTILITIES_MEM_USE_TYPE
return "unknown";
#endif
mem -= sizeof(void *);
if (mem == nil) {
return "unknown";
}
#ifdef UTILITIES_MEM_USE_RETAIN_COUNT
mem -= sizeof(uint_t);
if (mem == nil) {
return "unknown";
}
#endif
return *((const char **)mem);
}
/*
* @_mem_get_retain_count
* Returns retain count of allocated memory.
*/
uint_t _mem_get_retain_count(void *mem)
{
if (mem == nil) {
return 0;
}
#ifndef UTILITIES_MEM_USE_RETAIN_COUNT
return 1;
#endif
mem -= sizeof(uint_t);
if (mem == nil) {
return 0;
}
return VOID_POINTER_TO_UINT_T(mem, 0);
}
/*
* @_mem_alloc
* Allocates num * size Bytes.
* Zeros memory.
*/
void *_mem_alloc(size_t num, size_t size, const char *datatype, BOOL zero)
{
void *mem;
size_t absolute_size;
size_t relative_size;
if (num == 0 || size == 0) {
return nil;
}
relative_size = num * size;
absolute_size = relative_size + UTILITIES_MEM_OFFSET;
if (zero == TRUE) {
#if UTILITIES_MEM_ZERO == 0x00
mem = calloc(absolute_size, 1);
#else
mem = malloc(absolute_size);
#endif
} else {
mem = malloc(absolute_size);
}
if (mem == nil) {
return nil;
}
VOID_POINTER_TO_SIZE_T(mem, 0) = num;
VOID_POINTER_TO_SIZE_T(mem, 1) = size;
// Add offset
mem += sizeof(size_t) * 2;
#ifdef UTILITIES_MEM_USE_TYPE
*((void **)mem) = (void *)datatype;
// Add offset
mem += sizeof(void *);
#endif
#ifdef UTILITIES_MEM_USE_RETAIN_COUNT
VOID_POINTER_TO_UINT_T(mem, 0) = 1;
// Add offset
mem += sizeof(uint_t);
#endif
#if UTILITIES_MEM_ZERO != 0x00
if (zero == TRUE) {
memset(mem, UTILITIES_MEM_ZERO, relative_size);
}
#endif
return mem;
}
/*
* @_mem_alloc_static
* Allocates static memory.
*/
void *_mem_alloc_static(size_t num, size_t size, BOOL zero)
{
void *mem;
size_t relative_size;
size_t absolute_size;
if (num == 0 || size == 0) {
return nil;
}
relative_size = num * size;
absolute_size = relative_size + sizeof(size_t);
if (zero == TRUE) {
#if UTILITIES_MEM_ZERO == 0x00
mem = calloc(absolute_size, 1);
#else
mem = malloc(absolute_size);
#endif
} else {
mem = malloc(absolute_size);
}
if (mem == nil) {
return nil;
}
VOID_POINTER_TO_SIZE_T(mem, 0) = relative_size;
mem += sizeof(size_t);
return mem;
}
/*
* @_mem_grow
* Grows memory.
*/
BOOL _mem_grow(void **mem_ptr, size_t num, BOOL zero)
{
void *mem;
void *new_mem;
size_t element_size;
size_t num_elements;
size_t relative_size;
size_t new_relative_size;
size_t new_absolute_size;
if (mem_ptr == nil) {
return FALSE;
} else if (*mem_ptr == nil) {
return FALSE;
} else if (num == 0) {
return FALSE;
}
mem = *mem_ptr;
element_size = mem_get_element_size(mem);
num_elements = mem_get_num_elements(mem);
relative_size = mem_get_relative_size(mem);
new_relative_size = (element_size * num);
new_relative_size += relative_size;
if (new_relative_size <= relative_size) {
return FALSE;
}
new_absolute_size = new_relative_size + UTILITIES_MEM_OFFSET;
new_mem = realloc(mem - UTILITIES_MEM_OFFSET, new_absolute_size);
if (new_mem == nil) {
return FALSE;
}
VOID_POINTER_TO_SIZE_T(new_mem, 0) = num_elements + num;
new_mem += UTILITIES_MEM_OFFSET;
// Zero out new memory
if (zero == TRUE) {
void *start;
start = ((uchar_t *)new_mem) + relative_size;
memset(start, UTILITIES_MEM_ZERO, (new_relative_size - relative_size));
}
*mem_ptr = new_mem;
return TRUE;
}
/*
* @_mem_shrink
* Shrinks memory.
*/
BOOL _mem_shrink(void **mem_ptr, size_t num)
{
void *mem;
void *new_mem;
size_t num_elements;
size_t new_num_elements;
if (mem_ptr == nil) {
return FALSE;
} else if (*mem_ptr == nil) {
return FALSE;
} else if (num == 0) {
return FALSE;
}
mem = *mem_ptr;
num_elements = mem_get_num_elements(mem);
if (num_elements <= num) {
return FALSE;
}
new_num_elements = num_elements - num;
new_mem = _mem_alloc(new_num_elements, mem_get_element_size(mem), mem_get_datatype(mem), FALSE);
if (new_mem == nil) {
return FALSE;
}
memcpy(new_mem, (const void *)mem, mem_get_relative_size(new_mem));
*mem_ptr = new_mem;
return TRUE;
}
/*
* @_mem_dealloc
* Deallocates memory.
*/
BOOL _mem_dealloc(void **mem_ptr, BOOL is_static)
{
if (mem_ptr == nil) {
return FALSE;
} else if (*mem_ptr == nil) {
return FALSE;
}
if (is_static) {
*mem_ptr -= sizeof(size_t);
} else {
*mem_ptr -= UTILITIES_MEM_OFFSET;
}
free(*mem_ptr);
*mem_ptr = nil;
return TRUE;
}
/*
* @_mem_release
* Releases memory.
*/
BOOL _mem_release(void **mem_ptr)
{
#ifndef UTILITIES_MEM_USE_RETAIN_COUNT
return _mem_free(mem_ptr);
#else
void *mem;
uint_t retain_count;
if (mem_ptr == nil) {
return FALSE;
} else if (*mem_ptr == nil) {
return FALSE;
}
mem = *mem_ptr;
mem -= sizeof(uint_t);
if (mem == nil) {
return FALSE;
}
retain_count = VOID_POINTER_TO_UINT_T(mem, 0);
if (retain_count >= 1) {
retain_count -= 1;
VOID_POINTER_TO_UINT_T(mem, 0) = retain_count;
if (retain_count == 0) {
return _mem_dealloc(mem_ptr, FALSE);
}
}
return FALSE;
#endif
}
/*
* @_mem_retain
* Retains memory.
*/
void _mem_retain(void *mem)
{
#ifndef UTILITIES_MEM_USE_RETAIN_COUNT
return;
#else
uint_t retain_count;
if (mem == nil) {
return;
}
mem -= sizeof(uint_t);
if (mem == nil) {
return;
}
retain_count = VOID_POINTER_TO_UINT_T(mem, 0);
if (retain_count == UINT_MAX) {
return;
}
retain_count++;
VOID_POINTER_TO_UINT_T(mem, 0) = retain_count;
#endif
}
#define UTILITIES_MEM_DUMP_COLS 8
/*
* @_mem_dump
* Dumps memory.
*/
void _mem_dump(void *mem, BOOL is_static)
{
size_t relative_size;
size_t num_elements;
size_t element_size;
size_t absolute_size;
if (mem == nil) {
return;
}
if (is_static == FALSE) {
// Get relative size
relative_size = mem_get_relative_size(mem);
// Get number of elements
num_elements = mem_get_num_elements(mem);
// Get size of single element
element_size = mem_get_element_size(mem);
// Get absolute size
absolute_size = mem_get_absolute_size(mem);
} else {
relative_size = mem_get_static_size(mem);
num_elements = element_size = 0;
absolute_size = relative_size + sizeof(size_t);
}
fprintf(stderr, "\n+-----------------[ MEMORY INFORMATION ]-----------------+\n");
if (is_static == FALSE) {
fprintf(stderr, "| Datatype : %-32s |\n", mem_get_datatype(mem));
fprintf(stderr, "| Retain count : %-32u |\n", mem_get_retain_count(mem));
fprintf(stderr, "| Absolute address : %-32p |\n", mem - UTILITIES_MEM_OFFSET);
fprintf(stderr, "| Absolute size : %-26zu Bytes |\n", absolute_size);
fprintf(stderr, "| Offset : %-26zu Bytes |\n", UTILITIES_MEM_OFFSET);
} else {
fprintf(stderr, "| Absolute address : %-32p |\n", mem - sizeof(size_t));
fprintf(stderr, "| Absolute size : %-26zu Bytes |\n", absolute_size);
fprintf(stderr, "| Offset : %-26zu Bytes |\n", sizeof(size_t));
}
fprintf(stderr, "+--------------------------------------------------------+\n");
fprintf(stderr, "| Relative address : %-32p |\n", mem);
if (relative_size == 1) {
fprintf(stderr, "| Relative size : %-27zu Byte |\n", relative_size);
} else {
fprintf(stderr, "| Relative size : %-26zu Bytes |\n", relative_size);
}
if (is_static == FALSE) {
fprintf(stderr, "+--------------------------------------------------------+\n");
if (num_elements == 1) {
fprintf(stderr, "| Number of elements : %-24zu Element |\n", num_elements);
} else {
fprintf(stderr, "| Number of elements : %-23zu Elements |\n", num_elements);
}
if (element_size == 1) {
fprintf(stderr, "| Size of one element : %-27zu Byte |\n", element_size);
} else {
fprintf(stderr, "| Size of one element : %-26zu Bytes |\n", element_size);
}
}
fprintf(stderr, "+-----------------[ MEMORY DUMP ]-----------------+\n");
{
size_t i, x;
for (i = 0; i < relative_size + ((relative_size % UTILITIES_MEM_DUMP_COLS) ? (UTILITIES_MEM_DUMP_COLS - relative_size % UTILITIES_MEM_DUMP_COLS) : 0); ++i) {
if (i % UTILITIES_MEM_DUMP_COLS == 0) {
fprintf(stderr, "| 0x%-17zx : ", i + (size_t)mem);
}
if (i < relative_size) {
fprintf(stderr, "%02X ", 0xFF & ((uchar_t *)mem)[i]);
} else {
fprintf(stderr, " ");
}
if (i % UTILITIES_MEM_DUMP_COLS == (UTILITIES_MEM_DUMP_COLS - 1)) {
for (x = i - (UTILITIES_MEM_DUMP_COLS - 1); x <= i; ++x) {
if (x >= relative_size) {
fprintf(stderr, " ");
} else if (isprint(((uchar_t *)mem)[x])) {
fprintf(stderr, "%c", 0xFF & ((uchar_t *)mem)[x]);
} else {
fprintf(stderr, ".");
}
}
fprintf(stderr, " |\n");
}
}
}
fprintf(stderr, "+--------------------------------------------------------+\n\n");
}