- /* copy old */
- memcpy(newinfo+1, oldinfo+1, oldinfo->byte);
-
- /* free old */
- if (oldinfo->prev)
- oldinfo->prev->next = oldinfo->next;
- if (oldinfo->next)
- oldinfo->next->prev = oldinfo->prev;
- if (oldinfo == mem_start)
- mem_start = oldinfo->next;
-
- /* fill info */
- newinfo->line = line;
- newinfo->byte = byte;
- newinfo->file = file;
- newinfo->prev = NULL;
- newinfo->next = mem_start;
- if (mem_start)
- mem_start->prev = newinfo;
- mem_start = newinfo;
-
- mem_ab -= oldinfo->byte;
- mem_ab += newinfo->byte;
-
- free(oldinfo);
-
- return newinfo+1;
-}
-
-void util_meminfo() {
- struct memblock_t *info;
-
- if (!opts.memchk)
- return;
-
- for (info = mem_start; info; info = info->next) {
- util_debug("MEM", "lost: % 8u (bytes) at %s:%u\n",
- info->byte,
- info->file,
- info->line);
- }
-
- util_debug("MEM", "Memory information:\n\
- Total allocations: %llu\n\
- Total deallocations: %llu\n\
- Total allocated: %llu (bytes)\n\
- Total deallocated: %llu (bytes)\n\
- Leaks found: lost %llu (bytes) in %d allocations\n",
- mem_at, mem_dt,
- mem_ab, mem_db,
- (mem_ab - mem_db),
- (mem_at - mem_dt)
- );
-}
-
-/*
- * Some string utility functions, because strdup uses malloc, and we want
- * to track all memory (without replacing malloc).
- */
-char *util_strdup(const char *s) {
- size_t len = 0;
- char *ptr = NULL;
-
- if (!s)
- return NULL;
-
- if ((len = strlen(s)) && (ptr = mem_a(len+1))) {
- memcpy(ptr, s, len);
- ptr[len] = '\0';
- }
- return ptr;
-}
-
-/*
- * Remove quotes from a string, escapes from \ in string
- * as well. This function shouldn't be used to create a
- * char array that is later freed (it uses pointer arith)
- */
-char *util_strrq(const char *s) {
- char *dst = (char*)s;
- char *src = (char*)s;
- char chr;
- while ((chr = *src++) != '\0') {
- if (chr == '\\') {
- *dst++ = chr;
- if ((chr = *src++) == '\0')
- break;
- *dst++ = chr;
- } else if (chr != '"')
- *dst++ = chr;
- }
- *dst = '\0';
- return dst;
-}
-
-/*
- * Chops a substring from an existing string by creating a
- * copy of it and null terminating it at the required position.
- */
-char *util_strchp(const char *s, const char *e) {
- const char *c = NULL;
- if (!s || !e)
- return NULL;
-
- c = s;
- while (c != e)
- c++;
-
- return util_strdup(s);
-}
-
-/*
- * Returns true if string is all uppercase, otherwise
- * it returns false.
- */
-bool util_strupper(const char *str) {
- while (*str) {
- if(!isupper(*str))
- return false;
- str++;
- }
- return true;
-}
-
-/*
- * Returns true if string is all digits, otherwise
- * it returns false.
- */
-bool util_strdigit(const char *str) {
- while (*str) {
- if(!isdigit(*str))
- return false;
- str++;
- }
- return true;
-}
-
-bool util_strncmpexact(const char *src, const char *ned, size_t len) {
- return (!strncmp(src, ned, len) && !src[len]);
-}