From 25b6775cc6eef8deea8c06b83a131c362efd8979 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Jan 2023 14:20:12 -0700 Subject: [malloc-stats] Measure calloc --- util/malloc-stats.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'util') diff --git a/util/malloc-stats.c b/util/malloc-stats.c index eba0b2ac0..a086b0543 100644 --- a/util/malloc-stats.c +++ b/util/malloc-stats.c @@ -173,6 +173,7 @@ func_stats_add (const void *caller, int is_realloc, size_t size) #include 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; @@ -194,6 +195,22 @@ malloc(size_t size) return old_malloc (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) { @@ -218,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()); -- cgit v1.2.1