Customize Lua GC, add object-memory-lock API.
authorRobin Gareus <robin@gareus.org>
Mon, 19 Mar 2018 00:33:13 +0000 (01:33 +0100)
committerRobin Gareus <robin@gareus.org>
Tue, 29 Oct 2019 04:45:43 +0000 (05:45 +0100)
Add custom API to prevent Lua Objects from being garbage collected.
This is intended to for Ardour LuaBridge bindings (~1MB Objects:
tables, functions and userdata).
The bindings are persistent and the gc can skip them in mark & sweep
phases. This is a significant performance improvement for garbage
collection.

Note. The next version of Lua (5.4) will come with a generational-gc
rather than an incremental, so extending the API at this point in time
is acceptable.

libs/lua/lua-5.3.5/lgc.c
libs/lua/lua-5.3.5/lstate.c
libs/lua/lua-5.3.5/lstate.h
libs/lua/lua-5.3.5/lua.h

index db4df82922e08b8e9a67bff456201be2b76e98f1..c4ea0e979d74d8300507d48af92c62fcd7c5f57d 100644 (file)
@@ -210,8 +210,14 @@ GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) {
   GCObject *o = cast(GCObject *, luaM_newobject(L, novariant(tt), sz));
   o->marked = luaC_white(g);
   o->tt = tt;
-  o->next = g->allgc;
-  g->allgc = o;
+  if (g->gcmlock) {
+    white2gray(o); /* gray forever */
+    o->next = g->fixedgc;
+    g->fixedgc = o;
+  } else {
+    o->next = g->allgc;
+    g->allgc = o;
+  }
   return o;
 }
 
@@ -1176,4 +1182,7 @@ void luaC_fullgc (lua_State *L, int isemergency) {
 
 /* }====================================================== */
 
-
+LUA_API void lua_mlock (lua_State *L, int en) {
+  global_State *g = G(L);
+  g->gcmlock = en;
+}
index c1a76643c33753087750c05712c717df0d7eebc7..06b505b6f37e75c6900d54b5204be420fdcfc8e5 100644 (file)
@@ -328,6 +328,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
   g->gcfinnum = 0;
   g->gcpause = LUAI_GCPAUSE;
   g->gcstepmul = LUAI_GCMUL;
+  g->gcmlock = 0;
   for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
   if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
     /* memory allocation error: free partial state */
index 56b374100007de4986018e3420eb2b64fa10a73f..0e8e4d0277bda1a79b9a733489d7adf7d5bc02e8 100644 (file)
@@ -162,6 +162,7 @@ typedef struct global_State {
   unsigned int gcfinnum;  /* number of finalizers to call in each GC step */
   int gcpause;  /* size of pause between successive GCs */
   int gcstepmul;  /* GC 'granularity' */
+  int gcmlock;  /* memory lock new objects - fixedgc */
   lua_CFunction panic;  /* to be called in unprotected errors */
   struct lua_State *mainthread;
   const lua_Number *version;  /* pointer to version number */
index c236e360957949816ff78973f8695300b2a63b96..b56fa91c8a891918efe0328e6f391aa1b50f178d 100644 (file)
@@ -311,6 +311,8 @@ LUA_API int (lua_isyieldable) (lua_State *L);
 
 LUA_API int (lua_gc) (lua_State *L, int what, int data);
 
+LUA_API void (lua_mlock) (lua_State *L, int enable);
+
 
 /*
 ** miscellaneous functions