|
| 1 | + |
| 2 | +type OwnedData<t> = { |
| 3 | + reference-count: USize, |
| 4 | + occupied: USize, |
| 5 | + capacity: USize, |
| 6 | + data: t[]+FlexibleArrayMember |
| 7 | +}; |
| 8 | + |
| 9 | +let mk-owned-data(tt: Type<t>, capacity: USize, occupied: USize): CompatOwnedData<t>[] = ( |
| 10 | + let od = safe-alloc( |
| 11 | + # TODO: make sizeof return USize |
| 12 | + (sizeof(CompatOwnedData<t>) as USize) + (sizeof(t) as USize) * capacity, |
| 13 | + type(CompatOwnedData<t>) |
| 14 | + ); |
| 15 | + od.reference-count = 1; |
| 16 | + od.occupied = occupied; |
| 17 | + od.capacity = capacity; |
| 18 | + od |
| 19 | +); |
| 20 | + |
| 21 | +let mk-owned-data(tt: Type<t>, capacity: USize): CompatOwnedData<t>[] = ( |
| 22 | + mk-owned-data(tt, capacity, 0) |
| 23 | +); |
| 24 | + |
| 25 | +let $"[]"(od: CompatOwnedData<t>[], idx: USize): t = ( |
| 26 | + if (od as USize)==0 |
| 27 | + then fail(c"OwnedData [] Access Null Pointer"); |
| 28 | + if idx >= od.occupied then fail("OwnedData [\{idx}] Index Access Out of Bounds"); |
| 29 | + od.data[idx] |
| 30 | +); |
| 31 | + |
| 32 | +let $"set[]"(od: CompatOwnedData<t>[], idx: USize, val: t): Nil = ( |
| 33 | + if (od as USize)==0 |
| 34 | + then fail(c"OwnedData set[] Access Null Pointer"); |
| 35 | + if idx >= od.occupied then fail("OwnedData set[\{idx}] Index Access Out of Bounds"); |
| 36 | + if type(t) <: type(MustRelease) then od.data[idx].release; |
| 37 | + od.data[idx] = val; |
| 38 | +); |
| 39 | + |
| 40 | +let .push(od: CompatOwnedData<t>[], d: t): Nil = ( |
| 41 | + if (od as USize)==0 |
| 42 | + then fail(c"OwnedData .push Into Null Pointer"); |
| 43 | + if od.occupied >= od.capacity |
| 44 | + then fail(c"OwnedData .push Exceeds Maximum Length"); |
| 45 | + od.data[od.occupied] = d; |
| 46 | + od.occupied = od.occupied + 1; |
| 47 | +); |
| 48 | + |
| 49 | +let .pop(od: CompatOwnedData<t>[]): t = ( |
| 50 | + if (od as USize)==0 |
| 51 | + then fail(c"OwnedData .pop From Null Pointer"); |
| 52 | + if od.occupied == 0 |
| 53 | + then fail(c"OwnedData .pop From Empty Data"); |
| 54 | + od.occupied = od.occupied - 1; |
| 55 | + od.data[od.occupied] |
| 56 | +); |
| 57 | + |
| 58 | +let open(od: CompatOwnedData<t>[]): t = ( |
| 59 | + od[0]; |
| 60 | +); |
| 61 | + |
| 62 | +let close-owned(d: t): CompatOwnedData<t>[] = ( |
| 63 | + let od = mk-owned-data(type(t), 1); |
| 64 | + od.push(d); |
| 65 | + od |
| 66 | +); |
| 67 | + |
| 68 | +let .release(od: CompatOwnedData<t>[]): Nil = ( |
| 69 | + if (od as USize)!=0 { |
| 70 | + if od.reference-count==0 then fail(c"OwnedData.release called when reference count is already zero. This object has already been freed!"); |
| 71 | + od.reference-count = od.reference-count - 1; |
| 72 | + if od.reference-count == 0 { |
| 73 | + if type(t) <: type(MustRelease) { |
| 74 | + let dlo = 0_sz; |
| 75 | + let dhi = od.occupied; |
| 76 | + while dlo < dhi { |
| 77 | + od.data[dlo].release; |
| 78 | + dlo = dlo + 1; |
| 79 | + }; |
| 80 | + }; |
| 81 | + safe-free(od); |
| 82 | + }; |
| 83 | + }; |
| 84 | +); |
| 85 | + |
| 86 | +let .retain(od: CompatOwnedData<t>[]): Nil = ( |
| 87 | + if (od as USize)!=0 { |
| 88 | + if od.reference-count==0 then fail(c"OwnedData.retain called when reference count is zero. This object has already been freed!"); |
| 89 | + od.reference-count = od.reference-count + 1; |
| 90 | + } |
| 91 | +); |
0 commit comments