Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <signal.h>
- #include <dlfcn.h>
- /* compile this object, Link w/ it before libc or smartheap */
- #define BREAK() (kill(getpid(), SIGTRAP))
- void * malloc(size_t size);
- void free(void * ptr);
- void * calloc(size_t num_objs, size_t size);
- void * realloc(void * ptr, size_t new_size);
- long malloc_count=0;
- long free_count=0;
- long free_null_ptr_count=0;
- long calloc_count=0;
- long realloc_count=0;
- long mallocvfree_count=0;
- long malloc_total_bytes=0;
- void *malloc(size_t size)
- {
- static void *( *malloc_ptr) (size_t) = NULL;
- void * ptr;
- if ( malloc_ptr == NULL ) {
- malloc_ptr=dlsym(RTLD_NEXT,"malloc");
- if ( malloc_ptr == NULL )
- BREAK();
- }
- ptr=(*malloc_ptr)(size);
- /* stats */
- malloc_count++;
- mallocvfree_count++;
- malloc_total_bytes+=size;
- return ptr;
- }
- void free(void * ptr)
- {
- static void *( *free_ptr) (void *) = NULL;
- if ( free_ptr == NULL ) {
- free_ptr=dlsym(RTLD_NEXT,"free");
- if ( free_ptr==NULL )
- BREAK();
- }
- (*free_ptr)(ptr);
- /*stats */
- if ( ptr == NULL )
- free_null_ptr_count++;
- else {
- free_count++;
- mallocvfree_count--;
- }
- }
- void * calloc(size_t num_objs, size_t size)
- {
- static void * (* calloc_ptr) ( size_t, size_t) = NULL;
- void * ptr;
- if ( calloc_ptr == NULL ) {
- calloc_ptr=dlsym(RTLD_NEXT,"calloc");
- if ( calloc_ptr == NULL )
- BREAK();
- }
- ptr=(*calloc_ptr)(num_objs,size);
- /* stats */
- calloc_count++;
- malloc_total_bytes+=size;
- return ptr;
- }
- void * realloc(void * ptr, size_t new_size)
- {
- static void * (* realloc_ptr) ( void *, size_t) = NULL;
- void * new_ptr;
- if ( realloc_ptr == NULL ) {
- realloc_ptr=dlsym(RTLD_NEXT,"realloc");
- if ( realloc_ptr == NULL )
- BREAK();
- }
- new_ptr=(*realloc_ptr)(ptr,new_size);
- /* stats */
- realloc_count++;
- return new_ptr;
- }
Add Comment
Please, Sign In to add comment