1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-06-27 01:25:34 +00:00

[curl] Added info about allocations in CONFIG_DEBUG

This commit is contained in:
Witold Filipczyk 2023-10-24 17:39:26 +02:00
parent 007fef1d6c
commit 1145193290
5 changed files with 222 additions and 3 deletions

View File

@ -275,6 +275,9 @@ get_resource_info(struct terminal *term, void *data)
#ifdef CONFIG_GZIP
add_format_to_string(&info, "\nGzip: used times: %ld active: %ld, size: %ld", get_gzip_total_allocs(), get_gzip_active(), get_gzip_size());
#endif
#ifdef CONFIG_LIBCURL
add_format_to_string(&info, "\nCurl: used times: %ld active: %ld, size: %ld", get_curl_total_allocs(), get_curl_active(), get_curl_size());
#endif
#ifdef CONFIG_LIBSIXEL
add_format_to_string(&info, "\nSixel: used times: %ld active: %ld, size: %ld", get_sixel_total_allocs(), get_sixel_active(), get_sixel_size());
#endif

View File

@ -42,6 +42,9 @@
#include "terminal/terminal.h"
#include "terminal/window.h"
#include "util/conv.h"
#ifdef CONFIG_DEBUG
#include "util/memcount.h"
#endif
#include "util/string.h"
#include "viewer/text/draw.h"
#include "viewer/text/view.h" /* current_frame() */
@ -783,7 +786,11 @@ init_ecmascript_module(struct module *module)
#endif
}
ecmascript_enabled = get_opt_bool("ecmascript.enable", NULL);
curl_global_init(CURL_GLOBAL_ALL);
#ifdef CONFIG_DEBUG
curl_global_init_mem(CURL_GLOBAL_DEFAULT, el_curl_malloc, el_curl_free, el_curl_realloc, el_curl_strdup, el_curl_calloc);
#else
curl_global_init(CURL_GLOBAL_DEFAULT);
#endif
}
static void

View File

