diff options
Diffstat (limited to 'print_status_on_exit.c')
| -rw-r--r-- | print_status_on_exit.c | 60 |
1 files changed, 1 insertions, 59 deletions
diff --git a/print_status_on_exit.c b/print_status_on_exit.c index 23df6bc..1d72384 100644 --- a/print_status_on_exit.c +++ b/print_status_on_exit.c @@ -4,17 +4,6 @@ #include <stdio.h> #include <stdlib.h> -char tmpbuff[1024]; -unsigned long tmppos = 0; -unsigned long tmpallocs = 0; - -/*========================================================= - * * interception points - * */ - -static void * (*myfn_malloc)(size_t size); -static void (*myfn_free)(void *ptr); - static void print_status(void) { char buf[4096]; @@ -41,55 +30,8 @@ static void print_status(void) fclose(status); } -static void init() +static void __attribute__((constructor)) init() { - myfn_malloc = dlsym(RTLD_NEXT, "malloc"); - myfn_free = dlsym(RTLD_NEXT, "free"); - - if (!myfn_malloc || !myfn_free) - { - fprintf(stderr, "Error in `dlsym`: %s\n", dlerror()); - exit(1); - } atexit(print_status); } -void *malloc(size_t size) -{ - static int initializing = 0; - if (myfn_malloc == NULL) - { - if (!initializing) - { - initializing = 1; - init(); - initializing = 0; - } - else - { - if (tmppos + size < sizeof(tmpbuff)) - { - void *retptr = tmpbuff + tmppos; - tmppos += size; - ++tmpallocs; - return retptr; - } - else - { - fprintf(stderr, "jcheck: too much memory requested during initialisation - increase tmpbuff size\n"); - exit(1); - } - } - } - return myfn_malloc(size); -} - -void free(void *ptr) -{ - // something wrong if we call free before one of the allocators! - if (myfn_malloc == NULL) - init(); - if (!(ptr >= (void*) tmpbuff && ptr <= (void*)(tmpbuff + tmppos))) - myfn_free(ptr); -} - |
