summaryrefslogtreecommitdiff
path: root/memdebug
diff options
context:
space:
mode:
authorTobias Klauser <tklauser@distanz.ch>2010-03-05 11:04:40 +0100
committerTobias Klauser <klto@zhaw.ch>2010-03-05 11:04:40 +0100
commita1d2d05ddc38c091e4dc9ff750c0d141995e46dc (patch)
tree5ec32a11e1323661694ac90cd3e1c590ca4b132b /memdebug
parentde1c4780206373f857a877dfed3940de3cbef2c5 (diff)
Add memdebug module, a simple malloc debugger
Diffstat (limited to 'memdebug')
-rw-r--r--memdebug/memdebug.c113
-rw-r--r--memdebug/memdebug.h44
2 files changed, 157 insertions, 0 deletions
diff --git a/memdebug/memdebug.c b/memdebug/memdebug.c
new file mode 100644
index 0000000..9725cb1
--- /dev/null
+++ b/memdebug/memdebug.c
@@ -0,0 +1,113 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+/* define/undef to enable tracing of errors */
+#define MEMDEBUG_TRACE
+/* define/undef to enable tracing of double free errors */
+#define MEMDEBUG_TRACE_DOUBLE_FREE
+
+#include "memdebug.h"
+
+static struct memdebug_heap_item *heap_head = NULL;
+static struct memdebug_heap_item *heap_tail = NULL;
+
+void *__memdebug_malloc(size_t size, const char *func,
+ const char *file, const unsigned int line)
+{
+ void *ret;
+ struct memdebug_heap_item *hi = malloc(sizeof(struct memdebug_heap_item));
+
+ if (!hi)
+ goto out_err;
+
+ ret = malloc(size);
+ if (!ret) {
+ free(hi);
+ goto out_err;
+ }
+
+ hi->next = NULL;
+ hi->addr = ret;
+ hi->size = size;
+ hi->func = func;
+ hi->file = file;
+ hi->line = line;
+#ifdef MEMDEBUG_TRACE_DOUBLE_FREE
+ hi->freed_func = NULL;
+ hi->freed_file = NULL;
+ hi->line = 0;
+#endif
+
+ if (!heap_head)
+ heap_head = hi;
+ if (!heap_tail)
+ heap_tail = hi;
+ else {
+ heap_tail->next = hi;
+ heap_tail = hi;
+ }
+
+ return ret;
+
+out_err:
+ memdebug_trace("malloc failed (%s:%d:%s)", file, line, func);
+ return NULL;
+}
+
+void __memdebug_free(void *ptr, const char *func,
+ const char *file, const unsigned int line)
+{
+ struct memdebug_heap_item *hi, *prev;
+
+ if (!ptr) {
+ memdebug_trace("trying to free NULL pointer (%s:%d:%s)", file, line, func);
+ return;
+ }
+
+ for (hi = heap_head, prev = NULL; hi; prev = hi, hi = hi->next)
+ if (hi->addr ==ptr)
+ break;
+
+ if (!hi) {
+ memdebug_trace("no memory allocated at %p (%s:%d:%s)", ptr, file, line, func);
+ return;
+ }
+
+#ifdef MEMDEBUG_TRACE_DOUBLE_FREE
+ if (hi->freed_func) {
+ memdebug_trace("double free at %p (%s:%d:%s)\nalready freed by %s:%d:%s",
+ ptr, file, line, func, hi->freed_file, hi->freed_line, hi->freed_func);
+ return;
+ }
+
+ hi->freed_func = func;
+ hi->freed_file = file;
+ hi->freed_line = line;
+#else
+ /* Delete the item from the linked list */
+ if (prev)
+ prev->next = hi->next;
+ else
+ heap_head = hi->next;
+ if (heap_tail == hi)
+ heap_tail = prev;
+
+ free(hi);
+#endif
+
+ free(ptr);
+}
+
+void memdebug_report(void)
+{
+ struct memdebug_heap_item *hi;
+ size_t total_size = 0;
+
+ fprintf(stderr, "\n\n*** Reporting allocated memory ***\n");
+ for (hi = heap_head; hi; hi = hi->next) {
+ fprintf(stderr, " %zu bytes at %p (%s:%d:%s)\n", hi->size, hi->addr, hi->file, hi->line, hi->func);
+ total_size += hi->size;
+ }
+ fprintf(stderr, "Total size: %zu bytes\n\n", total_size);
+ fflush(stderr);
+}
diff --git a/memdebug/memdebug.h b/memdebug/memdebug.h
new file mode 100644
index 0000000..d96e356
--- /dev/null
+++ b/memdebug/memdebug.h
@@ -0,0 +1,44 @@
+#ifndef _MEMDEBUG_H_
+#define _MEMDEBUG_H_
+
+#include <stdlib.h>
+
+#ifdef MEMDEBUG_TRACE
+# define memdebug_trace(fmt, args...) \
+ ({ \
+ fprintf(stderr, "\n\n*** " fmt " ***\n\n", ##args); \
+ fflush(stderr); \
+ })
+#else
+# define memdebug_trace(fmt, args...)
+#endif
+
+struct memdebug_heap_item {
+ struct memdebug_heap_item *next;
+
+ void *addr;
+ size_t size;
+
+ const char *func;
+ const char *file;
+ unsigned int line;
+
+#ifdef MEMDEBUG_TRACE_DOUBLE_FREE
+ const char *freed_func;
+ const char *freed_file;
+ unsigned int freed_line;
+#endif
+};
+
+#define memdebug_malloc(size) \
+ __memdebug_malloc(size, __func__, __FILE__, __LINE__)
+#define memdebug_free(ptr) \
+ __memdebug_free(ptr, __func__, __FILE__, __LINE__)
+
+void *__memdebug_malloc(size_t size, const char *func,
+ const char *file, const unsigned int line);
+void __memdebug_free(void *ptr, const char *func,
+ const char *file, const unsigned int line);
+void memdebug_report(void);
+
+#endif /* _MEMDEBUG_H_ */