lua-users home
lua-l archive

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


Hi.

> I'm currently working on the third major project with Lua as an
> integral part, and in each project we've found it necessary to wrap a
> Lua_State* inside another structure that contains ancillary
> information (some statistics, a mutex, linked list pointers etc etc).
> I'm sure we're not the only ones doing this…

What about using container_of() [1] ?

struct LuaPrivateState {
	lua_State *pVM;
	void *extra_data;
	struct foobar baz;
};

And in a function that gets a pointer to the lua_State, do:

int whatever(lua_State *L, ...)
{
	struct LuaPrivateState *state =
		container_of(L, struct LuaPrivateState, pVM);

	...
}


1: http://www.kroah.com/log/linux/container_of.html

~ Jow

Attachment: signature.asc
Description: OpenPGP digital signature