5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is furnished to do
10 * so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 unsigned long long mem_ab = 0;
28 unsigned long long mem_db = 0;
29 unsigned long long mem_at = 0;
30 unsigned long long mem_dt = 0;
38 void *util_memory_a(unsigned int byte, unsigned int line, const char *file) {
39 struct memblock_t *info = malloc(sizeof(struct memblock_t) + byte);
40 void *data =(void*)((uintptr_t)info+sizeof(struct memblock_t));
41 if (!data) return NULL;
46 util_debug("MEM", "allocation: %08u (bytes) address 0x%08X @ %s:%u\n", byte, data, file, line);
52 void util_memory_d(void *ptrn, unsigned int line, const char *file) {
54 void *data = (void*)((uintptr_t)ptrn-sizeof(struct memblock_t));
55 struct memblock_t *info = (struct memblock_t*)data;
57 util_debug("MEM", "released: %08u (bytes) address 0x%08X @ %s:%u\n", info->byte, data, file, line);
64 util_debug("MEM", "Memory information:\n\
65 Total allocations: %llu\n\
66 Total deallocations: %llu\n\
67 Total allocated: %llu (bytes)\n\
68 Total deallocated: %llu (bytes)\n",
75 #define mem_d(x) util_memory_d((x), __LINE__, __FILE__)
78 #define mem_a(x) util_memory_a((x), __LINE__, __FILE__)
82 * Some string utility functions, because strdup uses malloc, and we want
83 * to track all memory (without replacing malloc).
85 char *util_strdup(const char *s) {
104 * Remove quotes from a string, escapes from \ in string
105 * as well. This function shouldn't be used to create a
106 * char array that is later freed (it uses pointer arith)
108 char *util_strrq(char *s) {
112 while ((chr = *src++) != '\0') {
115 if ((chr = *src++) == '\0')
118 } else if (chr != '"')
126 * Remove newline from a string (if it exists). This is
127 * done pointer wise instead of strlen(), and an array
130 char *util_strrnl(char *src) {
131 if (!src) return NULL;
133 while (*cpy && *cpy != '\n')
140 void util_debug(const char *area, const char *ms, ...) {
143 fprintf (stdout, "DEBUG: ");
145 fprintf (stdout, area);
146 fputs ("] ", stdout);
147 vfprintf(stdout, ms, va);
152 * Implements libc getline for systems that don't have it, which is
153 * assmed all. This works the same as getline().
155 int util_getline(char **lineptr, size_t *n, FILE *stream) {
160 if (!lineptr || !n || !stream)
163 if (!(*lineptr = mem_a((*n = 64))))
171 int c = getc(stream);
174 char *tmp = mem_a((*n+=(*n>16)?*n:64));
178 chr = *n + *lineptr - pos;
179 strcpy(tmp,*lineptr);
180 if (!(*lineptr = tmp))
183 pos = *n - chr + *lineptr;
201 return (ret = pos - *lineptr);