1import core/mem
2
3--- Ledger entry storing an allocated address and its byte extent. A region
4--- records one for every payload it hands out.
5public allocation: type = struct
6 address: usize
7 extent: usize
8end allocation
9
10-- How many records are in use, of a bounded ledger or of a growing
11-- ledger's newest block, and that block: its address, zero when there is
12-- none, and the records it holds.
13book_value: type = struct
14 used: usize
15 address: usize
16 room: usize
17end book_value
18
19space_value: type (fixed capacity: usize) = struct
20 book: book_value
21 records: [capacity]allocation
22end space_value
23
24--- Caller-owned state of a region: its ledger. With a positive `capacity`
25--- the ledger is these `capacity` records and no more; with zero it grows
26--- through the parent. Declare it `zeroed` and give it to `over`, which
27--- fills it; it must outlive the region handle, and `release` empties it.
28public space: type (fixed capacity: usize) = space_value(capacity)
29
30-- A capacity of zero means the growing ledger: spec.md's D356.
31--- Caller-owned state of a region whose ledger grows through its parent:
32--- the one ledger a capacity of zero means, since a bounded ledger of no
33--- records could hold nothing. Declare it `zeroed`.
34public growing_space: type = space_value(0)
35
36region_value: type (provider: type) = struct
37 parent: provider
38 book: ptr mut book_value
39 records: []mut allocation
40end region_value
41
42--- An allocator that takes its payloads from a parent and records each one.
43--- It is a handle: copies share one ledger. Releasing its state frees all
44--- recorded payloads; the parent must outlive the region.
45public region: type (provider: type) = region_value(provider)
46
47--- Fill `state` for a region over `parent` and return its handle. The handle
48--- holds a copy of the parent's handle. Every allocation takes one ledger
49--- record until `release`, since an individual free returns nothing. A
50--- bounded ledger that is full reports `mem.out_of_memory` and returns the
51--- new payload to the parent; a growing ledger's own allocation failure does
52--- the same.
53public over: (fixed capacity: usize, provider: type is mem.allocator,
54 inout state: space(capacity), parent: provider)
55 -> (handle: region(provider) from state, parent) =
56 state.book = (used: 0, address: 0, room: 0)
57 handle = (parent: parent, book: addr state.book,
58 records: state.records[0..<capacity])
59end over
60
61record_size: usize = sizeof allocation
62
63region_alloc: (provider: type is mem.allocator,
64 self: region(provider), size: usize, alignment: usize)
65 -> (block: ptr mut u8) ! mem.out_of_memory =
66 block = try mem.allocate(self.parent, size, alignment)
67 record: allocation = (address: usize(block), extent: size)
68 record_allocation(self, record) else (problem)
69 mem.release(self.parent, block, size)
70 fail problem
71 end
72end region_alloc
73
74-- Keep one record, in the caller's bounded ledger or in the newest block of
75-- a growing ledger.
76record_allocation: (provider: type is mem.allocator,
77 self: region(provider), record: allocation)
78 -> none ! mem.out_of_memory =
79 book: ptr mut book_value = self.book
80 records: []mut allocation = self.records
81 if lenof records > 0 then
82 fail mem.out_of_memory when book.val.used == lenof records
83 records[book.val.used] = record
84 inc book.val.used
85 return
86 end if
87 if book.val.used == book.val.room then
88 try grow_ledger(self)
89 end if
90 inc book.val.used
91 slot: ptr mut allocation =
92 ptr(book.val.address + book.val.used * record_size)
93 slot.val = record
94end record_allocation
95
96-- A growing ledger is a chain of blocks taken from the parent, newest
97-- first, so that no record is ever copied. A block's first record is the
98-- ledger's record of the block before it, zero for the first block, and
99-- the region's records follow. The first block is seven records long and
100-- each later one twice as long and one more, to at most 255: one record
101-- short of a power of two, so that with a parent's two-word header it
102-- fills a power-of-two size class. A small region takes little, and a
103-- large one a block per 254 allocations.
104first_block: usize = 7
105largest_block: usize = 255
106
107-- Start a new block in front of the ledger. A failure leaves the ledger as
108-- it was.
109grow_ledger: (provider: type is mem.allocator, self: region(provider))
110 -> none ! mem.out_of_memory =
111 book: ptr mut book_value = self.book
112 mut length: usize = first_block
113 mut older: allocation = (address: 0, extent: 0)
114 if book.val.room > 0 then
115 length = 2 * (book.val.room + 1) + 1
116 if length > largest_block then length = largest_block end if
117 older = (address: book.val.address,
118 extent: (book.val.room + 1) * record_size)
119 end if
120 fresh: ptr mut u8 = try mem.allocate(self.parent, length * record_size,
121 alignof allocation)
122 first: ptr mut allocation = ptr(usize(fresh))
123 first.val = older
124 book.val.address = usize(fresh)
125 book.val.used = 0
126 book.val.room = length - 1
127end grow_ledger
128
129region_free: (provider: type is mem.allocator, self: region(provider),
130 block: ptr mut u8, size: usize) -> none =
131 _ = self.book
132 _ = block
133 _ = size
134end region_free
135
136-- The block the newest record holds grows where it is when its parent
137-- grows it, and its record follows; any other block is refused, as a
138-- provider may always refuse, and is moved by a resize instead.
139region_grow: (provider: type is mem.allocator,
140 self: region(provider), block: ptr mut u8,
141 old_size: usize, new_size: usize, alignment: usize)
142 -> (grown: bool) =
143 grown = false
144 newest: usize = newest_record(self)
145 return when newest == 0
146 record: ptr mut allocation = ptr(newest)
147 return when record.val.address <> usize(block)
148 or record.val.extent <> old_size
149 grown = provider.grow(self.parent, block, old_size, new_size, alignment)
150 if grown then
151 record.val.extent = new_size
152 end if
153end region_grow
154
155-- Where the newest record is, or zero when there is none: the last of a
156-- bounded ledger's records, or the last in a growing ledger's newest
157-- block.
158newest_record: (provider: type, self: region(provider))
159 -> (address: usize) =
160 book: ptr mut book_value = self.book
161 records: []mut allocation = self.records
162 address = 0
163 return when book.val.used == 0
164 if lenof records > 0 then
165 address = usize(addr records[book.val.used - 1])
166 else
167 address = book.val.address + book.val.used * record_size
168 end if
169end newest_record
170
171-- The block the newest record holds is resized through the parent, which
172-- may resize it in place or move it, and its record follows it. Any other
173-- block is moved into a fresh allocation, which takes a record of its own,
174-- while the old one stays recorded until `release`, as a freed one does.
175region_resize: (provider: type is mem.allocator,
176 self: region(provider), block: ptr mut u8,
177 old_size: usize, new_size: usize, alignment: usize)
178 -> (moved: ptr mut u8) ! mem.out_of_memory =
179 newest: usize = newest_record(self)
180 if newest <> 0 then
181 record: ptr mut allocation = ptr(newest)
182 if record.val.address == usize(block)
183 and record.val.extent == old_size then
184 moved = try mem.resize(self.parent, block, old_size, new_size,
185 alignment)
186 record.val = (address: usize(moved), extent: new_size)
187 return
188 end if
189 end if
190 moved = try mem.relocate(self, block, old_size, new_size, alignment)
191end region_resize
192
193(provider: type is mem.allocator) region_value(provider) is mem.allocator
194 (alloc: region_alloc, grow: region_grow, resize: region_resize,
195 free: region_free)
196
197--- Free recorded payloads in reverse order through `parent`, which must be
198--- the parent the region was made over, and give a growing ledger's blocks
199--- back to it; `state` is then empty and may be given to `over` again. End
200--- every payload use and every use of the region's handle first. An arena
201--- parent keeps the bytes it handed out, since it reclaims nothing until its
202--- backing ends.
203public release: (fixed capacity: usize, provider: type is mem.allocator,
204 inout state: space(capacity), parent: provider) -> none =
205 if capacity > 0 then
206 mut remaining: usize = state.book.used
207 while remaining > 0 do
208 dec remaining
209 record: allocation = state.records[remaining]
210 block: ptr mut u8 = ptr(record.address)
211 mem.release(parent, block, record.extent)
212 end while
213 else
214 -- Each block's records from the last, then the block itself, whose
215 -- first record gives the block before it; that block is full.
216 mut ledger: allocation =
217 (address: state.book.address,
218 extent: (state.book.room + 1) * record_size)
219 mut filled: usize = state.book.used + 1
220 while ledger.address <> 0 do
221 while filled > 1 do
222 dec filled
223 kept: ptr allocation =
224 ptr(ledger.address + filled * record_size)
225 payload: ptr mut u8 = ptr(kept.val.address)
226 mem.release(parent, payload, kept.val.extent)
227 end while
228 first: ptr allocation = ptr(ledger.address)
229 older: allocation = first.val
230 held: ptr mut u8 = ptr(ledger.address)
231 mem.release(parent, held, ledger.extent)
232 ledger = older
233 filled = ledger.extent / record_size
234 end while
235 end if
236 state.book = (used: 0, address: 0, room: 0)
237end release