1 /* alloc-srvx.c - Debug allocation wrapper
2 * Copyright 2005 srvx Development Team
4 * This file is part of srvx.
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.
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.
22 /* cookies for anybody who recognizes these bytes without help :) */
23 #define ALLOC_MAGIC 0x1acf
24 #define FREE_MAGIC 0xfc1d
25 const char redzone[] = { '\x03', '\x47', '\x76', '\xc7' };
28 unsigned int file_id : 8;
29 unsigned int size : 24;
30 unsigned int line : 16;
31 unsigned int magic : 16;
34 static char file_id_map[256][32];
35 static unsigned int file_ids_used;
36 unsigned long alloc_count, alloc_size;
39 file_id_cmp(const void *a_, const void *b_)
41 return strcmp(a_, b_);
45 get_file_id(const char *fname)
49 entry = bsearch(fname, file_id_map, file_ids_used, sizeof(file_id_map[0]), file_id_cmp);
51 return ((char*)entry - file_id_map[0]) / sizeof(file_id_map[0]);
52 strcpy(file_id_map[file_ids_used++], fname);
53 qsort(file_id_map, file_ids_used, sizeof(file_id_map[0]), file_id_cmp);
54 return file_ids_used - 1;
58 srvx_malloc(const char *file, unsigned int line, size_t size)
60 struct alloc_header *block;
62 block = malloc(sizeof(*block) + size + sizeof(redzone));
63 assert(block != NULL);
64 memset(block, 0, sizeof(*block) + size);
65 memcpy((char*)(block + 1) + size, redzone, sizeof(redzone));
66 block->file_id = get_file_id(file);
69 block->magic = ALLOC_MAGIC;
76 srvx_realloc(const char *file, unsigned int line, void *ptr, size_t size)
78 struct alloc_header *block = NULL, *newblock;
81 block = (struct alloc_header *)ptr - 1;
82 assert(block->magic == ALLOC_MAGIC);
83 assert(0 == memcmp((char*)(block + 1) + block->size, redzone, sizeof(redzone)));
84 if (block->size >= size)
88 newblock = malloc(sizeof(*newblock) + size + sizeof(redzone));
89 assert(newblock != NULL);
90 memset(newblock, 0, sizeof(*newblock) + size + sizeof(redzone));
91 memcpy((char*)(newblock + 1) + size, redzone, sizeof(redzone));
92 newblock->file_id = get_file_id(file);
93 newblock->line = line;
94 newblock->size = size;
95 newblock->magic = ALLOC_MAGIC;
100 memcpy(newblock + 1, block + 1, block->size);
101 size = block->size + sizeof(*block);
102 memset(block, 0, size);
103 block->magic = FREE_MAGIC;
106 alloc_size -= size - sizeof(*block);
113 srvx_strdup(const char *file, unsigned int line, const char *src)
118 len = strlen(src) + 1;
119 target = srvx_malloc(file, line, len);
120 memcpy(target, src, len);
125 srvx_free(const char *file, unsigned int line, void *ptr)
127 struct alloc_header *block;
132 block = (struct alloc_header *)ptr - 1;
133 assert(block->magic == ALLOC_MAGIC);
134 assert(0 == memcmp((char*)(block + 1) + block->size, redzone, sizeof(redzone)));
135 size = block->size + sizeof(*block);
136 memset(block, 0, size);
137 block->magic = FREE_MAGIC;
140 alloc_size -= size - sizeof(*block);
141 (void)file; (void)line;