Bug 801818 - Add an API to check if the platform is a low-memory one. r=bsmedberg, blassey

This commit is contained in:
Kartikaya Gupta 2012-10-31 14:59:55 -04:00
Родитель 453addf297
Коммит 7acbfada91
3 изменённых файлов: 50 добавлений и 1 удалений

Просмотреть файл

@ -37,7 +37,7 @@
* realloc() has failed.
*/
[scriptable, uuid(59e7e77a-38e4-11d4-8cf5-0060b0fc14a3)]
[scriptable, uuid(6aef11c4-8615-44a6-9711-98f43805693d)]
interface nsIMemory : nsISupports
{
/**
@ -93,5 +93,13 @@ interface nsIMemory : nsISupports
* DEPRECATED - Always returns false. See bug 592308.
*/
boolean isLowMemory();
/**
* This predicate can be used to determine if the platform is a "low-memory"
* platform. Callers may use this to dynamically tune their behaviour
* to favour reduced memory usage at the expense of performance. The value
* returned by this function will not change over the lifetime of the process.
*/
boolean isLowMemoryPlatform();
};

Просмотреть файл

@ -20,6 +20,11 @@
#include "nsString.h"
#include "mozilla/Services.h"
#ifdef ANDROID
#include <stdio.h>
#define LOW_MEMORY_THRESHOLD_KB (256 * 1024)
#endif
static nsMemoryImpl sGlobalMemory;
NS_IMPL_QUERY_INTERFACE1(nsMemoryImpl, nsIMemory)
@ -56,6 +61,37 @@ nsMemoryImpl::IsLowMemory(bool *result)
return NS_OK;
}
NS_IMETHODIMP
nsMemoryImpl::IsLowMemoryPlatform(bool *result)
{
#ifdef ANDROID
static int sLowMemory = -1; // initialize to unknown, lazily evaluate to 0 or 1
if (sLowMemory == -1) {
sLowMemory = 0; // assume "not low memory" in case file operations fail
*result = false;
// check if MemTotal from /proc/meminfo is greater than LOW_MEMORY_THRESHOLD_KB
FILE* fd = fopen("/proc/meminfo", "r");
if (!fd) {
return NS_OK;
}
uint64_t mem = 0;
int rv = fscanf(fd, "MemTotal: %lu kB", &mem);
if (fclose(fd)) {
return NS_OK;
}
if (rv != 1) {
return NS_OK;
}
sLowMemory = (mem > LOW_MEMORY_THRESHOLD_KB) ? 0 : 1;
}
*result = (sLowMemory == 1);
#else
*result = false;
#endif
return NS_OK;
}
/*static*/ nsresult
nsMemoryImpl::Create(nsISupports* outer, const nsIID& aIID, void **aResult)
{

Просмотреть файл

@ -148,6 +148,11 @@ NS_IMETHODIMP BackwardsAllocator::IsLowMemory(bool* retval)
return NS_OK;
}
NS_IMETHODIMP BackwardsAllocator::IsLowMemoryPlatform(bool* retval)
{
*retval = false;
return NS_OK;
}
nsresult TestBackwardsAllocator()
{