Re: A few questions about memory allocation
Denis Dos Santos Silva <[email protected]>
| Newsgroups | gmane.comp.lang.lua.general |
|---|---|
| Message-ID | <[email protected]> |
- Is it possible to allocate data on the stack rather than the heap?
you can try implement a `arena allocator` like using a large buffer
Em sexta-feira, 20 de fevereiro de 2026 às 21:29:44 UTC-3, Sean Conner
escreveu:
> It was thus said that the Great 'Sewbacca' via lua-l once stated:
> > I was wondering the other day how Lua handles memory allocations for a
> > few reasons.
> >
> > 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!
>
> Read up on lua_Alloc: <
> https://www.lua.org/manual/5.5/manual.html#lua_Alloc>
>
> It states:
>
> typedef void * (*lua_Alloc) (void *ud,
> void *ptr,
> size_t osize,
> size_t nsize);
>
> The type of the memory-allocator function used by Lua states. The
> allocator function must provide a functionality similar to realloc,
> but not exactly the same. Its arguments are ud, an opaque pointer
> passed to lua_newstate; ptr, a pointer to the block being
> allocated/reallocated/freed; osize, the original size of the block
> or some code about what is being allocated; and nsize, the new size
> of the block.
>
> When ptr is not NULL, osize is the size of the block pointed by ptr,
> that is, the size given when it was allocated or reallocated.
>
> When ptr is NULL, osize encodes the kind of object that Lua is
> allocating. osize is any of LUA_TSTRING, LUA_TTABLE, LUA_TFUNCTION,
> LUA_TUSERDATA, or LUA_TTHREAD when (and only when) Lua is creating a
> new object of that type. When osize is some other value, Lua is
> allocating memory for something else.
>
>
> So just provide your own allocation function (via lua_newstate()). I think
> something like:
>
> void *my_alloc(void *ud,void *ptr,size_t osize,size_t nsize)
> {
> if (ptr == NULL) /* grabbing some memory */
> {
> if ((osize == LUA_TUSERDATA) && (nsize == sizeof(arenea_object))
> return next_arenaobject();
> else
> return realloc(ptr,nsize);
> }
> else
> {
> if (nsize == 0) /* freeing memory */
> {
> if (is_arenaobject(ptr))
> mark_areanaobject_free(ptr);
> else
> free(ptr);
> }
> else
> return realloc(ptr,nsize);
> }
> }
>
> -spc
>
>
--
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/7bc02c55-09d8-48f9-9fb4-56bb142d9d71n%40googlegroups.com.