lua-users home
lua-l archive

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


Pls correct me if I am wrong (someone).

newuserdata() returns a void * pointer to a block of
memory off the lua internal memory pool.

For small (less than a few K) block of memory is (on
my environments) better than using malloc().

Because it comes from the lua pool, lua will garbage
collect the userdata when there are no more references
to it.

+++++++++++++++++++++++++++++++++
>Does anyone know what 'lua_newuserdata()' really is and why / where it
>should be used?
>
>I'm doing an (experimental) gluax compatibility cover over old-style C API.
>
>The following extract is from 'luasocket.c':
>
>    /* declare new Lua tags for used userdata values */
>    p_tags tags = (p_tags) lua_newuserdata(L, sizeof(t_tags));
>    tags->client = lua_newtag(L);
>    tags->server = lua_newtag(L);
>    tags->table = lua_newtag(L);
>    tags->udp = lua_newtag(L);
>
>'p_tags' is simply (same source file):
>
>	typedef struct t_tags {
>	    int client, server, table, udp;
>	} t_tags;
>	typedef t_tags *p_tags;
>
>Question is, what is really the importance / need for 'lua_newuserdata()'?
>It's not documented in 4.0 PDF and in my understanding, is just a fancy
>'alloc'. I must be missing something...?
>
>Declaration in 'luaapi.c':
>
>	LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
>	  TString *ts = luaS_newudata(L, size, NULL);
>	  tsvalue(L->top) = ts;
>	  ttype(L->top) = LUA_TUSERDATA;
>	  api_incr_top(L);
>	  return ts->u.d.value;
>	}
>
>-ak
>
>###########################################
>This message has been scanned by F-Secure Anti-Virus for Microsoft Exchange.