aboutsummaryrefslogtreecommitdiff
path: root/tests/programs/replace_owner.zc
blob: 83186161097b1b76e245f8988d9840c493d1c601 (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
namespace;
include rt;

// a released owner can receive a same-scope replacement, without copying owners.
int*.u growing(int n) {
	int* buffer = rt.new(int), next; int i = 0;
	while (i < n) {
		if (!(i & 1)) {
			next = rt.new(int);
			*next = i;
			rt.free(buffer);
			buffer = next;
		}
		i++;
	}
	return buffer;
}
fn main() {
	let base = rt.live_objects();
	{
		let result = growing(100); rt.put_n(*result); rt.put_c(32);
		int* slot = rt.new(int), replacement = rt.new(int); *replacement = 7;
		rt.free(slot); slot = replacement; rt.put_n(*slot); rt.put_c(32);
		rt.free(slot); slot = rt.new(int); *slot = 9; rt.put_n(*slot); rt.put_c(32);
	}
	rt.gc(); rt.put_n(rt.live_objects() - base); rt.put_c(10);
}