lua-users home
lua-l archive

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


> I guess the way to
> implement it is to get the default allocator with lua_getallocf and wrap
> that in a function which I pass to lua_setallocf.

No, this is not recommended.
Because since the time you have called luaL_newstate, and particularly
if you have already called luaL_openlibs, you will miss plenty of
allocations in the statistics.
Worse: if the new allocator is not fully compatible with the default
one (for example because you have offset the returned pointer to
include a UID in the chunk), Lua will crash at exit.

The best way is to replace the initial call to the convenience
function luaL_newstate() with a call to the core function
lua_newstate(lua_Alloc f, void *ud), passing along your new allocator
function.
To write that allocation function, start by copying the reference
implementation showed in [1]. Then study how it is called using some
printf's or a debugger. Then modify it carefully, and beware: even a
slight bug in that function will crash Lua sooner or later.

¨[1] http://www.lua.org/manual/5.2/manual.html#lua_Alloc