9 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
10 #define __STDC_WANT_LIB_EXT1__ 1
17 #if defined(__APPLE__)
18 #include <mach/mach.h>
19 #include <sys/sysctl.h>
22 #elif defined(unix) || defined(__unix) || defined(__unix__)
23 #include <sys/sysinfo.h>
26 #elif defined(_WIN32) || defined(_WIN64)
35 #if defined(__APPLE__)
37 size_t size =
sizeof(memsize);
38 if (sysctlbyname(
"hw.memsize", &memsize, &size,
nullptr, 0) == 0)
42 #elif defined(unix) || defined(__unix) || defined(__unix__)
43 int64_t pages = sysconf(_SC_PHYS_PAGES);
44 int64_t page_size = sysconf(_SC_PAGESIZE);
45 if ((pages > 0) && (page_size > 0))
46 return pages * page_size;
49 #elif defined(_WIN32) || defined(_WIN64)
50 MEMORYSTATUSEX status;
51 status.dwLength =
sizeof(status);
52 GlobalMemoryStatusEx(&status);
53 return status.ullTotalPhys;
55 #error Unsupported platform
61 #if defined(__APPLE__)
62 mach_port_t host_port = mach_host_self();
63 if (host_port == MACH_PORT_NULL)
66 vm_size_t page_size = 0;
67 host_page_size(host_port, &page_size);
69 vm_statistics_data_t vmstat;
70 mach_msg_type_number_t count = HOST_VM_INFO_COUNT;
71 kern_return_t kernReturn = host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vmstat, &count);
72 if (kernReturn != KERN_SUCCESS)
75 [[maybe_unused]] int64_t used_mem = (vmstat.active_count + vmstat.inactive_count + vmstat.wire_count) * page_size;
76 int64_t free_mem = vmstat.free_count * page_size;
78 #elif defined(unix) || defined(__unix) || defined(__unix__)
79 int64_t pages = sysconf(_SC_AVPHYS_PAGES);
80 int64_t page_size = sysconf(_SC_PAGESIZE);
81 if ((pages > 0) && (page_size > 0))
82 return pages * page_size;
85 #elif defined(_WIN32) || defined(_WIN64)
86 MEMORYSTATUSEX status;
87 status.dwLength =
sizeof(status);
88 GlobalMemoryStatusEx(&status);
89 return status.ullAvailPhys;
91 #error Unsupported platform
97 const char* ptr = (
const char*)buffer;
98 for (
size_t i = 0; i < size; ++i)
106 #ifdef __STDC_LIB_EXT1__
107 memset_s(buffer, size, 0, size);
108 #elif defined(_WIN32) || defined(_WIN64)
109 SecureZeroMemory(buffer, size);
111 volatile char* ptr = (
volatile char*)buffer;
119 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
120 int fd = open(
"/dev/urandom", O_RDONLY);
123 ssize_t count = read(fd, buffer, size);
126 int result = close(fd);
129 #elif defined(_WIN32) || defined(_WIN64)
130 char* ptr = (
char*)buffer;
131 for(
size_t i = 0; i < size; ++i)
132 ptr[i] = rand() % 256;
138 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
139 int fd = open(
"/dev/random", O_RDONLY);
142 ssize_t count = read(fd, buffer, size);
145 int result = close(fd);
148 #elif defined(_WIN32) || defined(_WIN64)
149 HCRYPTPROV hCryptContext;
150 if (!CryptAcquireContext(&hCryptContext, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
152 if (!CryptGenRandom(hCryptContext, (DWORD)size, (BYTE*)buffer))
154 if (!CryptReleaseContext(hCryptContext, 0))
static bool IsZero(const void *buffer, size_t size) noexcept
Is the given memory buffer filled with zeros?
static void CryptoFill(void *buffer, size_t size)
Fill the given memory buffer with cryptographic strong random bytes.
static int64_t RamFree()
Free RAM in bytes.
static int64_t RamTotal()
Total RAM in bytes.
static void ZeroFill(void *buffer, size_t size)
Fill the given memory buffer with zeros.
static void RandomFill(void *buffer, size_t size)
Fill the given memory buffer with random bytes.
#define throwex
Throw extended exception macro.
Memory management definition.
C++ Common project definitions.