047cb95db528b2d5035d429bc7b47e62eb90e1fe
[srvx.git] / src / alloc-srvx.c
1 /* alloc-srvx.c - Debug allocation wrapper
2  * Copyright 2005 srvx Development Team
3  *
4  * This file is part of srvx.
5  *
6  * srvx is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  */
16
17 #include "common.h"
18
19 #undef malloc
20 #undef free
21
22 #define ALLOC_MAGIC 0x1acf
23 #define FREE_MAGIC  0xfc1d
24 const char redzone[] = { '\x03', '\x47', '\x76', '\xc7' };
25
26 struct alloc_header {
27     unsigned int file_id : 8;
28     unsigned int size : 24;
29     unsigned int line : 16;
30     unsigned int magic : 16;
31 };
32
33 static char file_id_map[256][32];
34 static unsigned int file_ids_used;
35 unsigned long alloc_count, alloc_size;
36
37 static int
38 file_id_cmp(const void *a_, const void *b_)
39 {
40     return strcmp(a_, b_);
41 }
42
43 static unsigned int
44 get_file_id(const char *fname)
45 {
46     void *entry;
47
48     entry = bsearch(fname, file_id_map, file_ids_used, sizeof(file_id_map[0]), file_id_cmp);
49     if (entry)
50         return ((char*)entry - file_id_map[0]) / sizeof(file_id_map[0]);
51     strcpy(file_id_map[file_ids_used++], fname);
52     qsort(file_id_map, file_ids_used, sizeof(file_id_map[0]), file_id_cmp);
53     return file_ids_used - 1;
54 }
55
56 void *
57 srvx_malloc(const char *file, unsigned int line, size_t size)
58 {
59     struct alloc_header *block;
60
61     block = malloc(sizeof(*block) + size + sizeof(redzone));
62     assert(block != NULL);
63     memset(block, 0, sizeof(*block) + size);
64     memcpy((char*)(block + 1) + size, redzone, sizeof(redzone));
65     block->file_id = get_file_id(file);
66     block->line = line;
67     block->size = size;
68     block->magic = ALLOC_MAGIC;
69     alloc_count++;
70     alloc_size += size;
71     return block + 1;
72 }
73
74 void *
75 srvx_realloc(const char *file, unsigned int line, void *ptr, size_t size)
76 {
77     struct alloc_header *block = NULL, *newblock;
78
79     if (ptr) {
80         block = (struct alloc_header *)ptr - 1;
81         assert(block->magic == ALLOC_MAGIC);
82         assert(0 == memcmp((char*)(block + 1) + block->size, redzone, sizeof(redzone)));
83         if (block->size >= size)
84             return block + 1;
85     }
86
87     newblock = malloc(sizeof(*newblock) + size + sizeof(redzone));
88     assert(newblock != NULL);
89     memset(newblock, 0, sizeof(*newblock) + size + sizeof(redzone));
90     memcpy((char*)(newblock + 1) + size, redzone, sizeof(redzone));
91     newblock->file_id = get_file_id(file);
92     newblock->line = line;
93     newblock->size = size;
94     newblock->magic = ALLOC_MAGIC;
95     alloc_count++;
96     alloc_size += size;
97
98     if (ptr) {
99         memcpy(newblock + 1, block + 1, block->size);
100         size = block->size + sizeof(*block);
101         memset(block, 0, size);
102         block->magic = FREE_MAGIC;
103         free(block);
104         alloc_count--;
105         alloc_size -= size - sizeof(*block);
106     }
107
108     return newblock + 1;
109 }
110
111 char *
112 srvx_strdup(const char *file, unsigned int line, const char *src)
113 {
114     char *target;
115     size_t len;
116
117     len = strlen(src) + 1;
118     target = srvx_malloc(file, line, len);
119     memcpy(target, src, len);
120     return target;
121 }
122
123 void
124 srvx_free(const char *file, unsigned int line, void *ptr)
125 {
126     struct alloc_header *block;
127     size_t size;
128
129     if (!ptr)
130         return;
131     block = (struct alloc_header *)ptr - 1;
132     assert(block->magic == ALLOC_MAGIC);
133     assert(0 == memcmp((char*)(block + 1) + block->size, redzone, sizeof(redzone)));
134     size = block->size;
135     memset(block + 1, 0xde, size);
136     block->magic = FREE_MAGIC;
137     free(block);
138     alloc_count--;
139     alloc_size -= size;
140     (void)file; (void)line;
141 }