[gnome-software/wip/lantw/freebsd-memory-total] Implement gs_utils_get_memory_total for FreeBSD



commit 930d44194ce00ceb9c715e384a92ff1e3e3db78e
Author: Ting-Wei Lan <lantw src gnome org>
Date:   Wed Jan 31 03:30:33 2018 +0800

    Implement gs_utils_get_memory_total for FreeBSD
    
    sysinfo is a Linux-specific system call which is not available on other
    operating systems. This commit adds an implementation using the value
    come from 'sysctl hw.physmem' for FreeBSD.

 lib/gs-utils.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
---
diff --git a/lib/gs-utils.c b/lib/gs-utils.c
index 9b754ed9..0b3de28b 100644
--- a/lib/gs-utils.c
+++ b/lib/gs-utils.c
@@ -38,7 +38,13 @@
 #include <string.h>
 #include <glib/gstdio.h>
 #include <json-glib/json-glib.h>
+
+#if defined(__linux__)
 #include <sys/sysinfo.h>
+#elif defined(__FreeBSD__)
+#include <sys/types.h>
+#include <sys/sysctl.h>
+#endif
 
 #ifdef HAVE_POLKIT
 #include <polkit/polkit.h>
@@ -1035,9 +1041,17 @@ gs_utils_is_low_resolution (GtkWidget *toplevel)
 guint
 gs_utils_get_memory_total (void)
 {
+#ifdef defined(__linux__)
        struct sysinfo si = { 0 };
        sysinfo (&si);
        return si.totalram / MB_IN_BYTES / si.mem_unit;
+#elif defined(__FreeBSD__)
+       unsigned long physmem;
+       sysctl ((int[]){ CTL_HW, HW_PHYSMEM }, 2, &physmem, &(size_t){ sizeof (physmem) }, NULL, 0);
+       return physmem / MB_IN_BYTES;
+#else
+#error "Please implement gs_utils_get_memory_total for your system."
+#endif
 }
 
 /* vim: set noexpandtab: */


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