From 0eb401cdcc624ce3fad6aaab7f0d23018c6d52c3 Mon Sep 17 00:00:00 2001 From: Drath Date: Fri, 2 Jan 2026 13:44:01 +0100 Subject: [PATCH] use std::array instead of c-style bufs --- src/cpu.cpp | 8 +++++--- src/gpu.cpp | 17 +++++++++-------- src/sys.cpp | 14 +++++++------- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/cpu.cpp b/src/cpu.cpp index ef09845..7c735bd 100644 --- a/src/cpu.cpp +++ b/src/cpu.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -22,7 +23,7 @@ void detectHwmon(cpuSensors* target, const std::string& pci) { } void update(cpuSensors* c) { - static char buf[32]; + static std::array buf; struct EnergyTs { uint64_t energy = 0; std::chrono::time_point ts; @@ -41,8 +42,9 @@ void update(cpuSensors* c) { // lots of reads but still faster than reading /proc/cpuinfo c->avg_freq = 0; for (uint8_t i = 0; i < N_CPU; i++) { - utils::readFile("/sys/devices/system/cpu/cpu" + std::to_string(i) + "/cpufreq/cpuinfo_avg_freq", buf, sizeof(buf)); - c->avg_freq += strtol(buf, nullptr, 10) / 1000; + uint32_t t = 0; + utils::fastReadNum<1000>("/sys/devices/system/cpu/cpu" + std::to_string(i) + "/cpufreq/cpuinfo_avg_freq", buf, t); + c->avg_freq += t; } c->avg_freq /= N_CPU; diff --git a/src/gpu.cpp b/src/gpu.cpp index d2995e4..37e3205 100644 --- a/src/gpu.cpp +++ b/src/gpu.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -24,8 +25,8 @@ void detectHwmon(gpuSensors* target, const std::string& pci) { } /* some metrics are missing in gpu_metrics, we always fill them from fs, unused metrics are commented out */ -void fillMissingMetrics(gpuSensors* g) { - static char buf[32]; +void fillMissingMetrics(gpuSensors* g) {; + static std::array buf; utils::fastReadNum<1000>(g->hwmon + "/in0_input", buf, g->vcore); // g->fan_pwm = std::stoi(readFile(g->hwmon + "/pwm1", 4)) * 100 / 256; utils::fastReadNum(g->hwmon + "/fan1_input", buf, g->fan_speed); @@ -37,7 +38,7 @@ void fillMissingMetrics(gpuSensors* g) { /* get sensors for pre-vega cards (rx480/580) */ void updateFromFs(gpuSensors* g) { - static char buf[32]; + static std::array buf; utils::fastReadNum<1000000>(g->hwmon + "/freq1_input", buf, g->clock_core); utils::fastReadNum<1000000>(g->hwmon + "/freq2_input", buf, g->clock_mem); utils::fastReadNum<1000000>(g->hwmon + "/power1_input", buf, g->power_avg); @@ -47,13 +48,13 @@ void updateFromFs(gpuSensors* g) { /* get sensors for vega and newer (rx5700xt, rx7800xt [*]) */ void updateFromMetrics(gpuSensors* g) { - char raw_metrics[max_metrics_length]; - if (!utils::readFile(g->hwmon + rel_metrics_path, raw_metrics, sizeof(raw_metrics))) return; + std::array raw_metrics{}; + if (!utils::readFile(g->hwmon + rel_metrics_path, raw_metrics)) return; - switch (const auto header = reinterpret_cast(raw_metrics); header->format_revision * 10 + header->content_revision) { + switch (const auto header = reinterpret_cast(raw_metrics.data()); header->format_revision * 10 + header->content_revision) { /* rx5700xt, rx7800xt */ case 13: { - const auto metrics = reinterpret_cast(raw_metrics); + const auto metrics = reinterpret_cast(raw_metrics.data()); g->usage_core = metrics->average_gfx_activity; g->usage_umc = metrics->average_umc_activity; g->usage_media = metrics->average_mm_activity; @@ -80,7 +81,7 @@ void updateFromMetrics(gpuSensors* g) { /* ryzen 5 5600g */ case 22: { - const auto metrics = reinterpret_cast(raw_metrics); + const auto metrics = reinterpret_cast(raw_metrics.data()); g->temp_edge = metrics->temperature_gfx / 100; break; } diff --git a/src/sys.cpp b/src/sys.cpp index 8d05274..6c012b2 100644 --- a/src/sys.cpp +++ b/src/sys.cpp @@ -30,8 +30,8 @@ void sys::getTimeDate(TimeDate* td) { } void sys::getRam(Memory* mem) { - static char buf[128]; // file is longer but we only need first 128 chars - utils::readFile("/proc/meminfo", buf, sizeof(buf)); + static std::array buf; // file is longer but we only need first 128 chars + utils::readFile("/proc/meminfo", buf); mem->free = getVal("MemAvailable:", std::string_view(buf)); mem->total = getVal("MemTotal:", std::string_view(buf)); // shouldn't really update this on each loop mem->used = mem->total - mem->free; @@ -46,15 +46,15 @@ void sys::getDisk(Disk* disk) { } void sys::getNetwork(Network* net, const std::string& device) { - struct _net { + struct Network { std::chrono::time_point ts; uint64_t rx = 0; uint64_t tx = 0; }; - static char buf[32]; - static _net prev; - _net current; + static std::array buf; + static Network prev; + Network current; current.ts = std::chrono::steady_clock::now(); utils::fastReadNum("/sys/class/net/" + device + "/statistics/rx_bytes", buf, current.rx); @@ -78,7 +78,7 @@ void sys::detectHwmon(sysSensors* s, const std::string& platform) { void sys::updateSensors(sysSensors* s) { if (s->hwmon.empty()) return; - static char buf[8]; + static std::array buf; utils::fastReadNum(s->hwmon + "/fan2_input", buf, s->fan_speed[0]); utils::fastReadNum(s->hwmon + "/fan7_input", buf, s->fan_speed[1]); utils::fastReadNum<1000>(s->hwmon + "/temp5_input", buf, s->t_sensor);