lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi,

Is there a way to let the garbage collector know how much space a newly
allocated userdata really takes, other than changing the library sources ?

I am allocating a lot of objects, each taking about 1K of space and storing them as a pointer in the buffer returned by lua_newuserdata(L, sizeof(void *)) (using c++ objects that get allocated elsewhere and it probably isn't easy to pass the already allocated buffer to the constructor). And by the time the collector starts
finalizing 100000's of objects, it runs out of memory.

Attached quick fix appears to do the job but it would be nice if I could take the code that uses it elsewhere. It allows me to call lua_gc() after lua_newuserdata()
with data= +sizeof(object) and in __gc, to call with data= -sizeof(object).


Regards,
Bogdan

diff -aur lua-5.1.2/src/lapi.c lua-5.1.2-gcudata/src/lapi.c
--- lua-5.1.2/src/lapi.c	2006-06-07 14:37:17.000000000 +0200
+++ lua-5.1.2-gcudata/src/lapi.c	2007-06-06 18:36:49.000000000 +0200
@@ -898,6 +898,10 @@
   lua_lock(L);
   g = G(L);
   switch (what) {
+    case LUA_GCUDATA: {
+      g->totalbytes += data;
+      break;
+    }
     case LUA_GCSTOP: {
       g->GCthreshold = MAX_LUMEM;
       break;
diff -aur lua-5.1.2/src/lua.h lua-5.1.2-gcudata/src/lua.h
--- lua-5.1.2/src/lua.h	2007-03-23 18:12:31.000000000 +0100
+++ lua-5.1.2-gcudata/src/lua.h	2007-06-06 18:37:17.000000000 +0200
@@ -226,6 +226,7 @@
 #define LUA_GCSTEP		5
 #define LUA_GCSETPAUSE		6
 #define LUA_GCSETSTEPMUL	7
+#define LUA_GCUDATA		8
 
 LUA_API int (lua_gc) (lua_State *L, int what, int data);