diff --git a/src/dialogs/info.cpp b/src/dialogs/info.cpp index 1f9785c8..0e324f8f 100644 --- a/src/dialogs/info.cpp +++ b/src/dialogs/info.cpp @@ -269,8 +269,11 @@ get_resource_info(struct terminal *term, void *data) add_format_to_string(&info, ": %ld.", val); #ifdef CONFIG_DEBUG +#ifdef CONFIG_BROTLI + add_format_to_string(&info, "\nBrotli: used times: %ld active: %ld, size: %ld", get_brotli_total_allocs(), get_brotli_active(), get_brotli_size()); +#endif #ifdef CONFIG_GZIP - add_format_to_string(&info, "\nGzip: %ld used times: %ld active: %ld, size: %ld\n", get_gzip_total_allocs(), get_gzip_active(), get_gzip_size()); + add_format_to_string(&info, "\nGzip: used times: %ld active: %ld, size: %ld", get_gzip_total_allocs(), get_gzip_active(), get_gzip_size()); #endif #endif diff --git a/src/encoding/brotli.c b/src/encoding/brotli.c index 78f2a35b..2b69959c 100644 --- a/src/encoding/brotli.c +++ b/src/encoding/brotli.c @@ -18,6 +18,10 @@ #include "encoding/brotli.h" #include "encoding/encoding.h" #include "util/math.h" + +#ifdef CONFIG_DEBUG +#include "util/memcount.h" +#endif #include "util/memory.h" #define ELINKS_BROTLI_BUFFER_LENGTH 4096 @@ -51,7 +55,11 @@ brotli_open(struct stream_encoded *stream, int fd) if (!data) { return -1; } +#ifdef CONFIG_DEBUG + data->state = BrotliDecoderCreateInstance(el_brotli_alloc, el_brotli_free, NULL); +#else data->state = BrotliDecoderCreateInstance(NULL, NULL, NULL); +#endif if (!data->state) { mem_free(data); diff --git a/src/util/memcount.cpp b/src/util/memcount.cpp index ab3efa91..62bc2b06 100644 --- a/src/util/memcount.cpp +++ b/src/util/memcount.cpp @@ -10,6 +10,62 @@ #include #include +#ifdef CONFIG_BROTLI + +static std::map el_brotli_allocs; +static uint64_t el_brotli_total_allocs; +static uint64_t el_brotli_size; + +void * +el_brotli_alloc(void *opaque, size_t size) +{ + void *res = malloc(size); + + if (res) { + el_brotli_allocs[res] = size; + el_brotli_total_allocs++; + el_brotli_size += size; + } + + return res; +} + +void +el_brotli_free(void *opaque, void *ptr) +{ + if (!ptr) { + return; + } + + auto el = el_brotli_allocs.find(ptr); + + if (el == el_brotli_allocs.end()) { + fprintf(stderr, "brotli %p not found\n", ptr); + return; + } + el_brotli_size -= el->second; + el_brotli_allocs.erase(el); +} + +uint64_t +get_brotli_total_allocs(void) +{ + return el_brotli_total_allocs; +} + +uint64_t +get_brotli_size(void) +{ + return el_brotli_size; +} + +uint64_t +get_brotli_active(void) +{ + return el_brotli_allocs.size(); +} +#endif + #ifdef CONFIG_GZIP static std::map el_gzip_allocs; diff --git a/src/util/memcount.h b/src/util/memcount.h index 495c645a..bae61de3 100644 --- a/src/util/memcount.h +++ b/src/util/memcount.h @@ -5,6 +5,14 @@ extern "C" { #endif +#ifdef CONFIG_BROTLI +void *el_brotli_alloc(void *opaque, size_t size); +void el_brotli_free(void *opaque, void *ptr); +uint64_t get_brotli_total_allocs(void); +uint64_t get_brotli_size(void); +uint64_t get_brotli_active(void); +#endif + #ifdef CONFIG_GZIP void *el_gzip_alloc(void *opaque, unsigned int items, unsigned int size); void el_gzip_free(void *opaque, void *ptr);