@ -73,6 +73,9 @@ do { \
#include "session/download.h"
#include "terminal/terminal.h"
#include "util/error.h"
#ifdef CONFIG_DEBUG
#include "util/memcount.h"
#endif
#include "util/memory.h"
#include "util/time.h"
@ -1111,7 +1114,11 @@ select_loop(void (*init)(void))
#if defined(CONFIG_LIBCURL) && defined(CONFIG_LIBEVENT)
memset(&g, 0, sizeof(GlobalInfo));
g.evbase = event_base;
#ifdef CONFIG_DEBUG
curl_global_init_mem(CURL_GLOBAL_DEFAULT, el_curl_malloc, el_curl_free, el_curl_realloc, el_curl_strdup, el_curl_calloc);
#else
curl_global_init(CURL_GLOBAL_DEFAULT);
#endif
g.multi = curl_multi_init();
//fprintf(stderr, "multi_init\n");
@ -1130,7 +1137,11 @@ select_loop(void (*init)(void))
#if defined(CONFIG_LIBCURL) && defined(CONFIG_LIBEV)
memset(&g, 0, sizeof(GlobalInfo));
g.loop = ev_default_loop(0);
#ifdef CONFIG_DEBUG
curl_global_init_mem(CURL_GLOBAL_DEFAULT, el_curl_malloc, el_curl_free, el_curl_realloc, el_curl_strdup, el_curl_calloc);
#else
curl_global_init(CURL_GLOBAL_DEFAULT);
#endif
g.multi = curl_multi_init();
//fprintf(stderr, "multi_init\n");
@ -1178,7 +1189,11 @@ select_loop(void (*init)(void))
{
#if defined(CONFIG_LIBCURL)
memset(&g, 0, sizeof(GlobalInfo));
#ifdef CONFIG_DEBUG
curl_global_init_mem(CURL_GLOBAL_DEFAULT, el_curl_malloc, el_curl_free, el_curl_realloc, el_curl_strdup, el_curl_calloc);
#else
curl_global_init(CURL_GLOBAL_DEFAULT);
#endif
g.multi = curl_multi_init();
/* setup the generic multi interface options we want */

View File

@ -3,6 +3,10 @@
#endif
#include "elinks.h"
#include <string.h>
#include <pthread.h>
#include "util/memcount.h"
#include <cstdint>
@ -45,6 +49,7 @@ el_brotli_free(void *opaque, void *ptr)
}
el_brotli_size -= el->second;
el_brotli_allocs.erase(el);
free(ptr);
}
uint64_t
@ -99,6 +104,7 @@ el_gzip_free(void *opaque, void *ptr)
}
el_gzip_size -= el->second;
el_gzip_allocs.erase(el);
free(ptr);
}
uint64_t
@ -120,6 +126,159 @@ get_gzip_active(void)
}
#endif
#ifdef CONFIG_LIBCURL
static std::map<void *, uint64_t> el_curl_allocs;
static uint64_t el_curl_total_allocs;
static uint64_t el_curl_size;
/* call custom malloc() */
void *
el_curl_malloc(
size_t /* in */ size) /* allocation size */
{
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_lock(&mutex);
void *res = malloc(size);
if (res) {
el_curl_allocs[res] = size;
el_curl_total_allocs++;
el_curl_size += size;
}
pthread_mutex_unlock(&mutex);
return res;
}
/* call custom strdup() */
char *
el_curl_strdup(const char *str)
{
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_lock(&mutex);
if (!str) {
pthread_mutex_unlock(&mutex);
return NULL;
}
size_t size = strlen(str) + 1;
char *ret = strdup(str);
if (ret) {
el_curl_allocs[(void *)ret] = size;
el_curl_total_allocs++;
el_curl_size += size;
}
pthread_mutex_unlock(&mutex);
return ret;
}
/* call custom calloc() */
void *
el_curl_calloc(
size_t /* in */ nelm, /* allocation size */
size_t /* in */ elsize) /* allocation size */
{
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_lock(&mutex);
uint64_t alloc_size = nelm * elsize;
void *res = calloc(nelm, elsize);
if (res) {
el_curl_allocs[res] = alloc_size;
el_curl_total_allocs++;
el_curl_size += alloc_size;
}
pthread_mutex_unlock(&mutex);
return res;
}
/* call custom realloc() */
void *
el_curl_realloc(
void /* in */ *p, /* existing buffer to be re-allocated */
size_t /* in */ n) /* re-allocation size */
{
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
if (!p) {
return el_curl_malloc(n);
}
pthread_mutex_lock(&mutex);
auto el = el_curl_allocs.find(p);
size_t size = 0;
bool todelete = false;
if (el == el_curl_allocs.end()) {
fprintf(stderr, "curl realloc %p not found\n", p);
} else {
size = el->second;
todelete = true;
}
void *ret = realloc(p, n);
if (todelete) {
el_curl_allocs.erase(el);
}
if (ret) {
el_curl_allocs[ret] = n;
el_curl_total_allocs++;
el_curl_size += n - size;
}
pthread_mutex_unlock(&mutex);
return ret;
}
/* call custom free() */
void
el_curl_free(
void /* in */ *p) /* existing buffer to be freed */
{
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
if (!p) {
return;
}
pthread_mutex_lock(&mutex);
auto el = el_curl_allocs.find(p);
if (el == el_curl_allocs.end()) {
fprintf(stderr, "curl free %p not found\n", p);
pthread_mutex_unlock(&mutex);
return;
}
el_curl_size -= el->second;
el_curl_allocs.erase(el);
free(p);
pthread_mutex_unlock(&mutex);
}
uint64_t
get_curl_total_allocs(void)
{
return el_curl_total_allocs;
}
uint64_t
get_curl_size(void)
{
return el_curl_size;
}
uint64_t
get_curl_active(void)
{
return el_curl_allocs.size();
}
#endif
#ifdef CONFIG_LIBSIXEL
static std::map<void *, uint64_t> el_sixel_allocs;
@ -167,8 +326,31 @@ el_sixel_realloc(
void /* in */ *p, /* existing buffer to be re-allocated */
size_t /* in */ n) /* re-allocation size */
{
el_sixel_free(p);
return el_sixel_malloc(n);
if (!p) {
return el_sixel_malloc(n);
}
auto el = el_sixel_allocs.find(p);
size_t size = 0;
bool todelete = false;
if (el == el_sixel_allocs.end()) {
fprintf(stderr, "sixel %p not found\n", p);
} else {
size = el->second;
todelete = true;
}
void *ret = realloc(p, n);
if (todelete) {
el_sixel_allocs.erase(el);
}
if (ret) {
el_sixel_allocs[ret] = n;
el_sixel_total_allocs++;
el_sixel_size += n - size;
}
return ret;
}
/* call custom free() */
@ -188,6 +370,7 @@ el_sixel_free(
}
el_sixel_size -= el->second;
el_sixel_allocs.erase(el);
free(p);
}
uint64_t

View File

@ -25,6 +25,17 @@ uint64_t get_gzip_size(void);
uint64_t get_gzip_active(void);
#endif
#ifdef CONFIG_LIBCURL
void *el_curl_malloc(size_t size);
void *el_curl_calloc(size_t nelm, size_t elsize);
void *el_curl_realloc(void *p, size_t n);
char *el_curl_strdup(const char *str);
void el_curl_free(void *p);
uint64_t get_curl_total_allocs(void);
uint64_t get_curl_size(void);
uint64_t get_curl_active(void);
#endif
#ifdef CONFIG_LIBSIXEL
void *el_sixel_malloc(size_t size);
void *el_sixel_calloc(size_t nelm, size_t elsize);