A few questions about memory allocation
"'Sewbacca' via lua-l" <[email protected]>
| Newsgroups | gmane.comp.lang.lua.general |
|---|---|
| Message-ID | <[email protected]> |
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;
* Are full userdatas always allocated on the heap, no matter how small
(i.e. pointer sized)?
* Allocation wise it is always better to allocate the data directly in
the space provided by Lua rather than just saving a pointer and
calling malloc?
* Is there a way to do pass by value rather than reference for tables
and/or (full) userdatas?
* Is it possible to allocate data on the stack rather than the heap?
--
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 [email protected].
To view this discussion visit https://groups.google.com/d/msgid/lua-l/725e868b-3877-4550-a4d4-d75ed98bac4f%40kolabnow.com.