Skip to content

Commit

Permalink
Fix xtd::native::memory::get_total_physical_memory and xtd::native::m…
Browse files Browse the repository at this point in the history
…emory::get_used_physical_memory methods on FreeBSD
  • Loading branch information
gammasoft71 committed Aug 11, 2024
1 parent 684ee7c commit 52bcecd
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/xtd.core.native.unix/src/xtd/native/unix/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#undef __XTD_CORE_NATIVE_LIBRARY__

#include <unistd.h>
#include <sys/sysctl.h>
#include <sys/types.h>
#include <sys/resource.h>
#include <sys/sysinfo.h>
Expand All @@ -11,11 +12,14 @@
#include <fstream>
#include <string>


using namespace xtd::native;

size_t memory::get_total_physical_memory() {
return sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGE_SIZE);
auto mib = std::array<int32_t, 2> {CTL_HW, HW_MEMSIZE};
auto total_physical_memory = size_t {};
auto length = sizeof(size_t);
sysctl(mib.data(), 2, &total_physical_memory, &length, nullptr, 0);
return total_physical_memory;
}

size_t memory::get_total_process_memory() {
Expand All @@ -39,7 +43,11 @@ size_t memory::get_total_virtual_memory() {
}

size_t memory::get_used_physical_memory() {
return get_total_physical_memory() - sysconf(_SC_AVPHYS_PAGES) * sysconf(_SC_PAGE_SIZE);
auto mib = std::array<int32_t, 2> {CTL_HW, VM_FREE_COUNT};
auto free_physical_memory = size_t {};
auto length = sizeof(size_t);
sysctl(mib.data(), 2, &free_physical_memory, &length, nullptr, 0);
return get_total_physical_memory() - free_physical_memory;
}

size_t memory::get_used_process_memory() {
Expand Down

0 comments on commit 52bcecd

Please sign in to comment.