base/system/memory/private/linux_impl/swap_memory.cpp¶
Swap Space Query Implementation (Linux) More...
Namespaces¶
| Name |
|---|
| cf |
| cf::linux_impl |
Detailed Description¶
Swap Space Query Implementation (Linux)
Author: Charliechen114514 (chengh1922@mails.jlu.edu.cn)
Version: 0.1
Date: 2026-02-27
Copyright: Copyright © 2026
Source code¶
#include "swap_memory.h"
#include <cstdint>
#include <cstdio>
#include <cstring>
namespace cf {
namespace linux_impl {
namespace {
bool parseMemInfoLine(const char* line, const char* fieldName, uint64_t& outKb) {
size_t fieldNameLen = strlen(fieldName);
if (strncmp(line, fieldName, fieldNameLen) != 0) {
return false;
}
const char* p = line + fieldNameLen;
while (*p == ':' || *p == ' ' || *p == '\t') {
p++;
}
if (*p == '\0') {
return false;
}
char* end;
unsigned long value = strtoul(p, &end, 10);
if (end == p) {
return false;
}
outKb = static_cast<uint64_t>(value);
return true;
}
} // anonymous namespace
void querySwapMemory(SwapMemory& swap) {
FILE* fp = fopen("/proc/meminfo", "r");
if (!fp) {
swap.total_bytes = 0;
swap.free_bytes = 0;
return;
}
uint64_t swapTotal = 0;
uint64_t swapFree = 0;
char line[256];
while (fgets(line, sizeof(line), fp)) {
uint64_t value;
if (parseMemInfoLine(line, "SwapTotal", value)) {
swapTotal = value;
} else if (parseMemInfoLine(line, "SwapFree", value)) {
swapFree = value;
}
if (swapTotal > 0 && swapFree > 0) {
break;
}
}
fclose(fp);
swap.total_bytes = swapTotal * 1024;
swap.free_bytes = swapFree * 1024;
}
} // namespace linux_impl
} // namespace cf
Updated on 2026-03-09 at 10:14:01 +0000