From 49ff2f44c8a6af85a09e38a66af93f56256c0d91 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Mon, 17 Sep 2018 12:27:50 +0200 Subject: use library constructor to register print_status --- print_status_on_exit.c | 60 +------------------------------------------------- 1 file changed, 1 insertion(+), 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 #include -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); -} - -- cgit v1.2.3