$OpenBSD: patch-core_libs_kmemoryinfo_kmemoryinfo_backend_cpp,v 1.1 2019/04/02 09:59:39 rsadowski Exp $

Index: core/libs/kmemoryinfo/kmemoryinfo_backend.cpp
--- core/libs/kmemoryinfo/kmemoryinfo_backend.cpp.orig
+++ core/libs/kmemoryinfo/kmemoryinfo_backend.cpp
@@ -76,7 +76,8 @@ static int fillMemoryInfo(Digikam::KMemoryInfo::KMemor
 #include <sys/param.h>
 #include <sys/types.h>
 #include <sys/sysctl.h>
-#include <sys/unistd.h>
+#include <sys/vmmeter.h>
+#include <unistd.h>
 #endif
 #ifdef Q_OS_HPUX
 #include <sys/param.h>
@@ -148,7 +149,7 @@ kvm_t* sg_get_kvm2()
 }
 #endif // (defined(Q_OS_FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD)
 
-#if defined(Q_OS_NETBSD) || defined(Q_OS_OPENBSD)
+#if defined(Q_OS_NETBSD)
 struct uvmexp* sg_get_uvmexp()
 {
     int                  mib[2];
@@ -167,7 +168,7 @@ struct uvmexp* sg_get_uvmexp()
 
     return &uvm;
 }
-#endif // defined(Q_OS_NETBSD) || defined(Q_OS_OPENBSD)
+#endif // defined(Q_OS_NETBSD)
 
 #ifdef Q_OS_HPUX
 struct pst_KMemoryInfo::static* sg_get_pstat_static()
@@ -230,10 +231,10 @@ int get_mem_stats(Digikam::KMemoryInfo::KMemoryInfoDat
 #endif // defined(Q_OS_NETBSD)
 
 #if defined(Q_OS_OPENBSD)
-    int    mib[2];
-    struct vmtotal vmtotal;
+    int uvmexp_mib[] = {CTL_VM, VM_UVMEXP};
     size_t size;
-    int    pagesize, page_multiplier;
+    struct uvmexp uvmexp;
+    int    pagesize, page_multiplier, pageshift;
 #endif // defined(Q_OS_OPENBSD)
 
 #ifdef Q_OS_WIN
@@ -444,44 +445,24 @@ int get_mem_stats(Digikam::KMemoryInfo::KMemoryInfoDat
      * converting the memory stats in pages to bytes in base 2.
      */
 
-    /* All memory stats in OpenBSD are returned as the number of pages.
-     * To convert this into the number of bytes we need to know the
-     * page size on this system.
-     */
-    pagesize = sysconf(_SC_PAGESIZE);
-
-    /* The pagesize gives us the base 10 multiplier, so we need to work
-     * out what the base 2 multiplier is. This means dividing
-     * pagesize by 2 until we reach unity, and counting the number of
-     * divisions required.
-     */
-    page_multiplier = 0;
-
-    while (pagesize > 1)
-    {
-        page_multiplier++;
-        pagesize >>= 1;
+    size = sizeof(uvmexp);
+    if (sysctl(uvmexp_mib, 2, &uvmexp, &size, NULL, 0) < 0) {
+        return 0;
     }
 
-    /* We can now ret the raw VM stats (in pages) using the
-     * sysctl interface.
-     */
-    mib[0] = CTL_VM;
-    mib[1] = VM_METER;
-    size   = sizeof(vmtotal);
-
-    if (sysctl(mib, 2, &vmtotal, &size, NULL, 0) < 0)
-    {
-        bzero(&vmtotal, sizeof(vmtotal));
-        //sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_VM.VM_METER");
-        return 0;
+    pagesize = getpagesize();
+    pageshift = 0;
+    while (pagesize > 1) {
+            pageshift++;
+            pagesize >>= 1;
     }
 
-    /* Convert the raw stats to bytes, and return these to the caller
-     */
-    data->usedRam  = (vmtotal.t_rm << page_multiplier);   /* total real mem in use */
+    /* we only need the amount of log(2)1024 for our conversion */
+    pageshift -= 10;
+
+    data->usedRam  = (uvmexp.active << page_multiplier);   /* total real mem in use */
     data->cacheRam = 0;                                  /* no cache stats */
-    data->freeRam  = (vmtotal.t_free << page_multiplier); /* free memory pages */
+    data->freeRam  = (uvmexp.free << page_multiplier); /* free memory pages */
     data->totalRam = (data->usedRam + data->freeRam);
 
     return 1;
@@ -537,12 +518,15 @@ int get_mem_stats(Digikam::KMemoryInfo::KMemoryInfoDat
 #endif
 #include <unistd.h>
 #endif
-#if defined(Q_OS_NETBSD) || defined(Q_OS_OPENBSD)
+#if defined(Q_OS_NETBSD)
 #include <sys/param.h>
 #include <sys/time.h>
 #include <uvm/uvm.h>
 #include <unistd.h>
 #endif
+#if defined(Q_OS_OPENBSD)
+#include <QtGlobal>
+#endif
 #ifdef Q_OS_HPUX
 #include <sys/param.h>
 #include <sys/pstat.h>
@@ -590,9 +574,9 @@ int get_swap_stats(Digikam::KMemoryInfo::KMemoryInfoDa
 
 #endif // defined(Q_OS_FREEBSD)
 
-#if defined(Q_OS_NETBSD) || defined(Q_OS_OPENBSD)
+#if defined(Q_OS_NETBSD)
     struct uvmexp* uvm = 0;
-#endif // defined(Q_OS_NETBSD) || defined(Q_OS_OPENBSD)
+#endif // defined(Q_OS_NETBSD)
 
 #ifdef Q_OS_WIN
     MEMORYSTATUSEX memstats;
@@ -761,7 +745,7 @@ int get_swap_stats(Digikam::KMemoryInfo::KMemoryInfoDa
     return 1;
 #endif // defined(Q_OS_FREEBSD) || defined(Q_OS_DFBSD)
 
-#if defined(Q_OS_NETBSD) || defined(Q_OS_OPENBSD)
+#if defined(Q_OS_NETBSD)
     if ((uvm = sg_get_uvmexp()) == NULL)
     {
         return 0;
@@ -772,7 +756,12 @@ int get_swap_stats(Digikam::KMemoryInfo::KMemoryInfoDa
     data->freeSwap  = data->totalSwap - data->usedSwap;
 
     return 1;
-#endif // defined(Q_OS_NETBSD) || defined(Q_OS_OPENBSD)
+#endif // defined(Q_OS_NETBSD)
+
+#if defined(Q_OS_OPENBSD)
+    Q_UNUSED(data);
+    return 1;
+#endif // defined(Q_OS_OPENBSD)
 
 #ifdef Q_OS_WIN
     memstats.dwLength = sizeof(memstats);
