summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2023-05-05 00:24:25 +0800
committerruki <[email protected]>2023-05-05 00:24:25 +0800
commit4efc89f7b422d7b22c9ef76a68aff51bcb95c8c3 (patch)
tree6c57e3b58e61dee2d1235972828ad9fed3591855
parent67ac27c751ea9b7c706bdd221a78a216f74e2109 (diff)
add memory hook
-rw-r--r--core/src/xmake/engine.c21
-rw-r--r--core/src/xmake/xmake.c7
2 files changed, 27 insertions, 1 deletions
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c
index a68de8c12..8b24984de 100644
--- a/core/src/xmake/engine.c
+++ b/core/src/xmake/engine.c
@@ -50,6 +50,8 @@
/* //////////////////////////////////////////////////////////////////////////////////////
* macros
*/
+
+// proc/self
#if defined(TB_CONFIG_OS_LINUX)
# define XM_PROC_SELF_FILE "/proc/self/exe"
#elif defined(TB_CONFIG_OS_BSD) && !defined(__OpenBSD__)
@@ -62,6 +64,9 @@
# endif
#endif
+// hook lua memory allocator
+#define XM_HOOK_LUA_MEMALLOC (0)
+
/* //////////////////////////////////////////////////////////////////////////////////////
* types
*/
@@ -1034,6 +1039,17 @@ static tb_void_t xm_engine_init_signal(xm_engine_t* engine)
#endif
}
+#if XM_HOOK_LUA_MEMALLOC
+static tb_pointer_t xm_engine_lua_realloc(tb_pointer_t udata, tb_pointer_t data, size_t osize, size_t nsize)
+{
+// tb_trace_i("data: %p, osize: %d nsize: %d", data, (int)osize, (int)nsize);
+ if (nsize == 0 && data) tb_free(data);
+ else if (!data) return tb_malloc((tb_size_t)nsize);
+ else return tb_ralloc(data, (tb_size_t)nsize);
+ return tb_null;
+}
+#endif
+
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
@@ -1055,6 +1071,11 @@ xm_engine_ref_t xm_engine_init(tb_char_t const* name, xm_engine_lni_initalizer_c
engine->lua = luaL_newstate();
tb_assert_and_check_break(engine->lua);
+#if XM_HOOK_LUA_MEMALLOC
+ // hook lua memmory
+ lua_setallocf(engine->lua, xm_engine_lua_realloc, engine->lua);
+#endif
+
// open lua libraries
luaL_openlibs(engine->lua);
diff --git a/core/src/xmake/xmake.c b/core/src/xmake/xmake.c
index d75470bed..b3e74364a 100644
--- a/core/src/xmake/xmake.c
+++ b/core/src/xmake/xmake.c
@@ -94,8 +94,13 @@ tb_bool_t xm_init_(tb_size_t mode, tb_hize_t build)
// check version
xm_version_check(build);
- // init tbox
+#if 0
+ // init tbox, we always use the tbox's default allocator
+ if (!tb_init(tb_null, tb_default_allocator(tb_null, 0))) return tb_false;
+#else
+ // init tbox, since small compilation mode is enabled, it still uses the native allocator
if (!tb_init(tb_null, tb_null)) return tb_false;
+#endif
// trace
tb_trace_d("init: ok");