1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-09-27 02:56:18 -04:00

[mujs] Added info when CONFIG_DEBUG

This commit is contained in:
Witold Filipczyk 2023-10-24 19:37:16 +02:00
parent a9ea05f0de
commit dae67eee8a
4 changed files with 80 additions and 1 deletions

View File

@ -281,6 +281,9 @@ get_resource_info(struct terminal *term, void *data)
#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
#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
#endif
#ifdef DEBUG_MEMLEAK

View File

@ -50,6 +50,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"
@ -131,8 +134,11 @@ mujs_get_interpreter(struct ecmascript_interpreter *interpreter)
{
assert(interpreter);
#ifdef CONFIG_DEBUG
js_State *J = js_newstate(el_mujs_alloc, NULL, 0);
#else
js_State *J = js_newstate(NULL, NULL, 0);
#endif
if (!J) {
return NULL;
}

View File

@ -391,3 +391,66 @@ get_sixel_active(void)
return el_sixel_allocs.size();
}
#endif
#ifdef CONFIG_MUJS
static std::map<void *, uint64_t> el_mujs_allocs;
static uint64_t el_mujs_total_allocs;
static uint64_t el_mujs_size;
void *
el_mujs_alloc(void *memctx, void *ptr, int size)
{
if (size == 0) {
if (!ptr) {
return NULL;
}
auto el = el_mujs_allocs.find(ptr);
if (el == el_mujs_allocs.end()) {
fprintf(stderr, "mujs free %p not found\n", ptr);
return NULL;
}
free(ptr);
el_mujs_allocs.erase(el);
return NULL;
}
size_t oldsize = 0;
if (ptr) {
auto el = el_mujs_allocs.find(ptr);
if (el == el_mujs_allocs.end()) {
fprintf(stderr, "mujs realloc %p not found\n", ptr);
} else {
oldsize = el->second;
el_mujs_allocs.erase(el);
}
}
void *ret = realloc(ptr, (size_t)size);
if (ret) {
el_mujs_allocs[ret] = (uint64_t)size;
el_mujs_total_allocs++;
el_mujs_size += size - oldsize;
}
return ret;
}
uint64_t
get_mujs_total_allocs(void)
{
return el_mujs_total_allocs;
}
uint64_t
get_mujs_size(void)
{
return el_mujs_size;
}
uint64_t
get_mujs_active(void)
{
return el_mujs_allocs.size();
}
#endif

View File

@ -46,6 +46,13 @@ uint64_t get_sixel_size(void);
uint64_t get_sixel_active(void);
#endif
#ifdef CONFIG_MUJS
void *el_mujs_alloc(void *memctx, void *ptr, int size);
uint64_t get_mujs_total_allocs(void);
uint64_t get_mujs_size(void);
uint64_t get_mujs_active(void);
#endif
#ifdef __cplusplus
}
#endif