Skip to content

Commit 6975a5f

Browse files
minyezPeizeLin
authored andcommitted
enable build and run on macOS
1 parent 9a127aa commit 6975a5f

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

include/Comm/global/Global_Func.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,25 @@
1111
#include <string>
1212
#include <stdexcept>
1313

14+
// BSD like macOS does not have /proc/meminfo
15+
// include unistd.h to get the number of system memory.
16+
#if defined(__MACH__)
17+
#include <unistd.h>
18+
#endif
19+
1420
namespace Comm
1521
{
1622

1723
namespace Global_Func
1824
{
1925
inline std::size_t memory_available()
2026
{
27+
#if defined(__MACH__)
28+
// HACK: always assume half of the system memory free. Only for build and test on macOS
29+
std::size_t pages = sysconf(_SC_PHYS_PAGES);
30+
std::size_t page_size = sysconf(_SC_PAGE_SIZE);
31+
return pages * page_size / 2;
32+
#else
2133
constexpr std::size_t kB_to_B = 1024;
2234
std::ifstream ifs("/proc/meminfo");
2335
int num = 0;
@@ -42,6 +54,7 @@ namespace Global_Func
4254
}
4355
}
4456
throw std::runtime_error("read /proc/meminfo error in " + std::string(__FILE__) + " line " + std::to_string(__LINE__));
57+
#endif
4558
}
4659
}
4760

0 commit comments

Comments
 (0)