Why is there no API to realloc a full userdata?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Why is there no API to realloc a full userdata?

Reuben Thomas-5
Why is there no C API to realloc a full userdata? Is it just not
common enough a need?

My use case: I am making alien (the Lua FFI extension) buffers be
reallocable. I would like to be able to say simply:

  lua_reallocuserdata(L, ud, nsize)

Without this I have to box a pointer in a full userdata (to get
garbage collection) and then duplicate the code to keep track of a
memory block and (so that I can use the Lua state's allocator) its
size. Furthermore, any time I want to use a userdata I need to
consider whether to use the usual lua_touserdata to get its "raw"
address, or whether to use alien_touserdata, which unboxes
alien-created boxed userdata.

Searching the list archives, I can see others discussing the "boxed
pointer" technique, but nothing about why there's no C API for it, nor
anything about the extra step of keeping track of the block's size.

--
http://rrt.sc3d.org

Reply | Threaded
Open this post in threaded view
|

Re: Why is there no API to realloc a full userdata?

Roberto Ierusalimschy
> Why is there no C API to realloc a full userdata? Is it just not
> common enough a need?

Because then Lua would have to "box" userdata. (That is, Lua would
do what you do not want to do, even for those that do not need it.)

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: Why is there no API to realloc a full userdata?

Rob Kendrick-2
In reply to this post by Reuben Thomas-5
On Mon, Jun 11, 2012 at 11:45:27PM +0100, Reuben Thomas wrote:
> Why is there no C API to realloc a full userdata? Is it just not
> common enough a need?

My initial thought is that there would have to be an extra layer of
indirection between userdata and actual memory: realloc can move the
data, and other parts of Lua may hold references, etc.

B.