lua-users home
lua-l archive

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


On Mon, Aug 10, 2020 at 2:34 PM Stefan <ste@evelance.de> wrote:

> I think Lua relies on the behaviour that the realloc-like function
> does not return a different pointer (copy) for shrinking an existing
> block. Maybe that could be a problem for some allocators.

Such a behavior is not documented for realloc() either by the ISO C
Standard or by POSIX. Many modern allocators use different pools of
memory blocks for dissimilar allocation sizes, say one block for up to
16 byte allocations and another for 16 to 32, etc. Therefore shrinking
an allocation in size, if it is to have any effect, would necessarily
move it into another block of memory. To use a practical example,
tcmalloc may move allocations when shrinking.

I'd be surprised if Lua really assumed this. What it does assume, and
that is documented, is "that the allocator never fails when osize >=
nsize.", which not an intrinsically safe assumption about realloc(),
but which can be assured on top of it in lua_Alloc(). The above
mentioned tcmalloc, by looking at its code, seems capable of returning
a null pointer when shrinking.

Cheers,
V.