[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Userdata and GC activity
- From: Florian Weimer <fw@...>
- Date: Tue, 18 Nov 2008 10:33:30 +0100
* Alex Sandro Queiroz e. Silva:
> Florian Weimer wrote:
>> Can the C memory region associated with a userdata object change its
>> address during the lifetime of the object?
> No, Lua uses a precise non-moving incremental mark-and-sweep
> collector. It does not move memory around, which would be a bad thing
> for a language designed to be embedded in C.
Thanks. It wasn't clear to me if this was an implementation artifact
or a VM property.
It turns out that I don't even need this at the moment.
Here's what I'm trying to do. I've got a C library which provides two
objects, databases and statements. On the C side, databases can only
be closed if all statements have been closed before. However, on the
Lua side, I want to close all statements automatically when the
database is closed. So I wrap both types of objects (actually,
pointers to them) in userdata. I maintain a table mapping light
userdata of statement pointers to Lua statement objects, and when I
notice that closing a database fails, I loop over all the open
statements, closing them. When a Lua statement is closed explicitly,
I remove it from this table and close the C version.
When a statement is closed (either on the C or Lua side), the pointer
in its userdata object is nulled. All Lua wrappers check that it is
not null and error out.
I'n wondering if this is the canonical way for dealing with this