forked from cryfs/cryfs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_total_memory.cpp
More file actions
58 lines (47 loc) · 1.13 KB
/
get_total_memory.cpp
File metadata and controls
58 lines (47 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "get_total_memory.h"
#include <stdexcept>
#include <string>
#if defined(__APPLE__)
#include <sys/types.h>
#include <sys/sysctl.h>
namespace cpputils {
namespace system {
uint64_t get_total_memory() {
uint64_t mem;
size_t size = sizeof(mem);
int result = sysctlbyname("hw.memsize", &mem, &size, nullptr, 0);
if (0 != result) {
throw std::runtime_error("sysctlbyname syscall failed");
}
return mem;
}
}
}
#elif defined(__linux__) || defined(__FreeBSD__)
#include <unistd.h>
namespace cpputils {
namespace system {
uint64_t get_total_memory() {
long numRAMPages = sysconf(_SC_PHYS_PAGES);
long pageSize = sysconf(_SC_PAGESIZE);
return numRAMPages * pageSize;
}
}
}
#elif defined(_MSC_VER)
#include <Windows.h>
namespace cpputils {
namespace system {
uint64_t get_total_memory() {
MEMORYSTATUSEX status;
status.dwLength = sizeof(status);
if (!::GlobalMemoryStatusEx(&status)) {
throw std::runtime_error("Couldn't get system memory information. Error code: " + std::to_string(GetLastError()));
}
return status.ullTotalPhys;
}
}
}
#else
#error Unsupported platform
#endif