|
13 | 13 | * limitations under the License. |
14 | 14 | */ |
15 | 15 |
|
16 | | -module ir; |
| 16 | +module ref_list; |
| 17 | + |
| 18 | +import ir; |
17 | 19 |
|
18 | 20 | import string local; |
19 | 21 | import stdlib; |
20 | 22 |
|
21 | 23 | public type RefList struct { |
22 | 24 | u32 count; |
23 | 25 | u32 capacity; |
24 | | - Ref* refs; |
| 26 | + ir.Ref* refs; |
| 27 | + ir.Ref[4] stack; |
25 | 28 | } |
26 | 29 |
|
27 | | -public fn void RefList.init(RefList* l, u32 initial_size) { |
| 30 | +public fn void RefList.init(RefList* l) { |
28 | 31 | l.count = 0; |
29 | | - l.refs = nil; |
30 | | - if (initial_size) l.resize(initial_size); |
| 32 | + l.capacity = elemsof(l.stack); |
| 33 | + l.refs = l.stack; |
31 | 34 | } |
32 | 35 |
|
33 | 36 | public fn void RefList.free(RefList* l) { |
34 | | - if (l.refs) stdlib.free(l.refs); |
| 37 | + if (l.count > elemsof(l.stack)) stdlib.free(l.refs); |
35 | 38 | } |
36 | 39 |
|
37 | 40 | fn void RefList.resize(RefList* l, u32 cap) { |
| 41 | + ir.Ref* refs2 = stdlib.malloc(cap * sizeof(ir.Ref)); |
| 42 | + if (l.count != 0) memcpy(refs2, l.refs, l.count * sizeof(ir.Ref)); |
| 43 | + if (l.capacity > elemsof(l.stack)) stdlib.free(l.refs); |
38 | 44 | l.capacity = cap; |
39 | | - // TODO only memset new part? |
40 | | - Ref* refs2 = stdlib.calloc(l.capacity, sizeof(Ref)); |
41 | | - if (l.count != 0) memcpy(refs2, l.refs, l.count * sizeof(Ref)); |
42 | | - if (l.refs) stdlib.free(l.refs); |
43 | 45 | l.refs = refs2; |
44 | 46 | } |
45 | 47 |
|
46 | | -public fn void RefList.add(RefList* l, Ref ref) { |
47 | | - if (l.count == l.capacity) l.resize(l.capacity ? l.capacity * 2 : 4); |
| 48 | +public fn void RefList.add(RefList* l, ir.Ref ref) { |
| 49 | + if (l.count == l.capacity) l.resize(l.capacity * 2); |
48 | 50 |
|
49 | 51 | l.refs[l.count] = ref; |
50 | 52 | l.count++; |
51 | 53 | } |
52 | 54 |
|
53 | | -public fn u32 RefList.addList(RefList* l, const RefList* l2) { |
54 | | - u32 start = l.count; |
55 | | - if (l.capacity < l.count + l2.count) l.resize(l.count + l2.count); |
56 | | - memcpy(&l.refs[l.count], l2.refs, l2.count * sizeof(Ref)); |
57 | | - l.count += l2.count; |
58 | | - return start; |
59 | | -} |
60 | | - |
61 | | -public fn void RefList.clear(RefList* l) { |
62 | | - l.count = 0; |
63 | | -} |
64 | | - |
65 | 55 | public fn u32 RefList.getCount(const RefList* l) { |
66 | 56 | return l.count; |
67 | 57 | } |
68 | 58 |
|
69 | | -public fn Ref* RefList.get(const RefList* l, u32 idx) { |
70 | | - return &l.refs[idx]; |
| 59 | +public fn const ir.Ref* RefList.get(const RefList* l) { |
| 60 | + return l.refs; |
71 | 61 | } |
72 | 62 |
|
0 commit comments