I was wondering the other day how Lua handles memory allocations
for a few reasons.
Sometimes it's nice to do calculations on the stack rather than on
the heap, if I know that the data is only used within that
callframe.
This is especially true for vector calculations. In fact sometimes
it would be even better if I could pass vectors by value rather
than by reference,
because vectors are most of the time two to four numbers, so
treating them like numbers would be nice, but AFAIK Lua doesn't
support that directly.
For some applications it would be beneficial to allocate on the
stack rather than the heap, even some larger userdatas/tables.
The next best thing would be to preallocate an arena and just
hand userdatas with pointers.
If I allocate a userdata object of a small size, say the size of a
pointer, does Lua allocate it on the heap or does it manage an
arena of some kind?
I suspect it allocates the data on the heap, so it's probably best
to allocate the C structure directly in the memory provided by
Lua, rather than calling malloc myself and just storing just a
pointer right?
Of course could manage my own arena and hand out light userdata
objects, but this means I cannot attach different metatables to
different pointers.
Any comments and or suggestions would be appreciated!
TLDR;
I was wondering the other day how Lua handles memory allocations for a few reasons.
Sometimes it's nice to do calculations on the stack rather than on the heap, if I know that the data is only used within that callframe.
This is especially true for vector calculations. In fact sometimes it would be even better if I could pass vectors by value rather than by reference,
because vectors are most of the time two to four numbers, so treating them like numbers would be nice, but AFAIK Lua doesn't support that directly.
For some applications it would be beneficial to allocate on the stack rather than the heap, even some larger userdatas/tables.
The next best thing would be to preallocate an arena and just hand userdatas with pointers.
If I allocate a userdata object of a small size, say the size of a pointer, does Lua allocate it on the heap or does it manage an arena of some kind?
I suspect it allocates the data on the heap, so it's probably best to allocate the C structure directly in the memory provided by Lua, rather than calling malloc myself and just storing just a pointer right?
Of course could manage my own arena and hand out light userdata objects, but this means I cannot attach different metatables to different pointers.