1 // vim: set tabstop=4 shiftwidth=4 noexpandtab:
3 Gwenview: an image viewer
4 Copyright 2008 Aurélien Gâteau <aurelien.gateau@free.fr>
5 Copyright (C) 2004-2005 by Enrico Ros <eros.kde@email.it>
6 Copyright (C) 2004-2007 by Albert Astals Cid <aacid@kde.org>
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA.
24 #include "memoryutils.h"
28 #include <QTextStream>
33 #define _WIN32_WINNT 0x0500
40 namespace MemoryUtils
{
42 // This code has been copied from okular/core/document.cpp
43 qulonglong
getTotalMemory() {
44 static qulonglong cachedValue
= 0;
48 #if defined(Q_OS_LINUX)
49 // if /proc/meminfo doesn't exist, return 128MB
50 QFile
memFile( "/proc/meminfo" );
51 if ( !memFile
.open( QIODevice::ReadOnly
) )
52 return (cachedValue
= 134217728);
54 // read /proc/meminfo and sum up the contents of 'MemFree', 'Buffers'
55 // and 'Cached' fields. consider swapped memory as used memory.
56 QTextStream
readStream( &memFile
);
59 QString entry
= readStream
.readLine();
60 if ( entry
.isNull() ) break;
61 if ( entry
.startsWith( "MemTotal:" ) )
62 return (cachedValue
= (1024 * entry
.section( ' ', -2, -2 ).toInt()));
64 #elif defined(Q_OS_WIN)
67 GlobalMemoryStatusEx (&stat
);
69 return ( cachedValue
= stat
.ullTotalPhys
);
71 return (cachedValue
= 134217728);
75 qulonglong
getFreeMemory() {
76 static QTime lastUpdate
= QTime::currentTime();
77 static qulonglong cachedValue
= 0;
79 if ( lastUpdate
.secsTo( QTime::currentTime() ) <= 2 )
82 #if defined(Q_OS_LINUX)
83 // if /proc/meminfo doesn't exist, return MEMORY FULL
84 QFile
memFile( "/proc/meminfo" );
85 if ( !memFile
.open( QIODevice::ReadOnly
) )
88 // read /proc/meminfo and sum up the contents of 'MemFree', 'Buffers'
89 // and 'Cached' fields. consider swapped memory as used memory.
90 qulonglong memoryFree
= 0;
92 QTextStream
readStream( &memFile
);
95 entry
= readStream
.readLine();
96 if ( entry
.isNull() ) break;
97 if ( entry
.startsWith( "MemFree:" ) ||
98 entry
.startsWith( "Buffers:" ) ||
99 entry
.startsWith( "Cached:" ) ||
100 entry
.startsWith( "SwapFree:" ) )
101 memoryFree
+= entry
.section( ' ', -2, -2 ).toInt();
102 if ( entry
.startsWith( "SwapTotal:" ) )
103 memoryFree
-= entry
.section( ' ', -2, -2 ).toInt();
107 lastUpdate
= QTime::currentTime();
109 return ( cachedValue
= (1024 * memoryFree
) );
110 #elif defined(Q_OS_WIN)
113 GlobalMemoryStatusEx (&stat
);
115 lastUpdate
= QTime::currentTime();
117 return ( cachedValue
= stat
.ullAvailPhys
);
119 // tell the memory is full.. will act as in LOW profile
124 } // MemoryUtils namespace
126 } // Gwenview namespace