aboutsummaryrefslogtreecommitdiff
path: root/tests/programs/buddy.zc
blob: c92a7907ef64c808104821f4d7ca7860b1365e9d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
namespace;
include rt;

// the buddy allocator: 20000 small blocks, freed, merge back into one big one
let keep[20000];
fn main() {
	let i = 0, p;
	while (i < 20000) { keep[i] = rt.new(2); i++; }   // 20000 8-word blocks
	rt.put_n(rt.largest_free()); rt.put_c(' ');
	i = 0; while (i < 20000) { rt.free(keep[i]); keep[i] = 0; i++; }
	rt.scrub();	// stale stack words would keep the quarantined blocks
	rt.gc();
	rt.put_n(rt.largest_free()); rt.put_c(' ');
	p = rt.new(200000);	// only possible if the small blocks merged back
	rt.put_n(rt.osize(p) >= 200000); rt.put_c(' ');
	rt.free(d*p);
	rt.put_n(rt.live_objects()); rt.put_c(10);
	return 0;
}