Allocator interface: make shrinkFn optional #2292
Labels
proposal
This issue suggests modifications. If it also has the "accepted" label then it is planned.
standard library
This issue involves writing Zig code for the standard library.
Milestone
I propose that
shrinkFn
be an optional pointer. An implementation of the Allocator interface would set this tonull
when it does not provide a way to reclaim memory. The Allocator interface wrapper code would provide a shim implementation of shrink which simply didreturn old_mem[0..new_size];
.The benefit of this proposal is that the Allocator interface could have a new function called, perhaps,
canReclaimMemory()
which is simplyself.shrinkFn != null
. IfcanReclaimMemory
returnsfalse
then the user of the allocator knows they don't have to bother withfree()
.This would be used, for example, here:
zig/std/zig/parse.zig
Lines 12 to 13 in bddbbef
This code currently unconditionally creates an arena allocator (which does not reclaim memory with
free()
) on top of the provided allocator. This is a waste of memory and computations if the provided allocator already does not reclaim memory. So it would be adjusted to only create the arena allocator if the provided allocator reportedtrue
forcanReclaimMemory
.Finally, this would also simplify some allocator implementations since they could just do
.shrinkFn = null
rather than provide a useless shrink function.The text was updated successfully, but these errors were encountered: