diff options
author | dougt%meer.net <devnull@localhost> | 2005-06-22 05:16:18 +0000 |
---|---|---|
committer | dougt%meer.net <devnull@localhost> | 2005-06-22 05:16:18 +0000 |
commit | 8e3b7d17949d0518c27aeddce165a8de5159bec9 (patch) | |
tree | 43d529fd5942bf71b3512e6faebeb1635ace0759 | |
parent | 71aeeb5ede1adfbfd6f1f7ab5d60bf1d29b61ae0 (diff) | |
download | nspr-hg-8e3b7d17949d0518c27aeddce165a8de5159bec9.tar.gz |
Adding PR_GetPhysicalMemorySize to WINCE Branch.
-rw-r--r-- | pr/include/prsystem.h | 16 | ||||
-rw-r--r-- | pr/src/misc/prsystem.c | 131 | ||||
-rw-r--r-- | pr/tests/system.c | 1 |
3 files changed, 147 insertions, 1 deletions
diff --git a/pr/include/prsystem.h b/pr/include/prsystem.h index 0dc420b8..b0dfcf9e 100644 --- a/pr/include/prsystem.h +++ b/pr/include/prsystem.h @@ -116,6 +116,22 @@ NSPR_API(PRInt32) PR_GetPageShift(void); */ NSPR_API(PRInt32) PR_GetNumberOfProcessors( void ); +/* +** PR_GetPhysicalMemorySize() -- returns the amount of system RAM +** +** Description: +** PR_GetPhysicalMemorySize() determines the amount of physical RAM +** in the system and returns the size in bytes. +** +** Parameters: +** none +** +** Returns: +** The amount of system RAM, or 0 on failure. +** +*/ +NSPR_API(PRUint64) PR_GetPhysicalMemorySize(void); + PR_END_EXTERN_C #endif /* prsystem_h___ */ diff --git a/pr/src/misc/prsystem.c b/pr/src/misc/prsystem.c index 898cc3ce..d0775d34 100644 --- a/pr/src/misc/prsystem.c +++ b/pr/src/misc/prsystem.c @@ -1,4 +1,4 @@ -/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0/LGPL 2.1 * @@ -38,6 +38,7 @@ #include "primpl.h" #include "prsystem.h" #include "prprf.h" +#include "prlong.h" #if defined(BEOS) #include <kernel/OS.h> @@ -45,6 +46,7 @@ #if defined(OS2) #define INCL_DOS +#define INCL_DOSMISC #include <os2.h> /* define the required constant if it is not already defined in the headers */ #ifndef QSV_NUMPROCESSORS @@ -60,8 +62,14 @@ #include <sys/sysctl.h> #endif +#if defined(DARWIN) +#include <mach/mach_init.h> +#include <mach/mach_host.h> +#endif + #if defined(HPUX) #include <sys/mpctl.h> +#include <sys/pstat.h> #endif #if defined(XP_UNIX) @@ -69,6 +77,29 @@ #include <sys/utsname.h> #endif +#if defined(AIX) +#include <cf.h> +#include <sys/cfgodm.h> +#endif + +#if defined(WIN32) +/* This struct is not present in VC6 headers, so declare it here */ +typedef struct { + DWORD dwLength; + DWORD dwMemoryLoad; + DWORDLONG ullTotalPhys; + DWORDLONG ullAvailPhys; + DWORDLONG ullToalPageFile; + DWORDLONG ullAvailPageFile; + DWORDLONG ullTotalVirtual; + DWORDLONG ullAvailVirtual; + DWORDLONG ullAvailExtendedVirtual; +} PR_MEMORYSTATUSEX; + +/* Typedef for dynamic lookup of GlobalMemoryStatusEx(). */ +typedef BOOL (WINAPI *GlobalMemoryStatusExFn)(PR_MEMORYSTATUSEX *); +#endif + PR_IMPLEMENT(char) PR_GetDirectorySeparator(void) { return PR_DIRECTORY_SEPARATOR; @@ -231,3 +262,101 @@ PR_IMPLEMENT(PRInt32) PR_GetNumberOfProcessors( void ) #endif return(numCpus); } /* end PR_GetNumberOfProcessors() */ + +/* +** PR_GetPhysicalMemorySize() +** +** Implementation notes: +** Every platform does it a bit different. +** bytes is the returned value. +** for each platform's "if defined" section +** declare your local variable +** do your thing, assign to bytes. +** +*/ +PR_IMPLEMENT(PRUint64) PR_GetPhysicalMemorySize(void) +{ + PRUint64 bytes = LL_ZERO; + +#if defined(LINUX) || defined(SOLARIS) + + long pageSize = sysconf(_SC_PAGESIZE); + long pageCount = sysconf(_SC_PHYS_PAGES); + LL_I2L(bytes, pageSize * pageCount); + +#elif defined(HPUX) + + struct pst_static info; + int result = pstat_getstatic(&info, sizeof(info), 1, 0); + if (result == 1) + LL_I2L(bytes, info.physical_memory * info.page_size); + +#elif defined(DARWIN) + + struct host_basic_info hInfo; + mach_msg_type_number_t count; + + int result = host_info(mach_host_self(), + HOST_BASIC_INFO, + (host_info_t) &hInfo, + &count); + if (result == KERN_SUCCESS) + LL_I2L(bytes, hInfo.memory_size); + +#elif defined(WIN32) + + /* Try to use the newer GlobalMemoryStatusEx API for Windows 2000+. */ + GlobalMemoryStatusExFn globalMemory = (GlobalMemoryStatusExFn) NULL; + HMODULE module = GetModuleHandle("kernel32.dll"); + + if (module) { + globalMemory = (GlobalMemoryStatusExFn)GetProcAddress(module, "GlobalMemoryStatusEx"); + + if (globalMemory) { + PR_MEMORYSTATUSEX memStat; + memStat.dwLength = sizeof(memStat); + + if (globalMemory(&memStat)) + LL_UI2L(bytes, memStat.ullTotalPhys); + } + } + + if (LL_EQ(bytes, LL_ZERO)) { + /* Fall back to the older API. */ + MEMORYSTATUS memStat; + memset(&memStat, 0, sizeof(memStat)); + GlobalMemoryStatus(&memStat); + LL_I2L(bytes, memStat.dwTotalPhys); + } + +#elif defined(OS2) + + ULONG ulPhysMem; + DosQuerySysInfo(QSV_TOTPHYSMEM, + QSV_TOTPHYSMEM, + &ulPhysMem, + sizeof(ulPhysMem)); + LL_I2L(bytes, ulPhysMem); + +#elif defined(AIX) + + if (odm_initialize() == 0) { + int how_many; + struct CuAt *obj = getattr("sys0", "realmem", 0, &how_many); + if (obj != NULL) { + PRUint64 kbytes; + LL_I2L(kbytes, atoi(obj->value)); + LL_MUL(bytes, kbytes, 1024); + free(obj); + } + odm_terminate(); + } + +#else + + PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0); + +#endif + + return bytes; +} /* end PR_GetPhysicalMemorySize() */ diff --git a/pr/tests/system.c b/pr/tests/system.c index b4a89a0a..e75a4520 100644 --- a/pr/tests/system.c +++ b/pr/tests/system.c @@ -75,6 +75,7 @@ PRIntn main(PRIntn argc, char **argv) PR_fprintf(output, "Host page size is %d\n", PR_GetPageSize()); PR_fprintf(output, "Page shift is %d\n", PR_GetPageShift()); PR_fprintf(output, "Number of processors is: %d\n", PR_GetNumberOfProcessors()); + PR_fprintf(output, "Physical memory size is: %llu\n", PR_GetPhysicalMemorySize()); return 0; } /* main */ |