Line data Source code
1 : //
2 : // utils.c
3 : // cloudsync
4 : //
5 : // Created by Marco Bambini on 21/08/24.
6 : //
7 :
8 : #include "utils.h"
9 : #include <ctype.h>
10 : #include <stdlib.h>
11 :
12 : #ifdef _WIN32
13 : #include <windows.h>
14 : #include <objbase.h>
15 : #include <bcrypt.h>
16 : #include <ntstatus.h> //for STATUS_SUCCESS
17 : #include <io.h>
18 : #define file_close _close
19 : #else
20 : #include <unistd.h>
21 : #if defined(__APPLE__) && !defined(CLOUDSYNC_POSTGRESQL_BUILD)
22 : #include <Security/Security.h>
23 : #elif !defined(__ANDROID__)
24 : #include <sys/random.h>
25 : #endif
26 : #define file_close close
27 : #endif
28 :
29 : #ifdef CLOUDSYNC_DESKTOP_OS
30 : #include <fcntl.h>
31 : #include <errno.h>
32 : #include <sys/stat.h>
33 : #include <sys/types.h>
34 : #endif
35 :
36 : #define FNV_OFFSET_BASIS 0xcbf29ce484222325ULL
37 : #define FNV_PRIME 0x100000001b3ULL
38 : #define HASH_CHAR(_c) do { h ^= (uint8_t)(_c); h *= FNV_PRIME; h_final = h;} while (0)
39 :
40 : // MARK: - UUIDv7 -
41 :
42 : /*
43 : UUIDv7 is a 128-bit unique identifier like it's older siblings, such as the widely used UUIDv4.
44 : But unlike v4, UUIDv7 is time-sortable with 1 ms precision.
45 : By combining the timestamp and the random parts, UUIDv7 becomes an excellent choice for record identifiers in databases, including distributed ones.
46 :
47 : UUIDv7 offers several advantages.
48 : It includes a 48-bit Unix timestamp with millisecond accuracy and will overflow far in the future (10899 AD).
49 : It also include 74 random bits which means billions can be created every second without collisions.
50 : Because of its structure UUIDv7s are globally sortable and can be created in parallel in a distributed system.
51 :
52 : https://antonz.org/uuidv7/#c
53 : https://www.rfc-editor.org/rfc/rfc9562.html#name-uuid-version-7
54 : */
55 :
56 4303 : int cloudsync_uuid_v7 (uint8_t value[UUID_LEN]) {
57 : // fill the buffer with high-quality random data
58 : #ifdef _WIN32
59 : if (BCryptGenRandom(NULL, (BYTE*)value, UUID_LEN, BCRYPT_USE_SYSTEM_PREFERRED_RNG) != STATUS_SUCCESS) return -1;
60 : #elif defined(__APPLE__) && !defined(CLOUDSYNC_POSTGRESQL_BUILD)
61 : // Use SecRandomCopyBytes for macOS/iOS
62 4303 : if (SecRandomCopyBytes(kSecRandomDefault, UUID_LEN, value) != errSecSuccess) return -1;
63 : #elif defined(__APPLE__) && defined(CLOUDSYNC_POSTGRESQL_BUILD)
64 : // PostgreSQL build: use getentropy to avoid Security.framework type conflicts
65 : if (getentropy(value, UUID_LEN) != 0) return -1;
66 : #elif defined(__ANDROID__)
67 : //arc4random_buf doesn't have a return value to check for success
68 : arc4random_buf(value, UUID_LEN);
69 : #else
70 : if (getentropy(value, UUID_LEN) != 0) return -1;
71 : #endif
72 :
73 : // get current timestamp in ms
74 : struct timespec ts;
75 : #ifdef __ANDROID__
76 : if (clock_gettime(CLOCK_REALTIME, &ts) != 0) return -1;
77 : #else
78 4303 : if (timespec_get(&ts, TIME_UTC) == 0) return -1;
79 : #endif
80 :
81 : // add timestamp part to UUID
82 4303 : uint64_t timestamp = (uint64_t)ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
83 4303 : value[0] = (timestamp >> 40) & 0xFF;
84 4303 : value[1] = (timestamp >> 32) & 0xFF;
85 4303 : value[2] = (timestamp >> 24) & 0xFF;
86 4303 : value[3] = (timestamp >> 16) & 0xFF;
87 4303 : value[4] = (timestamp >> 8) & 0xFF;
88 4303 : value[5] = timestamp & 0xFF;
89 :
90 : // version and variant
91 4303 : value[6] = (value[6] & 0x0F) | 0x70; // UUID version 7
92 4303 : value[8] = (value[8] & 0x3F) | 0x80; // RFC 4122 variant
93 :
94 4303 : return 0;
95 4303 : }
96 :
97 4339 : char *cloudsync_uuid_v7_stringify (uint8_t uuid[UUID_LEN], char value[UUID_STR_MAXLEN], bool dash_format) {
98 4339 : if (dash_format) {
99 2078 : snprintf(value, UUID_STR_MAXLEN, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
100 : uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], uuid[6], uuid[7],
101 : uuid[8], uuid[9], uuid[10], uuid[11], uuid[12], uuid[13], uuid[14], uuid[15]
102 : );
103 2078 : } else {
104 2261 : snprintf(value, UUID_STR_MAXLEN, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
105 : uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], uuid[6], uuid[7],
106 : uuid[8], uuid[9], uuid[10], uuid[11], uuid[12], uuid[13], uuid[14], uuid[15]
107 : );
108 : }
109 :
110 4339 : return (char *)value;
111 : }
112 :
113 2077 : char *cloudsync_uuid_v7_string (char value[UUID_STR_MAXLEN], bool dash_format) {
114 : uint8_t uuid[UUID_LEN];
115 :
116 2077 : if (cloudsync_uuid_v7(uuid) != 0) return NULL;
117 2077 : return cloudsync_uuid_v7_stringify(uuid, value, dash_format);
118 2077 : }
119 :
120 1003 : int cloudsync_uuid_v7_compare (uint8_t value1[UUID_LEN], uint8_t value2[UUID_LEN]) {
121 : // reconstruct the timestamp by reversing the bit shifts and combining the bytes
122 3009 : uint64_t t1 = ((uint64_t)value1[0] << 40) | ((uint64_t)value1[1] << 32) | ((uint64_t)value1[2] << 24) |
123 2006 : ((uint64_t)value1[3] << 16) | ((uint64_t)value1[4] << 8) | ((uint64_t)value1[5]);
124 3009 : uint64_t t2 = ((uint64_t)value2[0] << 40) | ((uint64_t)value2[1] << 32) | ((uint64_t)value2[2] << 24) |
125 2006 : ((uint64_t)value2[3] << 16) | ((uint64_t)value2[4] << 8) | ((uint64_t)value2[5]);
126 :
127 1003 : if (t1 == t2) return memcmp(value1, value2, UUID_LEN);
128 1 : return (t1 > t2) ? 1 : -1;
129 1003 : }
130 :
131 : // MARK: - General -
132 :
133 30720 : char *cloudsync_string_ndup_v2 (const char *str, size_t len, bool lowercase) {
134 30720 : if (str == NULL) return NULL;
135 :
136 30718 : char *s = (char *)cloudsync_memory_alloc((uint64_t)(len + 1));
137 30718 : if (!s) return NULL;
138 :
139 30718 : if (lowercase) {
140 : // convert each character to lowercase and copy it to the new string
141 12198 : for (size_t i = 0; i < len; i++) {
142 10828 : s[i] = (char)tolower(str[i]);
143 10828 : }
144 1370 : } else {
145 29348 : memcpy(s, str, len);
146 : }
147 :
148 : // null-terminate the string
149 30718 : s[len] = '\0';
150 :
151 30718 : return s;
152 30720 : }
153 :
154 3496 : char *cloudsync_string_ndup (const char *str, size_t len) {
155 3496 : return cloudsync_string_ndup_v2(str, len, false);
156 : }
157 :
158 2 : char *cloudsync_string_ndup_lowercase (const char *str, size_t len) {
159 2 : return cloudsync_string_ndup_v2(str, len, true);
160 : }
161 :
162 25852 : char *cloudsync_string_dup (const char *str) {
163 25852 : return cloudsync_string_ndup_v2(str, (str) ? strlen(str) : 0, false);
164 : }
165 :
166 1370 : char *cloudsync_string_dup_lowercase (const char *str) {
167 1370 : return cloudsync_string_ndup_v2(str, (str) ? strlen(str) : 0, true);
168 : }
169 :
170 8 : int cloudsync_blob_compare(const char *blob1, size_t size1, const char *blob2, size_t size2) {
171 8 : if (size1 != size2) return (size1 > size2) ? 1 : -1; // blobs are different if sizes are different
172 5 : return memcmp(blob1, blob2, size1); // use memcmp for byte-by-byte comparison
173 8 : }
174 :
175 50003 : void cloudsync_rowid_decode (int64_t rowid, int64_t *db_version, int64_t *seq) {
176 : // use unsigned 64-bit integer for intermediate calculations
177 : // when db_version is large enough, it can cause overflow, leading to negative values
178 : // to handle this correctly, we need to ensure the calculations are done in an unsigned 64-bit integer context
179 : // before converting back to int64_t as needed
180 50003 : uint64_t urowid = (uint64_t)rowid;
181 :
182 : // define the bit mask for seq (30 bits)
183 50003 : const uint64_t SEQ_MASK = 0x3FFFFFFF; // (2^30 - 1)
184 :
185 : // extract seq by masking the lower 30 bits
186 50003 : *seq = (int64_t)(urowid & SEQ_MASK);
187 :
188 : // extract db_version by shifting 30 bits to the right
189 50003 : *db_version = (int64_t)(urowid >> 30);
190 50003 : }
191 :
192 2 : char *cloudsync_string_replace_prefix(const char *input, char *prefix, char *replacement) {
193 : //const char *prefix = "sqlitecloud://";
194 : //const char *replacement = "https://";
195 2 : size_t prefix_len = strlen(prefix);
196 2 : size_t replacement_len = strlen(replacement);
197 :
198 2 : if (strncmp(input, prefix, prefix_len) == 0) {
199 : // allocate memory for new string
200 1 : size_t input_len = strlen(input);
201 1 : size_t new_len = input_len - prefix_len + replacement_len;
202 1 : char *result = cloudsync_memory_alloc(new_len + 1); // +1 for null terminator
203 1 : if (!result) return NULL;
204 :
205 : // copy replacement and the rest of the input string
206 1 : strcpy(result, replacement);
207 1 : strcpy(result + replacement_len, input + prefix_len);
208 1 : return result;
209 : }
210 :
211 : // If no match, return the original string
212 1 : return (char *)input;
213 2 : }
214 :
215 : /*
216 : Compute a normalized hash of a CREATE TABLE statement.
217 :
218 : * Normalization:
219 : * - Skips comments (-- and / * )
220 : * - Skips non-printable characters
221 : * - Collapses runs of whitespace to single space
222 : * - Case-insensitive outside quotes
223 : * - Preserves quoted string content exactly
224 : * - Handles escaped quotes
225 : * - Trims trailing spaces and semicolons from the effective hash
226 : */
227 291 : uint64_t fnv1a_hash (const char *data, size_t len) {
228 291 : uint64_t h = FNV_OFFSET_BASIS;
229 291 : int q = 0; // quote state: 0 / '\'' / '"'
230 291 : int cmt = 0; // comment state: 0 / 1=line / 2=block
231 291 : int last_space = 1; // prevent leading space
232 291 : uint64_t h_final = h; // hash state after last non-space, non-semicolon char
233 :
234 70259 : for (size_t i = 0; i < len; i++) {
235 69968 : int c = data[i];
236 69968 : int next = (i + 1 < len) ? data[i + 1] : 0;
237 :
238 : // detect start of comments
239 69968 : if (!q && !cmt && c == '-' && next == '-') {cmt = 1; i += 1; continue;}
240 69967 : if (!q && !cmt && c == '/' && next == '*') {cmt = 2; i += 1; continue;}
241 :
242 : // skip comments
243 69966 : if (cmt == 1) {if (c == '\n') cmt = 0; continue;}
244 69958 : if (cmt == 2) {if (c == '*' && next == '/') { cmt = 0; i += 1; } continue;}
245 :
246 : // handle quotes
247 69948 : if (c == '\'' || c == '"') {
248 1398 : if (q == c) {
249 439 : if (next == c) {HASH_CHAR(c); i += 1; continue;}
250 439 : q = 0;
251 1398 : } else if (!q) q = c;
252 1398 : HASH_CHAR(c);
253 1398 : last_space = 0;
254 1398 : continue;
255 : }
256 :
257 : // inside quote → hash exactly
258 68550 : if (q) {HASH_CHAR(c); last_space = 0; continue;}
259 :
260 : // skip non-printable
261 24398 : if (!isprint((unsigned char)c)) continue;
262 :
263 : // whitespace normalization
264 24382 : if (isspace((unsigned char)c)) {
265 : // look ahead to next non-space, non-comment char
266 2049 : size_t j = i + 1;
267 2059 : while (j < len && isspace((unsigned char)data[j])) j++;
268 :
269 2049 : int next_c = (j < len) ? data[j] : 0;
270 :
271 : // if next char is punctuation where space is irrelevant → skip space
272 2049 : if (next_c == '(' || next_c == ')' || next_c == ',' || next_c == ';' || next_c == 0) {
273 : // skip inserting space
274 8 : last_space = 1;
275 8 : continue;
276 : }
277 :
278 : // else, insert one space
279 2041 : if (!last_space) {HASH_CHAR(' '); last_space = 1;}
280 2041 : continue;
281 : }
282 :
283 : // skip semicolons at end
284 22333 : if (c == ';') {last_space = 1; continue;}
285 :
286 : // normal visible char
287 22333 : HASH_CHAR(tolower(c));
288 22333 : last_space = 0;
289 22333 : }
290 :
291 291 : return h_final;
292 : }
293 :
294 : // MARK: - Files -
295 :
296 : #ifdef CLOUDSYNC_DESKTOP_OS
297 :
298 0 : bool cloudsync_file_delete (const char *path) {
299 : #ifdef _WIN32
300 : return DeleteFileA(path);
301 : #else
302 0 : return (unlink(path) == 0);
303 : #endif
304 : }
305 :
306 0 : static bool cloudsync_file_read_all (int fd, char *buf, size_t n) {
307 0 : size_t off = 0;
308 0 : while (off < n) {
309 : #ifdef _WIN32
310 : int r = _read(fd, buf + off, (unsigned)(n - off));
311 : if (r <= 0) return false;
312 : #else
313 0 : ssize_t r = read(fd, buf + off, n - off);
314 0 : if (r < 0) {
315 0 : if (errno == EINTR) continue;
316 0 : return false;
317 : }
318 0 : if (r == 0) return false; // unexpected EOF
319 : #endif
320 0 : off += (size_t)r;
321 : }
322 0 : return true;
323 0 : }
324 :
325 0 : char *cloudsync_file_read (const char *path, int64_t *len) {
326 0 : int fd = -1;
327 0 : char *buffer = NULL;
328 :
329 : #ifdef _WIN32
330 : fd = _open(path, _O_RDONLY | _O_BINARY);
331 : #else
332 0 : fd = open(path, O_RDONLY);
333 : #endif
334 0 : if (fd < 0) goto abort_read;
335 :
336 : // Get size after open to reduce TOCTTOU
337 : #ifdef _WIN32
338 : struct _stat64 st;
339 : if (_fstat64(fd, &st) != 0 || st.st_size < 0) goto abort_read;
340 : int64_t isz = st.st_size;
341 : #else
342 : struct stat st;
343 0 : if (fstat(fd, &st) != 0 || st.st_size < 0) goto abort_read;
344 0 : int64_t isz = st.st_size;
345 : #endif
346 :
347 0 : size_t sz = (size_t)isz;
348 : // optional: guard against huge files that don't fit in size_t
349 0 : if ((int64_t)sz != isz) goto abort_read;
350 :
351 0 : buffer = (char *)cloudsync_memory_alloc(sz + 1);
352 0 : if (!buffer) goto abort_read;
353 0 : buffer[sz] = '\0';
354 :
355 0 : if (!cloudsync_file_read_all(fd, buffer, sz)) goto abort_read;
356 0 : if (len) *len = sz;
357 :
358 0 : file_close(fd);
359 0 : return buffer;
360 :
361 : abort_read:
362 : //fprintf(stderr, "file_read: failed to read '%s': %s\n", path, strerror(errno));
363 0 : if (len) *len = -1;
364 0 : if (buffer) cloudsync_memory_free(buffer);
365 0 : if (fd >= 0) file_close(fd);
366 0 : return NULL;
367 0 : }
368 :
369 0 : int cloudsync_file_create (const char *path) {
370 : #ifdef _WIN32
371 : int flags = _O_WRONLY | _O_CREAT | _O_TRUNC | _O_BINARY;
372 : int mode = _S_IWRITE; // Windows ignores most POSIX perms
373 : return _open(path, flags, mode);
374 : #else
375 0 : int flags = O_WRONLY | O_CREAT | O_TRUNC;
376 0 : mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
377 0 : return open(path, flags, mode);
378 : #endif
379 : }
380 :
381 0 : static bool cloudsync_file_write_all (int fd, const char *buf, size_t n) {
382 0 : size_t off = 0;
383 0 : while (off < n) {
384 : #ifdef _WIN32
385 : int w = _write(fd, buf + off, (unsigned)(n - off));
386 : if (w <= 0) return false;
387 : #else
388 0 : ssize_t w = write(fd, buf + off, n - off);
389 0 : if (w < 0) {
390 0 : if (errno == EINTR) continue;
391 0 : return false;
392 : }
393 0 : if (w == 0) return false;
394 : #endif
395 0 : off += (size_t)w;
396 : }
397 0 : return true;
398 0 : }
399 :
400 0 : bool cloudsync_file_write (const char *path, const char *buffer, size_t len) {
401 0 : int fd = cloudsync_file_create(path);
402 0 : if (fd < 0) return false;
403 :
404 0 : bool res = cloudsync_file_write_all(fd, buffer, len);
405 :
406 0 : file_close(fd);
407 0 : return res;
408 0 : }
409 :
410 : #endif
411 :
412 : // MARK: - Memory Debugger -
413 :
414 : #if CLOUDSYNC_DEBUG_MEMORY
415 : #include <execinfo.h>
416 : #include <inttypes.h>
417 : #include <assert.h>
418 :
419 : #include "khash.h"
420 : KHASH_MAP_INIT_INT64(HASHTABLE_INT64_VOIDPTR, void*)
421 :
422 : #define STACK_DEPTH 128
423 : #define BUILD_ERROR(...) char current_error[1024]; snprintf(current_error, sizeof(current_error), __VA_ARGS__)
424 : #define BUILD_STACK(v1,v2) size_t v1; char **v2 = _ptr_stacktrace(&v1)
425 :
426 : typedef struct {
427 : void *ptr;
428 : size_t size;
429 : bool deleted;
430 : size_t nrealloc;
431 :
432 : // record where it has been allocated/reallocated
433 : size_t nframe;
434 : char **frames;
435 :
436 : // record where it has been freed
437 : size_t nframe2;
438 : char **frames2;
439 : } mem_slot;
440 :
441 : static void memdebug_report (char *str, char **stack, size_t nstack, mem_slot *slot);
442 :
443 : static khash_t(HASHTABLE_INT64_VOIDPTR) *htable;
444 : static uint64_t nalloc, nrealloc, nfree, mem_current, mem_max;
445 :
446 : static void *_ptr_lookup (void *ptr) {
447 : khiter_t k = kh_get(HASHTABLE_INT64_VOIDPTR, htable, (int64_t)ptr);
448 : void *result = (k == kh_end(htable)) ? NULL : (void *)kh_value(htable, k);
449 : return result;
450 : }
451 :
452 : static bool _ptr_insert (void *ptr, mem_slot *slot) {
453 : int err = 0;
454 : khiter_t k = kh_put(HASHTABLE_INT64_VOIDPTR, htable, (int64_t)ptr, &err);
455 : if (err != -1) kh_value(htable, k) = (void *)slot;
456 : return (err != -1);
457 : }
458 :
459 : static char **_ptr_stacktrace (size_t *nframes) {
460 : #if _WIN32
461 : // http://www.codeproject.com/Articles/11132/Walking-the-callstack
462 : // https://spin.atomicobject.com/2013/01/13/exceptions-stack-traces-c/
463 : #else
464 : void *callstack[STACK_DEPTH];
465 : int n = backtrace(callstack, STACK_DEPTH);
466 : char **strs = backtrace_symbols(callstack, n);
467 : *nframes = (size_t)n;
468 : return strs;
469 : #endif
470 : }
471 :
472 : static mem_slot *_ptr_add (void *ptr, size_t size) {
473 : mem_slot *slot = (mem_slot *)calloc(1, sizeof(mem_slot));
474 : assert(slot);
475 :
476 : slot->ptr = ptr;
477 : slot->size = size;
478 : slot->frames = _ptr_stacktrace(&slot->nframe);
479 : bool ok = _ptr_insert(ptr, slot);
480 : assert(ok);
481 :
482 : ++nalloc;
483 : mem_current += size;
484 : if (mem_current > mem_max) mem_max = mem_current;
485 :
486 : return slot;
487 : }
488 :
489 : static void _ptr_remove (void *ptr) {
490 : mem_slot *slot = (mem_slot *)_ptr_lookup(ptr);
491 : if (!slot) {
492 : BUILD_ERROR("Unable to find old pointer to free.");
493 : memdebug_report(current_error, NULL, 0, NULL);
494 : return;
495 : }
496 :
497 : if (slot->deleted) {
498 : BUILD_ERROR("Pointer already freed.");
499 : BUILD_STACK(n, stack);
500 : memdebug_report(current_error, stack, n, slot);
501 : }
502 :
503 : size_t old_size = slot->size;
504 : slot->deleted = true;
505 : slot->frames2 = _ptr_stacktrace(&slot->nframe2);
506 :
507 : ++nfree;
508 : mem_current -= old_size;
509 : }
510 :
511 : static void _ptr_replace (void *old_ptr, void *new_ptr, size_t new_size) {
512 : if (old_ptr == NULL) {
513 : _ptr_add(new_ptr, new_size);
514 : return;
515 : }
516 :
517 : // remove old ptr (implicit free performed by realloc)
518 : _ptr_remove(old_ptr);
519 :
520 : // add newly allocated prt (implicit alloc performed by realloc)
521 : mem_slot *slot = _ptr_add(new_ptr, new_size);
522 : ++slot->nrealloc;
523 :
524 : ++nrealloc;
525 : if (mem_current > mem_max) mem_max = mem_current;
526 : }
527 :
528 : // MARK: -
529 :
530 : static bool stacktrace_is_internal(const char *s) {
531 : static const char *reserved[] = {"??? ", "libdyld.dylib ", "memdebug_", "_ptr_", NULL};
532 :
533 : const char **r = reserved;
534 : while (*r) {
535 : if (strstr(s, *r)) return true;
536 : ++r;
537 : }
538 : return false;
539 : }
540 :
541 : static void memdebug_report (char *str, char **stack, size_t nstack, mem_slot *slot) {
542 : printf("%s\n", str);
543 : for (size_t i=0; i<nstack; ++i) {
544 : if (stacktrace_is_internal(stack[i])) continue;
545 : printf("%s\n", stack[i]);
546 : }
547 :
548 : if (slot) {
549 : printf("\nallocated:\n");
550 : for (size_t i=0; i<slot->nframe; ++i) {
551 : if (stacktrace_is_internal(slot->frames[i])) continue;
552 : printf("%s\n", slot->frames[i]);
553 : }
554 :
555 : printf("\nfreed:\n");
556 : for (size_t i=0; i<slot->nframe2; ++i) {
557 : if (stacktrace_is_internal(slot->frames2[i])) continue;
558 : printf("%s\n", slot->frames2[i]);
559 : }
560 : }
561 : }
562 :
563 : void memdebug_init (int once) {
564 : if (htable == NULL) htable = kh_init(HASHTABLE_INT64_VOIDPTR);
565 : }
566 :
567 : void memdebug_finalize (void) {
568 : printf("\n========== MEMORY STATS ==========\n");
569 : printf("Allocations count: %" PRIu64 "\n", nalloc);
570 : printf("Reallocations count: %" PRIu64 "\n", nrealloc);
571 : printf("Free count: %" PRIu64 "\n", nfree);
572 : printf("Leaked: %" PRIu64 " (bytes)\n", mem_current);
573 : printf("Max memory usage: %" PRIu64 " (bytes)\n", mem_max);
574 : printf("==================================\n\n");
575 :
576 : if (mem_current > 0) {
577 : printf("\n========== LEAKS DETAILS ==========\n");
578 :
579 : khiter_t k;
580 : for (k = kh_begin(htable); k != kh_end(htable); ++k) {
581 : if (kh_exist(htable, k)) {
582 : mem_slot *slot = (mem_slot *)kh_value(htable, k);
583 : if ((!slot->ptr) || (slot->deleted)) continue;
584 :
585 : printf("Block %p size: %zu (reallocated %zu)\n", slot->ptr, slot->size, slot->nrealloc);
586 : printf("Call stack:\n");
587 : printf("===========\n");
588 : for (size_t j=0; j<slot->nframe; ++j) {
589 : if (stacktrace_is_internal(slot->frames[j])) continue;
590 : printf("%s\n", slot->frames[j]);
591 : }
592 : printf("===========\n\n");
593 : }
594 : }
595 : }
596 : }
597 :
598 : void *memdebug_alloc (uint64_t size) {
599 : void *ptr = dbmem_alloc(size);
600 : if (!ptr) {
601 : BUILD_ERROR("Unable to allocated a block of %" PRIu64" bytes", size);
602 : BUILD_STACK(n, stack);
603 : memdebug_report(current_error, stack, n, NULL);
604 : return NULL;
605 : }
606 : _ptr_add(ptr, size);
607 : return ptr;
608 : }
609 :
610 : void *memdebug_zeroalloc (uint64_t size) {
611 : void *ptr = memdebug_alloc(size);
612 : if (!ptr) return NULL;
613 :
614 : memset(ptr, 0, (size_t)size);
615 : return ptr;
616 : }
617 :
618 : void *memdebug_realloc (void *ptr, uint64_t new_size) {
619 : if (!ptr) return memdebug_alloc(new_size);
620 :
621 : mem_slot *slot = _ptr_lookup(ptr);
622 : if (!slot) {
623 : BUILD_ERROR("Pointer being reallocated was now previously allocated.");
624 : BUILD_STACK(n, stack);
625 : memdebug_report(current_error, stack, n, NULL);
626 : return NULL;
627 : }
628 :
629 : void *back_ptr = ptr;
630 : void *new_ptr = dbmem_realloc(ptr, new_size);
631 : if (!new_ptr) {
632 : BUILD_ERROR("Unable to reallocate a block of %" PRIu64 " bytes.", new_size);
633 : BUILD_STACK(n, stack);
634 : memdebug_report(current_error, stack, n, slot);
635 : return NULL;
636 : }
637 :
638 : _ptr_replace(back_ptr, new_ptr, new_size);
639 : return new_ptr;
640 : }
641 :
642 : char *memdebug_vmprintf (const char *format, va_list list) {
643 : char *ptr = dbmem_vmprintf(format, list);
644 : if (!ptr) {
645 : BUILD_ERROR("Unable to allocated for dbmem_vmprintf with format %s", format);
646 : BUILD_STACK(n, stack);
647 : memdebug_report(current_error, stack, n, NULL);
648 : return NULL;
649 : }
650 :
651 : _ptr_add(ptr, dbmem_size(ptr));
652 : return ptr;
653 : }
654 :
655 : char *memdebug_mprintf(const char *format, ...) {
656 : va_list ap;
657 : char *z;
658 :
659 : va_start(ap, format);
660 : z = memdebug_vmprintf(format, ap);
661 : va_end(ap);
662 :
663 : return z;
664 : }
665 :
666 : uint64_t memdebug_msize (void *ptr) {
667 : return dbmem_size(ptr);
668 : }
669 :
670 : void memdebug_free (void *ptr) {
671 : if (!ptr) {
672 : BUILD_ERROR("Trying to deallocate a NULL ptr.");
673 : BUILD_STACK(n, stack);
674 : memdebug_report(current_error, stack, n, NULL);
675 : }
676 :
677 : // ensure ptr has been previously allocated by malloc, calloc or realloc and not yet freed with free
678 : mem_slot *slot = _ptr_lookup(ptr);
679 :
680 : if (!slot) {
681 : BUILD_ERROR("Pointer being freed was not previously allocated.");
682 : BUILD_STACK(n, stack);
683 : memdebug_report(current_error, stack, n, NULL);
684 : return;
685 : }
686 :
687 : if (slot->deleted) {
688 : BUILD_ERROR("Pointer already freed.");
689 : BUILD_STACK(n, stack);
690 : memdebug_report(current_error, stack, n, slot);
691 : return;
692 : }
693 :
694 : _ptr_remove(ptr);
695 : dbmem_free(ptr);
696 : }
697 :
698 : #endif
|