Replace deprecated malloc_hook

This commit is contained in:
Adrian Johnson 2022-03-08 21:17:18 +10:30
parent 1c0a9aac01
commit d2f1827cde
3 changed files with 38 additions and 60 deletions

View file

@ -17,7 +17,7 @@ Build by:
and use by: and use by:
LD_PRELOAD=$PWD/malloc-stats.so app-to-run LD_PRELOAD=$(blddir)/util/libmalloc-stats.so app-to-run
cairo-trace cairo-trace
----------- -----------

View file

@ -170,76 +170,54 @@ func_stats_add (const void *caller, int is_realloc, size_t size)
/* wrapper stuff */ /* wrapper stuff */
#include <malloc.h> #include <dlfcn.h>
static void *(*old_malloc)(size_t, const void *); static void *(*old_malloc)(size_t);
static void *(*old_realloc)(void *, size_t, const void *); static void *(*old_realloc)(void *, size_t);
static int enable_hook = 0;
static void *my_malloc(size_t, const void *); void *
static void *my_realloc(void *, size_t, const void *); malloc(size_t size)
static void
save_hooks (void)
{ {
old_malloc = __malloc_hook; if (enable_hook) {
old_realloc = __realloc_hook; enable_hook = 0;
} void *caller = __builtin_return_address(0);
static void
old_hooks (void)
{
__malloc_hook = old_malloc;
__realloc_hook = old_realloc;
}
static void
my_hooks (void)
{
/* should always save the current value */
save_hooks ();
__malloc_hook = my_malloc;
__realloc_hook = my_realloc;
}
static void *
my_malloc(size_t size, const void *caller)
{
void *ret;
old_hooks ();
func_stats_add (caller, 0, size); func_stats_add (caller, 0, size);
enable_hook = 1;
}
ret = malloc (size); return old_malloc (size);
my_hooks ();
return ret;
} }
static void * void *
my_realloc(void *ptr, size_t size, const void *caller) realloc(void *ptr, size_t size)
{ {
void *ret; if (enable_hook) {
enable_hook = 0;
old_hooks (); void *caller = __builtin_return_address(0);
func_stats_add (caller, 1, size); func_stats_add (caller, 1, size);
enable_hook = 1;
}
ret = realloc (ptr, size); return old_realloc (ptr, size);
my_hooks ();
return ret;
} }
static void static void __attribute__ ((constructor))
my_init_hook(void) { init(void)
my_hooks (); {
old_malloc = dlsym(RTLD_NEXT, "malloc");
if (!old_malloc) {
fprintf(stderr, "%s\n", dlerror());
exit(1);
}
old_realloc = dlsym(RTLD_NEXT, "realloc");
if (!old_realloc) {
fprintf(stderr, "%s\n", dlerror());
exit(1);
}
enable_hook = 1;
} }
void (*__volatile __malloc_initialize_hook) (void) = my_init_hook;
/* reporting */ /* reporting */
#include <locale.h> #include <locale.h>
@ -319,7 +297,7 @@ malloc_stats (void)
unsigned int i, j; unsigned int i, j;
struct func_stat_t *sorted_func_stats; struct func_stat_t *sorted_func_stats;
old_hooks (); enable_hook = 0;
if (! func_stats_num) if (! func_stats_num)
return; return;

View file

@ -59,6 +59,6 @@ foreach util : cairo_utils
) )
endforeach endforeach
if cc.has_header_symbol('malloc.h', '__malloc_hook') and cc.has_header('execinfo.h') if conf.get('CAIRO_HAS_DLSYM', 0) == 1 and cc.has_header('execinfo.h')
libmallocstats = library('malloc-stats', 'malloc-stats.c') libmallocstats = library('malloc-stats', 'malloc-stats.c', dependencies : dl_dep)
endif endif