summaryrefslogtreecommitdiffstats
path: root/src/detection/memory/memory_apple.c
diff options
context:
space:
mode:
authorsumuel <samuel@yakubos.org>2026-08-17 20:44:55 +0000
committersumuel <samuel@yakubos.org>2026-08-17 20:44:55 +0000
commit76424950e373d3b04ac3dd13019151bfba3e8423 (patch)
tree4c3cfdbda039e592b9186be3e28f8d7cfd439e8a /src/detection/memory/memory_apple.c
Add the files
Diffstat (limited to 'src/detection/memory/memory_apple.c')
-rw-r--r--src/detection/memory/memory_apple.c35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/detection/memory/memory_apple.c b/src/detection/memory/memory_apple.c
new file mode 100644
index 0000000..6203949
--- /dev/null
+++ b/src/detection/memory/memory_apple.c
@@ -0,0 +1,35 @@
+#include "memory.h"
+#include "common/debug.h"
+
+#include <string.h>
+#include <mach/mach.h>
+#include <sys/sysctl.h>
+#include <unistd.h>
+
+const char* ffDetectMemory(FFMemoryResult* ram) {
+ size_t length = sizeof(ram->bytesTotal);
+
+#if FF_APPLE_MEMSIZE_USABLE
+ if (sysctlbyname("hw.memsize_usable", &ram->bytesTotal, &length, NULL, 0) != 0) {
+ return "Failed to read hw.memsize_usable";
+ }
+#else
+ if (sysctl((int[]) { CTL_HW, HW_MEMSIZE }, 2, &ram->bytesTotal, &length, NULL, 0) != 0) {
+ return "Failed to read hw.memsize";
+ }
+#endif
+
+ mach_msg_type_number_t count = HOST_VM_INFO64_COUNT;
+ vm_statistics64_data_t vmstat;
+ if (host_statistics64(mach_host_self(), HOST_VM_INFO64, (host_info64_t) (&vmstat), &count) != KERN_SUCCESS) {
+ return "Failed to read host_statistics64";
+ }
+
+ // https://github.com/st3fan/osx-10.9/blob/34e34a6a539b5a822cda4074e56a7ced9b57da71/system_cmds-597.1.1/vm_stat.tproj/vm_stat.c#L139
+
+ uint64_t pagesFree = vmstat.free_count - vmstat.speculative_count;
+ uint64_t pagesFileBacked = vmstat.external_page_count; // Cached files
+ ram->bytesUsed = ram->bytesTotal - (pagesFree + pagesFileBacked) * instance.state.platform.sysinfo.pageSize;
+
+ return NULL;
+}