Skip to content

Commit

Permalink
Use Apple-specific API to determine system memory on macOS
Browse files Browse the repository at this point in the history
The unistd.h API that had been used for both Linux and macOS is not
always available in macOS environments, for example when building with
upstream clang rather than AppleClang.

Closes #1923
  • Loading branch information
wshanks committed Dec 18, 2023
1 parent 49667a0 commit 11f6afa
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/framework/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@
#include <intrin.h>
#endif

#if defined(__linux__) || defined(__APPLE__)
#if defined(__linux__)
#include <unistd.h>
#elif defined(__APPLE__)
#include <sys/types.h>
#include <sys/sysctl.h>
#elif defined(_WIN64) || defined(_WIN32)
// This is needed because windows.h redefine min()/max() so interferes with
// std::min/max
Expand Down Expand Up @@ -1270,10 +1273,13 @@ uint_t (*popcount)(uint_t) = &_naive_weight;

size_t get_system_memory_mb() {
size_t total_physical_memory = 0;
#if defined(__linux__) || defined(__APPLE__)
#if defined(__linux__)
size_t pages = (size_t)sysconf(_SC_PHYS_PAGES);
size_t page_size = (size_t)sysconf(_SC_PAGE_SIZE);
total_physical_memory = pages * page_size;
#elif defined(__APPLE__)
size_t len = sizeof(total_physical_memory);
sysctlbyname("hw.memsize", &total_physical_memory, &len, NULL, 0);
#elif defined(_WIN64) || defined(_WIN32)
MEMORYSTATUSEX status;
status.dwLength = sizeof(status);
Expand Down

0 comments on commit 11f6afa

Please sign in to comment.