about summary refs log tree commit diff
path: root/src/rt/rust_builtin.cpp
diff options
context:
space:
mode:
authorGareth Daniel Smith <garethdanielsmith@gmail.com>2012-05-20 14:06:54 +0100
committerBrian Anderson <banderson@mozilla.com>2012-05-21 17:38:05 -0700
commitc9f8ae02bcf078aee6ebcdc55f96ac18a9753c26 (patch)
tree7e91e2eafd38d3893c583251b108d79240bea888 /src/rt/rust_builtin.cpp
parent64130f158950d5b4746b7dce47cf4ce20dd934dd (diff)
downloadrust-c9f8ae02bcf078aee6ebcdc55f96ac18a9753c26.tar.gz
rust-c9f8ae02bcf078aee6ebcdc55f96ac18a9753c26.zip
add a seeded random number generator so that sequences of random numbers can be easily reproduced (for https://github.com/mozilla/rust/issues/2379)
Diffstat (limited to 'src/rt/rust_builtin.cpp')
-rw-r--r--src/rt/rust_builtin.cpp29
1 files changed, 27 insertions, 2 deletions
diff --git a/src/rt/rust_builtin.cpp b/src/rt/rust_builtin.cpp
index aa7597f6a26..b8b8bc30163 100644
--- a/src/rt/rust_builtin.cpp
+++ b/src/rt/rust_builtin.cpp
@@ -178,16 +178,41 @@ rust_str_push(rust_vec** sp, uint8_t byte) {
     (*sp)->fill = fill + 1;
 }
 
+extern "C" CDECL rust_vec*
+rand_seed() {
+    size_t size = sizeof(ub4) * RANDSIZ;
+    rust_task *task = rust_get_current_task();
+    rust_vec *v = (rust_vec *) task->kernel->malloc(vec_size<uint8_t>(size),
+                                            "rand_seed");
+    v->fill = v->alloc = size;
+    isaac_seed((uint8_t*) &v->data);
+    return v;
+}
+
 extern "C" CDECL void *
 rand_new() {
     rust_task *task = rust_get_current_task();
     rust_sched_loop *thread = task->sched_loop;
-    randctx *rctx = (randctx *) task->malloc(sizeof(randctx), "randctx");
+    randctx *rctx = (randctx *) task->malloc(sizeof(randctx), "rand_new");
+    if (!rctx) {
+        task->fail();
+        return NULL;
+    }
+    isaac_init(thread->kernel, rctx, NULL);
+    return rctx;
+}
+
+extern "C" CDECL void *
+rand_new_seeded(rust_vec* seed) {
+    rust_task *task = rust_get_current_task();
+    rust_sched_loop *thread = task->sched_loop;
+    randctx *rctx = (randctx *) task->malloc(sizeof(randctx),
+                                             "rand_new_seeded");
     if (!rctx) {
         task->fail();
         return NULL;
     }
-    isaac_init(thread->kernel, rctx);
+    isaac_init(thread->kernel, rctx, seed);
     return rctx;
 }