3 ms·
This appears to assume that there will be no intervening allocations that are allowed to use the same region of memory, since otherwise your "hole" will be very
by sfink 4y ago
This appears to assume that there will be no intervening allocations that are allowed to use the same region of memory, since otherwise your "hole" will be very discontiguous.
But if that is the case, then why are you releasing memory? That's just costing you time moving the data. With a doubling mechanism:
Start with an initial allocation of 10 bytes of memory.
On growth allocate another 10, and don't copy anything, leaving no hole at all.
On growth allocate another 20, and don't copy anything, leaving no hole at all.
etc.
In what scenario would the growth factor of 1.5 actually help? If you're restricting the allocation API to only malloc then you can't grow the size of your allocation and what you said might make sense as something the allocator could take advantage of internally. But realloc exists, and will hopefully try to extend an existing allocation if possible. (If your earlier allocation was fairly small, then the allocator might have put it in an arena for fixed-size allocations so it won't be possible to merge two of them, but once things get big they generally get their own VMA area. Obviously totally dependent on the allocator implementation, and there are many.)