1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-12-04 14:46:47 -05:00

[memcount] libevent malloc, realloc, free replacement functions

This commit is contained in:
Witold Filipczyk 2023-12-26 19:09:32 +01:00
parent b1bf9e8dba
commit 204234b921
4 changed files with 121 additions and 0 deletions

View File

@ -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

View File

@ -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++)

View File

@ -281,6 +281,109 @@ get_curl_active(void)
}
#endif
#ifdef CONFIG_LIBEVENT
static std::map<void *, uint64_t> 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<void *, uint64_t> el_mujs_allocs;
static uint64_t el_mujs_total_allocs;

View File

@ -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);