summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlbert Astals Cid <tsdgeos@terra.es>2012-06-19 22:33:43 (GMT)
committerAlbert Astals Cid <tsdgeos@terra.es>2012-06-19 22:38:17 (GMT)
commit5fb937a46aaf4e531ada816692b1e47fc95b20b0 (patch)
tree881c7d076df2d4ad154cd15199ff4012646bd3e9
parent2a5257af50253500e47e8e701f1a7f3713c8237c (diff)
Fix underflow in getFreeMemory()
It actually serves three purposes: a) Make sure all the values are there (this should be always true, but doesn't hurt making sure) because if SwapFree was there but SwapTotal was not, it'd be a mess b) add up things in order so we don't underflow, currently the code did process stuff as it came in the file, and it happens that SwapTotal appears before SwapFree in /proc/meminfo so it actually did "MemFree:" + "Buffers:" + "Cached:" - "SwapTotal:" + "SwapFree:", which can underflow if "MemFree:" + "Buffers:" + "Cached:" < "SwapTotal:" c) Do not underflow at all, so if "MemFree:" + "Buffers:" + "Cached:" + "SwapFree:" < "SwapTotal:" we return 0 correctly not a zillion of free memory Aurélien you might want to update gwenviews copy of this code (there's a few other bugfixes we did a while ago you didn't update either) CCMAIL: agateau@kde.org BUGS: 291129 (cherry picked from commit 444e6b7b19bee5285f39a44007cf00f6e0b235d2)
-rw-r--r--core/document.cpp29
1 files changed, 22 insertions, 7 deletions
diff --git a/core/document.cpp b/core/document.cpp
index 2e39221..3d40e8d 100644
--- a/core/document.cpp
+++ b/core/document.cpp
@@ -288,19 +288,34 @@ qulonglong DocumentPrivate::getFreeMemory()
qulonglong memoryFree = 0;
QString entry;
QTextStream readStream( &memFile );
+ static const int nElems = 5;
+ QString names[nElems] = { "MemFree:", "Buffers:", "Cached:", "SwapFree:", "SwapTotal:" };
+ qulonglong values[nElems] = { 0, 0, 0, 0, 0 };
+ bool foundValues[nElems] = { false, false, false, false, false };
while ( true )
{
entry = readStream.readLine();
if ( entry.isNull() ) break;
- if ( entry.startsWith( "MemFree:" ) ||
- entry.startsWith( "Buffers:" ) ||
- entry.startsWith( "Cached:" ) ||
- entry.startsWith( "SwapFree:" ) )
- memoryFree += entry.section( ' ', -2, -2 ).toULongLong();
- if ( entry.startsWith( "SwapTotal:" ) )
- memoryFree -= entry.section( ' ', -2, -2 ).toULongLong();
+ for ( int i = 0; i < nElems; ++i )
+ {
+ if ( entry.startsWith( names[i] ) )
+ {
+ values[i] = entry.section( ' ', -2, -2 ).toULongLong( &foundValues[i] );
+ }
+ }
}
memFile.close();
+ bool found = true;
+ for ( int i = 0; found && i < nElems; ++i )
+ found = found && foundValues[i];
+ if ( found )
+ {
+ memoryFree = values[0] + values[1] + values[2] + values[3];
+ if ( values[4] > memoryFree )
+ memoryFree = 0;
+ else
+ memoryFree -= values[4];
+ }
lastUpdate = QTime::currentTime();