Skip to content

Instantly share code, notes, and snippets.

@mafintosh
Last active April 7, 2022 19:25
Show Gist options
  • Save mafintosh/e52b32d5f7a7e7cff30d327d3ad70707 to your computer and use it in GitHub Desktop.
Save mafintosh/e52b32d5f7a7e7cff30d327d3ad70707 to your computer and use it in GitHub Desktop.
#include <dlfcn.h>
static int mallocs_active = 0;
void *malloc(size_t size) {
void * (*real_malloc)(size_t);
real_malloc = dlsym(RTLD_NEXT, "malloc");
mallocs_active++;
void *ptr = real_malloc(size);
fprintf(stderr, "DEBUG: malloc(%zu) = %p, pointers=%i\n", size, ptr, mallocs_active);
return ptr;
}
void free(void *ptr) {
void * (*real_free)(void *ptr);
real_free = dlsym(RTLD_NEXT, "free");
mallocs_active--;
fprintf(stderr, "DEBUG: free(%p), pointers=%i\n", ptr, mallocs_active);
real_free(ptr);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment