lua-users home
lua-l archive

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


Adrian Sietsma wrote:
Chris Marrin wrote:
 > Why not just point the userdata at a memory block that you allocate and
then pass that to the other thread? Since you allocate it, you can be sure its address will not be changed by Lua, even if the userdata pointing at it moves around.

You mean use a lightuserdata.

That is my current approach, but I was contemplating using Lua-allocated memory if it is safe to assume constancy of userdata.

No, I mean a full userdata with a pointer to a void*. Then you allocate the memory to pass to your thread and point at it from the userdata. I believe Lua calls this a "boxed pointer" and it has the advantage that you can use Lua to collect it by adding an __gc metamethod to the userdata.


given this from Roberto :

"We have no intention of
allowing userdata addresses to change during GC. Unlike strings, which
are an internal data in Lua, the only purpose of userdata is to be used
by C code, which prefer that things stay where they are :)"

I will now happily pass full userdata to a background thread (IOCP on Windows, fyi).

I'm not sure if Roberto intended what you think you read. If and when Lua gets a compacting garbage collector, a userdata's address would move around just like any other gc object. Maybe that will never happen and maybe they have some clever way to guarantee that userdatas never move. But it seems like a better idea to not rely on this and use your own data.

--
chris marrin              ,""$, "As a general rule,don't solve puzzles
chris@marrin.com        b`    $  that open portals to Hell" ,,.
        ,.`           ,b`    ,`                            , 1$'
     ,|`             mP    ,`                              :$$'     ,mm
   ,b"              b"   ,`            ,mm      m$$    ,m         ,`P$$
  m$`             ,b`  .` ,mm        ,'|$P   ,|"1$`  ,b$P       ,`  :$1
 b$`             ,$: :,`` |$$      ,`   $$` ,|` ,$$,,`"$$     .`    :$|
b$|            _m$`,:`    :$1   ,`     ,$Pm|`    `    :$$,..;"'     |$:
P$b,      _;b$$b$1"       |$$ ,`      ,$$"             ``'          $$
 ```"```'"    `"`         `""`        ""`                          ,P`