From a05f51ee0803658f3661a5daa10fcb4195d1219e Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Tue, 16 Feb 2016 22:27:07 +0100 Subject: [PATCH] Replace sysctl on Mac with a syscall that is supported in all kernels --- .../testfake/FakeBlockStore.cpp | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/blockstore/implementations/testfake/FakeBlockStore.cpp b/src/blockstore/implementations/testfake/FakeBlockStore.cpp index 8113000b..d16d32d6 100644 --- a/src/blockstore/implementations/testfake/FakeBlockStore.cpp +++ b/src/blockstore/implementations/testfake/FakeBlockStore.cpp @@ -1,6 +1,7 @@ #include "FakeBlock.h" #include "FakeBlockStore.h" #include +#include using std::make_shared; using std::string; @@ -77,10 +78,21 @@ uint64_t FakeBlockStore::numBlocks() const { } uint64_t FakeBlockStore::estimateNumFreeBytes() const { - //For windows, see http://stackoverflow.com/a/2513561/829568 + uint64_t mem; +#ifdef __APPLE__ + size_t size = sizeof(mem); + int result = sysctlbyname("hw.memsize", &mem, &size, nullptr, 0); + if (0 != result) { + throw std::runtime_error("sysctlbyname syscall failed"); + } +#elif __linux__ long numRAMPages = sysconf(_SC_PHYS_PAGES); - long pageSize = sysconf(_SC_PAGE_SIZE); - return numRAMPages*pageSize; + long pageSize = sysconf(_SC_PAGESIZE); + mem = numRAMPages * pageSize; +#else +#error Not supported on windows yet, TODO http://stackoverflow.com/a/2513561/829568 +#endif + return mem; } }