I would suggest using the _data pointer instead of _heap , which always refers to your data store. _heap == NULL will become _data == _stack , etc., but in all situations that do not change the length of the data, you could avoid the difference in the case.
The _capacity element is not included in the current sketch to track the selected space. To do this, you will need to implement the โadd to it, redistribute if necessaryโ part if you do not want to redistribute for each change in the length of the container allocated by the heap.
You might also consider freeing up heap space until your data hits the stack. Otherwise, there may be applications that add and remove one element only at this boundary, causing a distribution each time. Therefore, either implement hysteresis , or do not free up heap space at all as soon as you allocate it. In general, I would say that freeing up heap memory should go along with a shrinking heap of memory. Both of you can do this automatically, in response to a specific function call, for example, shrink_to_fit , or not at all, but there is little point in doing one and not the other in a similar situation.
In addition, I believe that your decision is almost everything you can hope for. Perhaps specify a default value for MinSize . If MinSize is small to avoid, then wasting this space will not be a big problem, right?
Of course, ultimately it all depends on your actual application, since many unused stack allocations of this form can have an adverse effect, for example. to stack memory caching. Given the fact that default allocators can be pretty smart, you should probably check to see if you really get anything from this optimization for this application.
source share