summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAaron Ball <nullspoon@oper.io>2022-06-21 20:49:35 -0600
committerAaron Ball <nullspoon@oper.io>2022-06-21 20:49:35 -0600
commit7e18c143e16cf25210a1ab19b84f4e140d360d08 (patch)
tree28ccf828bb9934ce40c9095ef3becbe8b173d4a8 /src
parentaeadf189497920f0be8d7077809041fab2ed1d9a (diff)
downloadcmon-7e18c143e16cf25210a1ab19b84f4e140d360d08.tar.gz
cmon-7e18c143e16cf25210a1ab19b84f4e140d360d08.tar.xz
Move status struct from heap to stack
This updates the relevant code to move the status struct from the heap into the stack. This simplifies memory management and code.
Diffstat (limited to 'src')
-rw-r--r--src/main.c31
-rw-r--r--src/status.c11
-rw-r--r--src/status.h2
3 files changed, 19 insertions, 25 deletions
diff --git a/src/main.c b/src/main.c
index 4eb6c24..7ee3027 100644
--- a/src/main.c
+++ b/src/main.c
@@ -28,11 +28,11 @@
int main(int argc, char* argv[]) {
struct meminfo minfo; // Struct for relevant data from /proc/meminfo
- struct status* status = status_init();
- if(!status)
- return 1;
+ struct status status; // Struct for tracking measurement statuses
- status->count += 1;
+ if(! status_init(&status))
+ return 1;
+ status.count += 1;
if(! meminfo_init(&minfo)) {
fprintf(stderr, "ERROR: Could not read meminfo\n");
@@ -40,28 +40,27 @@ int main(int argc, char* argv[]) {
}
// Calculate max memory usage and average memory usage
- if(status->memmax < (minfo.used))
- status->memmax = minfo.used;
- status->memavg = (((status->count - 1) * status->memavg) + minfo.used) / status->count;
+ if(status.memmax < (minfo.used))
+ status.memmax = minfo.used;
+ status.memavg = (((status.count - 1) * status.memavg) + minfo.used) / status.count;
// Calculate load max and incremental load average
double load = cpuinfo_load1m();
- if(status->loadmax < load)
- status->loadmax = load;
- status->loadavg = (((status->count - 1) * status->loadavg) + load) / status->count;
+ if(status.loadmax < load)
+ status.loadmax = load;
+ status.loadavg = (((status.count - 1) * status.loadavg) + load) / status.count;
// Store number of processors to make load values more useful
- status->nprocs = get_nprocs();
+ status.nprocs = get_nprocs();
// Store number of processors to make load values more useful
- status->nettx = net_x_bytes("wlan0", 't');
- status->netrx = net_x_bytes("wlan0", 'r');
+ status.nettx = net_x_bytes("wlan0", 't');
+ status.netrx = net_x_bytes("wlan0", 'r');
// Store uptime
- status->uptime = proc_uptime();
+ status.uptime = proc_uptime();
// Cleanup!
- status_write(status);
- status_free(status);
+ status_write(&status);
return 0;
}
diff --git a/src/status.c b/src/status.c
index 00b9047..8cbf523 100644
--- a/src/status.c
+++ b/src/status.c
@@ -17,8 +17,7 @@
*/
#include "status.h"
-struct status* status_init() {
- struct status* s = malloc(sizeof(struct status));
+int status_init(struct status* s) {
int status = 0;
s->memmax = 0;
@@ -43,11 +42,11 @@ struct status* status_init() {
status += fscanf(fd, "count:%ld\n", &s->count);
if(status != 9) {
fprintf(stderr, "ERROR reading status file\n");
- return NULL;
+ return 0;
}
fclose(fd);
}
- return s;
+ return 1;
}
int status_write(struct status* s) {
@@ -66,7 +65,3 @@ int status_write(struct status* s) {
fclose(fd);
return 1;
}
-
-void status_free(struct status* s) {
- free(s);
-}
diff --git a/src/status.h b/src/status.h
index 05fe3d3..92ac911 100644
--- a/src/status.h
+++ b/src/status.h
@@ -30,7 +30,7 @@ struct status {
long count;
};
-struct status* status_init();
+int status_init(struct status*);
int status_write(struct status*);

Generated by cgit