1import core/mem
2
3list_value: type (item: type) = struct
4 values: mem.storage(item)
5end list_value
6
7--- Growable contiguous list with explicit allocator arguments; no
8--- allocator is stored in the list. Its storage is the module's own, so
9--- nothing outside it can put another block in a list's place, or take
10--- one out, behind its length and capacity.
11public list: type (item: type) = list_value(item)
12
13--- Create an empty list without allocating. Supply the allocator when growing
14--- or releasing it.
15public new: (item: type) -> (result: list(item)) =
16 store := mem.empty_storage(item: item)
17 result = (values: store)
18end new
19
20--- Return the number of initialized items.
21public length: (item: type, value: list(item)) -> (count: usize) =
22 count = mem.length(value.values)
23end length
24
25--- Return the number of items that fit without further growth.
26public capacity: (item: type, value: list(item)) -> (count: usize) =
27 count = mem.capacity(value.values)
28end capacity
29
30--- Ensure room for at least `want` items. May grow backing in place or copy
31--- to a replacement. Failure preserves the old length, capacity and values;
32--- successful relocation invalidates old views. Use the original provider for
33--- existing backing.
34public reserve: (item: type, provider: type is mem.allocator,
35 inout value: list(item), memory: provider, want: usize)
36 -> none ! mem.out_of_memory =
37 try mem.reserve_storage(value.values, memory, want)
38end reserve
39
40--- Append a value, growing capacity when needed. Reports `mem.out_of_memory`
41--- without appending on allocation failure. Successful growth may invalidate
42--- views into the previous allocation.
43public push: (item: type, provider: type is mem.allocator,
44 inout value: list(item), memory: provider,
45 escaping into value added: item) -> none ! mem.out_of_memory =
46 if length(value) == capacity(value) then
47 next: usize = grown(capacity(value)) else (problem)
48 _ = problem
49 fail mem.out_of_memory
50 end
51 try reserve(value, memory, next)
52 end if
53 admitted: usize = mem.admit(value.values, added) else (problem)
54 -- After successful growth or a spare-slot check, mem.out_of_memory
55 -- would indicate an internal invariant defect. Retain the established
56 -- public fallback without claiming the provider exhausted memory.
57 _ = problem
58 fail mem.out_of_memory
59 end
60 _ = admitted
61end push
62
63--- Copy the indexed item, preserving origins of references it contains.
64--- Reports `mem.out_of_bounds` outside the initialized prefix.
65public get: (item: type, value: list(item), index: usize)
66 -> (result: item from held value) ! mem.out_of_bounds =
67 result = try mem.get(value.values, index)
68end get
69
70--- Lend a writable pointer to an indexed item for in-place update. Reports
71--- `mem.out_of_bounds` for a missing slot. While the result lives the list
72--- binding may not be passed inout or sink [0800]: bind it, update through
73--- it, and let it end before the next push, reserve or release.
74public at: (item: type, inout value: list(item), index: usize)
75 -> (slot: ptr mut item from value) ! mem.out_of_bounds =
76 view: []mut item = mem.used(value.values)
77 fail mem.out_of_bounds when index >= lenof view
78 slot = addr view[index]
79end at
80
81--- Copy the last item without removing it. Reports `mem.empty` for an empty
82--- list.
83public last: (item: type, value: list(item))
84 -> (result: item from held value) ! mem.empty =
85 count: usize = length(value)
86 fail mem.empty when count == 0
87 result = mem.get(value.values, count - 1) else (problem)
88 _ = problem
89 fail mem.empty
90 end
91end last
92
93--- Remove and return the last item. Reports `mem.empty` without mutation when
94--- empty; retains capacity.
95public pop: (item: type, inout value: list(item))
96 -> (result: item from held value) ! mem.empty =
97 result = try mem.withdraw(value.values)
98end pop
99
100--- Discard all items and retain capacity. Does not release resources
101--- referenced by discarded values.
102public clear: (item: type, inout value: list(item)) -> none =
103 mem.clear(value.values)
104end clear
105
106--- Discard items, free backing through the original provider and reset the
107--- list. End all views first; nested resources remain the caller's
108--- responsibility.
109public release: (item: type, provider: type is mem.allocator,
110 inout value: list(item), memory: provider) -> none =
111 mem.release_storage(value.values, memory)
112end release
113
114-- A list of bytes is the library's growable byte container (D425): the
115-- operations below append a slice and let a read fill the spare capacity,
116-- and the list grows as `push` grows it, its bytes copied a word at a time
117-- where `mem.copy` moves words.
118
119-- The capacity that holds `extra` more bytes after the initialized ones:
120-- the one the list has when that is enough, and otherwise double it, as
121-- `push` grows, or exactly what is wanted when doubling is not enough or
122-- cannot be represented.
123roomy: (value: list(u8), extra: usize)
124 -> (slots: usize) ! mem.out_of_memory =
125 count: usize = length(value)
126 slots = capacity(value)
127 return when extra <= slots - count
128 maximum: usize = 0 -% 1
129 fail mem.out_of_memory when extra > maximum - count
130 wanted: usize = count + extra
131 next: usize = grown(slots) else (problem)
132 _ = problem
133 wanted
134 end
135 slots = if next > wanted then next else wanted end if
136end roomy
137
138-- Make room for `extra` more bytes after the initialized ones.
139make_room: (provider: type is mem.allocator, inout value: list(u8),
140 memory: provider, extra: usize) -> none ! mem.out_of_memory =
141 slots: usize = try roomy(value, extra)
142 try reserve(value, memory, slots)
143end make_room
144
145--- Append every byte of `bytes`, growing capacity as `push` does, and
146--- copying them a word at a time where `mem.copy` moves words. Reports
147--- `mem.out_of_memory` without appending anything when growth fails.
148--- `bytes` may be a view of the list's own bytes, `vec.used(value)` or a
149--- part of it: the bytes appended are the ones it held when the call
150--- began, read before a growth frees the block they were in (D1122).
151--- Successful growth invalidates views into the previous allocation once
152--- the call has returned, that one among them.
153public append_bytes: (provider: type is mem.allocator,
154 inout value: list(u8), memory: provider, bytes: []u8)
155 -> none ! mem.out_of_memory =
156 slots: usize = try roomy(value, lenof bytes)
157 try mem.reserve_admit_copy(value.values, memory, slots, bytes)
158end append_bytes
159
160--- Lend the spare capacity past the list's bytes, at least `extra` bytes
161--- of it, growing as `push` does when there are fewer: the buffer a read
162--- fills, `io.read(host, file, room)`, before `extend_bytes` counts what it
163--- wrote. Its bytes are whatever the storage held. End uses of the view
164--- before the next append, reserve or release.
165public spare_bytes: (provider: type is mem.allocator, inout value: list(u8),
166 memory: provider, extra: usize)
167 -> (room: []mut u8 from value) ! mem.out_of_memory =
168 try make_room(value, memory, extra)
169 room = mem.spare_bytes(value.values)
170end spare_bytes
171
172--- Count the first `count` bytes of the spare capacity, which the caller
173--- has written through `spare_bytes`, as appended. Reports
174--- `mem.out_of_bounds`, changing nothing, when fewer than `count` are spare.
175public extend_bytes: (inout value: list(u8), count: usize)
176 -> none ! mem.out_of_bounds =
177 mem.admit_bytes(value.values, count) else (problem)
178 _ = problem
179 fail mem.out_of_bounds
180 end
181end extend_bytes