Landin library reference discuss source

core/zone/zone.ldn

1--  A growing bump allocator: it takes blocks from a parent, hands out the
2--  bytes of the newest one in order and takes another, twice as large, when
3--  that one is full.  Individual frees do nothing.  `reset` gives every
4--  allocation back at once and keeps the newest block for the next round;
5--  `release` gives every block back to the parent.  The caller holds the
6--  state; the handle holds a copy of the parent's handle and a pointer to
7--  the state.
8
9import core/mem
10
11--  Where a block starts: the block before it, zero for the first, and that
12--  block's extent.  The region's own bytes follow.
13link: type = struct
14    address: usize
15    extent: usize
16end link
17
18--  The newest block, zero when there is none, its extent and the bytes of
19--  it in use, its link included.
20zone_state: type = struct
21    block: usize
22    extent: usize
23    used: usize
24end zone_state
25
26--- The caller's state of a zone: where its newest block is and how much
27--- of that block is in use. Declare it `zeroed` and give it to `over`,
28--- which fills it. It must outlive the zone's handle. `release` empties it.
29public space: type = zone_state
30
31zone_value: type (provider: type) = struct
32    parent: provider
33    state: ptr mut zone_state
34end zone_value
35
36--- A bump allocator that grows by taking blocks from a parent allocator.
37--- The value is a handle, so copies share one zone. Freeing a single
38--- allocation reclaims nothing. `reset` gives every allocation back at
39--- once, and `release` returns the blocks to the parent. Only the most
40--- recent allocation grows in place or gives back its tail. Growing any
41--- other moves it, and its old bytes are not reclaimed.
42public zone: type (provider: type) = zone_value(provider)
43
44--- Fill `state` for a zone over `parent` and return the zone's handle.
45--- Nothing is allocated. The handle holds a copy of the parent's handle,
46--- and the parent must outlive the zone.
47public over: (provider: type is mem.allocator, inout state: space,
48              parent: provider)
49             -> (handle: zone(provider) from state, parent) =
50    state = (block: 0, extent: 0, used: 0)
51    handle = (parent: parent, state: addr state)
52end over
53
54link_size: usize = sizeof link
55
56--  A parent's own header beside each block, two words in hosted/heap: a
57--  block of a power of two less this fills a size class or whole pages.
58slack: usize = 2 * sizeof usize
59
60--  The first block, and the one after a block of `extent`: twice as large.
61first_extent: usize = 1024 - slack
62
63next_extent: (extent: usize) -> (next: usize) =
64    maximum: usize = 0 -% 1
65    next = extent
66    if extent < (maximum - slack) / 2 - slack then
67        next = 2 * (extent + slack) - slack
68    end if
69end next_extent
70
71--  The address of a fresh block of `extent` bytes from the parent, or zero
72--  when the parent refuses it.
73take_block: (provider: type is mem.allocator, self: zone(provider),
74             extent: usize) -> (address: usize) =
75    address = 0
76    fresh: ptr mut u8 = mem.allocate(self.parent, extent, alignof link)
77        else (problem)
78        _ = problem
79        return
80    end
81    address = usize(fresh)
82end take_block
83
84--  Put a block that holds `size` bytes at `alignment` in front of the
85--  chain: the next block's extent, or just enough when the parent refuses
86--  that.  A failure leaves the zone as it was.
87add_block: (provider: type is mem.allocator, self: zone(provider),
88            size: usize, alignment: usize) -> none ! mem.out_of_memory =
89    state: ptr mut zone_state = self.state
90    maximum: usize = 0 -% 1
91    mut padding: usize = 0
92    if alignment > 1 then
93        padding = alignment - 1
94    end if
95    fail mem.out_of_memory when padding > maximum - link_size
96    fail mem.out_of_memory when size > maximum - link_size - padding
97    needed: usize = link_size + padding + size
98    mut extent: usize = first_extent
99    if state.val.block <> 0 then
100        extent = next_extent(state.val.extent)
101    end if
102    if extent < needed then
103        extent = needed
104    end if
105    mut address: usize = take_block(self, extent)
106    if address == 0 and extent > needed then
107        extent = needed
108        address = take_block(self, extent)
109    end if
110    fail mem.out_of_memory when address == 0
111    head: ptr mut link = ptr(address)
112    head.val = (address: state.val.block, extent: state.val.extent)
113    state.val.block = address
114    state.val.extent = extent
115    state.val.used = link_size
116end add_block
117
118--  The offset of `size` bytes at `alignment` in the newest block, or the
119--  largest `usize` when it has no room for them.
120room: (state: ptr mut zone_state, size: usize, alignment: usize)
121      -> (offset: usize) =
122    offset = 0 -% 1
123    return when state.val.block == 0
124    base: ptr mut u8 = ptr(state.val.block)
125    offset = mem.aligned_offset(base, state.val.used, state.val.extent,
126                                size, alignment) else 0 -% 1
127end room
128
129zone_alloc: (provider: type is mem.allocator, self: zone(provider),
130             size: usize, alignment: usize)
131            -> (block: ptr mut u8) ! mem.out_of_memory =
132    state: ptr mut zone_state = self.state
133    mut offset: usize = room(state, size, alignment)
134    if offset == 0 -% 1 then
135        try add_block(self, size, alignment)
136        offset = room(state, size, alignment)
137    end if
138    --  The block a fresh link starts always holds the request.
139    state.val.used = offset + size
140    --  [0810]: the block borrows nothing, as an arena's does; its lifetime
141    --  is the zone's blocks', which `reset` and `release` end.
142    block = ptr(state.val.block + offset)
143end zone_alloc
144
145zone_free: (provider: type, self: zone(provider), block: ptr mut u8,
146            size: usize) -> none =
147    _ = self.state
148    _ = block
149    _ = size
150end zone_free
151
152--  Only the newest allocation of the newest block grows, where it is.
153zone_grow: (provider: type, self: zone(provider), block: ptr mut u8,
154            old_size: usize, new_size: usize, alignment: usize)
155           -> (grown: bool) =
156    grown = false
157    state: ptr mut zone_state = self.state
158    return when state.val.block == 0 or new_size <= old_size
159    address: usize = usize(block)
160    return when address < state.val.block
161    offset: usize = address - state.val.block
162    return when offset > state.val.used or old_size <> state.val.used - offset
163    return when alignment > 1 and address % alignment <> 0
164    return when new_size > state.val.extent - offset
165    state.val.used = offset + new_size
166    grown = true
167end zone_grow
168
169--  A smaller extent keeps the block where it is, and the newest allocation
170--  gives its tail back; a larger one grows in place when `zone_grow` can,
171--  and otherwise moves the block to a fresh allocation.
172zone_resize: (provider: type is mem.allocator, self: zone(provider),
173              block: ptr mut u8, old_size: usize, new_size: usize,
174              alignment: usize)
175             -> (moved: ptr mut u8) ! mem.out_of_memory =
176    --  [0810]: the block borrows nothing, as an allocation does.
177    moved = ptr(usize(block))
178    if new_size <= old_size then
179        state: ptr mut zone_state = self.state
180        address: usize = usize(block)
181        return when state.val.block == 0 or address < state.val.block
182        offset: usize = address - state.val.block
183        if offset <= state.val.used and old_size == state.val.used - offset
184        then
185            state.val.used = offset + new_size
186        end if
187        return
188    end if
189    return when zone_grow(self, block, old_size, new_size, alignment)
190    moved = try mem.relocate(self, block, old_size, new_size, alignment)
191end zone_resize
192
193(provider: type is mem.allocator) zone_value(provider) is mem.allocator
194    (alloc: zone_alloc, grow: zone_grow, resize: zone_resize,
195     free: zone_free)
196
197--- Give every allocation of the zone back at once. The newest block,
198--- which is the largest, stays for the allocations that follow. Every
199--- older block goes back to the parent. Every allocation the zone made is
200--- then free, and a program must not use one, just as after `free`.
201public reset: (provider: type is mem.allocator, memory: zone(provider))
202              -> none =
203    state: ptr mut zone_state = memory.state
204    return when state.val.block == 0
205    newest: ptr mut link = ptr(state.val.block)
206    mut older: link = newest.val
207    while older.address <> 0 do
208        held: ptr link = ptr(older.address)
209        before: link = held.val
210        bytes: ptr mut u8 = ptr(older.address)
211        mem.release(memory.parent, bytes, older.extent)
212        older = before
213    end while
214    newest.val = (address: 0, extent: 0)
215    state.val.used = link_size
216end reset
217
218--- Return every block to `parent`, which must be the parent the zone was
219--- made over. `state` is then empty and may be given to `over` again. End
220--- every use of the zone's allocations first. The checker refuses the call
221--- while the zone's handle is still used.
222public release: (provider: type is mem.allocator, inout state: space,
223                 parent: provider) -> none =
224    mut current: link = (address: state.block, extent: state.extent)
225    while current.address <> 0 do
226        held: ptr link = ptr(current.address)
227        before: link = held.val
228        bytes: ptr mut u8 = ptr(current.address)
229        mem.release(parent, bytes, current.extent)
230        current = before
231    end while
232    state = (block: 0, extent: 0, used: 0)
233end release