shithub: mc

Download patch

ref: f09039219801304f624d87309a320b23b3cedb92
parent: 3a49659e6d6d1d0289b7b0501eb03f23d48e828f
author: Ori Bernstein <[email protected]>
date: Fri Aug 15 11:29:37 EDT 2014

Add shuffled alloc/free to the alloc test.

--- a/bench/copious-allocs.myr
+++ b/bench/copious-allocs.myr
@@ -24,5 +24,29 @@
 		for i = a.len; i > 0; i--
 			std.free(a[i - 1])
 		;;
+
+		/* alloc forwards, dealloc randomly */
+		for i = 0; i < a.len; i++
+			a[i] = std.alloc()
+		;;
+		shuffle(a[:])
+		for i = a.len; i > 0; i--
+			std.free(a[i - 1])
+		;;
 	;;
 }
+
+const shuffle = {a
+	var t
+	var rng
+	var i, j
+
+	rng = std.mksrng(123)
+	for i = 0; i < a.len - 1; i++
+		j = std.rand(rng, i, a.len)
+		t = a[j]
+		a[j] = a[i]
+		a[i] = t
+	;;
+}
+
--- a/libstd/rand.myr
+++ b/libstd/rand.myr
@@ -49,6 +49,7 @@
 	type rng
 
 	const mksrng	: (seed : uint32 -> rng#)
+	const delrng	: (rng : rng# -> void)
 	generic rand	: (rng : rng#, lo : @a::(numeric,integral), hi : @a::(numeric,integral) -> @a::(numeric,integral))
 	generic randN	: (rng : rng# -> @a::(numeric,integral))
 	const rand32	: (rng : rng# -> uint32)
@@ -66,6 +67,10 @@
 	rng = alloc()
 	init(rng, seed)
 	-> rng
+}
+
+const delrng = {rng
+	free(rng)
 }
 
 /* initializes a random number generator from the seed `seed`. */