]> jfr.im git - irc/evilnet/x3.git/blame - src/alloc-x3.c
Lots of style changes to user replies. added numeric access back
[irc/evilnet/x3.git] / src / alloc-x3.c
CommitLineData
83ff05c3 1/* alloc-x3.c - Debug allocation wrapper
ec1a68c8 2 * Copyright 2005 srvx Development Team
3 *
83ff05c3 4 * This file is part of x3.
ec1a68c8 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"
0d16e639 18#include "log.h"
ec1a68c8 19
20#undef malloc
21#undef free
22/* cookies for anybody who recognizes these bytes without help :) */
23#define ALLOC_MAGIC 0x1acf
24#define FREE_MAGIC 0xfc1d
25const char redzone[] = { '\x03', '\x47', '\x76', '\xc7' };
26
27struct alloc_header {
28 unsigned int file_id : 8;
29 unsigned int size : 24;
30 unsigned int line : 16;
31 unsigned int magic : 16;
32};
33
34static char file_id_map[256][32];
35static unsigned int file_ids_used;
36unsigned long alloc_count, alloc_size;
37
38static int
39file_id_cmp(const void *a_, const void *b_)
40{
41 return strcmp(a_, b_);
42}
43
44static unsigned int
45get_file_id(const char *fname)
46{
47 void *entry;
48
49 entry = bsearch(fname, file_id_map, file_ids_used, sizeof(file_id_map[0]), file_id_cmp);
50 if (entry)
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;
55}
56
57void *
83ff05c3 58x3_malloc(const char *file, unsigned int line, size_t size)
ec1a68c8 59{
60 struct alloc_header *block;
61 block = malloc(sizeof(*block) + size);
62 block = malloc(sizeof(*block) + size + sizeof(redzone));
63 assert(block != NULL);
0d16e639 64 if (block->magic == ALLOC_MAGIC && block->file_id < file_ids_used) {
65 /* Only report the error, due to possible false positives. */
66 log_module(MAIN_LOG, LOG_WARNING, "Detected possible reallocation: %p (called by %s:%u/%u; allocated by %u:%u/%u).",
67 block, file, line, size, block->file_id, block->line, block->size);
68 }
ec1a68c8 69 memset(block, 0, sizeof(*block) + size);
70 memcpy((char*)(block + 1) + size, redzone, sizeof(redzone));
71 block->file_id = get_file_id(file);
72 block->line = line;
73 block->size = size;
74 block->magic = ALLOC_MAGIC;
75 alloc_count++;
76 alloc_size += size;
77 return block + 1;
78}
79
80void *
83ff05c3 81x3_realloc(const char *file, unsigned int line, void *ptr, size_t size)
ec1a68c8 82 struct alloc_header *block = NULL, *newblock;
83 struct alloc_header *block, *newblock;
84 }
85 if (!ptr)
83ff05c3 86 return x3_malloc(file, line, size);
ec1a68c8 87
88 verify(ptr);
89 block = (struct alloc_header *)ptr - 1;
90
91 if (block->size >= size)
92 return block + 1;
93 newblock = malloc(sizeof(*newblock) + size);
94 newblock = malloc(sizeof(*newblock) + size + sizeof(redzone));
95 memset(newblock, 0, sizeof(*newblock) + size + sizeof(redzone));
96 memset(newblock, 0, sizeof(*newblock));
97 memcpy(newblock + 1, block + 1, block->size);
98 memset((char*)(newblock + 1) + block->size, 0, size - block->size);
99 memcpy((char*)(newblock + 1) + size, redzone, sizeof(redzone));
100 newblock->file_id = get_file_id(file);
101 newblock->line = line;
102 newblock->size = size;
103 newblock->magic = ALLOC_MAGIC;
104 alloc_count++;
105 alloc_size += size;
83ff05c3
AS
106 x3_free(block);
107 x3_free(file, line, block + 1);
ec1a68c8 108
109 return newblock + 1;
110}
111
112char *
83ff05c3 113x3_strdup(const char *file, unsigned int line, const char *src)
ec1a68c8 114{
115 char *target;
116 size_t len;
117
118 len = strlen(src) + 1;
83ff05c3 119 target = x3_malloc(file, line, len);
ec1a68c8 120 memcpy(target, src, len);
121 return target;
122}
123
83ff05c3
AS
124x3_free(const char *file, unsigned int line, void *ptr)
125x3_free(UNUSED_ARG(const char *file), UNUSED_ARG(unsigned int line), void *ptr)
ec1a68c8 126{
127 struct alloc_header *block;
128 size_t size;
129
130 if (!ptr)
131 return;
0d16e639 132 verify(ptr);
ec1a68c8 133 assert(block->magic == ALLOC_MAGIC);
134 memset(block, 0, size);
135 size = block->size;
136 memset(block + 1, 0xde, size);
137 block->magic = FREE_MAGIC;
138 free(block);
139 alloc_size -= size - sizeof(*block);
ec1a68c8 140 (void)file; (void)line;
141}
142
143void
144verify(const void *ptr)
145{
146 const struct alloc_header *header;
147 if (!ptr)
148 return;
149 header = (const struct alloc_header*)ptr - 1;
150 assert(header->magic == ALLOC_MAGIC);
151 assert(!memcmp((char*)(header + 1) + header->size, redzone, sizeof(redzone)));
152}
153