diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..567609b12 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build/ diff --git a/quest/src/core/memory.cpp b/quest/src/core/memory.cpp index 36eedb596..07de5bde2 100644 --- a/quest/src/core/memory.cpp +++ b/quest/src/core/memory.cpp @@ -21,6 +21,16 @@ #include +// Platform-specific includes for RAM querying +#if defined(__linux__) + #include +#elif defined(__APPLE__) + #include + #include +#elif defined(_WIN32) + #include +#endif + /* @@ -196,11 +206,23 @@ qindex mem_getMaxNumKrausMapMatricesBeforeLocalMemSizeofOverflow(int numQubits) qindex mem_tryGetLocalRamCapacityInBytes() { - - /// @todo attempt to find total Ram - - // if we're unable to find total RAM, throw an exception - // (which the caller should catch and gracefully continue) + #if defined(__linux__) + struct sysinfo info; + if (sysinfo(&info) == 0) + return (qindex) info.totalram * info.mem_unit; + #elif defined(__APPLE__) + int mib[2] = {CTL_HW, HW_MEMSIZE}; + int64_t memsize = 0; + size_t len = sizeof(memsize); + if (sysctl(mib, 2, &memsize, &len, NULL, 0) == 0 && memsize > 0) + return (qindex) memsize; + #elif defined(_WIN32) + MEMORYSTATUSEX statex; + statex.dwLength = sizeof(statex); + if (GlobalMemoryStatusEx(&statex)) + return (qindex) statex.ullTotalPhys; + #endif + // fallback: throw exception throw (mem::COULD_NOT_QUERY_RAM) false; }