00001 //---------------------------------------------------------------------------- 00002 /** @file SgPlatform.cpp */ 00003 //---------------------------------------------------------------------------- 00004 00005 #include "SgSystem.h" 00006 #include "SgPlatform.h" 00007 00008 #include <algorithm> 00009 00010 #ifdef WIN32 00011 #include <windows.h> 00012 #else 00013 #include <unistd.h> 00014 #endif 00015 #ifdef HAVE_SYS_SYSCTL_H 00016 #include <sys/sysctl.h> 00017 #endif 00018 00019 using namespace std; 00020 00021 //---------------------------------------------------------------------------- 00022 00023 std::size_t SgPlatform::TotalMemory() 00024 { 00025 #if defined WIN32 00026 MEMORYSTATUSEX status; 00027 status.dwLength = sizeof(status); 00028 if (! GlobalMemoryStatusEx(&status)) 00029 return 0; 00030 size_t totalVirtual = static_cast<size_t>(status.ullTotalVirtual); 00031 size_t totalPhys = static_cast<size_t>(status.ullTotalPhys); 00032 return min(totalVirtual, totalPhys); 00033 #elif defined _SC_PHYS_PAGES 00034 long pages = sysconf(_SC_PHYS_PAGES); 00035 if (pages < 0) 00036 return 0; 00037 long pageSize = sysconf(_SC_PAGE_SIZE); 00038 if (pageSize < 0) 00039 return 0; 00040 return static_cast<size_t>(pages) * static_cast<size_t>(pageSize); 00041 #elif defined HW_PHYSMEM 00042 // Mac OSX, BSD 00043 unsigned int mem; 00044 size_t len = sizeof mem; 00045 int mib[2] = { CTL_HW, HW_PHYSMEM }; 00046 if (sysctl(mib, 2, &mem, &len, 0, 0) != 0 || len != sizeof mem) 00047 return 0; 00048 else 00049 return mem; 00050 #else 00051 return 0; 00052 #endif 00053 } 00054 00055 //---------------------------------------------------------------------------- 00056