Check result of fgets() and sscanf() in wxGetFreeMemory().
This fixes g++ -Wunused-result warnings and also actually makes the code more robust. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@76530 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -1185,22 +1185,29 @@ wxMemorySize wxGetFreeMemory()
|
|||||||
// /proc/meminfo changed its format in kernel 2.6
|
// /proc/meminfo changed its format in kernel 2.6
|
||||||
if ( wxPlatformInfo().CheckOSVersion(2, 6) )
|
if ( wxPlatformInfo().CheckOSVersion(2, 6) )
|
||||||
{
|
{
|
||||||
unsigned long cached, buffers;
|
if ( sscanf(buf, "MemFree: %ld", &memFree) == 1 )
|
||||||
sscanf(buf, "MemFree: %ld", &memFree);
|
{
|
||||||
|
// We consider memory used by the IO buffers and cache as
|
||||||
|
// being "free" too as Linux aggressively uses free memory
|
||||||
|
// for caching and the amount of memory reported as really
|
||||||
|
// free is far too low for lightly loaded system.
|
||||||
|
if ( fgets(buf, WXSIZEOF(buf), fp) )
|
||||||
|
{
|
||||||
|
unsigned long buffers;
|
||||||
|
if ( sscanf(buf, "Buffers: %lu", &buffers) == 1 )
|
||||||
|
memFree += buffers;
|
||||||
|
}
|
||||||
|
|
||||||
fgets(buf, WXSIZEOF(buf), fp);
|
if ( fgets(buf, WXSIZEOF(buf), fp) )
|
||||||
sscanf(buf, "Buffers: %lu", &buffers);
|
{
|
||||||
|
unsigned long cached;
|
||||||
|
if ( sscanf(buf, "Cached: %lu", &cached) == 1 )
|
||||||
|
memFree += cached;
|
||||||
|
}
|
||||||
|
|
||||||
fgets(buf, WXSIZEOF(buf), fp);
|
// values here are always expressed in kB and we want bytes
|
||||||
sscanf(buf, "Cached: %lu", &cached);
|
memFree *= 1024;
|
||||||
|
}
|
||||||
// add to "MemFree" also the "Buffers" and "Cached" values as
|
|
||||||
// free(1) does as otherwise the value never makes sense: for
|
|
||||||
// kernel 2.6 it's always almost 0
|
|
||||||
memFree += buffers + cached;
|
|
||||||
|
|
||||||
// values here are always expressed in kB and we want bytes
|
|
||||||
memFree *= 1024;
|
|
||||||
}
|
}
|
||||||
else // Linux 2.4 (or < 2.6, anyhow)
|
else // Linux 2.4 (or < 2.6, anyhow)
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user