Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.95 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define __USE_GNU
  4. #include <dlfcn.h>
  5.  
  6. extern void *__libc_free(void *ptr);
  7.  
  8. static void *(*real_malloc) (size_t);
  9. int cnt_blocks = 0;
  10.  
  11. void *malloc (size_t size)
  12. {
  13.     if (cnt_blocks >= 77) {
  14.         fprintf (stderr, "number of allocated blocks is more then 77\n");        
  15.         return (NULL);
  16.     }
  17.     cnt_blocks++;
  18.     return (*real_malloc) (size);
  19. }
  20.  
  21. void free(void *ptr)
  22. {
  23.     printf("%d\n", ptr);
  24.     __libc_free(ptr);
  25.     cnt_blocks--;
  26.     printf("Nubmer of allocated blocks: %d\n", cnt_blocks);
  27.     printf("%d\n", ptr);
  28. }
  29.  
  30. static void __malloc_init (void) __attribute__((constructor));
  31. static void __malloc_init (void)
  32. {
  33.     void *ptr;
  34.     ptr = dlopen (NULL, RTLD_LAZY);
  35.     if (ptr == NULL)
  36.         abort ();
  37.     real_malloc = dlsym (RTLD_NEXT, "malloc");
  38.     if (NULL == real_malloc) {
  39.         fprintf (stderr, "Error in `dlsym`: %s\n", dlerror ());
  40.         return;
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement