A free by address must leave the owner's set, and release must walk a copy
With two dicts, every call has to keep both of them right.
owned = {"job-1": {0, 5}}
for start in owned["job-1"]:
owned["job-1"].discard(start)
RuntimeError: Set changed size during iteration
That loop is release in small: it walks an owner's set while each free removes a start from it. Two details keep the three states agreeing.
First, free by address has to take the start out of its owner's set. Otherwise a later release frees that address again:
- If the address is still free, that
freeraisesValueErrorpartway through the release, and the blocks the walk hadn't reached stay live. - If it was handed out again since,
releasesilently frees the new block, which may belong to another owner.
Second, release must walk a copy. A set can't change size while a loop walks it. A release that looped over self.owned.get(owner, ()) and called free raised the same RuntimeError after the first free, with two blocks for the owner. Walk list(...) or sorted(...) of the set, and the loop is safe.
release costs one free per block the owner holds, each O(log k) to search plus the shift. A set rather than a list for each owner's starts makes the removal in free O(1).
The Allocator in the editor has take, give_back and alloc written, with owners. Finish its free so it keeps owned up to date, and write its release.