[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Are there some potential problems if I use a self-defined memory acquirement function instead of realloc(3) in the l_alloc function?
- From: Viacheslav Usov <via.usov@...>
- Date: Mon, 10 Aug 2020 16:32:28 +0200
On Mon, Aug 10, 2020 at 2:34 PM Stefan <firstname.lastname@example.org> 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.