lua-users home
lua-l archive

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



On 9-Oct-06, at 11:06 AM, David Burgess wrote:

Yes, but given that the hook function gets called a lot. I was
hoping to avoid the registry lookups and just access my counters
on the C side (seaside?).

Take a look at the LUAI_EXTRASPACE definition in luaconf.h at line 698. This allows you to place arbitrary data in the lua_State (actually, just before the lua_State).

There are some defines just following that for functions which are called at particular moments, which allow you to maintain your data. In particular, luai_userstateopen(L) is called (well, macro-expanded) when a new (master) lua_State is created, and luai_userstatethread(L, L1) is called when a new thread is created.

So something like this might solve your problem: (untried)

#include <time.h>
typedef union my_stateinfo_u {
  double dummy; /* for alignment */
  clock_t started;
} my_stateinfo;

#define STARTED(L)  ((my_stateinfo *)L)[-1].started

#define LUAI_EXTRASPACE sizeof(my_stateinfo)

#define luai_userstateopen(L) STARTED(L) = clock()

#define luai_userstatethread(L, L1) STARTED(L1) = clock()