lua-users home
lua-l archive

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

Benoit Germain wrote:
>[how a FILE* is stored in userdata]
> I was wondering if it could not be possible in this particular case, to
> remove the need for this lonely intermediate pointer ?

You _have_ to store the pointer somewhere.

> This could be done through a return value of the metamethod, that, if
> non-nil/false, tells the gc if actual collection of its memory area is
> necessary.

??? If the GC won't free the userdata you have a memory leak...

> To enable this, one would only have to be able to tell lua, when creating a
> userdata, if this memory block is needed, or if it can use the provided
> pointer as is.

Maybe you should have a closer look at how userdata is allocated.  It's

  struct Udata {
    ... fields to manage userdata
    size_t len;            // the size from lua_newuserdata
    char data[Udata.len];  // variable length, available for app

>... what if it was changed to lua_newuserdata(L,p,size) ?

That's how it was before.  There was an additional fixed field in struct
Udata to hold a single pointer.  But what do you gain?  The struct gets
sizeof(void*) larger and len becomes 0.  The total size is the same.  But
if you do not need the additional pointer it is wasted.

> I don't know if I am too clear about this, so feel free to tell me what's
> wrong about my reasoning :-)

Maybe you thought that the management data and the application data area
are malloced independently.  As you can see, that's not the case.  It's
only one block: first few bytes (usually 12) are used by Lua, the
rest is for the application.

Ciao, ET.