1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
/* this code is stolen from the links browser, it was written by Mikulas Patocka */
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>
#include "config.h"
#include "cfg.h"
#include "error.h"
struct memory_list {
int n;
void *p[1];
};
struct list_head {
void *next;
void *prev;
};
#ifndef HAVE_TYPEOF
struct xlist_head {
struct xlist_head *next;
struct xlist_head *prev;
};
#endif
#define del_from_list(x) {((struct list_head *)(x)->next)->prev=(x)->prev; ((struct list_head *)(x)->prev)->next=(x)->next;}
#define add_at_pos(p,x) do {(x)->next=(p)->next; (x)->prev=(p); (p)->next=(x); (x)->next->prev=(x);} while(0)
#ifdef HAVE_TYPEOF
#define foreach(e,l) for ((e)=(l).next; (e)!=(typeof(e))&(l); (e)=(e)->next)
#define add_to_list(l,x) add_at_pos((typeof(x))&(l),(x))
#else
#define foreach(e,l) for ((e)=(l).next; (e)!=(void *)&(l); (e)=(e)->next)
#define add_to_list(l,x) add_at_pos((struct xlist_head *)&(l),(struct xlist_head *)(x))
#endif
/* inline */
#ifdef LEAK_DEBUG
extern long mem_amount;
extern long last_mem_amount;
#define ALLOC_MAGIC 0xa110c
#define ALLOC_FREE_MAGIC 0xf4ee
#define ALLOC_REALLOC_MAGIC 0x4ea110c
#ifndef LEAK_DEBUG_LIST
struct alloc_header {
int magic;
int size;
};
#else
struct alloc_header {
struct alloc_header *next;
struct alloc_header *prev;
int magic;
int size;
int line;
unsigned char *file;
unsigned char *comment;
};
#endif
#define L_D_S ((sizeof(struct alloc_header) + 7) & ~7)
#endif
/*-----------------------------------------------------------------------------*/
void do_not_optimize_here(void *p)
{
p=p;
/* stop GCC optimization - avoid bugs in it */
}
#ifdef LEAK_DEBUG
long mem_amount = 0;
long last_mem_amount = -1;
#ifdef LEAK_DEBUG_LIST
struct list_head memory_list = { &memory_list, &memory_list };
#endif
#endif
static inline void force_dump(void)
{
fprintf(stderr, "\n\033[1m%s\033[0m\n", "Forcing core dump");
fflush(stderr);
raise(SIGSEGV);
}
void check_memory_leaks(void)
{
#ifdef LEAK_DEBUG
if (mem_amount) {
fprintf(stderr, "\n\033[1mMemory leak by %ld bytes\033[0m\n", mem_amount);
#ifdef LEAK_DEBUG_LIST
fprintf(stderr, "\nList of blocks: ");
{
int r = 0;
struct alloc_header *ah;
foreach (ah, memory_list) {
fprintf(stderr, "%s%p:%d @ %s:%d", r ? ", ": "", (char *)ah + L_D_S, ah->size, ah->file, ah->line), r = 1;
if (ah->comment) fprintf(stderr, ":\"%s\"", ah->comment);
}
fprintf(stderr, "\n");
}
#endif
force_dump();
}
#endif
}
void er(int b, unsigned char *m, va_list l)
{
if (b) fprintf(stderr, "%c", (char)7);
vfprintf(stderr, m, l);
fprintf(stderr, "\n");
sleep(1);
}
void error(unsigned char *m, ...)
{
va_list l;
va_start(l, m);
er(1, m, l);
}
int errline;
unsigned char *errfile;
unsigned char errbuf[4096];
void int_error(unsigned char *m, ...)
{
va_list l;
va_start(l, m);
sprintf(errbuf, "\033[1mINTERNAL ERROR\033[0m at %s:%d: ", errfile, errline);
strcat(errbuf, m);
er(1, errbuf, l);
force_dump();
}
void debug_msg(unsigned char *m, ...)
{
va_list l;
va_start(l, m);
sprintf(errbuf, "DEBUG MESSAGE at %s:%d: ", errfile, errline);
strcat(errbuf, m);
er(0, errbuf, l);
}
#ifdef LEAK_DEBUG
void *debug_mem_alloc(unsigned char *file, int line, size_t size)
{
void *p;
#ifdef LEAK_DEBUG
struct alloc_header *ah;
#endif
if (!size) return DUMMY;
#ifdef LEAK_DEBUG
mem_amount += size;
size += L_D_S;
#endif
if (!(p = malloc(size))) {
error("ERROR: out of memory (malloc returned NULL)\n");
return NULL;
}
#ifdef LEAK_DEBUG
ah = p;
p = (char *)p + L_D_S;
ah->size = size - L_D_S;
ah->magic = ALLOC_MAGIC;
#ifdef LEAK_DEBUG_LIST
ah->file = file;
ah->line = line;
ah->comment = NULL;
add_to_list(memory_list, ah);
#endif
#endif
memset(p, 'A', size - L_D_S);
return p;
}
void *debug_mem_calloc(unsigned char *file, int line, size_t size)
{
void *p;
#ifdef LEAK_DEBUG
struct alloc_header *ah;
#endif
if (!size) return DUMMY;
#ifdef LEAK_DEBUG
mem_amount += size;
size += L_D_S;
#endif
if (!(p = x_calloc(size))) {
error("ERROR: out of memory (calloc returned NULL)\n");
return NULL;
}
#ifdef LEAK_DEBUG
ah = p;
p = (char *)p + L_D_S;
ah->size = size - L_D_S;
ah->magic = ALLOC_MAGIC;
#ifdef LEAK_DEBUG_LIST
ah->file = file;
ah->line = line;
ah->comment = NULL;
add_to_list(memory_list, ah);
#endif
#endif
return p;
}
void debug_mem_free(unsigned char *file, int line, void *p)
{
#ifdef LEAK_DEBUG
struct alloc_header *ah;
#endif
if (p == DUMMY) return;
if (!p) {
errfile = file, errline = line, int_error("mem_free(NULL)");
return;
}
#ifdef LEAK_DEBUG
p = (char *)p - L_D_S;
ah = p;
if (ah->magic != ALLOC_MAGIC) {
errfile = file, errline = line, int_error("mem_free: magic doesn't match: %08x", ah->magic);
return;
}
ah->magic = ALLOC_FREE_MAGIC;
#ifdef LEAK_DEBUG_LIST
del_from_list(ah);
if (ah->comment) free(ah->comment);
#endif
mem_amount -= ah->size;
#endif
free(p);
}
void *debug_mem_realloc(unsigned char *file, int line, void *p, size_t size)
{
#ifdef LEAK_DEBUG
struct alloc_header *ah;
#endif
if (p == DUMMY) return debug_mem_alloc(file, line, size);
if (!p) {
errfile = file, errline = line, int_error("mem_realloc(NULL, %d)", size);
return NULL;
}
if (!size) {
debug_mem_free(file, line, p);
return DUMMY;
}
#ifdef LEAK_DEBUG
p = (char *)p - L_D_S;
ah = p;
if (ah->magic != ALLOC_MAGIC) {
errfile = file, errline = line, int_error("mem_realloc: magic doesn't match: %08x", ah->magic);
return NULL;
}
ah->magic = ALLOC_REALLOC_MAGIC;
#endif
if (!(p = realloc(p, size + L_D_S))) {
error("ERROR: out of memory (realloc returned NULL)\n");
return NULL;
}
#ifdef LEAK_DEBUG
ah = p;
mem_amount += size - ah->size;
ah->size = size;
ah->magic = ALLOC_MAGIC;
#ifdef LEAK_DEBUG_LIST
ah->prev->next = ah;
ah->next->prev = ah;
#endif
#endif
return (char *)p + L_D_S;
}
void set_mem_comment(void *p, unsigned char *c, int l)
{
#ifdef LEAK_DEBUG_LIST
struct alloc_header *ah = (struct alloc_header *)((char *)p - L_D_S);
if (ah->comment) free(ah->comment);
if ((ah->comment = malloc(l + 1))) memcpy(ah->comment, c, l), ah->comment[l] = 0;
#endif
}
#endif
|