Commit 49ff2f44 authored by Florian Fischer's avatar Florian Fischer
Browse files

use library constructor to register print_status

parent 671e4a43
......@@ -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);
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment