test/loader/grown.rad 1.2 KiB raw
1
//! A package that grows slices of pointer-holding elements in a static heap, executed by hosted
2
//! compilation and the loader. The loader reads a package from one page, so the program stays small.
3
4
/// The allocation error, which the compiler knows.
5
@language union AllocError: Copy { OutOfMemory }
6
7
/// A heap, which holds elements of any type.
8
@language record Heap {}
9
10
/// Allocate the backing storage of a static heap.
11
@intrinsic constant fn new(size: u32) -> Heap;
12
13
14
/// The allocator view of a heap.
15
@language record Allocator {}
16
17
/// Memory for the slices of this package.
18
static POOL: Heap = new(256);
19
20
/// Numbers that holders point to.
21
static NUMBERS: [u32; 2] = [20, 22];
22
23
/// A record that holds a pointer.
24
record Holder {
25
    /// Owned pointer to a number.
26
    p: *u32,
27
}
28
29
/// Grow slices, and return the checked result.
30
@default fn main() -> i32 {
31
    let mut hs: *mut [Holder] = &mut [];
32
    for i in 0 .. 2 {
33
        try! hs.append(Holder { p: &NUMBERS[i] }, &POOL);
34
    }
35
    let mut big: *mut [Holder] = &mut [];
36
    try big.reserve(1000, &POOL) catch e {
37
        assert e == AllocError::OutOfMemory;
38
        return (*hs[0].p + *hs[1].p) as i32;
39
    };
40
    return 1;
41
}