CPMemUtils.cpp
Go to the documentation of this file.
00001 #include "CPMemUtils.h"
00002 
00003 unsigned long getPhysicalMemorySize()
00004 {
00005 #ifdef __APPLE__
00006 
00007         /*
00008         int mib[2];
00009         size_t len;
00010         unsigned long mem_size;
00011         len = sizeof(mem_size);
00012 
00013         mib[0] = CTL_HW;
00014         mib[1] = W_PHYSMEM;
00015         int res = sysctl(mib, 2, &mem_size, &len, NULL, 0);
00016         if(res == 0)
00017         {
00018                 return mem_size;
00019         }
00020         else
00021         {
00022                 return 0;
00023         }
00024         */
00025 
00026         return 1024 * 1024 * 1024;
00027  
00028 #else
00029 
00030 #ifdef _MSC_VER
00031         MEMORYSTATUSEX statex;
00032 
00033         statex.dwLength = sizeof (statex);
00034 
00035         GlobalMemoryStatusEx (&statex);
00036 
00037         return statex.ullTotalPhys;
00038 
00039 
00040 #else
00041         //struct sysinfo s_info;
00042         //sysinfo(&s_info);
00043 
00044         //printf("s_info.totalram %d", s_info.totalram);
00045         //return ((unsigned long)s_info.totalram) * ((unsigned long)s_info.mem_unit);
00046         unsigned long int pageSize = sysconf (_SC_PAGESIZE);
00047         unsigned long int pageNum = sysconf (_SC_PHYS_PAGES);
00048         
00049         // HACK: Cygwin running under PAE-enabled windows report page size of 65536
00050 #ifdef __CYGWIN__
00051         pageSize = 4096;
00052 //      printf("Compiled by Cygwin");
00053 #endif
00054         //printf("ram size %ld\n", pageSize * pageNum);
00055         //printf("page size %ld\n", pageSize );
00056         //printf("page num %ld\n",  pageNum);
00057         return pageSize * pageNum;
00058 
00059 #endif
00060 #endif
00061 
00062 
00063 }
00064 
00065 unsigned long getCurrentProcessMemoryUsage()
00066 {
00067 #ifdef __APPLE__
00068     // Inspired by:
00069     // http://miknight.blogspot.com/2005/11/resident-set-size-in-mac-os-x.html
00070     struct task_basic_info t_info;
00071     mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
00072     task_info(current_task(), TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count);
00073     size_t size = t_info.resident_size; //: t_info.virtual_size);
00074     return size;
00075 
00076 #else
00077 #ifdef _MSC_VER
00078         // Open current process
00079         HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, GetCurrentProcessId());
00080 
00081         if(hProcess)
00082         {
00083                 PROCESS_MEMORY_COUNTERS ProcessMemoryCounters;
00084 
00085                 memset(&ProcessMemoryCounters, 0, sizeof(ProcessMemoryCounters));
00086 
00087                 // Set size of structure
00088                 ProcessMemoryCounters.cb = sizeof(ProcessMemoryCounters);
00089 
00090                 // Get memory usage
00091                 if(GetProcessMemoryInfo(hProcess,       &ProcessMemoryCounters, sizeof(ProcessMemoryCounters)) == TRUE)
00092                 {
00093                         return ProcessMemoryCounters.WorkingSetSize;
00094                 }
00095                 else
00096                 {
00097                         return 0;
00098                         //::GetLastError()
00099                 }
00100 
00101                 // Close process
00102                 CloseHandle(hProcess);
00103         }
00104         else
00105                 return 0;
00106                 //std::cout << "Could not open process (Error " << ::GetLastError() << ")" << std::endl;
00107 #else
00108         struct mallinfo info;
00109 
00110         /* what is the largest ECB heap buffer currently available? */
00111         info = mallinfo();
00112 
00113         return info.arena;
00114 
00115 #endif
00116 #endif
00117 
00118 
00119 
00120 }
00121 
00122 unsigned long getPlatformMemoryLimit()
00123 {
00124 #ifdef __APPLE__
00125 
00126 #else
00127 #ifdef _MSC_VER
00128 
00129 
00130 #else
00131 
00132 #endif
00133 #endif
00134 
00135         static unsigned long memLimit = -1;
00136         if(memLimit == -1)
00137         {
00138                 memLimit = (unsigned long)(getPhysicalMemorySize() * 0.75);
00139         }
00140         return memLimit;
00141 }
00142 
00143 
00144 
00145 
00146 


appl
Author(s): petercai
autogenerated on Tue Jan 7 2014 11:02:28