A block grows in place only into the free run that starts where it ends
resize asks a block to grow or shrink without moving it.
Growing needs a free run that starts exactly at the block's end and reaches far enough
Predict
A ends at 4 and asks to end at 7. The free run that starts at 4 is (4, 6). Can A grow in place?
A ends at 4 and asks to end at 7
0123456789
run (4, 8): yes
AAAA
after the grow
AAAAAAA
The run is now (7, 8).
run (4, 6): no
AAAA
Unit 6 is used.
no run at 4: no
AAAA
Unit 4 is used.
A buffer that outgrows its block, like a request's cache that gets longer with every token, wants more room without being copied. C's realloc does this when it can, and the glibc manual says when it can't: "Since the space after the end of the block may be in use, realloc may find it necessary to copy the block to a new address where more free space is available."
That sentence is the whole rule. A block that ends at end can grow to new_end in place only if every unit from end to new_end - 1 is free. Since the free list's runs never touch, those units all belong to one run: the run that starts exactly at end. One binary search finds it, at position i.
If the run at i starts exactly at end and ends at or after new_end: yes. Take its front, and drop it if it's used up exactly.
If it starts exactly at end and ends before new_end: no. The units past its end are in use, or past the arena.
If it starts after end, or there's no run at i: no. The unit at end is in use, or end is the arena's end.
When the answer is no, change nothing and say so. The caller then does what realloc does: allocate a new block, copy, free the old one.
grow(free, end, new_end) is the grow half of resize, with the bisect_left written. Finish it so it takes units from the run that starts exactly at end, or returns False and changes nothing.
Pick an answer above to see the rest
Instructions
1
A live block ends at end and wants to end at new_end instead, which is larger. free is the free list. Finish grow(free, end, new_end): if the free run that starts exactly at end reaches at least new_end, take the units end to new_end - 1 from its front, change free in place and return True. Otherwise return False and change nothing.