diff system/System.cpp @ 170:b23eea68357e

* flesh out StorageAdviser
author Chris Cannam
date Tue, 26 Sep 2006 12:58:12 +0000
parents 603991c63ff6
children ebf55cf0d34d
line wrap: on
line diff
--- a/system/System.cpp	Mon Sep 25 20:32:44 2006 +0000
+++ b/system/System.cpp	Tue Sep 26 12:58:12 2006 +0000
@@ -18,6 +18,8 @@
 #include <QStringList>
 #include <QString>
 
+#include <stdint.h>
+
 #ifndef _WIN32
 #include <signal.h>
 #include <sys/statvfs.h>
@@ -74,17 +76,21 @@
 #endif
 }
 
-int
-GetRealMemoryMBAvailable()
+void
+GetRealMemoryMBAvailable(int &available, int &total)
 {
+    available = -1;
+    total = -1;
+
     FILE *meminfo = fopen("/proc/meminfo", "r");
-    if (!meminfo) return -1;
+    if (!meminfo) return;
 
     char buf[256];
     while (!feof(meminfo)) {
         fgets(buf, 256, meminfo);
-        if (strncmp(buf, "MemFree:", 8)) {
-            fclose(meminfo);
+        bool isMemFree = (strncmp(buf, "MemFree:", 8) == 0);
+        bool isMemTotal = (!isMemFree && (strncmp(buf, "MemTotal:", 9) == 0));
+        if (isMemFree || isMemTotal) {
             QString line = QString(buf).trimmed();
             QStringList elements = line.split(' ', QString::SkipEmptyParts);
             QString unit = "kB";
@@ -92,14 +98,20 @@
             int size = elements[1].toInt();
 //            std::cerr << "have size \"" << size << "\", unit \""
 //                      << unit.toStdString() << "\"" << std::endl;
-            if (unit.toLower() == "gb") return size * 1024;
-            if (unit.toLower() == "mb") return size;
-            if (unit.toLower() == "kb") return size / 1024;
-            return size / 1048576;
+            if (unit.toLower() == "gb") size = size * 1024;
+            else if (unit.toLower() == "mb") size = size;
+            else if (unit.toLower() == "kb") size = size / 1024;
+            else size = size / 1048576;
+
+            if (isMemFree) available = size;
+            else total = size;
+        }
+        if (available != -1 && total != -1) {
+            fclose(meminfo);
+            return;
         }
     }
     fclose(meminfo);
-    return -1;
 }
 
 int
@@ -109,6 +121,7 @@
     __int64 available, total, totalFree;
     if (GetDiskFreeSpaceEx(path, &available, &total, &totalFree)) {
         available /= 1048576;
+        if (available > INT_MAX) available = INT_MAX;
         return int(available);
     } else {
         std::cerr << "WARNING: GetDiskFreeSpaceEx failed: error code "
@@ -121,7 +134,9 @@
         // do the multiplies and divides in this order to reduce the
         // likelihood of arithmetic overflow
         std::cerr << "statvfs(" << path << ") says available: " << buf.f_bavail << ", block size: " << buf.f_bsize << std::endl;
-        return ((buf.f_bavail / 1024) * buf.f_bsize) / 1024;
+        uint64_t available = ((buf.f_bavail / 1024) * buf.f_bsize) / 1024;
+        if (available > INT_MAX) available = INT_MAX;
+        return int(available);
     } else {
         perror("statvfs failed");
         return -1;