lua-users home
lua-l archive

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


On Wed, 2005-01-12 at 15:46, William Ahern wrote:
> Having never written a single line of Lua code I briefly took a look at the
> interpreter source to see if it's possible to add malloc()/free() hooks. It
> doesn't seem like the functionality is there, though maybe not too difficult
> to add. Did I miss anything?


Hope i have this right being a Lua newbie too:

In lmem.c, in 5.0.2:

/*
** definition for realloc function. It must assure that l_realloc(NULL,
** 0, x) allocates a new block (ANSI C assures that). (`os' is the old
** block size; some allocators may use that.)
*/
#ifndef l_realloc
#define l_realloc(b,os,s)       realloc(b,s)
#endif

/*
** definition for free function. (`os' is the old block size; some
** allocators may use that.)
*/
#ifndef l_free
#define l_free(b,os)    free(b)
#endif

However, lua 5.1 (still work in progress) does this with
a runtime hook:

void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t
nsize) {
  global_State *g = G(L);
  lua_assert((osize == 0) == (block == NULL));
  block = (*g->realloc)(g->ud, block, osize, nsize);
----------*************


[BTW: those codes are spagetti .. :]


-- 
John Skaller, mailto:skaller@users.sf.net
voice: 061-2-9660-0850, 
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net