master ohmu/fastfetch-openbsd / src / detection / swap / swap_apple.c
ohmu  ·  2026-10-15
 1#include "swap.h"
 2
 3#include "common/debug.h"
 4#include "common/sysctl.h"
 5
 6#include <errno.h>
 7#include <string.h>
 8#include <mach/mach.h>
 9
10const char* ffDetectSwap(FFlist* result) {
11    struct xsw_usage xsw;
12    size_t size = sizeof(xsw);
13    if (sysctl((int[]) { CTL_VM, VM_SWAPUSAGE }, 2, &xsw, &size, nullptr, 0) != 0) {
14        FF_DEBUG("sysctl(vm.swapusage) failed: %s", strerror(errno));
15        return "Failed to read vm.swapusage";
16    }
17
18    if (xsw.xsu_total == 0) {
19        if (__builtin_available(macOS 26.0, *)) {
20            // "vm.compressor_mode" no longer exists in macOS 26.0
21        } else {
22            if (ffSysctlGetInt("vm.compressor_mode", 4) <= 2) {
23                return nullptr; // Swap is disabled
24            }
25        }
26    }
27
28    FFSwapResult* swap = FF_LIST_ADD(FFSwapResult, *result);
29    ffStrbufInitStatic(&swap->name, xsw.xsu_encrypted ? "Encrypted" : "Normal");
30    swap->bytesTotal = xsw.xsu_total;
31    swap->bytesUsed = xsw.xsu_used;
32    return nullptr;
33}