Replace sysctl on Mac with a syscall that is supported in all kernels

This commit is contained in:
Sebastian Messmer 2016-02-16 22:27:07 +01:00
parent 0f3c1c501e
commit a05f51ee08

View File

@ -1,6 +1,7 @@
#include "FakeBlock.h" #include "FakeBlock.h"
#include "FakeBlockStore.h" #include "FakeBlockStore.h"
#include <cpp-utils/assert/assert.h> #include <cpp-utils/assert/assert.h>
#include <sys/sysctl.h>
using std::make_shared; using std::make_shared;
using std::string; using std::string;
@ -77,10 +78,21 @@ uint64_t FakeBlockStore::numBlocks() const {
} }
uint64_t FakeBlockStore::estimateNumFreeBytes() 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 numRAMPages = sysconf(_SC_PHYS_PAGES);
long pageSize = sysconf(_SC_PAGE_SIZE); long pageSize = sysconf(_SC_PAGESIZE);
return numRAMPages*pageSize; mem = numRAMPages * pageSize;
#else
#error Not supported on windows yet, TODO http://stackoverflow.com/a/2513561/829568
#endif
return mem;
} }
} }