[tracker-miners/sam/malloc-trim-glibc-only] malloc_trim() is not available in musl libc




commit 3f6e4f383cce53509d92572875493bc16f8d701d
Author: Sam Thursfield <sam afuera me uk>
Date:   Mon Sep 21 18:19:09 2020 +0200

    malloc_trim() is not available in musl libc
    
    So detect it and use it conditionally.
    
    Closes https://gitlab.gnome.org/GNOME/tracker-miners/-/issues/135

 config-miners.h.meson.in     |  3 +++
 meson.build                  |  4 ++++
 src/miners/fs/tracker-main.c | 27 ++++++++++++++++++++++++---
 3 files changed, 31 insertions(+), 3 deletions(-)
---
diff --git a/config-miners.h.meson.in b/config-miners.h.meson.in
index 79fd4c17e..19db0a358 100644
--- a/config-miners.h.meson.in
+++ b/config-miners.h.meson.in
@@ -50,6 +50,9 @@
 /* Define if we have libseccomp */
 #mesondefine HAVE_LIBSECCOMP
 
+/* Define if we have malloc_trim() */
+#mesondefine HAVE_MALLOC_TRIM
+
 /* Define if we have NetworkManager for network status detection */
 #mesondefine HAVE_NETWORK_MANAGER
 
diff --git a/meson.build b/meson.build
index eae27e5ad..e3dcc98a5 100644
--- a/meson.build
+++ b/meson.build
@@ -327,6 +327,8 @@ endif
 
 conf = configuration_data()
 
+have_malloc_trim = meson.get_compiler('c').has_function('malloc_trim')
+
 # Config that goes in config.h
 conf.set('GUARANTEE_METADATA', get_option('guarantee_metadata') == true)
 conf.set('USING_UNZIPPSFILES', get_option('unzip_ps_gz_files') == true)
@@ -343,6 +345,7 @@ conf.set('HAVE_LIBICU_CHARSET_DETECTION', charset_library_name == 'icu')
 conf.set('HAVE_LIBEXIF', libexif.found())
 conf.set('HAVE_LIBIPTCDATA', libiptcdata.found())
 conf.set('HAVE_LIBSECCOMP', libseccomp.found())
+conf.set('HAVE_MALLOC_TRIM', have_malloc_trim)
 conf.set('HAVE_UPOWER', battery_detection_library_name == 'upower')
 conf.set('HAVE_NETWORK_MANAGER', have_network_manager)
 conf.set('DOMAIN_PREFIX', get_option('domain_prefix'))
@@ -470,6 +473,7 @@ summary = [
   '\nFeature Support:',
   '    Battery/mains power detection:          ' + battery_detection_library_name,
   '    Support for network status detection:   ' + have_network_manager.to_string(),
+  '    Releasing heap memory with malloc_trim: ' + have_malloc_trim.to_string(),
   '\nData Miners / Writebacks:',
   '    FS (File System):                       ' + have_tracker_miner_fs.to_string(),
   '    RSS:                                    ' + have_tracker_miner_rss.to_string(),
diff --git a/src/miners/fs/tracker-main.c b/src/miners/fs/tracker-main.c
index bd4a484e0..f5e00a7dd 100644
--- a/src/miners/fs/tracker-main.c
+++ b/src/miners/fs/tracker-main.c
@@ -26,6 +26,10 @@
 #include <sys/types.h>
 #include <unistd.h>
 
+#ifdef HAVE_MALLOC_TRIM
+#include <malloc.h>
+#endif
+
 #include <glib.h>
 #include <glib-unix.h>
 #include <glib-object.h>
@@ -433,11 +437,28 @@ miner_start (TrackerMiner  *miner,
                                                   miner);
 }
 
+#ifdef HAVE_MALLOC_TRIM
+
+static void
+release_heap_memory (void)
+{
+       malloc_trim (0);
+}
+
+#else
+
+static void
+release_heap_memory (void)
+{
+       g_debug ("release_heap_memory(): Doing nothing as malloc_trim() is not available on this platform.");
+}
+
+#endif
+
 static gboolean
 cleanup_cb (gpointer user_data)
 {
-       /* Reclaim as much memory as possible */
-       malloc_trim (0);
+       release_heap_memory ();
 
        cleanup_id = 0;
 
@@ -451,7 +472,7 @@ on_low_memory (GMemoryMonitor            *monitor,
                gpointer                   user_data)
 {
        if (level > G_MEMORY_MONITOR_WARNING_LEVEL_LOW)
-               malloc_trim (0);
+               release_heap_memory ();
 }
 #endif
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]