| 1 |
#ifndef ALLOC_DEBUG_H__ |
| 2 |
#define ALLOC_DEBUG_H__ |
| 3 |
|
| 4 |
#include <stdio.h> |
| 5 |
#include <stdlib.h> |
| 6 |
|
| 7 |
|
| 8 |
/* |
| 9 |
* Simplistic macros to help finding `hot spots'/bugs in memory handling. |
| 10 |
* Require that MALLOC(x) is never a single statement following an if(), |
| 11 |
* else() etc. without braces, and never in the middle of a variable |
| 12 |
* declarations list. |
| 13 |
*/ |
| 14 |
|
| 15 |
/* #define DEBUG_MEMORY_HANDLING */ |
| 16 |
|
| 17 |
#ifdef DEBUG_MEMORY_HANDLING |
| 18 |
#define XMALLOC(x,y) ( \ |
| 19 |
fprintf(stderr, "=== %s:%d: malloc(%p (%s), %d)\n", __FILE__, __LINE__, x, #x, y), \ |
| 20 |
xmalloc(y)) |
| 21 |
#define XREALLOC(x,y) ( \ |
| 22 |
fprintf(stderr, "=== %s:%d: realloc(%p (%s), %d)\n", __FILE__, __LINE__, x, #x, y), \ |
| 23 |
xrealloc(x, y)) |
| 24 |
#define FREE(x) ( \ |
| 25 |
fprintf(stderr, "=== %s:%d: free(%p (%s))\n", __FILE__, __LINE__, x, #x), \ |
| 26 |
free(x)) |
| 27 |
#else |
| 28 |
#define XMALLOC(x,y) xmalloc(y) |
| 29 |
#define XREALLOC(x,y) xrealloc(x,y) |
| 30 |
#define FREE(x) free(x) |
| 31 |
#endif |
| 32 |
|
| 33 |
#endif /* ALLOC_DEBUG_H__ */ |