aboutsummaryrefslogtreecommitdiff
path: root/tests/programs/growheap.zc
blob: ebb3f8bb0fba2d2fed83978c43c4c313fe88ef85 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
namespace;
include rt;

// the heap grows past its initial size: eight live blocks of 300000 words
// don't fit in the 393216-word start, so the allocator takes more memory from
// the host, and a block bigger than the start fits once they are freed
let keep[8];
unchecked fn fill(n) {
	let i = 0, a;
	while (i < n) { a = rt.new(300000); a[0] = i + 1; a[299999] = (i + 1) * 3; keep[i] = a; i++; }
	return 0;
}
unchecked fn total(n) {
	let i = 0, a, sum = 0;
	while (i < n) { a = keep[i]; sum = sum + a[0] + a[299999]; i++; }
	return sum;
}
unchecked fn drop(n) {
	let i = 0;
	while (i < n) { rt.free(keep[i]); keep[i] = 0; i++; }
	return 0;
}
fn main() {
	let a, start = rt.heap_size();
	fill(8);
	rt.put_n(total(8)); rt.put_c(' ');
	rt.put_n(rt.heap_size() > start); rt.put_c(' ');
	drop(8);
	rt.scrub(); rt.gc();
	a = rt.new(3000000);
	a[2999999] = 7;
	rt.put_n(a[2999999]); rt.put_c(' ');
	rt.put_n(rt.heap_size() >= 3000000); rt.put_c(10);
	return 0;
}