[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: one-time-set, many-times-use data
- From: C++ RTMP Server <crtmpserver@...>
- Date: Sun, 3 Apr 2011 02:40:36 +0300
On Apr 3, 2011, at 1:54 AM, liam mail wrote:
>
>
> On 2 April 2011 23:12, C++ RTMP Server <crtmpserver@gmail.com> wrote:
> Hi,
>
> I have a special situation in which I have to create an association between my app-specific state and the lua_State. For this I have 3 solutions:
>
> 1. create pair of maps: map <MyStateStruct*, lua_State *> and the other way around map <lua_State *, MyStateStruct*>
> 2. create an unique ID for each MyStateStruct and store that ID into GLOBALINDEX inside lua_State
> 3. Same as 2, but store MyStateStruct directly into lua_State GLOBALINDEX as user-data
>
> Honestly, I don't like any of them:
>
> 1st one introduces a lot of management code and is prone to errors
> 2nd and 3rd introduces few calls for extracting my MyStateStruct structure
>
> I hope that there is a way to associate a void * pointer with a lua_State so when lua calls C/C++ code I can get my hands on the MyStateStruct without calling additional stuff (get the global index) or indexing through those 2 maps. I was thinking more like simple dereferencing a void * inside lua_State
>
> int luaapi_application_getConfig(lua_State *L) {
> MyStateStruct *pMyState=(MyStateStruct *)L->pMagicUserDataField;
> //do stuff with pMyState
> ....
> }
>
> Thank you
> ------
> Eugen-Andrei Gavriloaie
> Web: http://www.rtmpd.com
>
>
>
> If there is a one to one mapping of application state to lua_State, you can register the luaCfunctions with the upvalue stored as a void pointer. See the section [1] in Programming in Lua which talks about this situation using the registry or upvalues.
>
> [1] http://www.lua.org/pil/27.3.html
Hi Liam,
That is the same thing as the 2nd or 3rd approach that I suggested. I was looking for some way of accessing ud from
LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud);
I can clearly see inside the lua sources (lstate.c) that ud is stored:
.....
g->frealloc = f;
g->ud = ud;
g->mainthread = L;
....
However, I didn't find any way of pulling it out from a lua_State. I know that is used inside the lua_Alloc kind of function, but is quite useless for my case. I want it accessible whenever I have a pointer to a lua_State as fast as possible without playing around with the stack/globalindex
Cheers,
Andrei
>
> Liam
>
------
Eugen-Andrei Gavriloaie
Web: http://www.rtmpd.com