]> git.xonotic.org Git - xonotic/gmqcc.git/blob - util.c
Count file lines
[xonotic/gmqcc.git] / util.c
1 /*
2  * Copyright (C) 2012 
3  *      Dale Weiler
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
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
21  * SOFTWARE.
22  */
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdarg.h>
27 #include "gmqcc.h"
28  
29 struct memblock_t {
30         const char  *file;
31         unsigned int line;
32         unsigned int byte;
33 };
34
35 void *util_memory_a(unsigned int byte, unsigned int line, const char *file) {
36         struct memblock_t *data = malloc(sizeof(struct memblock_t) + byte);
37         if (!data) return NULL;
38         data->line = line;
39         data->byte = byte;
40         data->file = file;
41         
42         printf("[MEM] allocation: %08u (bytes) at %s:%u\n", byte, file, line);
43         return (void*)((uintptr_t)data+sizeof(struct memblock_t));
44 }
45
46 void util_memory_d(void *ptrn, unsigned int line, const char *file) {
47         if (!ptrn) return;
48         void              *data = (void*)((uintptr_t)ptrn-sizeof(struct memblock_t));
49         struct memblock_t *info = (struct memblock_t*)data;
50         
51         printf("[MEM] released:   %08u (bytes) at %s:%u\n", info->byte, file, line);
52         free(data);
53 }
54
55 #ifndef mem_d
56 #define mem_d(x) util_memory_d((x), __LINE__, __FILE__)
57 #endif
58 #ifndef mem_a
59 #define mem_a(x) util_memory_a((x), __LINE__, __FILE__)
60 #endif
61
62 /*
63  * Some string utility functions, because strdup uses malloc, and we want
64  * to track all memory (without replacing malloc).
65  */
66 char *util_strdup(const char *s) {
67         size_t  len;
68         char   *ptr;
69         
70         if (!s)
71                 return NULL;
72                 
73         len = strlen(s);
74         ptr = mem_a (len+1);
75         
76         if (ptr && len) {
77                 memcpy(ptr, s, len);
78                 ptr[len] = '\0';
79         }
80         
81         return ptr;
82 }
83
84 void util_debug(const char *ms, ...) {
85         va_list  va;
86         va_start(va, ms);
87         vprintf (ms, va);
88         va_end  (va);
89 }