Skip to content

Commit 665e900

Browse files
committed
encapsulate isaac RNG in rust_rng struct
1 parent f4320b6 commit 665e900

File tree

6 files changed

+47
-33
lines changed

6 files changed

+47
-33
lines changed

src/libcore/rand.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,15 @@ impl<T: Rand> Rand for Option<T> {
116116
}
117117

118118
#[allow(non_camel_case_types)] // runtime type
119-
enum rctx {}
119+
enum rust_rng {}
120120

121121
#[abi = "cdecl"]
122122
extern mod rustrt {
123123
unsafe fn rand_seed() -> ~[u8];
124-
unsafe fn rand_new() -> *rctx;
125-
unsafe fn rand_new_seeded2(&&seed: ~[u8]) -> *rctx;
126-
unsafe fn rand_next(c: *rctx) -> u32;
127-
unsafe fn rand_free(c: *rctx);
124+
unsafe fn rand_new() -> *rust_rng;
125+
unsafe fn rand_new_seeded2(&&seed: ~[u8]) -> *rust_rng;
126+
unsafe fn rand_next(rng: *rust_rng) -> u32;
127+
unsafe fn rand_free(rng: *rust_rng);
128128
}
129129

130130
/// A random number generator
@@ -363,24 +363,24 @@ impl Rng {
363363
}
364364

365365
struct RandRes {
366-
c: *rctx,
366+
rng: *rust_rng,
367367
drop {
368368
unsafe {
369-
rustrt::rand_free(self.c);
369+
rustrt::rand_free(self.rng);
370370
}
371371
}
372372
}
373373

