1
0
mirror of https://github.com/rkd77/elinks.git synced 2025-02-02 15:09:23 -05:00

[quickjs] memcount

This commit is contained in:
Witold Filipczyk 2023-10-25 17:10:59 +02:00
parent dae67eee8a
commit 0b4007fd7e
4 changed files with 128 additions and 0 deletions

View File

@ -284,6 +284,9 @@ get_resource_info(struct terminal *term, void *data)
#ifdef CONFIG_MUJS
add_format_to_string(&info, "\nMuJS: calls: %ld active: %ld, size: %ld", get_mujs_total_allocs(), get_mujs_active(), get_mujs_size());
#endif
#ifdef CONFIG_QUICKJS
add_format_to_string(&info, "\nQuickJS: calls: %ld active: %ld, size: %ld", get_quickjs_total_allocs(), get_quickjs_active(), get_quickjs_size());
#endif
#endif
#ifdef DEBUG_MEMLEAK

View File

@ -51,6 +51,9 @@
#include "terminal/tab.h"
#include "terminal/terminal.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/form.h"
@ -148,7 +151,11 @@ quickjs_get_interpreter(struct ecmascript_interpreter *interpreter)
assert(interpreter);
// if (!js_module_init_ok) return NULL;
#ifdef CONFIG_DEBUG
interpreter->rt = JS_NewRuntime2(&el_quickjs_mf, NULL);
#else
interpreter->rt = JS_NewRuntime();
#endif
if (!interpreter->rt) {
return nullptr;
}

View File

@ -5,6 +5,7 @@
#include "elinks.h"
#include <string.h>
#include <malloc.h>
#include <pthread.h>
#include "util/memcount.h"
@ -454,3 +455,109 @@ get_mujs_active(void)
return el_mujs_allocs.size();
}
#endif
#ifdef CONFIG_QUICKJS
static std::map<void *, uint64_t> el_quickjs_allocs;
static uint64_t el_quickjs_total_allocs;
static uint64_t el_quickjs_size;
static void *
el_quickjs_malloc(JSMallocState *s, size_t size)
{
void *res = malloc(size);
if (res) {
el_quickjs_allocs[res] = size;
el_quickjs_total_allocs++;
el_quickjs_size += size;
}
return res;
}
static void
el_quickjs_free(JSMallocState *s, void *ptr)
{
if (!ptr) {
return;
}
auto el = el_quickjs_allocs.find(ptr);
if (el == el_quickjs_allocs.end()) {
fprintf(stderr, "quickjs free %p not found\n", ptr);
return;
}
el_quickjs_size -= el->second;
el_quickjs_allocs.erase(el);
free(ptr);
}
static void *
el_quickjs_realloc(JSMallocState *s, void *ptr, size_t n)
{
if (!ptr) {
return el_quickjs_malloc(s, n);
}
auto el = el_quickjs_allocs.find(ptr);
size_t size = 0;
if (el == el_quickjs_allocs.end()) {
fprintf(stderr, "quickjs realloc %p not found\n", ptr);
} else {
size = el->second;
el_quickjs_allocs.erase(el);
}
void *ret = realloc(ptr, n);
if (ret) {
el_quickjs_allocs[ret] = n;
el_quickjs_total_allocs++;
el_quickjs_size += n - size;
}
return ret;
}
static size_t
el_quickjs_malloc_usable_size(const void *ptr)
{
#if defined(__APPLE__)
return malloc_size(ptr);
#elif defined(_WIN32)
return _msize(ptr);
#elif defined(EMSCRIPTEN)
return 0;
#elif defined(__linux__)
return malloc_usable_size((void *)ptr);
#else
/* change this to `return 0;` if compilation fails */
return malloc_usable_size((void *)ptr);
#endif
}
const JSMallocFunctions el_quickjs_mf = {
el_quickjs_malloc,
el_quickjs_free,
el_quickjs_realloc,
el_quickjs_malloc_usable_size
};
uint64_t
get_quickjs_total_allocs(void)
{
return el_quickjs_total_allocs;
}
uint64_t
get_quickjs_size(void)
{
return el_quickjs_size;
}
uint64_t
get_quickjs_active(void)
{
return el_quickjs_allocs.size();
}
#endif

View File

@ -5,6 +5,10 @@
#include <sixel.h>
#endif
#ifdef CONFIG_QUICKJS
#include <quickjs/quickjs.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
@ -53,6 +57,13 @@ uint64_t get_mujs_size(void);
uint64_t get_mujs_active(void);
#endif
#ifdef CONFIG_QUICKJS
extern const JSMallocFunctions el_quickjs_mf;
uint64_t get_quickjs_total_allocs(void);
uint64_t get_quickjs_size(void);
uint64_t get_quickjs_active(void);
#endif
#ifdef __cplusplus
}
#endif