~ubuntu-branches/ubuntu/precise/gwenview/precise-proposed

« back to all changes in this revision

Viewing changes to lib/memoryutils.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2011-12-15 14:17:54 UTC
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: package-import@ubuntu.com-20111215141754-z043hyx69dulbggf
Tags: upstream-4.7.90
Import upstream version 4.7.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// vim: set tabstop=4 shiftwidth=4 noexpandtab:
 
1
// vim: set tabstop=4 shiftwidth=4 expandtab:
2
2
/*
3
3
Gwenview: an image viewer
4
4
Copyright 2008 Aurélien Gâteau <agateau@kde.org>
38
38
#include <vm/vm_param.h>
39
39
#endif
40
40
 
41
 
namespace Gwenview {
42
 
 
43
 
 
44
 
namespace MemoryUtils {
 
41
namespace Gwenview
 
42
{
 
43
 
 
44
namespace MemoryUtils
 
45
{
45
46
 
46
47
// This code has been copied from okular/core/document.cpp
47
 
qulonglong getTotalMemory() {
 
48
qulonglong getTotalMemory()
 
49
{
48
50
    static qulonglong cachedValue = 0;
49
 
    if ( cachedValue )
 
51
    if (cachedValue)
50
52
        return cachedValue;
51
53
 
52
54
#if defined(Q_OS_LINUX)
53
55
    // if /proc/meminfo doesn't exist, return 128MB
54
 
    QFile memFile( "/proc/meminfo" );
55
 
    if ( !memFile.open( QIODevice::ReadOnly ) )
 
56
    QFile memFile("/proc/meminfo");
 
57
    if (!memFile.open(QIODevice::ReadOnly))
56
58
        return (cachedValue = 134217728);
57
59
 
58
60
    // read /proc/meminfo and sum up the contents of 'MemFree', 'Buffers'
59
61
    // and 'Cached' fields. consider swapped memory as used memory.
60
 
    QTextStream readStream( &memFile );
61
 
     while ( true )
62
 
    {
 
62
    QTextStream readStream(&memFile);
 
63
    while (true) {
63
64
        QString entry = readStream.readLine();
64
 
        if ( entry.isNull() ) break;
65
 
        if ( entry.startsWith( "MemTotal:" ) )
66
 
            return (cachedValue = (Q_UINT64_C(1024) * entry.section( ' ', -2, -2 ).toULongLong()));
 
65
        if (entry.isNull()) break;
 
66
        if (entry.startsWith("MemTotal:"))
 
67
            return (cachedValue = (Q_UINT64_C(1024) * entry.section(' ', -2, -2).toULongLong()));
67
68
    }
68
69
#elif defined(Q_OS_FREEBSD)
69
70
    qulonglong physmem;
70
71
    int mib[] = {CTL_HW, HW_PHYSMEM};
71
 
    size_t len = sizeof( physmem );
72
 
    if ( sysctl( mib, 2, &physmem, &len, NULL, 0 ) == 0 )
 
72
    size_t len = sizeof(physmem);
 
73
    if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0)
73
74
        return (cachedValue = physmem);
74
75
#elif defined(Q_OS_WIN)
75
76
    MEMORYSTATUSEX stat;
76
77
    stat.dwLength = sizeof(stat);
77
 
    GlobalMemoryStatusEx (&stat);
 
78
    GlobalMemoryStatusEx(&stat);
78
79
 
79
 
    return ( cachedValue = stat.ullTotalPhys );
 
80
    return (cachedValue = stat.ullTotalPhys);
80
81
#endif
81
82
    return (cachedValue = 134217728);
82
83
}
83
84
 
84
 
 
85
 
qulonglong getFreeMemory() {
 
85
qulonglong getFreeMemory()
 
86
{
86
87
    static QTime lastUpdate = QTime::currentTime();
87
88
    static qulonglong cachedValue = 0;
88
89
 
89
 
    if ( lastUpdate.secsTo( QTime::currentTime() ) <= 2 )
 
90
    if (lastUpdate.secsTo(QTime::currentTime()) <= 2)
90
91
        return cachedValue;
91
92
 
92
93
#if defined(Q_OS_LINUX)
93
94
    // if /proc/meminfo doesn't exist, return MEMORY FULL
94
 
    QFile memFile( "/proc/meminfo" );
95
 
    if ( !memFile.open( QIODevice::ReadOnly ) )
 
95
    QFile memFile("/proc/meminfo");
 
96
    if (!memFile.open(QIODevice::ReadOnly))
96
97
        return 0;
97
98
 
98
99
    // read /proc/meminfo and sum up the contents of 'MemFree', 'Buffers'
99
100
    // and 'Cached' fields. consider swapped memory as used memory.
100
101
    qulonglong memoryFree = 0;
101
102
    QString entry;
102
 
    QTextStream readStream( &memFile );
103
 
    while ( true )
104
 
    {
 
103
    QTextStream readStream(&memFile);
 
104
    while (true) {
105
105
        entry = readStream.readLine();
106
 
        if ( entry.isNull() ) break;
107
 
        if ( entry.startsWith( "MemFree:" ) ||
108
 
                entry.startsWith( "Buffers:" ) ||
109
 
                entry.startsWith( "Cached:" ) ||
110
 
                entry.startsWith( "SwapFree:" ) )
111
 
            memoryFree += entry.section( ' ', -2, -2 ).toULongLong();
112
 
        if ( entry.startsWith( "SwapTotal:" ) )
113
 
            memoryFree -= entry.section( ' ', -2, -2 ).toULongLong();
 
106
        if (entry.isNull()) break;
 
107
        if (entry.startsWith("MemFree:") ||
 
108
                entry.startsWith("Buffers:") ||
 
109
                entry.startsWith("Cached:") ||
 
110
                entry.startsWith("SwapFree:"))
 
111
            memoryFree += entry.section(' ', -2, -2).toULongLong();
 
112
        if (entry.startsWith("SwapTotal:"))
 
113
            memoryFree -= entry.section(' ', -2, -2).toULongLong();
114
114
    }
115
115
    memFile.close();
116
116
 
117
117
    lastUpdate = QTime::currentTime();
118
118
 
119
 
    return ( cachedValue = (Q_UINT64_C(1024) * memoryFree) );
 
119
    return (cachedValue = (Q_UINT64_C(1024) * memoryFree));
120
120
#elif defined(Q_OS_FREEBSD)
121
121
    qulonglong cache, inact, free, psize;
122
122
    size_t cachelen, inactlen, freelen, psizelen;
123
 
    cachelen = sizeof( cache );
124
 
    inactlen = sizeof( inact );
125
 
    freelen = sizeof( free );
126
 
    psizelen = sizeof( psize );
 
123
    cachelen = sizeof(cache);
 
124
    inactlen = sizeof(inact);
 
125
    freelen = sizeof(free);
 
126
    psizelen = sizeof(psize);
127
127
    // sum up inactive, cached and free memory
128
 
    if ( sysctlbyname( "vm.stats.vm.v_cache_count", &cache, &cachelen, NULL, 0 ) == 0 &&
129
 
            sysctlbyname( "vm.stats.vm.v_inactive_count", &inact, &inactlen, NULL, 0 ) == 0 &&
130
 
            sysctlbyname( "vm.stats.vm.v_free_count", &free, &freelen, NULL, 0 ) == 0 &&
131
 
            sysctlbyname( "vm.stats.vm.v_page_size", &psize, &psizelen, NULL, 0 ) == 0 )
132
 
    {
 
128
    if (sysctlbyname("vm.stats.vm.v_cache_count", &cache, &cachelen, NULL, 0) == 0 &&
 
129
            sysctlbyname("vm.stats.vm.v_inactive_count", &inact, &inactlen, NULL, 0) == 0 &&
 
130
            sysctlbyname("vm.stats.vm.v_free_count", &free, &freelen, NULL, 0) == 0 &&
 
131
            sysctlbyname("vm.stats.vm.v_page_size", &psize, &psizelen, NULL, 0) == 0) {
133
132
        lastUpdate = QTime::currentTime();
134
133
        return (cachedValue = (cache + inact + free) * psize);
135
 
    }
136
 
    else
137
 
    {
 
134
    } else {
138
135
        return 0;
139
136
    }
140
137
#elif defined(Q_OS_WIN)
141
138
    MEMORYSTATUSEX stat;
142
139
    stat.dwLength = sizeof(stat);
143
 
    GlobalMemoryStatusEx (&stat);
 
140
    GlobalMemoryStatusEx(&stat);
144
141
 
145
142
    lastUpdate = QTime::currentTime();
146
143
 
147
 
    return ( cachedValue = stat.ullAvailPhys );
 
144
    return (cachedValue = stat.ullAvailPhys);
148
145
#else
149
146
    // tell the memory is full.. will act as in LOW profile
150
147
    return 0;