1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

random.c: no local copy of the seed

* random.c (make_seed_value): append leading-zero-guard and get
  rid of making a local copy of the seed.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54997 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2016-05-13 15:16:57 +00:00
parent 00a371c28d
commit d009cebf0f
2 changed files with 12 additions and 15 deletions

View file

@ -1,3 +1,8 @@
Sat May 14 00:16:54 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
* random.c (make_seed_value): append leading-zero-guard and get
rid of making a local copy of the seed.
Fri May 13 08:46:42 2016 cremno <cremno@mail.ru> Fri May 13 08:46:42 2016 cremno <cremno@mail.ru>
* NEWS: drop FreeBSD < 4 support. * NEWS: drop FreeBSD < 4 support.

View file

@ -567,21 +567,13 @@ fill_random_seed(uint32_t *seed, size_t cnt)
} }
static VALUE static VALUE
make_seed_value(const uint32_t *ptr) make_seed_value(uint32_t *ptr, size_t len)
{ {
VALUE seed; VALUE seed;
size_t len;
uint32_t buf[DEFAULT_SEED_CNT+1];
if (ptr[DEFAULT_SEED_CNT-1] <= 1) { if (ptr[len-1] <= 1) {
/* set leading-zero-guard */ /* set leading-zero-guard */
MEMCPY(buf, ptr, uint32_t, DEFAULT_SEED_CNT); ptr[len++] = 1;
buf[DEFAULT_SEED_CNT] = 1;
ptr = buf;
len = DEFAULT_SEED_CNT+1;
}
else {
len = DEFAULT_SEED_CNT;
} }
seed = rb_integer_unpack(ptr, len, sizeof(uint32_t), 0, seed = rb_integer_unpack(ptr, len, sizeof(uint32_t), 0,
@ -602,9 +594,9 @@ static VALUE
random_seed(void) random_seed(void)
{ {
VALUE v; VALUE v;
uint32_t buf[DEFAULT_SEED_CNT]; uint32_t buf[DEFAULT_SEED_CNT+1];
fill_random_seed(buf, DEFAULT_SEED_CNT); fill_random_seed(buf, DEFAULT_SEED_CNT);
v = make_seed_value(buf); v = make_seed_value(buf, DEFAULT_SEED_CNT);
explicit_bzero(buf, DEFAULT_SEED_LEN); explicit_bzero(buf, DEFAULT_SEED_LEN);
return v; return v;
} }
@ -1553,12 +1545,12 @@ Init_RandomSeedCore(void)
static VALUE static VALUE
init_randomseed(struct MT *mt) init_randomseed(struct MT *mt)
{ {
uint32_t initial[DEFAULT_SEED_CNT]; uint32_t initial[DEFAULT_SEED_CNT+1];
VALUE seed; VALUE seed;
fill_random_seed(initial, DEFAULT_SEED_CNT); fill_random_seed(initial, DEFAULT_SEED_CNT);
init_by_array(mt, initial, DEFAULT_SEED_CNT); init_by_array(mt, initial, DEFAULT_SEED_CNT);
seed = make_seed_value(initial); seed = make_seed_value(initial, DEFAULT_SEED_CNT);
explicit_bzero(initial, DEFAULT_SEED_LEN); explicit_bzero(initial, DEFAULT_SEED_LEN);
return seed; return seed;
} }