commit: 3cc76d94ed77dc412a3f2b8b89bb93975a2196d7
Author: Michał Górny <mgorny <AT> gentoo <DOT> org>
AuthorDate: Tue Dec 30 19:40:20 2025 +0000
Commit: Michał Górny <mgorny <AT> gentoo <DOT> org>
CommitDate: Tue Dec 30 19:40:20 2025 +0000
URL: https://gitweb.gentoo.org/proj/steve.git/commit/?id=3cc76d94
Refactor more meminfo logic
Signed-off-by: Michał Górny <mgorny <AT> gentoo.org>
src/steve.cxx | 18 +++++++++++-------
1 file changed, 11 insertions(+), 7 deletions(-)
diff --git a/src/steve.cxx b/src/steve.cxx
index 22e0f7d..435cca4 100644
--- a/src/steve.cxx
+++ b/src/steve.cxx
@@ -221,7 +221,7 @@ static std::optional<long> steve_get_meminfo(int fd,
std::string_view label)
return val;
}
-static void steve_get_memory_use(steve_state *state)
+static std::optional<long> steve_get_meminfo(steve_state *state,
std::string_view label)
{
if (state->meminfo_fd == -2) {
state->meminfo_fd = open("/proc/meminfo", O_RDONLY);
@@ -230,17 +230,21 @@ static void steve_get_memory_use(steve_state *state)
}
if (state->meminfo_fd != -1) {
- auto maybe_val = steve_get_meminfo(state->meminfo_fd,
"\nMemAvailable:");
- if (maybe_val.has_value()) {
- state->memory_avail = maybe_val.value() / 1024;
- return;
- }
+ auto maybe_val = steve_get_meminfo(state->meminfo_fd, label);
+ if (maybe_val.has_value())
+ return maybe_val;
close(state->meminfo_fd);
state->meminfo_fd = -1;
}
- state->memory_avail = -1;
+ return {};
+}
+
+static void steve_get_memory_use(steve_state *state)
+{
+ auto maybe_val = steve_get_meminfo(state, "\nMemAvailable:");
+ state->memory_avail = maybe_val.has_value() ? maybe_val.value() / 1024
: -1;
}
static steve_token_availability steve_can_give_token(steve_state *state,
uint64_t pid)