Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define __USE_GNU
- #include <dlfcn.h>
- extern void *__libc_free(void *ptr);
- static void *(*real_malloc) (size_t);
- int cnt_blocks = 0;
- void *malloc (size_t size)
- {
- if (cnt_blocks >= 77) {
- fprintf (stderr, "number of allocated blocks is more then 77\n");
- return (NULL);
- }
- cnt_blocks++;
- return (*real_malloc) (size);
- }
- void free(void *ptr)
- {
- printf("%d\n", ptr);
- __libc_free(ptr);
- cnt_blocks--;
- printf("Nubmer of allocated blocks: %d\n", cnt_blocks);
- printf("%d\n", ptr);
- }
- static void __malloc_init (void) __attribute__((constructor));
- static void __malloc_init (void)
- {
- void *ptr;
- ptr = dlopen (NULL, RTLD_LAZY);
- if (ptr == NULL)
- abort ();
- real_malloc = dlsym (RTLD_NEXT, "malloc");
- if (NULL == real_malloc) {
- fprintf (stderr, "Error in `dlsym`: %s\n", dlerror ());
- return;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement