1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-06-15 23:35:34 +00:00

[zstd] Added memcount (only for statically linked zstd)

This commit is contained in:
Witold Filipczyk 2023-10-26 18:17:16 +02:00
parent 17b288c935
commit 3834c52686
5 changed files with 91 additions and 1 deletions

View File

@ -366,7 +366,11 @@ if conf_data.get('CONFIG_BROTLI')
endif
if conf_data.get('CONFIG_ZSTD')
zstddeps = dependency('libzstd', static: st)
zstatic = st
if conf_data.get('CONFIG_DEBUG')
zstatic = true
endif
zstddeps = dependency('libzstd', static: zstatic)
deps += zstddeps
endif

View File

@ -273,6 +273,9 @@ get_resource_info(struct terminal *term, void *data)
#ifdef CONFIG_GZIP
add_format_to_string(&info, "\nGzip: calls: %ld active: %ld, size: %ld", get_gzip_total_allocs(), get_gzip_active(), get_gzip_size());
#endif
#ifdef CONFIG_ZSTD
add_format_to_string(&info, "\nZstd: calls: %ld active: %ld, size: %ld", get_zstd_total_allocs(), get_zstd_active(), get_zstd_size());
#endif
#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

View File

@ -10,6 +10,10 @@
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef CONFIG_DEBUG
#define ZSTD_STATIC_LINKING_ONLY
#endif
#include <zstd.h>
#include <errno.h>
@ -17,6 +21,7 @@
#include "encoding/zstd.h"
#include "encoding/encoding.h"
#include "util/memcount.h"
#include "util/memory.h"
/* How many bytes of compressed data to read before decompressing.
@ -44,7 +49,11 @@ zstd_open(struct stream_encoded *stream, int fd)
}
data->fdread = fd;
#ifdef CONFIG_DEBUG
data->zstd_stream = ZSTD_createDCtx_advanced(el_zstd_mf);
#else
data->zstd_stream = ZSTD_createDCtx();
#endif
if (!data->zstd_stream) {
mem_free(data);

View File

@ -561,3 +561,65 @@ get_quickjs_active(void)
return el_quickjs_allocs.size();
}
#endif
#ifdef CONFIG_ZSTD
static std::map<void *, uint64_t> el_zstd_allocs;
static uint64_t el_zstd_total_allocs;
static uint64_t el_zstd_size;
static void *
el_zstd_malloc(void *s, size_t size)
{
void *res = malloc(size);
if (res) {
el_zstd_allocs[res] = size;
el_zstd_total_allocs++;
el_zstd_size += size;
}
return res;
}
static void
el_zstd_free(void *s, void *ptr)
{
if (!ptr) {
return;
}
auto el = el_zstd_allocs.find(ptr);
if (el == el_zstd_allocs.end()) {
fprintf(stderr, "zstd free %p not found\n", ptr);
return;
}
el_zstd_size -= el->second;
el_zstd_allocs.erase(el);
free(ptr);
}
ZSTD_customMem el_zstd_mf = {
el_zstd_malloc,
el_zstd_free,
NULL
};
uint64_t
get_zstd_total_allocs(void)
{
return el_zstd_total_allocs;
}
uint64_t
get_zstd_size(void)
{
return el_zstd_size;
}
uint64_t
get_zstd_active(void)
{
return el_zstd_allocs.size();
}
#endif

View File

@ -11,6 +11,11 @@
#include <quickjs/quickjs.h>
#endif
#ifdef CONFIG_ZSTD
#define ZSTD_STATIC_LINKING_ONLY
#include <zstd.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
@ -66,6 +71,13 @@ uint64_t get_quickjs_size(void);
uint64_t get_quickjs_active(void);
#endif
#ifdef CONFIG_ZSTD
extern ZSTD_customMem el_zstd_mf;
uint64_t get_zstd_total_allocs(void);
uint64_t get_zstd_size(void);
uint64_t get_zstd_active(void);
#endif
#ifdef __cplusplus
}
#endif