diff --git a/src/dialogs/info.cpp b/src/dialogs/info.cpp index d410661e..d79fe123 100644 --- a/src/dialogs/info.cpp +++ b/src/dialogs/info.cpp @@ -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 diff --git a/src/ecmascript/mujs.cpp b/src/ecmascript/mujs.cpp index ecf0d36c..8bede9c6 100644 --- a/src/ecmascript/mujs.cpp +++ b/src/ecmascript/mujs.cpp @@ -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; } diff --git a/src/util/memcount.cpp b/src/util/memcount.cpp index 51306120..dae16575 100644 --- a/src/util/memcount.cpp +++ b/src/util/memcount.cpp @@ -391,3 +391,66 @@ get_sixel_active(void) return el_sixel_allocs.size(); } #endif + +#ifdef CONFIG_MUJS +static std::map 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 diff --git a/src/util/memcount.h b/src/util/memcount.h index b797d743..0bdc1bd6 100644 --- a/src/util/memcount.h +++ b/src/util/memcount.h @@ -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