aboutsummaryrefslogtreecommitdiff
path: root/chattymalloc.c
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fl.fischer@fau.de>2018-09-06 15:02:35 +0200
committerFlorian Fischer <florian.fl.fischer@fau.de>2018-09-06 15:02:35 +0200
commitc70ba69d146eee6392528cafd243ac7d0b17e6c6 (patch)
treede1e994b2fef1656bc8d9dc265c37164627e07e0 /chattymalloc.c
parent9a539b5f860f6a697881d9bc43951184fec1abdc (diff)
downloadallocbench-c70ba69d146eee6392528cafd243ac7d0b17e6c6.tar.gz
allocbench-c70ba69d146eee6392528cafd243ac7d0b17e6c6.zip
also output sizes of calloc and realloc
Diffstat (limited to 'chattymalloc.c')
-rw-r--r--chattymalloc.c59
1 files changed, 52 insertions, 7 deletions
diff --git a/chattymalloc.c b/chattymalloc.c
index f3db0d2..77732a1 100644
--- a/chattymalloc.c
+++ b/chattymalloc.c
@@ -5,18 +5,21 @@
#include <stdlib.h>
#include <string.h>
-char tmpbuff[1024];
-unsigned long tmppos = 0;
-unsigned long tmpallocs = 0;
+static char tmpbuff[1024];
+static unsigned long tmppos = 0;
+static unsigned long tmpallocs = 0;
-FILE* out = NULL;
+static FILE* out = NULL;
+static int in_fprintf = 0;
/*=========================================================
* * interception points
* */
static void * (*myfn_malloc)(size_t size);
-static void * (*myfn_free)(void* ptr);
+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 init()
{
@@ -29,8 +32,10 @@ static void init()
myfn_malloc = dlsym(RTLD_NEXT, "malloc");
myfn_free = dlsym(RTLD_NEXT, "free");
+ myfn_calloc = dlsym(RTLD_NEXT, "calloc");
+ myfn_realloc = dlsym(RTLD_NEXT, "realloc");
- if (!myfn_malloc || !myfn_free)
+ if (!myfn_malloc || !myfn_free || !myfn_calloc || !myfn_realloc)
{
fprintf(stderr, "Error in `dlsym`: %s\n", dlerror());
exit(1);
@@ -40,7 +45,6 @@ static void init()
void *malloc(size_t size)
{
static int initializing = 0;
- static int in_fprintf = 0;
if (myfn_malloc == NULL)
{
@@ -86,3 +90,44 @@ void free(void *ptr)
if (!(ptr >= (void*) tmpbuff && ptr <= (void*)(tmpbuff + tmppos)))
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;
+ }
+
+ if (!in_fprintf)
+ {
+ in_fprintf = 1;
+ fprintf(out, "%d\n", size);
+ in_fprintf = 0;
+ }
+ return myfn_realloc(ptr, size);
+}
+
+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;
+ }
+
+ if (!in_fprintf)
+ {
+ in_fprintf = 1;
+ fprintf(out, "%d\n", size*nmemb);
+ in_fprintf = 0;
+ }
+ return myfn_calloc(nmemb, size);
+}