move tracy from quickjs

This commit is contained in:
2025-02-06 09:10:54 -06:00
parent 8bd7dd00c7
commit f472e0bd02
6 changed files with 150 additions and 6 deletions

View File

@@ -1,6 +1,10 @@
debug: FORCE debug: FORCE
meson setup build_dbg -Dbuildtype=debugoptimized meson setup build_dbg -Dbuildtype=debugoptimized
meson compile -C build_dbg meson compile -C build_dbg
fast: FORCE
meson setup build_fast
meson compile -C build_fast
release: FORCE release: FORCE
meson setup -Dbuildtype=release -Db_lto=true -Db_ndebug=true build_release meson setup -Dbuildtype=release -Db_lto=true -Db_ndebug=true build_release

View File

@@ -338,7 +338,7 @@ var Register = {
var fns = []; var fns = [];
n.register = function (fn, oname) { n.register = function (fn, oname) {
if (!(fn instanceof Function)) return; if (typeof fn !== 'function') return;
var dofn = function (...args) { var dofn = function (...args) {
fn(...args); fn(...args);

View File

@@ -25,10 +25,6 @@ util.get = function (obj, path, defValue) {
return result === undefined ? defValue : result; return result === undefined ? defValue : result;
} }
util.isObject = function (o) {
return o instanceof Object && !(o instanceof Array);
};
util.isEmpty = function(o) { util.isEmpty = function(o) {
return Object.keys(obj).length === 0; return Object.keys(obj).length === 0;
} }

View File

@@ -7756,3 +7756,34 @@ void ffi_load(JSContext *js, int argc, char **argv) {
JS_FreeValue(js,globalThis); JS_FreeValue(js,globalThis);
} }
/*
#if defined(TRACY_ENABLE) && !defined(_WIN32)
const char *ccname = get_func_name(ctx,func_obj);
const char *file = "<native C>";
TracyCZoneCtx tracy_ctx = ___tracy_emit_zone_begin_alloc(___tracy_alloc_srcloc(1, file, strlen(file), ccname, strlen(ccname), (int)ccname), 1);
JS_FreeCString(ctx,ccname);
#endif
#ifdef TRACY_ENABLE
___tracy_emit_zone_end(tracy_ctx);
#endif
#ifdef TRACY_ENABLE
const char *fn_src = JS_AtomToCString(caller_ctx, js_fn_filename(caller_ctx,func_obj));
const char *js_func_name = get_func_name(caller_ctx, func_obj);
const char *fn_name;
if (!js_func_name || js_func_name[0] == '\0')
fn_name = "<anonymous>";
else
fn_name = js_func_name;
uint64_t srcloc;
srcloc = ___tracy_alloc_srcloc(js_fn_linenum(caller_ctx,func_obj), fn_src, strlen(fn_src), fn_name, strlen(fn_name), (int)fn_src);
TracyCZoneCtx tracy_ctx = ___tracy_emit_zone_begin_alloc(srcloc,1);
JS_FreeCString(caller_ctx,js_func_name);
JS_FreeCString(caller_ctx,fn_src);
#endif
*/

View File

@@ -121,7 +121,6 @@ TYPE *js2##TYPE (JSContext *js, JSValue val) { \
}\ }\
JSValue TYPE##2js(JSContext *js, TYPE *n) { \ JSValue TYPE##2js(JSContext *js, TYPE *n) { \
JSValue j = JS_NewObjectClass(js,js_##TYPE##_id);\ JSValue j = JS_NewObjectClass(js,js_##TYPE##_id);\
JS_PreventExtensions(js, j); \
JS_SetOpaque(j,n);\ JS_SetOpaque(j,n);\
__VA_ARGS__ \ __VA_ARGS__ \
TracyCAllocN(n, 1, #TYPE); \ TracyCAllocN(n, 1, #TYPE); \

View File

@@ -9,6 +9,8 @@
#include "jsffi.h" #include "jsffi.h"
#include <stdlib.h> #include <stdlib.h>
#include <assert.h> #include <assert.h>
#include "quickjs.h"
#include <malloc/malloc.h>
#include "physfs.h" #include "physfs.h"
@@ -27,8 +29,120 @@ JSValue on_exception = JS_UNDEFINED;
#define JS_EVAL_FLAGS JS_EVAL_FLAG_STRICT | JS_EVAL_FLAG_STRIP #define JS_EVAL_FLAGS JS_EVAL_FLAG_STRICT | JS_EVAL_FLAG_STRIP
#endif #endif
#ifdef TRACY_ENABLE
#include <tracy/TracyC.h>
#if defined(__APPLE__)
#define MALLOC_OVERHEAD 0
#else
#define MALLOC_OVERHEAD 8
#endif
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
/* default memory allocation functions with memory limitation */
static size_t js_tracy_malloc_usable_size(const void *ptr)
{
#if defined(__APPLE__)
return malloc_size(ptr);
#elif defined(_WIN32)
return _msize((void *)ptr);
#elif defined(EMSCRIPTEN)
return 0;
#elif defined(__linux__) || defined(__GLIBC__)
return malloc_usable_size((void *)ptr);
#else
/* change this to `return 0;` if compilation fails */
return malloc_usable_size((void *)ptr);
#endif
}
static void *js_tracy_malloc(JSMallocState *s, size_t size)
{
void *ptr;
/* Do not allocate zero bytes: behavior is platform dependent */
assert(size != 0);
if (unlikely(s->malloc_size + size > s->malloc_limit))
return NULL;
ptr = malloc(size);
if (!ptr)
return NULL;
s->malloc_count++;
s->malloc_size += js_tracy_malloc_usable_size(ptr) + MALLOC_OVERHEAD;
TracyCAllocN(ptr,js_tracy_malloc_usable_size(ptr) + MALLOC_OVERHEAD, "quickjs");
return ptr;
}
static void js_tracy_free(JSMallocState *s, void *ptr)
{
if (!ptr)
return;
s->malloc_count--;
s->malloc_size -= js_tracy_malloc_usable_size(ptr) + MALLOC_OVERHEAD;
TracyCFreeN(ptr, "quickjs");
free(ptr);
}
static void *js_tracy_realloc(JSMallocState *s, void *ptr, size_t size)
{
size_t old_size;
if (!ptr) {
if (size == 0)
return NULL;
return js_tracy_malloc(s, size);
}
old_size = js_tracy_malloc_usable_size(ptr);
if (size == 0) {
s->malloc_count--;
s->malloc_size -= old_size + MALLOC_OVERHEAD;
TracyCFreeN(ptr, "quickjs");
free(ptr);
return NULL;
}
if (s->malloc_size + size - old_size > s->malloc_limit)
return NULL;
TracyCFreeN(ptr, "quickjs");
ptr = realloc(ptr, size);
if (!ptr)
return NULL;
s->malloc_size += js_tracy_malloc_usable_size(ptr) - old_size;
TracyCAllocN(ptr,js_tracy_malloc_usable_size(ptr) + MALLOC_OVERHEAD, "quickjs");
return ptr;
}
static const JSMallocFunctions tracy_malloc_funcs = {
js_tracy_malloc,
js_tracy_free,
js_tracy_realloc,
js_tracy_malloc_usable_size
};
#endif
void script_startup(int argc, char **argv) { void script_startup(int argc, char **argv) {
#ifdef TRACY_ENABLE
rt = JS_NewRuntime2(&tracy_malloc_funcs, NULL);
#else
rt = JS_NewRuntime(); rt = JS_NewRuntime();
#endif
js = JS_NewContextRaw(rt); js = JS_NewContextRaw(rt);
JS_AddIntrinsicBaseObjects(js); JS_AddIntrinsicBaseObjects(js);
JS_AddIntrinsicEval(js); JS_AddIntrinsicEval(js);