A bump arena reserves one byte region and advances a cursor for each allocation. Alignment may leave padding before a span, so the next address is the aligned cursor, not necessarily the old cursor itself. A checkpoint captures both the cursor and the next handle number. Rewinding restores the cursor and invalidates handles allocated after that checkpoint. Earlier handles stay valid because their spans lie before the saved cursor. This model zeroes every newly allocated span, including space reused after a rewind, and uses monotonically increasing handle numbers so an old handle cannot accidentally name a replacement span. The region has a fixed capacity; an allocation that does not fit raises an error without changing state. There is no individual free operation. The useful lifetime is a request, parse, or batch whose objects can all be discarded together.
Checkpoint arenas: reclaim a region by lifetime
Operational case
A pump inspection stores the seven-byte tag PUMP-47 at an aligned offset. The service begins a checkpoint, reserves twelve bytes for a temporary report, then abandons that report. Rewinding releases the whole temporary tail in one step. A new twelve-byte reservation may begin at the same byte offset, while the original tag remains readable through its earlier handle and the abandoned handle is rejected. Writing a shorter payload into a reused allocation would otherwise leave old bytes visible in the unread tail, so the model clears the full span before returning it. Alignment padding counts against capacity even though the caller did not request those bytes.
Working Python program
class CheckpointArena:
def __init__(self, capacity):
if capacity < 1:
raise ValueError("capacity must be positive")
self.storage = bytearray(capacity)
self.cursor = 0
self.next_handle = 0
self.next_mark = 0
self.active = {}
self.marks = []
def allocate(self, size, alignment=1):
if size < 1 or alignment < 1 or alignment & (alignment - 1):
raise ValueError("size must be positive and alignment a power of two")
start = (self.cursor + alignment - 1) & -alignment
if start + size > len(self.storage):
raise MemoryError("arena exhausted")
handle = self.next_handle
self.next_handle += 1
self.storage[start:start + size] = bytes(size)
self.active[handle] = (start, size)
self.cursor = start + size
return handle
def span(self, handle):
return self.active[handle]
def write(self, handle, payload):
start, size = self.active[handle]
if len(payload) > size:
raise ValueError("payload exceeds allocation")
self.storage[start:start + len(payload)] = payload
def read(self, handle):
start, size = self.active[handle]
return bytes(self.storage[start:start + size])
def mark(self):
token = (self.next_mark, self.cursor, self.next_handle)
self.next_mark += 1
self.marks.append(token)
return token
def rewind(self, token):
if token not in self.marks:
raise ValueError("unknown checkpoint")
_, self.cursor, first_invalid = token
self.active = {handle: span for handle, span in self.active.items()
if handle < first_invalid}
self.marks = self.marks[:self.marks.index(token)]
def reset(self):
self.cursor = 0
self.active.clear()
self.marks.clear()
arena = CheckpointArena(64)
stable = arena.allocate(7, alignment=8)
arena.write(stable, b"PUMP-47")
checkpoint = arena.mark()
discarded = arena.allocate(12, alignment=8)
old_offset = arena.span(discarded)[0]
arena.rewind(checkpoint)
reused = arena.allocate(12, alignment=8)
print("stable=", arena.read(stable).decode(), " reused=", arena.span(reused)[0] == old_offset,
" stale=", discarded not in arena.active, sep="")Output
stable=PUMP-47 reused=True stale=TrueTime, space, and tradeoff
For an arena of capacity C and alignment A, an allocation computes its aligned offset and clears S allocated bytes, taking O(S) time because this teaching model promises zeroed bytes. Without zeroing, cursor placement itself is O(1). A read or write of S bytes costs O(S). Marking is O(1). Rewind scans the active-handle dictionary to discard later handles, so it costs O(H) time for H active handles; a lower-level arena could instead use a stack of allocation records. The byte region takes O(C) space, and Python handle records take O(H) additional space. Reset invalidates all handles but retains the reserved region for reuse.
Common Mistakes
- Do not treat alignment padding as usable payload bytes.
- Do not reuse handle numbers after rewind or reset while stale handles may survive.
- Do not read uninitialized tails after reusing a span; this model clears them.
- Do not promise individual object release from an arena that only rewinds a tail.
Connected lessons
- Arrays
- Data Structures
- Resizable arrays: account for growth and shifting
- Index snapshots: publish related maps as one in-memory version
- Generational slots: reject stale handles after reuse
- Projects
- Quizzes
Compare ownership and reuse with Free spans: first-fit allocation and adjacent coalescing, Buddy blocks: split powers of two and reunite partners, Slab slots: reuse fixed-size pages with generation checks, then run the storage audit and contract quiz.