374-
fn RandRes(c: *rctx) -> RandRes {
374+
fn RandRes(rng: *rust_rng) -> RandRes {
375375
RandRes {
376-
c: c
376+
rng: rng
377377
}
378378
}
379379

380380
impl Rng for @RandRes {
381381
fn next() -> u32 {
382382
unsafe {
383-
return rustrt::rand_next((*self).c);
383+
return rustrt::rand_next((*self).rng);
384384
}
385385
}
386386
}

src/rt/rust_builtin.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -135,35 +135,35 @@ rand_seed() {
135135
rust_vec *v = (rust_vec *) task->kernel->malloc(vec_size<uint8_t>(size),
136136
"rand_seed");
137137
v->fill = v->alloc = size;
138-
isaac_seed(task->kernel, (uint8_t*) &v->data, size);
138+
rng_gen_seed(task->kernel, (uint8_t*) &v->data, size);
139139
return v;
140140
}
141141

142142
extern "C" CDECL void *
143143
rand_new() {
144144
rust_task *task = rust_get_current_task();
145145
rust_sched_loop *thread = task->sched_loop;
146-
randctx *rctx = (randctx *) task->malloc(sizeof(randctx), "rand_new");
147-
if (!rctx) {
146+
rust_rng *rng = (rust_rng *) task->malloc(sizeof(rust_rng), "rand_new");
147+
if (!rng) {
148148
task->fail();
149149
return NULL;
150150
}
151-
isaac_init(thread->kernel, rctx, NULL);
152-
return rctx;
151+
rng_init(thread->kernel, rng, NULL);
152+
return rng;
153153
}
154154

155155
extern "C" CDECL void *
156156
rand_new_seeded(rust_vec_box* seed) {
157157
rust_task *task = rust_get_current_task();
158158
rust_sched_loop *thread = task->sched_loop;
159-
randctx *rctx = (randctx *) task->malloc(sizeof(randctx),
160-
"rand_new_seeded");
161-
if (!rctx) {
159+
rust_rng *rng = (rust_rng *) task->malloc(sizeof(rust_rng),
160+
"rand_new_seeded");
161+
if (!rng) {
162162
task->fail();
163163
return NULL;
164164
}
165-
isaac_init(thread->kernel, rctx, seed);
166-
return rctx;
165+
rng_init(thread->kernel, rng, seed);
166+
return rng;
167167
}
168168

169169
extern "C" CDECL void *
@@ -172,14 +172,14 @@ rand_new_seeded2(rust_vec_box** seed) {
172172
}
173173

174174
extern "C" CDECL uint32_t
175-
rand_next(randctx *rctx) {
176-
return isaac_rand(rctx);
175+
rand_next(rust_rng *rng) {
176+
return rng_gen_u32(rng);
177177
}
178178

179179
extern "C" CDECL void
180-
rand_free(randctx *rctx) {
180+
rand_free(rust_rng *rng) {
181181
rust_task *task = rust_get_current_task();
182-
task->free(rctx);
182+
task->free(rng);
183183
}
184184

185185

src/rt/rust_rng.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// Initialization helpers for ISAAC RNG
1616

1717
void
18-
isaac_seed(rust_kernel* kernel, uint8_t* dest, size_t size) {
18+
rng_gen_seed(rust_kernel* kernel, uint8_t* dest, size_t size) {
1919
#ifdef __WIN32__
2020
HCRYPTPROV hProv;
2121
kernel->win32_require
@@ -47,7 +47,7 @@ isaac_seed(rust_kernel* kernel, uint8_t* dest, size_t size) {
4747
#endif
4848
}
4949

50-
void
50+
static void
5151
isaac_init(rust_kernel *kernel, randctx *rctx, rust_vec_box* user_seed) {
5252
memset(rctx, 0, sizeof(randctx));
5353

@@ -64,12 +64,22 @@ isaac_init(rust_kernel *kernel, randctx *rctx, rust_vec_box* user_seed) {
6464
seed = (seed + 0x7ed55d16) + (seed << 12);
6565
}
6666
} else {
67-
isaac_seed(kernel, (uint8_t*) &rctx->randrsl, sizeof(rctx->randrsl));
67+
rng_gen_seed(kernel, (uint8_t*)&rctx->randrsl, sizeof(rctx->randrsl));
6868
}
6969

7070
randinit(rctx, 1);
7171
}
7272

73+
void
74+
rng_init(rust_kernel* kernel, rust_rng* rng, rust_vec_box* user_seed) {
75+
isaac_init(kernel, &rng->rctx, user_seed);
76+
}
77+
78+
uint32_t
79+
rng_gen_u32(rust_rng* rng) {
80+
return isaac_rand(&rng->rctx);
81+
}
82+
7383
//
7484
// Local Variables:
7585
// mode: C++

src/rt/rust_rng.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,13 @@ struct rust_vec_box;
1818

1919
// Initialization helpers for ISAAC RNG
2020

21-
void isaac_seed(rust_kernel* kernel, uint8_t* dest, size_t size);
22-
void isaac_init(rust_kernel *kernel, randctx *rctx, rust_vec_box* user_seed);
21+
struct rust_rng {
22+
randctx rctx;
23+
};
24+
25+
void rng_gen_seed(rust_kernel* kernel, uint8_t* dest, size_t size);
26+
void rng_init(rust_kernel *kernel, rust_rng *rng, rust_vec_box* user_seed);
27+
uint32_t rng_gen_u32(rust_rng *rng);
2328

2429
//
2530
// Local Variables:

src/rt/rust_sched_loop.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ rust_sched_loop::rust_sched_loop(rust_scheduler *sched, int id, bool killed) :
4141
name("main")
4242
{
4343
LOGPTR(this, "new dom", (uintptr_t)this);
44-
isaac_init(kernel, &rctx, NULL);
44+
rng_init(kernel, &rng, NULL);
4545

4646
if (!tls_initialized)
4747
init_tls();
@@ -151,7 +151,7 @@ rust_task *
151151
rust_sched_loop::schedule_task() {
152152
lock.must_have_lock();
153153
if (running_tasks.length() > 0) {
154-
size_t k = isaac_rand(&rctx);
154+
size_t k = rng_gen_u32(&rng);
155155
size_t i = k % running_tasks.length();
156156
return (rust_task *)running_tasks[i];
157157
}

src/rt/rust_sched_loop.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ struct rust_sched_loop
6262
#endif
6363

6464
context c_context;
65-
65+
rust_rng rng;
6666
bool should_exit;
6767

6868
stk_seg *cached_c_stack;
@@ -103,7 +103,6 @@ struct rust_sched_loop
103103
size_t min_stack_size;
104104
memory_region local_region;
105105

106-
randctx rctx;
107106
const char *const name; // Used for debugging
108107

109108
// Only a pointer to 'name' is kept, so it must live as long as this

0 commit comments

Comments
 (0)