aboutsummaryrefslogtreecommitdiff
path: root/chattymalloc.c
diff options
context:
space:
mode:
Diffstat (limited to 'chattymalloc.c')
-rw-r--r--chattymalloc.c80
1 files changed, 54 insertions, 26 deletions
diff --git a/chattymalloc.c b/chattymalloc.c
index 77732a1..54708d6 100644
--- a/chattymalloc.c
+++ b/chattymalloc.c
@@ -1,16 +1,20 @@
#define _GNU_SOURCE
#include <dlfcn.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <unistd.h>
static char tmpbuff[1024];
static unsigned long tmppos = 0;
static unsigned long tmpallocs = 0;
-static FILE* out = NULL;
-static int in_fprintf = 0;
+static int out = -1;
+static int prevent_recursion = 0;
/*=========================================================
* * interception points
@@ -20,22 +24,42 @@ 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 = fopen("chattymalloc.data", "w");
- if (out == NULL)
+ 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\n");
+ 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_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)
+ if (!myfn_malloc || !myfn_free || !myfn_calloc || !myfn_realloc || !myfn_memalign)
{
fprintf(stderr, "Error in `dlsym`: %s\n", dlerror());
exit(1);
@@ -66,19 +90,15 @@ void *malloc(size_t size)
}
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);
}
}
}
- if (!in_fprintf)
- {
- in_fprintf = 1;
- fprintf(out, "%d\n", size);
- in_fprintf = 0;
- }
void *ptr = myfn_malloc(size);
+ write_output("m %zu %p\n", size, ptr);
return ptr;
}
@@ -88,7 +108,10 @@ void free(void *ptr)
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)
@@ -104,13 +127,9 @@ void* realloc(void *ptr, size_t size)
return nptr;
}
- if (!in_fprintf)
- {
- in_fprintf = 1;
- fprintf(out, "%d\n", size);
- in_fprintf = 0;
- }
- return myfn_realloc(ptr, size);
+ 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)
@@ -123,11 +142,20 @@ void* calloc(size_t nmemb, size_t size)
return ptr;
}
- if (!in_fprintf)
+ 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)
{
- in_fprintf = 1;
- fprintf(out, "%d\n", size*nmemb);
- in_fprintf = 0;
+ fprintf(stderr, "called memalign before or during init");
+ exit(1);
}
- return myfn_calloc(nmemb, size);
+
+ void* ptr = myfn_memalign(alignment, size);
+ write_output("mm %zu %zu %p\n", alignment, size, ptr);
+ return ptr;
}