diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-05-13 15:16:57 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-05-13 15:16:57 +0000 |
commit | d009cebf0faff38a99c7fcb47b08b052303cf3db (patch) | |
tree | c34fdd596615473c96b754057c3e3a9d468ec7b8 | |
parent | 00a371c28d1cd964efc60dfe9da1ff59b2096175 (diff) | |
download | ruby-d009cebf0faff38a99c7fcb47b08b052303cf3db.tar.gz |
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
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | random.c | 22 |
2 files changed, 12 insertions, 15 deletions
@@ -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> * NEWS: drop FreeBSD < 4 support. @@ -567,21 +567,13 @@ fill_random_seed(uint32_t *seed, size_t cnt) } static VALUE -make_seed_value(const uint32_t *ptr) +make_seed_value(uint32_t *ptr, size_t len) { 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 */ - MEMCPY(buf, ptr, uint32_t, DEFAULT_SEED_CNT); - buf[DEFAULT_SEED_CNT] = 1; - ptr = buf; - len = DEFAULT_SEED_CNT+1; - } - else { - len = DEFAULT_SEED_CNT; + ptr[len++] = 1; } seed = rb_integer_unpack(ptr, len, sizeof(uint32_t), 0, @@ -602,9 +594,9 @@ static VALUE random_seed(void) { VALUE v; - uint32_t buf[DEFAULT_SEED_CNT]; + uint32_t buf[DEFAULT_SEED_CNT+1]; 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); return v; } @@ -1553,12 +1545,12 @@ Init_RandomSeedCore(void) static VALUE init_randomseed(struct MT *mt) { - uint32_t initial[DEFAULT_SEED_CNT]; + uint32_t initial[DEFAULT_SEED_CNT+1]; VALUE seed; fill_random_seed(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); return seed; } |