#if !defined(spp_memory_h_guard) #define spp_memory_h_guard #include #include #include #if defined(_WIN32) || defined( __CYGWIN__) #define SPP_WIN #endif #ifdef SPP_WIN #include #include #undef min #undef max #else #include #include #endif namespace spp { uint64_t GetSystemMemory() { #ifdef SPP_WIN MEMORYSTATUSEX memInfo; memInfo.dwLength = sizeof(MEMORYSTATUSEX); GlobalMemoryStatusEx(&memInfo); return static_cast(memInfo.ullTotalPageFile); #else struct sysinfo memInfo; sysinfo (&memInfo); auto totalVirtualMem = memInfo.totalram; totalVirtualMem += memInfo.totalswap; totalVirtualMem *= memInfo.mem_unit; return static_cast(totalVirtualMem); #endif } uint64_t GetTotalMemoryUsed() { #ifdef SPP_WIN MEMORYSTATUSEX memInfo; memInfo.dwLength = sizeof(MEMORYSTATUSEX); GlobalMemoryStatusEx(&memInfo); return static_cast(memInfo.ullTotalPageFile - memInfo.ullAvailPageFile); #else struct sysinfo memInfo; sysinfo(&memInfo); auto virtualMemUsed = memInfo.totalram - memInfo.freeram; virtualMemUsed += memInfo.totalswap - memInfo.freeswap; virtualMemUsed *= memInfo.mem_unit; return static_cast(virtualMemUsed); #endif } uint64_t GetProcessMemoryUsed() { #ifdef SPP_WIN PROCESS_MEMORY_COUNTERS_EX pmc; GetProcessMemoryInfo(GetCurrentProcess(), reinterpret_cast(&pmc), sizeof(pmc)); return static_cast(pmc.PrivateUsage); #else auto parseLine = [](char* line)->int { auto i = strlen(line); while(*line < '0' || *line > '9') { line++; } line[i-3] = '\0'; i = atoi(line); return i; }; auto file = fopen("/proc/self/status", "r"); auto result = -1; char line[128]; while(fgets(line, 128, file) != nullptr) { if(strncmp(line, "VmSize:", 7) == 0) { result = parseLine(line); break; } } fclose(file); return static_cast(result) * 1024; #endif } uint64_t GetPhysicalMemory() { #ifdef SPP_WIN MEMORYSTATUSEX memInfo; memInfo.dwLength = sizeof(MEMORYSTATUSEX); GlobalMemoryStatusEx(&memInfo); return static_cast(memInfo.ullTotalPhys); #else struct sysinfo memInfo; sysinfo(&memInfo); auto totalPhysMem = memInfo.totalram; totalPhysMem *= memInfo.mem_unit; return static_cast(totalPhysMem); #endif } } #endif // spp_memory_h_guard