lua-users home
lua-l archive

Re: Using Lua and C with a Garbage Collector

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


In Lua 5.1 it is trivial to change the memory management.
Acording the manual, the function to create lua state
receives as an argument a function that manages the memory:
 lua_State *lua_newstate (lua_Alloc f, void *ud);
In manual paragraph 3.7:
 typedef void * (*lua_Alloc) (void *ud,
 void *ptr,
 size_t osize,
 size_t nsize);
Perhaps your one will be something like:
static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
 (void)ud; /* not used */
 (void)osize; /* not used */
 if (nsize == 0) {
 GC_FREE(ptr); /* ANSI requires that free(NULL) has no effect */
 return NULL;
 }
 else
 return GC_REALLOC(ptr, nsize);
}
though I'm not expert in using Hans's GC.
If you are using 5.0 - you must re-define some preprocessor macros but
I have never done this before.
Regards,
Todor
On 2006年3月15日 01:07:07 +0200, SevenThunders <mattcbro@earthlink.net> wrote:
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.
Is there any clean way to do this? The solutions I can think of all have
issues.

AltStyle によって変換されたページ (->オリジナル) /