Mysterious Lua C API functions lua_getallocf() and lua_setallocf()

69 views
Skip to first unread message

Alexander Walz

unread,
Sep 20, 2025, 10:21:33 AM (7 days ago) Sep 20
to Lua

Hello,

 

I stumbled about the Lua C API functions lua_getallocf() and lua_setallocf() recently, described shortly in the Lua 5.1+ manuals.

 

Searching the Internet, there are no helpful hints, unfortunately.

 

What are they for ?

Yours,

Alex
http://agena.sourceforge.net



Sainan

unread,
Sep 20, 2025, 10:42:27 AM (7 days ago) Sep 20
to lu...@googlegroups.com
They get/set the 'frealloc' function, initially set in lua_newstate. It handles allocating/resizing/freeing memory used by Lua. Useful to hook for memory limits.

You can take a look at lauxlib.c for the implementation of luaL_newstate and the 'l_alloc' function.

-- Sainan

Lourival Vieira Neto

unread,
Sep 20, 2025, 11:05:24 AM (7 days ago) Sep 20
to lu...@googlegroups.com
On Sat, Sep 20, 2025 at 11:21 AM Alexander Walz <al-a...@t-online.de> wrote:
>
> Hello,
>
> I stumbled about the Lua C API functions lua_getallocf() and lua_setallocf() recently, described shortly in the Lua 5.1+ manuals.
>
> Searching the Internet, there are no helpful hints, unfortunately.
>
> What are they for ?

They are used for setting a new memory allocator or retrieving the
current one from a given Lua state. Think about the case where you
have no C standard library available or don't want to use the standard
realloc function for any reason. We use both APIs on Lunatik [1, 2].
We use lua_setallocf to set a memory allocator suited for the specific
context we are running Lua (e.g., kernel threads, softirqs); and we
use lua_getallocf when a binding needs to follow the same allocation
parameters from a given Lua state (as we can have the same binding
being used for multiple states and contexts).

[1] https://github.com/luainkernel/lunatik/blob/master/lunatik_core.c#L282
[2] https://github.com/luainkernel/lunatik/blob/master/lunatik.h#L107

Regards,
--
Lourival Vieira Neto

Luau Project

unread,
Sep 20, 2025, 11:27:00 AM (7 days ago) Sep 20
to lu...@googlegroups.com

As others said, it allows one to get/set the default memory allocator function used by Lua (see https://www.lua.org/manual/5.1/manual.html#lua_Alloc ). In simple terms, it allows you to use (lua_getallocf) or redefine (lua_setallocf) `malloc', `realloc' and `free' routines used by Lua and its modules.

On that `lua_Alloc' description, `osize' is kind of current (old) size held by the pointer and `nsize' means the new size wanted for the pointer. Take a look in the following basic sample code in C for Lua 5.4 (not going for all checks):

 --
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lua-l+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/lua-l/1758378077149.153494.a970b60657db676524a751130b004f5f29409e82%40spica.telekom.de.

Luau Project

unread,
Sep 20, 2025, 12:07:09 PM (7 days ago) Sep 20
to lu...@googlegroups.com
noticed a mistake on my previous sample code: replace all occurrences of
`allocf(&ud` by `allocf(ud`.

Francisco Olarte

unread,
Sep 20, 2025, 1:03:11 PM (7 days ago) Sep 20
to lu...@googlegroups.com
On Sat, 20 Sept 2025 at 16:21, Alexander Walz <al-a...@t-online.de> wrote:

I stumbled about the Lua C API functions lua_getallocf() and lua_setallocf() recently, described shortly in the Lua 5.1+ manuals.

Your doubts seem to be answered in previous mail, but checking them in 5.4 docs I noticed the function is referred as both "the memory-allocation" and "the allocator function" in others. The description of lua_Alloc uses both in consecutive sentences, easy to see, but set uses one and get the other. It may be good to clean it up a bit ( I would suggest calling it "allocator function", which does memory allocation, due to how it is used in lua_newstate entry ).

Francisco Olarte.

Reply all
Reply to author
Forward
0 new messages