aboutsummaryrefslogtreecommitdiff
path: root/tests/programs/allocfast.zc
blob: d7b1d93c170de2d1a89e8b157065165f68a5c976 (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
36
37
38
39
40
41
42
43
44
45
46
47
48
namespace;
include rt;

// repeated allocations of a size known at compile time: new(T) of a struct of
// numbers, and new(n) with a constant n. each pass of the loop makes an object
// and frees it at the end of the block, so the allocator should hand back the
// block it just took. the sums prove the objects were zeroed every time.
struct point { int x, y; };
struct line { point* a; int len; };

fn loop_leaf(int n) {
	let total = 0;
	for (int i = 0; i < n; i++) {
		point* p = rt.new(point);
		total += p.x + p.y;		// zero from the allocator, every pass
		p.x = i; p.y = 2;
	}
	return total;
}
fn loop_words(int n) {
	let total = 0, i = 0;
	while (i < n) {
		let w = rt.new(6);
		total += w[0] + w[5];
		w[0] = 7; w[5] = 9;
		i++;
	}
	return total;
}
fn loop_owner(int n) {
	let i = 0, total = 0;
	while (i < n) {
		line* l = rt.new(line);
		l.a = rt.new(point);
		l.a.x = i;
		total += l.a.x + l.len;
		i++;
	}
	return total;
}
fn main() {
	let before = rt.live_objects();
	rt.put_n(loop_leaf(50000)); rt.put_c(' ');
	rt.put_n(loop_words(50000)); rt.put_c(' ');
	rt.put_n(loop_owner(1000)); rt.put_c(' ');
	rt.put_n(rt.live_objects() - before); rt.put_c(10);
	return 0;
}