summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Ball <nullspoon@oper.io>2022-06-21 20:29:03 -0600
committerAaron Ball <nullspoon@oper.io>2022-06-21 20:29:03 -0600
commitaeadf189497920f0be8d7077809041fab2ed1d9a (patch)
treebc31a53154059df47b16181ba26cad15f3051a6b
parentb30a324385f9595313819cc1d9c92d975a45e6fb (diff)
downloadcmon-aeadf189497920f0be8d7077809041fab2ed1d9a.tar.gz
cmon-aeadf189497920f0be8d7077809041fab2ed1d9a.tar.xz
Stop using heap for meminfo struct
This rewrites the meminfo constructor to take the struct as an input argument so it can be allocated in the stack.
-rw-r--r--src/main.c14
-rw-r--r--src/meminfo.c14
-rw-r--r--src/meminfo.h3
3 files changed, 15 insertions, 16 deletions
diff --git a/src/main.c b/src/main.c
index 19e9465..4eb6c24 100644
--- a/src/main.c
+++ b/src/main.c
@@ -27,17 +27,22 @@
#include "proc.h"
int main(int argc, char* argv[]) {
- struct meminfo* m = meminfo_init();
+ struct meminfo minfo; // Struct for relevant data from /proc/meminfo
struct status* status = status_init();
if(!status)
return 1;
status->count += 1;
+ if(! meminfo_init(&minfo)) {
+ fprintf(stderr, "ERROR: Could not read meminfo\n");
+ return 1;
+ }
+
// Calculate max memory usage and average memory usage
- if(status->memmax < (m->used))
- status->memmax = m->used;
- status->memavg = (((status->count - 1) * status->memavg) + m->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();
@@ -58,6 +63,5 @@ int main(int argc, char* argv[]) {
// Cleanup!
status_write(status);
status_free(status);
- meminfo_free(m);
return 0;
}
diff --git a/src/meminfo.c b/src/meminfo.c
index 4bfaebd..223a80c 100644
--- a/src/meminfo.c
+++ b/src/meminfo.c
@@ -17,28 +17,24 @@
*/
#include "meminfo.h"
-struct meminfo* meminfo_init() {
- struct meminfo* m = malloc(sizeof(struct meminfo));
- //char buf[256];
+int meminfo_init(struct meminfo* m) {
int status=0;
FILE* fd = fopen("/proc/meminfo", "r");
if (!fd)
- return NULL;
+ return 0;
status += fscanf(fd, "MemTotal: %ld kB\n", &m->total);
status += fscanf(fd, "MemFree: %ld kB\n", &m->free);
status += fscanf(fd, "MemAvailable: %ld kB\n", &m->avail);
status += fscanf(fd, "Buffers: %ld kB\n", &m->buffers);
status += fscanf(fd, "Cached: %ld kB\n", &m->cached);
-
+ // Custom value
m->used = m->total - m->free - m->buffers - m->cached;
if(status > 5) {
fprintf(stderr, "ERROR: Somehow read too many values from /proc/meminfo\n");
- return NULL;
+ return 0;
}
-
- return m;
+ return 1;
}
-void meminfo_free(struct meminfo* m) { }
diff --git a/src/meminfo.h b/src/meminfo.h
index d537683..8c14553 100644
--- a/src/meminfo.h
+++ b/src/meminfo.h
@@ -28,5 +28,4 @@ struct meminfo {
long used;
};
-struct meminfo* meminfo_init();
-void meminfo_free(struct meminfo*);
+int meminfo_init(struct meminfo*);

Generated by cgit