* random.c (rb_hash_start): moved from string.c.

* random.c (Init_RandomSeed2): register global address before set.

* random.c (Init_RandomSeed): initialize hashseed.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25640 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2009-11-03 23:50:51 +00:00
Родитель 252168ed1b
Коммит f41eaf8431
2 изменённых файлов: 37 добавлений и 35 удалений

Просмотреть файл

@ -729,6 +729,7 @@ static VALUE
rb_f_srand(int argc, VALUE *argv, VALUE obj) rb_f_srand(int argc, VALUE *argv, VALUE obj)
{ {
VALUE seed, old; VALUE seed, old;
struct Random *r = &default_rand;
rb_secure(4); rb_secure(4);
if (argc == 0) { if (argc == 0) {
@ -737,8 +738,8 @@ rb_f_srand(int argc, VALUE *argv, VALUE obj)
else { else {
rb_scan_args(argc, argv, "01", &seed); rb_scan_args(argc, argv, "01", &seed);
} }
old = default_rand.rnd.seed; old = r->rnd.seed;
default_rand.rnd.seed = rand_init(&default_rand.rnd.mt, seed); r->rnd.seed = rand_init(&r->rnd.mt, seed);
return old; return old;
} }
@ -1126,25 +1127,53 @@ rb_f_rand(int argc, VALUE *argv, VALUE obj)
return r; return r;
} }
static st_index_t hashseed;
void void
Init_RandomSeed(void) Init_RandomSeed(void)
{ {
fill_random_seed(default_rand.initial); struct Random *r = &default_rand;
init_by_array(&default_rand.rnd.mt, default_rand.initial, DEFAULT_SEED_CNT); struct MT *mt = &r->rnd.mt;
fill_random_seed(r->initial);
init_by_array(mt, r->initial, DEFAULT_SEED_CNT);
hashseed = genrand_int32(mt);
#if SIZEOF_ST_INDEX_T*CHAR_BIT > 4*8
hashseed <<= 32;
hashseed |= genrand_int32(mt);
#endif
#if SIZEOF_ST_INDEX_T*CHAR_BIT > 8*8
hashseed <<= 32;
hashseed |= genrand_int32(mt);
#endif
#if SIZEOF_ST_INDEX_T*CHAR_BIT > 12*8
hashseed <<= 32;
hashseed |= genrand_int32(mt);
#endif
}
st_index_t
rb_hash_start(st_index_t h)
{
return st_hash_start(hashseed + h);
} }
static void static void
Init_RandomSeed2(void) Init_RandomSeed2(void)
{ {
default_rand.rnd.seed = make_seed_value(default_rand.initial); struct Random *r = &default_rand;
memset(default_rand.initial, 0, DEFAULT_SEED_LEN); rb_global_variable(&r->rnd.seed);
r->rnd.seed = make_seed_value(r->initial);
memset(r->initial, 0, DEFAULT_SEED_LEN);
} }
void void
rb_reset_random_seed(void) rb_reset_random_seed(void)
{ {
uninit_genrand(&default_rand.rnd.mt); struct Random *r = &default_rand;
default_rand.rnd.seed = INT2FIX(0); uninit_genrand(&r->rnd.mt);
r->rnd.seed = INT2FIX(0);
} }
void void
@ -1153,7 +1182,6 @@ Init_Random(void)
Init_RandomSeed2(); Init_RandomSeed2();
rb_define_global_function("srand", rb_f_srand, -1); rb_define_global_function("srand", rb_f_srand, -1);
rb_define_global_function("rand", rb_f_rand, -1); rb_define_global_function("rand", rb_f_rand, -1);
rb_global_variable(&default_rand.rnd.seed);
rb_cRandom = rb_define_class("Random", rb_cObject); rb_cRandom = rb_define_class("Random", rb_cObject);
rb_define_alloc_func(rb_cRandom, random_alloc); rb_define_alloc_func(rb_cRandom, random_alloc);

Просмотреть файл

@ -1992,32 +1992,6 @@ rb_str_concat(VALUE str1, VALUE str2)
return rb_str_append(str1, str2); return rb_str_append(str1, str2);
} }
st_index_t
rb_hash_start(st_index_t h)
{
static int hashseed_init = 0;
static st_index_t hashseed;
if (!hashseed_init) {
hashseed = rb_genrand_int32();
#if SIZEOF_ST_INDEX_T*CHAR_BIT > 4*8
hashseed <<= 32;
hashseed |= rb_genrand_int32();
#endif
#if SIZEOF_ST_INDEX_T*CHAR_BIT > 8*8
hashseed <<= 32;
hashseed |= rb_genrand_int32();
#endif
#if SIZEOF_ST_INDEX_T*CHAR_BIT > 12*8
hashseed <<= 32;
hashseed |= rb_genrand_int32();
#endif
hashseed_init = 1;
}
return st_hash_start(hashseed + h);
}
st_index_t st_index_t
rb_memhash(const void *ptr, long len) rb_memhash(const void *ptr, long len)
{ {