[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: question regarding the use of luaL_ref/luaL_unref to implement reference counting
- From: RLake@...
- Date: Tue, 15 Jul 2003 09:30:15 -0500
> 1) Is it worth implementing a reference counting scheme
> to pin Lua objects so that they can be safely used
> in the "C" code without worrying about the garbage
> collector collecting such objects?
Personally, I don't think so -- but then I don't like
referencing counting. My solution is to allow non-Lua
objects to participate in garbage collection. Up to the
point where Lua gets a more complex garbage collector,
this is relatively simple; it is simply necessary to
associate a __mark metamethod with each such object.
This was relatively easy in Lua 4, because the __mark
method could be associated with a tag; it amounted to
about 10 lines of code (plus interfaces and, of course,
the mark methods themselves.) In Lua 5, it is a little
harder to see how the interfaces would work because there
is no concept of the type of an object, and one does not
really want to go trawling through a metatable in order to
find a __mark metamethod, although that is certainly
possible. One possibility would be to augment the userdata
type with a __mark function pointer.
> 2) Has this already been done and is the source code to
> it available?
I did the above for Lua 4 a couple of years ago, as a proof
of concept. However, the general discussion at the time was
that it was not useful, because a combination of the registry
and "weak references" could achieve the same result. (I do not
think that is true in general but I sometimes know when to
yield :) ) The fact that the next version of Lua will have
an incremental garbage collector means that any such modification
to Lua 5 will be difficult to support; the interfaces will
have to be a lot more complex than a simple __mark function.
Rici.