lua-users home
lua-l archive

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


Am 01.09.2013 18:32 schröbte Jo-Philipp Wich:

What about using container_of() [1] ?

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

This is basically what Mikhail proposed, but it will only work if the lua_State is included in LuaPrivateState by value and not by pointer (and lua_State is an incomplete type, private to `lstate.c`, so you can't include it by value).
There already is a LuaPrivateState equivalent in lstate.c:

    typedef struct LX {
    #if defined(LUAI_EXTRASPACE)
      char buff[LUAI_EXTRASPACE];
    #endif
      lua_State l;
    } LX;

exactly for this purpose, but it is not accessible outside of `lstate.c`.



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


Philipp