lua-users home
lua-l archive

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


I have two favorite ways to improve productivity when coding in C. The first
is the use of a scripting language for the high level logic and the second
is the use of a garbage collector to free one from the drudgery and bug
generation of manual memory management.  

Unfortunately it is not trivial to attempt to use both techniques at the
same time.  For example I have a C library that uses garbage collection
(Hans Boehm et. al.) to manage a complex tree-like data structure.  I wish
to expose the C library to Lua so that I can script a lot of my test
routines and perform debugging in LUA.  The problem is that the Hans Boehm
gc utilities can not see any of the C pointers that are being held in Lua
and thus all my C objects are eventually collected even though I am still
'using' them.

For this debugging application it is probably sufficient to expose some of
the garbage collection utilities to LUA and then to declare some pointers
inside LUA as part of the root set.  This should be OK since the root of the
tree eventually points to all the interesting objects I am looking at, but
it is not a good solution in general.

Is there any clean way to do this?  The solutions I can think of all have
issues.

1)  Recompile LUA so that all base memory allocations are done through the
garbage collector.  One can redefine malloc, calloc, realloc and free etc.
to point to the gc routines.  I am not sure how the LUA gc performs it's low
level memory access however or whether this would work.

2)  Attempt to add all high level Lua pointers to the root set of the
garbage collector.  I think the Hans Boehm collector will only follow
pointers that it knows have been allocated by the garbage collector.  Thus
finding the correct root set would be problematic.

3)  Make a 'protected' array as a global variable inside the C code that is
in the data segment and hence the root set of the Boehm garbage collector. 
Temporarily 'protect' C pointers by saving them in that array.  This seems
like a lot of program overhead and hassle to implement.  However is it
possible to force Lua to use memory whose pointers have been saved in the C
code data segment?

4) Don't use the Hans Boehm collector, but rather use Lua's gc.  Is this
possible?  Can memory allocated in Lua be used in C?  I'm not sure how Luas
garbage collector works.
--
View this message in context: http://www.nabble.com/Using-Lua-and-C-with-a-Garbage-Collector-t1281804.html#a3407146
Sent from the Lua - General forum at Nabble.com.