summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2014-08-15 15:29:37 -0400
committerOri Bernstein <ori@eigenstate.org>2014-08-15 15:29:37 -0400
commitb7d4386bf0a35245a2d8705d379aa68d56ea0dbe (patch)
tree2b7ecf9e694504efa8776cd2b245b220daf363e3
parent52af0fabaa1b7b36a1848f53b1abb4538dcebabc (diff)
downloadmc-b7d4386bf0a35245a2d8705d379aa68d56ea0dbe.tar.gz
Add shuffled alloc/free to the alloc test.
-rw-r--r--bench/copious-allocs.myr24
-rw-r--r--libstd/rand.myr5
2 files changed, 29 insertions, 0 deletions
diff --git a/bench/copious-allocs.myr b/bench/copious-allocs.myr
index 091b83b..886adde 100644
--- a/bench/copious-allocs.myr
+++ b/bench/copious-allocs.myr
@@ -24,5 +24,29 @@ const main = {
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
+ ;;
+}
+
diff --git a/libstd/rand.myr b/libstd/rand.myr
index eede48f..e6c3dbf 100644
--- a/libstd/rand.myr
+++ b/libstd/rand.myr
@@ -49,6 +49,7 @@ pkg std =
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)
@@ -68,6 +69,10 @@ const mksrng = {seed
-> rng
}
+const delrng = {rng
+ free(rng)
+}
+
/* initializes a random number generator from the seed `seed`. */
const init = {rng, seed
var i