From ce7710bdc80039afe12220099f1fbf76859afaa6 Mon Sep 17 00:00:00 2001 From: npyl Date: Tue, 7 Aug 2018 01:50:22 +0300 Subject: [PATCH] Finish up the algorithm. I think its now correct. Closes: #33 --- src/darwin.mm | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/darwin.mm b/src/darwin.mm index 638fe0222..34e1929e2 100644 --- a/src/darwin.mm +++ b/src/darwin.mm @@ -113,7 +113,7 @@ void eprintf(const char *fmt, ...) { /* * used by update_cpu_usage() */ -std::vector sample_handles; /* a registry of handles */ +struct cpusample *sample_handle = nullptr; static int getsysctl(const char *name, void *ptr, size_t len) { size_t nlen = len; @@ -273,20 +273,23 @@ static void get_cpu_sample(struct cpusample **sample) { } void allocate_cpu_sample(struct cpusample **sample) { + if (*sample != nullptr) + return; + /* * allocate ncpus+1 cpusample structs (one foreach CPU) * ** sample_handle[0] is overal CPU usage */ *sample = reinterpret_cast(malloc(sizeof(cpusample) * (info.cpu_count + 1))); memset(*sample, 0, sizeof(cpusample) * (info.cpu_count + 1)); - - sample_handles.push_back(*sample); /* register handle */ + + sample_handle = *sample; /* use a public handle for deallocating */ } void deallocate_cpu_sample(struct text_object *obj) { - while (sample_handles.size() > 0) { - free(sample_handles.back()); - sample_handles.pop_back(); + if (sample_handle != nullptr) { + free(sample_handle); + sample_handle = nullptr; } } @@ -889,7 +892,7 @@ int update_cpu_usage() { unsigned int malloc_cpu_size = 0; extern void *global_cpu; - struct cpusample *sample = nullptr; + static struct cpusample *sample = nullptr; static pthread_mutex_t last_stat_update_mutex = PTHREAD_MUTEX_INITIALIZER; static double last_stat_update = 0.0; @@ -922,7 +925,7 @@ int update_cpu_usage() { } allocate_cpu_sample(&sample); - + get_cpu_sample(&sample); /*