[cairo-commit] 2 commits - util/malloc-stats.c util/meson.build util/README

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Tue Mar 8 21:22:26 UTC 2022


 util/README         |    2 -
 util/malloc-stats.c |   92 +++++++++++++++++++---------------------------------
 util/meson.build    |    4 +-
 3 files changed, 38 insertions(+), 60 deletions(-)

New commits:
commit 777d35fa879396c055f4fd741695bc56a1a1576c
Merge: 1c0a9aac0 d2f1827cd
Author: Adrian Johnson <ajohnson at redneon.com>
Date:   Tue Mar 8 21:22:25 2022 +0000

    Merge branch 'malloc-hook' into 'master'
    
    Replace deprecated malloc_hook
    
    See merge request cairo/cairo!295

commit d2f1827cdebe2eee6595cf6aeef0ff005ec636d3
Author: Adrian Johnson <ajohnson at redneon.com>
Date:   Tue Mar 8 21:17:18 2022 +1030

    Replace deprecated malloc_hook

diff --git a/util/README b/util/README
index 146180ded..b75ae4336 100644
--- a/util/README
+++ b/util/README
@@ -17,7 +17,7 @@ Build 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
 -----------
diff --git a/util/malloc-stats.c b/util/malloc-stats.c
index 55ed51cad..132164c73 100644
--- a/util/malloc-stats.c
+++ b/util/malloc-stats.c
@@ -170,76 +170,54 @@ func_stats_add (const void *caller, int is_realloc, size_t size)
 
 /* wrapper stuff */
 
-#include <malloc.h>
+#include <dlfcn.h>
 
-static void *(*old_malloc)(size_t, const void *);
-static void *(*old_realloc)(void *, size_t, const void *);
+static void *(*old_malloc)(size_t);
+static void *(*old_realloc)(void *, size_t);
+static int enable_hook = 0;
 
-static void *my_malloc(size_t, const void *);
-static void *my_realloc(void *, size_t, const void *);
-
-static void
-save_hooks (void)
-{
-	old_malloc  = __malloc_hook;
-	old_realloc = __realloc_hook;
-}
-
-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 *
+malloc(size_t size)
 {
-	void *ret;
-
-	old_hooks ();
-
+    if (enable_hook) {
+	enable_hook = 0;
+	void *caller = __builtin_return_address(0);
 	func_stats_add (caller, 0, size);
+	enable_hook = 1;
+    }
 
-	ret = malloc (size);
-	my_hooks ();
-
-	return ret;
+    return old_malloc (size);
 }
 
-static void *
-my_realloc(void *ptr, size_t size, const void *caller)
+void *
+realloc(void *ptr, size_t size)
 {
-	void *ret;
-
-	old_hooks ();
-
+    if (enable_hook) {
+	enable_hook = 0;
+	void *caller = __builtin_return_address(0);
 	func_stats_add (caller, 1, size);
+	enable_hook = 1;
+    }
 
-	ret = realloc (ptr, size);
-	my_hooks ();
-
-	return ret;
+    return old_realloc (ptr, size);
 }
 
-static void
-my_init_hook(void) {
-	my_hooks ();
+static void __attribute__ ((constructor))
+init(void)
+{
+    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 */
 
 #include <locale.h>
@@ -319,7 +297,7 @@ malloc_stats (void)
 	unsigned int i, j;
 	struct func_stat_t *sorted_func_stats;
 
-	old_hooks ();
+	enable_hook = 0;
 
 	if (! func_stats_num)
 		return;
diff --git a/util/meson.build b/util/meson.build
index 152b23729..5cc209cc9 100644
--- a/util/meson.build
+++ b/util/meson.build
@@ -59,6 +59,6 @@ foreach util : cairo_utils
   )
 endforeach
 
-if cc.has_header_symbol('malloc.h', '__malloc_hook') and cc.has_header('execinfo.h')
-  libmallocstats = library('malloc-stats', 'malloc-stats.c')
+if conf.get('CAIRO_HAS_DLSYM', 0) == 1 and cc.has_header('execinfo.h')
+  libmallocstats = library('malloc-stats', 'malloc-stats.c', dependencies : dl_dep)
 endif


More information about the cairo-commit mailing list