lua-users home
lua-l archive

Re: caching userdata pointers, lua_pushuserdata?

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


Hello,
I use a similar solution for sharing data, but instead of :
 lua_pushlightuserdata( L, &myUDMapKey );	
 lua_gettable( L, LUA_REGISTRYINDEX );
I use a metatable that I have create to insert c_pointer as key and fullUserData as value that gives something like:
 lua_getmetatable( L, "myKeyMap" );
 lua_pushlightuserdata(L, c_pointer);
 lua_gettable(L,-2)
 lua_remove(L,-2)
Is there a lot of overhead using a metatable instead of the lua_registryindex table?
thanx
David


Mark Hamburg wrote: [Technically, this isn't safe. Amongst other things, if the userdata has been collected, this call corrupts the Lua state. That being said, I've written this code (though I later took it out). Someone else also posted essentially the same thing a month or two ago. So, it's clearly an issue that a lot of people encounter. The safe way to do this is to maintain a weak table mapping the userdata pointers as light userdata to the full userdata objects. Pushing then becomes something like the following: lua_pushlightuserdata( L, &myUDMapKey ); lua_gettable( L, LUA_REGISTRYINDEX ); lua_pushlightuserdata( L, theUserData ); lua_gettable( L, -2 ); lua_remove( L, -2 ); (I used a light userdata rather than an integer to key the registry because it's easier to guarantee unique.) Mark on 7/25/05 5:45 PM, Kriss at Kriss@XIXs.com wrote:
I was just thinking about tidying up some code and want to do the
following.
cache a pointer returned from lua_touserdata() on the C side (I am
already doing this anyway)
at some point later on, push that userdata onto the stack to be used as
a table look up key, I can guarantee that the userdata will not have
been GCd at this point. At the moment I am using a unique integer as a
table key, but it would be simpler to use the userdata instead.
however there is no lua_pushuserdata()
Is it safe just to use a modified version of lua_newuserdata() that
doesnt allocate any new userdata but takes a previously allocated
pointer and pushes it on the stack?
EG some untested 5.0 code :)
LUA_API void lua_pushuserdata (lua_State *L, void *data) {
lua_lock(L);
luaC_checkGC(L);
setuvalue(L->top, ((Udata *)(data))-1);
api_incr_top(L);
lua_unlock(L);
}
or is there some more magic surrounding userdata I should be aware of.
 
begin:vcard
fn:David HERVIOU
n:HERVIOU;David
org;quoted-printable:Ecole Nationale d'Ing=C3=A9nieurs de Brest
adr;quoted-printable;dom:BP 38;;25, rue Claude Chappe;Plouzan=C3=A9;France;29280
email;internet:herviou@enib.fr
title;quoted-printable:Centre Europ=C3=A9en de R=C3=A9alit=C3=A9 Virtuelle
tel;work:+33 (0)2 98 05 89 47
tel;fax:+33 (0)2 98 05 89 79
note;quoted-printable:Doctorant En Informatique au laboratoire LISyC (Laboratoire d'Informatiqu=
	e des Syst=C3=A8mes Complexes) dans le cadre du projet AR=C3=A9Vi.
url:http://www.cerv.fr/~herviou
version:2.1
end:vcard

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