LCOV - code coverage report
Current view: top level - src - utils.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 66.9 % 169 113
Test Date: 2026-03-16 13:50:46 Functions: 68.4 % 19 13

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

Generated by: LCOV version 2.4-0