Guest User

Untitled

a guest
Jan 23rd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.92 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <dlfcn.h>
  3. #include <stdio.h>
  4. #include <stdbool.h>
  5.  
  6. #define RTLD_NEXT ((void *) -1l)
  7.  
  8. static void *(*real_malloc)(size_t);
  9. static void (*real_free)(void *);
  10.  
  11. static long malloc_count = 0;
  12. static long free_count = 0;
  13.  
  14. static bool block = false;
  15.  
  16. void *malloc(size_t size)
  17. {
  18.    if (!real_malloc)
  19.       real_malloc = dlsym(RTLD_NEXT, "malloc");
  20.  
  21.    void *res = real_malloc(size);
  22.    if (block)
  23.       return res;
  24.  
  25.    if (size && res)
  26.       malloc_count++;
  27.  
  28.    block = true;
  29.    printf("Delta = %ld\n", malloc_count - free_count);
  30.    block = false;
  31.  
  32.    return res;
  33. }
  34.  
  35. void free(void *ptr)
  36. {
  37.    if (!real_free)
  38.       real_free = dlsym(RTLD_NEXT, "free");
  39.  
  40.    if (block)
  41.    {
  42.       real_free(ptr);
  43.       return;
  44.    }
  45.  
  46.    if (ptr)
  47.       free_count++;
  48.  
  49.    block = true;
  50.    printf("Delta = %ld\n", malloc_count - free_count);
  51.  
  52.    real_free(ptr);
  53.    block = false;
  54. }
Add Comment
Please, Sign In to add comment