summaryrefslogtreecommitdiff
path: root/util/malloc-stats.c
diff options
context:
space:
mode:
Diffstat (limited to 'util/malloc-stats.c')
-rw-r--r--util/malloc-stats.c32
1 files changed, 31 insertions, 1 deletions
diff --git a/util/malloc-stats.c b/util/malloc-stats.c
index cf712bd3a..a086b0543 100644
--- a/util/malloc-stats.c
+++ b/util/malloc-stats.c
@@ -173,12 +173,18 @@ func_stats_add (const void *caller, int is_realloc, size_t size)
#include <dlfcn.h>
static void *(*old_malloc)(size_t);
+static void *(*old_calloc)(size_t, size_t);
static void *(*old_realloc)(void *, size_t);
static int enable_hook = 0;
+static void init(void);
+
void *
malloc(size_t size)
{
+ if (!old_malloc)
+ init ();
+
if (enable_hook) {
enable_hook = 0;
void *caller = __builtin_return_address(0);
@@ -190,8 +196,27 @@ malloc(size_t size)
}
void *
+calloc(size_t nmemb, size_t size)
+{
+ if (!old_calloc)
+ init ();
+
+ if (enable_hook) {
+ enable_hook = 0;
+ void *caller = __builtin_return_address(0);
+ func_stats_add (caller, 0, nmemb * size);
+ enable_hook = 1;
+ }
+
+ return old_calloc (nmemb, size);
+}
+
+void *
realloc(void *ptr, size_t size)
{
+ if (!old_malloc)
+ init ();
+
if (enable_hook) {
enable_hook = 0;
void *caller = __builtin_return_address(0);
@@ -202,7 +227,7 @@ realloc(void *ptr, size_t size)
return old_realloc (ptr, size);
}
-static void __attribute__ ((constructor))
+static void
init(void)
{
old_malloc = dlsym(RTLD_NEXT, "malloc");
@@ -210,6 +235,11 @@ init(void)
fprintf(stderr, "%s\n", dlerror());
exit(1);
}
+ old_calloc = dlsym(RTLD_NEXT, "calloc");
+ if (!old_calloc) {
+ fprintf(stderr, "%s\n", dlerror());
+ exit(1);
+ }
old_realloc = dlsym(RTLD_NEXT, "realloc");
if (!old_realloc) {
fprintf(stderr, "%s\n", dlerror());