lua-users home
lua-l archive

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


On 10 January 2013 00:38, Luiz Henrique de Figueiredo
<lhf@tecgraf.puc-rio.br> wrote:
>> is there an easy way to convert from malloc and free to using the Lua allocator? Some macros perhaps? Any example code?
>
> #define LUA_ALLOC(p,size)       lua_getallocf(L,NULL)(NULL,p,0,size)
> #define malloc(size)            LUA_ALLOC(NULL,size)
> #define free(p)                 LUA_ALLOC(p,0)
> #define realloc(p)              LUA_ALLOC(p,size)
>
> This code assumes a Lua state named L in the scope. If that is not the case,
> fix LUA_ALLOC.
>

Be careful as this can result in undefined behaviour as malloc, free
and realloc can be reserved identifiers, see 7.1.3 C99 for more info.

--Liam