From f2327ada5590f40137416f301762cd3c11ab36b6 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Fri, 8 Feb 2019 13:31:11 +0100 Subject: s/target/allocator/ and remove analyse feature Use systemwide installed allocators by default --- src/chattymalloc.c | 161 ----------------------------------------------------- 1 file changed, 161 deletions(-) delete mode 100644 src/chattymalloc.c (limited to 'src/chattymalloc.c') diff --git a/src/chattymalloc.c b/src/chattymalloc.c deleted file mode 100644 index 54708d6..0000000 --- a/src/chattymalloc.c +++ /dev/null @@ -1,161 +0,0 @@ -#define _GNU_SOURCE -#include -#include -#include -#include -#include -#include -#include -#include -#include - -static char tmpbuff[1024]; -static unsigned long tmppos = 0; -static unsigned long tmpallocs = 0; - -static int out = -1; -static int prevent_recursion = 0; - -/*========================================================= - * * interception points - * */ - -static void * (*myfn_malloc)(size_t size); -static void (*myfn_free)(void* ptr); -static void * (*myfn_calloc)(size_t nmemb, size_t size); -static void * (*myfn_realloc)(void* ptr, size_t size); -static void * (*myfn_memalign)(size_t alignment, size_t size); - -static void write_output(const char* fmt, ...) -{ - if (!prevent_recursion) - { - prevent_recursion = 1; - - /* lockf(out, F_LOCK, 0); */ - - va_list args; - va_start(args, fmt); - vdprintf(out, fmt, args); - va_end(args); - - /* lockf(out, F_ULOCK, 0); */ - prevent_recursion = 0; - } -} - -static void init() -{ - out = open("chattymalloc.data", O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); - if (out == -1) - { - fprintf(stderr, "failed to open output file with %d\n", errno); - exit(1); - } - - myfn_malloc = dlsym(RTLD_NEXT, "malloc"); - myfn_free = dlsym(RTLD_NEXT, "free"); - myfn_calloc = dlsym(RTLD_NEXT, "calloc"); - myfn_realloc = dlsym(RTLD_NEXT, "realloc"); - myfn_memalign = dlsym(RTLD_NEXT, "memalign"); - - if (!myfn_malloc || !myfn_free || !myfn_calloc || !myfn_realloc || !myfn_memalign) - { - fprintf(stderr, "Error in `dlsym`: %s\n", dlerror()); - exit(1); - } -} - -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, "%d in %d allocs\n", tmppos, tmpallocs); - fprintf(stderr, "jcheck: too much memory requested during initialisation - increase tmpbuff size\n"); - exit(1); - } - } - } - - void *ptr = myfn_malloc(size); - write_output("m %zu %p\n", size, ptr); - return ptr; -} - -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))) - { - write_output("f %p\n", ptr); - myfn_free(ptr); - } -} - -void* realloc(void *ptr, size_t size) -{ - if (myfn_realloc == NULL) - { - void *nptr = malloc(size); - if (nptr && ptr) - { - memmove(nptr, ptr, size); - free(ptr); - } - return nptr; - } - - void* nptr = myfn_realloc(ptr, size); - write_output("r %p %zu %p\n", ptr, size, nptr); - return nptr; -} - -void* calloc(size_t nmemb, size_t size) -{ - if (myfn_calloc == NULL) - { - void *ptr = malloc(nmemb*size); - if (ptr) - memset(ptr, 0, nmemb*size); - return ptr; - } - - void* ptr = myfn_calloc(nmemb, size); - write_output("c %zu %zu %p\n", nmemb, size, ptr); - return ptr; -} - -void* memalign(size_t alignment, size_t size) -{ - if (myfn_memalign == NULL) - { - fprintf(stderr, "called memalign before or during init"); - exit(1); - } - - void* ptr = myfn_memalign(alignment, size); - write_output("mm %zu %zu %p\n", alignment, size, ptr); - return ptr; -} -- cgit v1.2.3