From 204234b9218a9967db5ab6da606846bed0e30133 Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Tue, 26 Dec 2023 19:09:32 +0100 Subject: [PATCH] [memcount] libevent malloc, realloc, free replacement functions --- src/dialogs/info.c | 3 ++ src/main/select.c | 5 ++ src/util/memcount.cpp | 104 ++++++++++++++++++++++++++++++++++++++++++ src/util/memcount.h | 9 ++++ 4 files changed, 121 insertions(+) diff --git a/src/dialogs/info.c b/src/dialogs/info.c index ad3961aa3..1ad517350 100644 --- a/src/dialogs/info.c +++ b/src/dialogs/info.c @@ -281,6 +281,9 @@ get_resource_info(struct terminal *term, void *data) #ifdef CONFIG_LIBCURL add_format_to_string(&info, "\nCurl: calls: %ld active: %ld, size: %ld", get_curl_total_allocs(), get_curl_active(), get_curl_size()); #endif +#ifdef CONFIG_LIBEVENT + add_format_to_string(&info, "\nLibevent: calls: %ld active: %ld, size: %ld", get_libevent_total_allocs(), get_libevent_active(), get_libevent_size()); +#endif #ifdef CONFIG_LIBSIXEL add_format_to_string(&info, "\nSixel: calls: %ld active: %ld, size: %ld", get_sixel_total_allocs(), get_sixel_active(), get_sixel_size()); #endif diff --git a/src/main/select.c b/src/main/select.c index caa2f600e..a0ce33dd4 100644 --- a/src/main/select.c +++ b/src/main/select.c @@ -877,6 +877,10 @@ enable_libevent(void) return; #endif +#if !defined(EVENT__DISABLE_MM_REPLACEMENT) && defined(CONFIG_DEBUG) + event_set_mem_functions(el_libevent_malloc, el_libevent_realloc, el_libevent_free); +#endif + #if defined(HAVE_EVENT_CONFIG_SET_FLAG) { struct event_config *cfg; @@ -903,6 +907,7 @@ enable_libevent(void) #else event_init(); #endif + event_enabled = 1; for (i = 0; i < w_max; i++) diff --git a/src/util/memcount.cpp b/src/util/memcount.cpp index 6fde64f16..10101136b 100644 --- a/src/util/memcount.cpp +++ b/src/util/memcount.cpp @@ -281,6 +281,109 @@ get_curl_active(void) } #endif +#ifdef CONFIG_LIBEVENT + +static std::map el_libevent_allocs; +static uint64_t el_libevent_total_allocs; +static uint64_t el_libevent_size; + +/* call custom malloc() */ +void * +el_libevent_malloc( + size_t /* in */ size) /* allocation size */ +{ + static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; + pthread_mutex_lock(&mutex); + + void *res = mem_alloc(size); + + if (res) { + el_libevent_allocs[res] = size; + el_libevent_total_allocs++; + el_libevent_size += size; + } + pthread_mutex_unlock(&mutex); + + return res; +} + +/* call custom realloc() */ +void * +el_libevent_realloc( + void /* in */ *p, /* existing buffer to be re-allocated */ + size_t /* in */ n) /* re-allocation size */ +{ + if (!p) { + return el_libevent_malloc(n); + } + static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; + pthread_mutex_lock(&mutex); + + auto el = el_libevent_allocs.find(p); + size_t size = 0; + bool todelete = false; + + if (el == el_libevent_allocs.end()) { + fprintf(stderr, "libevent %p not found\n", p); + } else { + size = el->second; + todelete = true; + } + void *ret = mem_realloc(p, n); + + if (todelete) { + el_libevent_allocs.erase(el); + } + if (ret) { + el_libevent_allocs[ret] = n; + el_libevent_total_allocs++; + el_libevent_size += n - size; + } + pthread_mutex_unlock(&mutex); + + return ret; +} + +/* call custom free() */ +void +el_libevent_free( + void /* in */ *p) /* existing buffer to be freed */ +{ + static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; + pthread_mutex_lock(&mutex); + + auto el = el_libevent_allocs.find(p); + + if (el == el_libevent_allocs.end()) { + fprintf(stderr, "libevent %p not found\n", p); + pthread_mutex_unlock(&mutex); + return; + } + el_libevent_size -= el->second; + el_libevent_allocs.erase(el); + mem_free(p); + pthread_mutex_unlock(&mutex); +} + +uint64_t +get_libevent_total_allocs(void) +{ + return el_libevent_total_allocs; +} + +uint64_t +get_libevent_size(void) +{ + return el_libevent_size; +} + +uint64_t +get_libevent_active(void) +{ + return el_libevent_allocs.size(); +} +#endif + #ifdef CONFIG_LIBSIXEL @@ -395,6 +498,7 @@ get_sixel_active(void) } #endif + #ifdef CONFIG_MUJS static std::map el_mujs_allocs; static uint64_t el_mujs_total_allocs; diff --git a/src/util/memcount.h b/src/util/memcount.h index b74c90ac0..d63eea54f 100644 --- a/src/util/memcount.h +++ b/src/util/memcount.h @@ -47,6 +47,15 @@ uint64_t get_curl_size(void); uint64_t get_curl_active(void); #endif +#ifdef CONFIG_LIBEVENT +void *el_libevent_malloc(size_t size); +void *el_libevent_realloc(void *p, size_t n); +void el_libevent_free(void *p); +uint64_t get_libevent_total_allocs(void); +uint64_t get_libevent_size(void); +uint64_t get_libevent_active(void); +#endif + #ifdef CONFIG_LIBSIXEL void *el_sixel_malloc(size_t size); void *el_sixel_calloc(size_t nelm, size_t elsize);