1-- A caller-backed reclaiming allocator. Unlike core/arena's arena, every
2-- successful allocation owns one uniform slot. Free indices form a min heap
3-- in caller-supplied metadata for lowest-index reuse. No operation imports
4-- or falls back to a backing allocator.
5
6import core/mem
7
8--- Caller-owned bookkeeping record for one pool slot. Supply an initialized
9--- slice to `over`; the provider maintains its fields after construction.
10public slot: type = struct
11 size: usize
12 free_index: usize
13end slot
14
15pool_state: type = struct
16 extent: usize
17 slot_size: usize
18 slot_count: usize
19 slot_alignment: usize
20 first_offset: usize
21 stride: usize
22 free_count: usize
23 allocations: usize
24 frees: usize
25 live_count: usize
26 rejected_frees: usize
27end pool_state
28
29--- Caller-owned state of a pool: its geometry, free-heap size and counters.
30--- Declare it `zeroed` and give it to `over`, which fills it; it must
31--- outlive the pool handle.
32public space: type = pool_state
33
34fixed_pool: type = struct
35 state: ptr mut pool_state
36 base: ptr mut u8
37 bookkeeping: []mut slot
38end fixed_pool
39
40--- A reclaiming allocator over uniform slots in caller storage. It is a
41--- handle: copies share one pool. It reuses the lowest free index and has no
42--- fallback allocator. Even a zero-byte allocation occupies a slot. A growth
43--- or a resize keeps the block in its slot, at any extent the slot holds,
44--- and refuses a larger one.
45public provider: type = fixed_pool
46
47slot_stride: (size: usize, alignment: usize)
48 -> (stride: usize) ! mem.out_of_memory =
49 fail mem.out_of_memory when size == 0
50 maximum: usize = 0 -% 1
51 mut padding: usize = 0
52 if alignment > 1 then
53 remainder: usize = size % alignment
54 if not (remainder == 0) then
55 padding = alignment - remainder
56 end if
57 end if
58 fail mem.out_of_memory when padding > maximum - size
59 stride = size + padding
60end slot_stride
61
62--- Fill `state` for a pool over caller backing and bookkeeping and return its
63--- handle. Validates positive slot size, alignment, extent and record
64--- capacity before changing `state`. Keep `state` and both backing ranges
65--- alive until all allocations end.
66---
67--- `slot_size` is positive. The caller supplies the initialized `bookkeeping`
68--- slice; its length is the finite bookkeeping capacity, and `slot_count` may
69--- be zero but may not exceed it. The maximum `usize` marks a free slot, so a
70--- pool with slots cannot use that slot size. Such a pool aligns its first
71--- slot from the absolute backing address, counts that padding against
72--- `extent`, and proves that every later slot and its complete payload fit
73--- both the backing extent and the target address space before publishing the
74--- provider.
75public over: (inout state: space, base: ptr mut u8, extent: usize,
76 slot_size: usize, slot_count: usize, slot_alignment: usize,
77 bookkeeping: []mut slot)
78 -> (handle: provider from state, base, bookkeeping)
79 ! mem.out_of_memory =
80 fail mem.out_of_memory when slot_count > lenof bookkeeping
81 empty: usize = 0 -% 1
82 fail mem.out_of_memory when slot_count > 0 and slot_size == empty
83
84 mut normalized_alignment: usize = slot_alignment
85 if normalized_alignment <= 1 then
86 normalized_alignment = 1
87 end if
88 step: usize = try slot_stride(slot_size, normalized_alignment)
89
90 mut offset: usize = 0
91 if slot_count > 0 then
92 offset = try mem.aligned_offset(base, 0, extent, slot_size,
93 normalized_alignment)
94 remaining_slots: usize = slot_count - 1
95 after_first: usize = extent - offset - slot_size
96 fail mem.out_of_memory when remaining_slots > after_first / step
97
98 maximum: usize = 0 -% 1
99 first_address: usize = usize(base) + offset
100 address_room: usize = maximum - first_address - slot_size
101 fail mem.out_of_memory when remaining_slots > address_room / step
102 end if
103
104 mut index: usize = 0
105 while index < slot_count do
106 bookkeeping[index] = (size: empty,
107 free_index: index)
108 inc index
109 end while
110
111 state = (extent: extent, slot_size: slot_size, slot_count: slot_count,
112 slot_alignment: normalized_alignment,
113 first_offset: offset, stride: step, free_count: slot_count,
114 allocations: 0, frees: 0, live_count: 0,
115 rejected_frees: 0)
116 handle = (state: addr state, base: base, bookkeeping: bookkeeping)
117end over
118
119-- Keep indexed heap access in shared routines. Each access still checks its
120-- slice bound, while freestanding callers do not duplicate the aggregate
121-- address calculation at every step of both heap operations.
122free_index_at: (self: fixed_pool, index: usize) -> (value: usize) =
123 value = self.bookkeeping[index].free_index
124end free_index_at
125
126store_free_index: (self: fixed_pool, index: usize, value: usize) -> none =
127 self.bookkeeping[index].free_index = value
128end store_free_index
129
130take_free_index: (self: fixed_pool) -> (index: usize) =
131 state: ptr mut pool_state = self.state
132 index = free_index_at(self, 0)
133 dec state.val.free_count
134 if state.val.free_count > 0 then
135 last: usize = free_index_at(self, state.val.free_count)
136 mut position: usize = 0
137 while position < state.val.free_count / 2 do
138 child: usize = position * 2 + 1
139 mut smallest: usize = child
140 if child + 1 < state.val.free_count
141 and free_index_at(self, child + 1)
142 < free_index_at(self, child)
143 then
144 smallest = child + 1
145 end if
146 if last <= free_index_at(self, smallest) then
147 break
148 end if
149 store_free_index(self, position, free_index_at(self, smallest))
150 position = smallest
151 end while
152 store_free_index(self, position, last)
153 end if
154end take_free_index
155
156return_free_index: (self: fixed_pool, index: usize) -> none =
157 state: ptr mut pool_state = self.state
158 mut position: usize = state.val.free_count
159 while position > 0 do
160 parent: usize = (position - 1) / 2
161 if free_index_at(self, parent) <= index then
162 break
163 end if
164 store_free_index(self, position, free_index_at(self, parent))
165 position = parent
166 end while
167 store_free_index(self, position, index)
168 inc state.val.free_count
169end return_free_index
170
171pool_alloc: (self: fixed_pool, size: usize, alignment: usize)
172 -> (block: ptr mut u8) ! mem.out_of_memory =
173 state: ptr mut pool_state = self.state
174 fail mem.out_of_memory when size > state.val.slot_size
175 if alignment > 1 then
176 -- A power-of-two request divides the slot alignment iff its low
177 -- bits are clear; other requests retain numeric divisibility.
178 slot_alignment: usize = state.val.slot_alignment
179 mask: usize = alignment - 1
180 if (alignment & mask) == 0 then
181 fail mem.out_of_memory when (slot_alignment & mask) <> 0
182 else
183 fail mem.out_of_memory when slot_alignment % alignment <> 0
184 end if
185 end if
186
187 fail mem.out_of_memory when state.val.free_count == 0
188 index: usize = take_free_index(self)
189 distance: usize = index * state.val.stride
190 -- [0810]: the block borrows nothing; the caller keeps the backing.
191 address: usize = usize(self.base) + state.val.first_offset + distance
192 block = ptr(address)
193 selected: ptr mut slot = addr self.bookkeeping[index]
194 selected.val.size = size
195 inc state.val.allocations
196 inc state.val.live_count
197end pool_alloc
198
199-- Record the extent `new_size` for the live slot that holds `block` with
200-- the extent `old_size`, and return its index, or return the slot count,
201-- changing nothing, when `block` is no such slot: a free, a growth and a
202-- resize each find their slot this way, so each refuses what the others do.
203retag: (self: fixed_pool, block: ptr mut u8, old_size: usize,
204 new_size: usize) -> (index: usize) =
205 state: ptr mut pool_state = self.state
206 index = state.val.slot_count
207 first_address: usize = usize(self.base) + state.val.first_offset
208 address: usize = usize(block)
209 return when address < first_address
210 distance: usize = address - first_address
211 return when distance % state.val.stride <> 0
212 found: usize = distance / state.val.stride
213 return when found >= index
214 selected: ptr mut slot = addr self.bookkeeping[found]
215 return when selected.val.size == 0 -% 1 or selected.val.size <> old_size
216 selected.val.size = new_size
217 index = found
218end retag
219
220pool_free: (self: fixed_pool, block: ptr mut u8, size: usize) -> none =
221 state: ptr mut pool_state = self.state
222 index: usize = retag(self, block, size, 0 -% 1)
223 if index == state.val.slot_count then
224 inc state.val.rejected_frees
225 return
226 end if
227 return_free_index(self, index)
228 inc state.val.frees
229 dec state.val.live_count
230end pool_free
231
232-- Every slot has the same size, so a block grows in its own slot or not
233-- at all: an extent the slot holds is recorded for the block's free, and
234-- a larger one, or a block that is not a live slot of `old_size`, is
235-- refused with the pool unchanged. The block stays where it is, so it
236-- keeps the alignment it was allocated with.
237pool_grow: (self: fixed_pool, block: ptr mut u8,
238 old_size: usize, new_size: usize, alignment: usize)
239 -> (grown: bool) =
240 _ = alignment
241 state: ptr mut pool_state = self.state
242 grown = new_size <= state.val.slot_size
243 and retag(self, block, old_size, new_size) <> state.val.slot_count
244end pool_grow
245
246-- A resize is a growth or a shrink in the block's own slot, and one the
247-- slot cannot hold is refused: another slot is no larger.
248pool_resize: (self: fixed_pool, block: ptr mut u8,
249 old_size: usize, new_size: usize, alignment: usize)
250 -> (moved: ptr mut u8) ! mem.out_of_memory =
251 fail mem.out_of_memory
252 when not pool_grow(self, block, old_size, new_size, alignment)
253 -- [0810]: the block borrows nothing, as an allocation does.
254 moved = ptr(usize(block))
255end pool_resize
256
257fixed_pool is mem.allocator (alloc: pool_alloc, grow: pool_grow,
258 resize: pool_resize, free: pool_free)
259
260--- Return the configured number of allocatable slots.
261public capacity: (memory: provider) -> (count: usize) =
262 count = memory.state.val.slot_count
263end capacity
264
265--- Return the length of the supplied metadata slice, which may exceed the
266--- configured slot count.
267public bookkeeping_capacity: (memory: provider) -> (count: usize) =
268 count = lenof memory.bookkeeping
269end bookkeeping_capacity
270
271--- Return the number of currently allocated slots.
272public live: (memory: provider) -> (count: usize) =
273 count = memory.state.val.live_count
274end live
275
276--- Return the number of successful allocations, including zero-byte
277--- allocations.
278public allocations: (memory: provider) -> (count: usize) =
279 count = memory.state.val.allocations
280end allocations
281
282--- Return the number of successful slot reclamations.
283public frees: (memory: provider) -> (count: usize) =
284 count = memory.state.val.frees
285end frees
286
287--- Return frees rejected for an invalid address, extent or already-free slot.
288--- A rejected free does not reclaim a slot.
289public rejected_frees: (memory: provider) -> (count: usize) =
290 count = memory.state.val.rejected_frees
291end rejected_frees