forked from sqliteai/sqlite-sync
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.c
More file actions
698 lines (573 loc) · 21.4 KB
/
utils.c
File metadata and controls
698 lines (573 loc) · 21.4 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
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
//
// utils.c
// cloudsync
//
// Created by Marco Bambini on 21/08/24.
//
#include "utils.h"
#include <ctype.h>
#include <stdlib.h>
#ifdef _WIN32
#include <windows.h>
#include <objbase.h>
#include <bcrypt.h>
#include <ntstatus.h> //for STATUS_SUCCESS
#include <io.h>
#define file_close _close
#else
#include <unistd.h>
#if defined(__APPLE__) && !defined(CLOUDSYNC_POSTGRESQL_BUILD)
#include <Security/Security.h>
#elif !defined(__ANDROID__)
#include <sys/random.h>
#endif
#define file_close close
#endif
#ifdef CLOUDSYNC_DESKTOP_OS
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#endif
#define FNV_OFFSET_BASIS 0xcbf29ce484222325ULL
#define FNV_PRIME 0x100000001b3ULL
#define HASH_CHAR(_c) do { h ^= (uint8_t)(_c); h *= FNV_PRIME; h_final = h;} while (0)
// MARK: - UUIDv7 -
/*
UUIDv7 is a 128-bit unique identifier like it's older siblings, such as the widely used UUIDv4.
But unlike v4, UUIDv7 is time-sortable with 1 ms precision.
By combining the timestamp and the random parts, UUIDv7 becomes an excellent choice for record identifiers in databases, including distributed ones.
UUIDv7 offers several advantages.
It includes a 48-bit Unix timestamp with millisecond accuracy and will overflow far in the future (10899 AD).
It also include 74 random bits which means billions can be created every second without collisions.
Because of its structure UUIDv7s are globally sortable and can be created in parallel in a distributed system.
https://antonz.org/uuidv7/#c
https://www.rfc-editor.org/rfc/rfc9562.html#name-uuid-version-7
*/
int cloudsync_uuid_v7 (uint8_t value[UUID_LEN]) {
// fill the buffer with high-quality random data
#ifdef _WIN32
if (BCryptGenRandom(NULL, (BYTE*)value, UUID_LEN, BCRYPT_USE_SYSTEM_PREFERRED_RNG) != STATUS_SUCCESS) return -1;
#elif defined(__APPLE__) && !defined(CLOUDSYNC_POSTGRESQL_BUILD)
// Use SecRandomCopyBytes for macOS/iOS
if (SecRandomCopyBytes(kSecRandomDefault, UUID_LEN, value) != errSecSuccess) return -1;
#elif defined(__APPLE__) && defined(CLOUDSYNC_POSTGRESQL_BUILD)
// PostgreSQL build: use getentropy to avoid Security.framework type conflicts
if (getentropy(value, UUID_LEN) != 0) return -1;
#elif defined(__ANDROID__)
//arc4random_buf doesn't have a return value to check for success
arc4random_buf(value, UUID_LEN);
#else
if (getentropy(value, UUID_LEN) != 0) return -1;
#endif
// get current timestamp in ms
struct timespec ts;
#ifdef __ANDROID__
if (clock_gettime(CLOCK_REALTIME, &ts) != 0) return -1;
#else
if (timespec_get(&ts, TIME_UTC) == 0) return -1;
#endif
// add timestamp part to UUID
uint64_t timestamp = (uint64_t)ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
value[0] = (timestamp >> 40) & 0xFF;
value[1] = (timestamp >> 32) & 0xFF;
value[2] = (timestamp >> 24) & 0xFF;
value[3] = (timestamp >> 16) & 0xFF;
value[4] = (timestamp >> 8) & 0xFF;
value[5] = timestamp & 0xFF;
// version and variant
value[6] = (value[6] & 0x0F) | 0x70; // UUID version 7
value[8] = (value[8] & 0x3F) | 0x80; // RFC 4122 variant
return 0;
}
char *cloudsync_uuid_v7_stringify (uint8_t uuid[UUID_LEN], char value[UUID_STR_MAXLEN], bool dash_format) {
if (dash_format) {
snprintf(value, UUID_STR_MAXLEN, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], uuid[6], uuid[7],
uuid[8], uuid[9], uuid[10], uuid[11], uuid[12], uuid[13], uuid[14], uuid[15]
);
} else {
snprintf(value, UUID_STR_MAXLEN, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], uuid[6], uuid[7],
uuid[8], uuid[9], uuid[10], uuid[11], uuid[12], uuid[13], uuid[14], uuid[15]
);
}
return (char *)value;
}
char *cloudsync_uuid_v7_string (char value[UUID_STR_MAXLEN], bool dash_format) {
uint8_t uuid[UUID_LEN];
if (cloudsync_uuid_v7(uuid) != 0) return NULL;
return cloudsync_uuid_v7_stringify(uuid, value, dash_format);
}
int cloudsync_uuid_v7_compare (uint8_t value1[UUID_LEN], uint8_t value2[UUID_LEN]) {
// reconstruct the timestamp by reversing the bit shifts and combining the bytes
uint64_t t1 = ((uint64_t)value1[0] << 40) | ((uint64_t)value1[1] << 32) | ((uint64_t)value1[2] << 24) |
((uint64_t)value1[3] << 16) | ((uint64_t)value1[4] << 8) | ((uint64_t)value1[5]);
uint64_t t2 = ((uint64_t)value2[0] << 40) | ((uint64_t)value2[1] << 32) | ((uint64_t)value2[2] << 24) |
((uint64_t)value2[3] << 16) | ((uint64_t)value2[4] << 8) | ((uint64_t)value2[5]);
if (t1 == t2) return memcmp(value1, value2, UUID_LEN);
return (t1 > t2) ? 1 : -1;
}
// MARK: - General -
char *cloudsync_string_ndup_v2 (const char *str, size_t len, bool lowercase) {
if (str == NULL) return NULL;
char *s = (char *)cloudsync_memory_alloc((uint64_t)(len + 1));
if (!s) return NULL;
if (lowercase) {
// convert each character to lowercase and copy it to the new string
for (size_t i = 0; i < len; i++) {
s[i] = (char)tolower(str[i]);
}
} else {
memcpy(s, str, len);
}
// null-terminate the string
s[len] = '\0';
return s;
}
char *cloudsync_string_ndup (const char *str, size_t len) {
return cloudsync_string_ndup_v2(str, len, false);
}
char *cloudsync_string_ndup_lowercase (const char *str, size_t len) {
return cloudsync_string_ndup_v2(str, len, true);
}
char *cloudsync_string_dup (const char *str) {
return cloudsync_string_ndup_v2(str, (str) ? strlen(str) : 0, false);
}
char *cloudsync_string_dup_lowercase (const char *str) {
return cloudsync_string_ndup_v2(str, (str) ? strlen(str) : 0, true);
}
int cloudsync_blob_compare(const char *blob1, size_t size1, const char *blob2, size_t size2) {
if (size1 != size2) return (size1 > size2) ? 1 : -1; // blobs are different if sizes are different
return memcmp(blob1, blob2, size1); // use memcmp for byte-by-byte comparison
}
void cloudsync_rowid_decode (int64_t rowid, int64_t *db_version, int64_t *seq) {
// use unsigned 64-bit integer for intermediate calculations
// when db_version is large enough, it can cause overflow, leading to negative values
// to handle this correctly, we need to ensure the calculations are done in an unsigned 64-bit integer context
// before converting back to int64_t as needed
uint64_t urowid = (uint64_t)rowid;
// define the bit mask for seq (30 bits)
const uint64_t SEQ_MASK = 0x3FFFFFFF; // (2^30 - 1)
// extract seq by masking the lower 30 bits
*seq = (int64_t)(urowid & SEQ_MASK);
// extract db_version by shifting 30 bits to the right
*db_version = (int64_t)(urowid >> 30);
}
char *cloudsync_string_replace_prefix(const char *input, char *prefix, char *replacement) {
//const char *prefix = "sqlitecloud://";
//const char *replacement = "https://";
size_t prefix_len = strlen(prefix);
size_t replacement_len = strlen(replacement);
if (strncmp(input, prefix, prefix_len) == 0) {
// allocate memory for new string
size_t input_len = strlen(input);
size_t new_len = input_len - prefix_len + replacement_len;
char *result = cloudsync_memory_alloc(new_len + 1); // +1 for null terminator
if (!result) return NULL;
// copy replacement and the rest of the input string
strcpy(result, replacement);
strcpy(result + replacement_len, input + prefix_len);
return result;
}
// If no match, return the original string
return (char *)input;
}
/*
Compute a normalized hash of a CREATE TABLE statement.
* Normalization:
* - Skips comments (-- and / * )
* - Skips non-printable characters
* - Collapses runs of whitespace to single space
* - Case-insensitive outside quotes
* - Preserves quoted string content exactly
* - Handles escaped quotes
* - Trims trailing spaces and semicolons from the effective hash
*/
uint64_t fnv1a_hash (const char *data, size_t len) {
uint64_t h = FNV_OFFSET_BASIS;
int q = 0; // quote state: 0 / '\'' / '"'
int cmt = 0; // comment state: 0 / 1=line / 2=block
int last_space = 1; // prevent leading space
uint64_t h_final = h; // hash state after last non-space, non-semicolon char
for (size_t i = 0; i < len; i++) {
int c = data[i];
int next = (i + 1 < len) ? data[i + 1] : 0;
// detect start of comments
if (!q && !cmt && c == '-' && next == '-') {cmt = 1; i += 1; continue;}
if (!q && !cmt && c == '/' && next == '*') {cmt = 2; i += 1; continue;}
// skip comments
if (cmt == 1) {if (c == '\n') cmt = 0; continue;}
if (cmt == 2) {if (c == '*' && next == '/') { cmt = 0; i += 1; } continue;}
// handle quotes
if (c == '\'' || c == '"') {
if (q == c) {
if (next == c) {HASH_CHAR(c); i += 1; continue;}
q = 0;
} else if (!q) q = c;
HASH_CHAR(c);
last_space = 0;
continue;
}
// inside quote → hash exactly
if (q) {HASH_CHAR(c); last_space = 0; continue;}
// skip non-printable
if (!isprint((unsigned char)c)) continue;
// whitespace normalization
if (isspace((unsigned char)c)) {
// look ahead to next non-space, non-comment char
size_t j = i + 1;
while (j < len && isspace((unsigned char)data[j])) j++;
int next_c = (j < len) ? data[j] : 0;
// if next char is punctuation where space is irrelevant → skip space
if (next_c == '(' || next_c == ')' || next_c == ',' || next_c == ';' || next_c == 0) {
// skip inserting space
last_space = 1;
continue;
}
// else, insert one space
if (!last_space) {HASH_CHAR(' '); last_space = 1;}
continue;
}
// skip semicolons at end
if (c == ';') {last_space = 1; continue;}
// normal visible char
HASH_CHAR(tolower(c));
last_space = 0;
}
return h_final;
}
// MARK: - Files -
#ifdef CLOUDSYNC_DESKTOP_OS
bool cloudsync_file_delete (const char *path) {
#ifdef _WIN32
return DeleteFileA(path);
#else
return (unlink(path) == 0);
#endif
}
static bool cloudsync_file_read_all (int fd, char *buf, size_t n) {
size_t off = 0;
while (off < n) {
#ifdef _WIN32
int r = _read(fd, buf + off, (unsigned)(n - off));
if (r <= 0) return false;
#else
ssize_t r = read(fd, buf + off, n - off);
if (r < 0) {
if (errno == EINTR) continue;
return false;
}
if (r == 0) return false; // unexpected EOF
#endif
off += (size_t)r;
}
return true;
}
char *cloudsync_file_read (const char *path, int64_t *len) {
int fd = -1;
char *buffer = NULL;
#ifdef _WIN32
fd = _open(path, _O_RDONLY | _O_BINARY);
#else
fd = open(path, O_RDONLY);
#endif
if (fd < 0) goto abort_read;
// Get size after open to reduce TOCTTOU
#ifdef _WIN32
struct _stat64 st;
if (_fstat64(fd, &st) != 0 || st.st_size < 0) goto abort_read;
int64_t isz = st.st_size;
#else
struct stat st;
if (fstat(fd, &st) != 0 || st.st_size < 0) goto abort_read;
int64_t isz = st.st_size;
#endif
size_t sz = (size_t)isz;
// optional: guard against huge files that don't fit in size_t
if ((int64_t)sz != isz) goto abort_read;
buffer = (char *)cloudsync_memory_alloc(sz + 1);
if (!buffer) goto abort_read;
buffer[sz] = '\0';
if (!cloudsync_file_read_all(fd, buffer, sz)) goto abort_read;
if (len) *len = sz;
file_close(fd);
return buffer;
abort_read:
//fprintf(stderr, "file_read: failed to read '%s': %s\n", path, strerror(errno));
if (len) *len = -1;
if (buffer) cloudsync_memory_free(buffer);
if (fd >= 0) file_close(fd);
return NULL;
}
int cloudsync_file_create (const char *path) {
#ifdef _WIN32
int flags = _O_WRONLY | _O_CREAT | _O_TRUNC | _O_BINARY;
int mode = _S_IWRITE; // Windows ignores most POSIX perms
return _open(path, flags, mode);
#else
int flags = O_WRONLY | O_CREAT | O_TRUNC;
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
return open(path, flags, mode);
#endif
}
static bool cloudsync_file_write_all (int fd, const char *buf, size_t n) {
size_t off = 0;
while (off < n) {
#ifdef _WIN32
int w = _write(fd, buf + off, (unsigned)(n - off));
if (w <= 0) return false;
#else
ssize_t w = write(fd, buf + off, n - off);
if (w < 0) {
if (errno == EINTR) continue;
return false;
}
if (w == 0) return false;
#endif
off += (size_t)w;
}
return true;
}
bool cloudsync_file_write (const char *path, const char *buffer, size_t len) {
int fd = cloudsync_file_create(path);
if (fd < 0) return false;
bool res = cloudsync_file_write_all(fd, buffer, len);
file_close(fd);
return res;
}
#endif
// MARK: - Memory Debugger -
#if CLOUDSYNC_DEBUG_MEMORY
#include <execinfo.h>
#include <inttypes.h>
#include <assert.h>
#include "khash.h"
KHASH_MAP_INIT_INT64(HASHTABLE_INT64_VOIDPTR, void*)
#define STACK_DEPTH 128
#define BUILD_ERROR(...) char current_error[1024]; snprintf(current_error, sizeof(current_error), __VA_ARGS__)
#define BUILD_STACK(v1,v2) size_t v1; char **v2 = _ptr_stacktrace(&v1)
typedef struct {
void *ptr;
size_t size;
bool deleted;
size_t nrealloc;
// record where it has been allocated/reallocated
size_t nframe;
char **frames;
// record where it has been freed
size_t nframe2;
char **frames2;
} mem_slot;
static void memdebug_report (char *str, char **stack, size_t nstack, mem_slot *slot);
static khash_t(HASHTABLE_INT64_VOIDPTR) *htable;
static uint64_t nalloc, nrealloc, nfree, mem_current, mem_max;
static void *_ptr_lookup (void *ptr) {
khiter_t k = kh_get(HASHTABLE_INT64_VOIDPTR, htable, (int64_t)ptr);
void *result = (k == kh_end(htable)) ? NULL : (void *)kh_value(htable, k);
return result;
}
static bool _ptr_insert (void *ptr, mem_slot *slot) {
int err = 0;
khiter_t k = kh_put(HASHTABLE_INT64_VOIDPTR, htable, (int64_t)ptr, &err);
if (err != -1) kh_value(htable, k) = (void *)slot;
return (err != -1);
}
static char **_ptr_stacktrace (size_t *nframes) {
#if _WIN32
// http://www.codeproject.com/Articles/11132/Walking-the-callstack
// https://spin.atomicobject.com/2013/01/13/exceptions-stack-traces-c/
#else
void *callstack[STACK_DEPTH];
int n = backtrace(callstack, STACK_DEPTH);
char **strs = backtrace_symbols(callstack, n);
*nframes = (size_t)n;
return strs;
#endif
}
static mem_slot *_ptr_add (void *ptr, size_t size) {
mem_slot *slot = (mem_slot *)calloc(1, sizeof(mem_slot));
assert(slot);
slot->ptr = ptr;
slot->size = size;
slot->frames = _ptr_stacktrace(&slot->nframe);
bool ok = _ptr_insert(ptr, slot);
assert(ok);
++nalloc;
mem_current += size;
if (mem_current > mem_max) mem_max = mem_current;
return slot;
}
static void _ptr_remove (void *ptr) {
mem_slot *slot = (mem_slot *)_ptr_lookup(ptr);
if (!slot) {
BUILD_ERROR("Unable to find old pointer to free.");
memdebug_report(current_error, NULL, 0, NULL);
return;
}
if (slot->deleted) {
BUILD_ERROR("Pointer already freed.");
BUILD_STACK(n, stack);
memdebug_report(current_error, stack, n, slot);
}
size_t old_size = slot->size;
slot->deleted = true;
slot->frames2 = _ptr_stacktrace(&slot->nframe2);
++nfree;
mem_current -= old_size;
}
static void _ptr_replace (void *old_ptr, void *new_ptr, size_t new_size) {
if (old_ptr == NULL) {
_ptr_add(new_ptr, new_size);
return;
}
// remove old ptr (implicit free performed by realloc)
_ptr_remove(old_ptr);
// add newly allocated prt (implicit alloc performed by realloc)
mem_slot *slot = _ptr_add(new_ptr, new_size);
++slot->nrealloc;
++nrealloc;
if (mem_current > mem_max) mem_max = mem_current;
}
// MARK: -
static bool stacktrace_is_internal(const char *s) {
static const char *reserved[] = {"??? ", "libdyld.dylib ", "memdebug_", "_ptr_", NULL};
const char **r = reserved;
while (*r) {
if (strstr(s, *r)) return true;
++r;
}
return false;
}
static void memdebug_report (char *str, char **stack, size_t nstack, mem_slot *slot) {
printf("%s\n", str);
for (size_t i=0; i<nstack; ++i) {
if (stacktrace_is_internal(stack[i])) continue;
printf("%s\n", stack[i]);
}
if (slot) {
printf("\nallocated:\n");
for (size_t i=0; i<slot->nframe; ++i) {
if (stacktrace_is_internal(slot->frames[i])) continue;
printf("%s\n", slot->frames[i]);
}
printf("\nfreed:\n");
for (size_t i=0; i<slot->nframe2; ++i) {
if (stacktrace_is_internal(slot->frames2[i])) continue;
printf("%s\n", slot->frames2[i]);
}
}
}
void memdebug_init (int once) {
if (htable == NULL) htable = kh_init(HASHTABLE_INT64_VOIDPTR);
}
void memdebug_finalize (void) {
printf("\n========== MEMORY STATS ==========\n");
printf("Allocations count: %" PRIu64 "\n", nalloc);
printf("Reallocations count: %" PRIu64 "\n", nrealloc);
printf("Free count: %" PRIu64 "\n", nfree);
printf("Leaked: %" PRIu64 " (bytes)\n", mem_current);
printf("Max memory usage: %" PRIu64 " (bytes)\n", mem_max);
printf("==================================\n\n");
if (mem_current > 0) {
printf("\n========== LEAKS DETAILS ==========\n");
khiter_t k;
for (k = kh_begin(htable); k != kh_end(htable); ++k) {
if (kh_exist(htable, k)) {
mem_slot *slot = (mem_slot *)kh_value(htable, k);
if ((!slot->ptr) || (slot->deleted)) continue;
printf("Block %p size: %zu (reallocated %zu)\n", slot->ptr, slot->size, slot->nrealloc);
printf("Call stack:\n");
printf("===========\n");
for (size_t j=0; j<slot->nframe; ++j) {
if (stacktrace_is_internal(slot->frames[j])) continue;
printf("%s\n", slot->frames[j]);
}
printf("===========\n\n");
}
}
}
}
void *memdebug_alloc (uint64_t size) {
void *ptr = dbmem_alloc(size);
if (!ptr) {
BUILD_ERROR("Unable to allocated a block of %" PRIu64" bytes", size);
BUILD_STACK(n, stack);
memdebug_report(current_error, stack, n, NULL);
return NULL;
}
_ptr_add(ptr, size);
return ptr;
}
void *memdebug_zeroalloc (uint64_t size) {
void *ptr = memdebug_alloc(size);
if (!ptr) return NULL;
memset(ptr, 0, (size_t)size);
return ptr;
}
void *memdebug_realloc (void *ptr, uint64_t new_size) {
if (!ptr) return memdebug_alloc(new_size);
mem_slot *slot = _ptr_lookup(ptr);
if (!slot) {
BUILD_ERROR("Pointer being reallocated was now previously allocated.");
BUILD_STACK(n, stack);
memdebug_report(current_error, stack, n, NULL);
return NULL;
}
void *back_ptr = ptr;
void *new_ptr = dbmem_realloc(ptr, new_size);
if (!new_ptr) {
BUILD_ERROR("Unable to reallocate a block of %" PRIu64 " bytes.", new_size);
BUILD_STACK(n, stack);
memdebug_report(current_error, stack, n, slot);
return NULL;
}
_ptr_replace(back_ptr, new_ptr, new_size);
return new_ptr;
}
char *memdebug_vmprintf (const char *format, va_list list) {
char *ptr = dbmem_vmprintf(format, list);
if (!ptr) {
BUILD_ERROR("Unable to allocated for dbmem_vmprintf with format %s", format);
BUILD_STACK(n, stack);
memdebug_report(current_error, stack, n, NULL);
return NULL;
}
_ptr_add(ptr, dbmem_size(ptr));
return ptr;
}
char *memdebug_mprintf(const char *format, ...) {
va_list ap;
char *z;
va_start(ap, format);
z = memdebug_vmprintf(format, ap);
va_end(ap);
return z;
}
uint64_t memdebug_msize (void *ptr) {
return dbmem_size(ptr);
}
void memdebug_free (void *ptr) {
if (!ptr) {
BUILD_ERROR("Trying to deallocate a NULL ptr.");
BUILD_STACK(n, stack);
memdebug_report(current_error, stack, n, NULL);
}
// ensure ptr has been previously allocated by malloc, calloc or realloc and not yet freed with free
mem_slot *slot = _ptr_lookup(ptr);
if (!slot) {
BUILD_ERROR("Pointer being freed was not previously allocated.");
BUILD_STACK(n, stack);
memdebug_report(current_error, stack, n, NULL);
return;
}
if (slot->deleted) {
BUILD_ERROR("Pointer already freed.");
BUILD_STACK(n, stack);
memdebug_report(current_error, stack, n, slot);
return;
}
_ptr_remove(ptr);
dbmem_free(ptr);
}
#endif