ohmu
·
2026-10-15
1#include "swap.h"
2#include "common/debug.h"
3#include <errno.h>
4#include <string.h>
5#include <sys/stat.h>
6#include <sys/swap.h>
7#include <limits.h>
8#include <stdalign.h>
9
10enum { FFMaxNSwap = 8 };
11
12const char* ffDetectSwap(FFlist* result) {
13 char strings[FFMaxNSwap][PATH_MAX];
14 alignas(swaptbl_t) uint8_t buffer[sizeof(swaptbl_t) + sizeof(swapent_t) * (FFMaxNSwap - 1)] = {};
15 swaptbl_t* table = (swaptbl_t*) buffer;
16 table->swt_n = FFMaxNSwap;
17 for (int i = 0; i < FFMaxNSwap; ++i) {
18 table->swt_ent[i].ste_path = strings[i];
19 }
20
21 int size = swapctl(SC_LIST, table);
22 if (size < 0) {
23 FF_DEBUG("swapctl() failed: %s", strerror(errno));
24 return "swapctl() failed";
25 }
26
27 const uint32_t pageSizeShift = instance.state.platform.sysinfo.pageSizeShift;
28 for (int i = 0; i < size; ++i) {
29 FFSwapResult* swap = FF_LIST_ADD(FFSwapResult, *result);
30 ffStrbufInitS(&swap->name, table->swt_ent[i].ste_path);
31 swap->bytesTotal = (uint64_t) table->swt_ent[i].ste_pages << pageSizeShift;
32 swap->bytesUsed = swap->bytesTotal - ((uint64_t) table->swt_ent[i].ste_free << pageSizeShift);
33 }
34
35 return nullptr;
36}