summaryrefslogtreecommitdiffstats
path: root/c/malloc_hook.c
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2017-02-21 12:04:57 +0100
committerJérémy Zurcher <jeremy@asynk.ch>2017-02-21 12:04:57 +0100
commit260131c35f7f1b68b66e5b29916ec05a38c4ca56 (patch)
treee00a2df2f76475ceaf56e9fbdb49757d4159c763 /c/malloc_hook.c
parentc3892aedf180a9fbc0742f5e1829346985c4a6b0 (diff)
downloadshare-260131c35f7f1b68b66e5b29916ec05a38c4ca56.zip
share-260131c35f7f1b68b66e5b29916ec05a38c4ca56.tar.gz
c : add bit_array, bit_compose, malloc_hook
Diffstat (limited to 'c/malloc_hook.c')
-rw-r--r--c/malloc_hook.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/c/malloc_hook.c b/c/malloc_hook.c
new file mode 100644
index 0000000..aa22379
--- /dev/null
+++ b/c/malloc_hook.c
@@ -0,0 +1,37 @@
+#include <stdlib.h>
+#include <stdio.h>
+
+#define __USE_GNU
+#include <dlfcn.h>
+
+typedef void* (*real_malloc_t) (size_t);
+
+static real_malloc_t real_malloc = NULL;
+
+static void __mtrace_init(void)
+{
+ real_malloc = (real_malloc_t) dlsym(RTLD_NEXT, "malloc");
+ if (NULL == real_malloc) {
+ fprintf(stderr, "Error in `dlsym`: %s\n", dlerror());
+ return;
+ }
+}
+
+void *malloc(size_t size)
+{
+ if(real_malloc == NULL)
+ __mtrace_init();
+
+ void *p = NULL;
+ fprintf(stderr, "malloc(%zu) = ", size);
+ p = real_malloc(size);
+ fprintf(stderr, "%p\n", p);
+ return p;
+}
+
+
+int main(int argc, char **argv, char **env)
+{
+ void * nothing = malloc(666);
+ free(nothing);
+}