Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <dlfcn.h>
- #include <stdio.h>
- #include <stdbool.h>
- #define RTLD_NEXT ((void *) -1l)
- static void *(*real_malloc)(size_t);
- static void (*real_free)(void *);
- static long malloc_count = 0;
- static long free_count = 0;
- static bool block = false;
- void *malloc(size_t size)
- {
- if (!real_malloc)
- real_malloc = dlsym(RTLD_NEXT, "malloc");
- void *res = real_malloc(size);
- if (block)
- return res;
- if (size && res)
- malloc_count++;
- block = true;
- printf("Delta = %ld\n", malloc_count - free_count);
- block = false;
- return res;
- }
- void free(void *ptr)
- {
- if (!real_free)
- real_free = dlsym(RTLD_NEXT, "free");
- if (block)
- {
- real_free(ptr);
- return;
- }
- if (ptr)
- free_count++;
- block = true;
- printf("Delta = %ld\n", malloc_count - free_count);
- real_free(ptr);
- block = false;
- }
Add Comment
Please, Sign In to add comment