aboutsummaryrefslogtreecommitdiff
path: root/print_status_on_exit.c
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fl.fischer@fau.de>2018-09-17 12:27:50 +0200
committerFlorian Fischer <florian.fl.fischer@fau.de>2018-09-17 12:27:50 +0200
commit49ff2f44c8a6af85a09e38a66af93f56256c0d91 (patch)
tree5d49a8432a994272774ebb2622fee745bfdbaba0 /print_status_on_exit.c
parent671e4a43716a604e1b9d6ee27528720da7c9edd9 (diff)
downloadallocbench-49ff2f44c8a6af85a09e38a66af93f56256c0d91.tar.gz
allocbench-49ff2f44c8a6af85a09e38a66af93f56256c0d91.zip
use library constructor to register print_status
Diffstat (limited to 'print_status_on_exit.c')
-rw-r--r--print_status_on_exit.c60
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);
-}
-