[前][次][番号順一覧][スレッド一覧]

ruby-changes:13840

From: nobu <ko1@a...>
Date: Wed, 4 Nov 2009 08:51:01 +0900 (JST)
Subject: [ruby-changes:13840] Ruby:r25640 (trunk): * random.c (rb_hash_start): moved from string.c.

nobu	2009-11-04 08:50:51 +0900 (Wed, 04 Nov 2009)

  New Revision: 25640

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=25640

  Log:
    * random.c (rb_hash_start): moved from string.c.
    
    * random.c (Init_RandomSeed2): register global address before set.
    
    * random.c (Init_RandomSeed): initialize hashseed.

  Modified files:
    trunk/random.c
    trunk/string.c

Index: string.c
===================================================================
--- string.c	(revision 25639)
+++ string.c	(revision 25640)
@@ -1993,32 +1993,6 @@
 }
 
 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
 rb_memhash(const void *ptr, long len)
 {
     return st_hash(ptr, len, rb_hash_start(0));
Index: random.c
===================================================================
--- random.c	(revision 25639)
+++ random.c	(revision 25640)
@@ -729,6 +729,7 @@
 rb_f_srand(int argc, VALUE *argv, VALUE obj)
 {
     VALUE seed, old;
+    struct Random *r = &default_rand;
 
     rb_secure(4);
     if (argc == 0) {
@@ -737,8 +738,8 @@
     else {
 	rb_scan_args(argc, argv, "01", &seed);
     }
-    old = default_rand.rnd.seed;
-    default_rand.rnd.seed = rand_init(&default_rand.rnd.mt, seed);
+    old = r->rnd.seed;
+    r->rnd.seed = rand_init(&r->rnd.mt, seed);
 
     return old;
 }
@@ -1126,25 +1127,53 @@
     return r;
 }
 
+static st_index_t hashseed;
+
 void
 Init_RandomSeed(void)
 {
-    fill_random_seed(default_rand.initial);
-    init_by_array(&default_rand.rnd.mt, default_rand.initial, DEFAULT_SEED_CNT);
+    struct Random *r = &default_rand;
+    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
 Init_RandomSeed2(void)
 {
-    default_rand.rnd.seed = make_seed_value(default_rand.initial);
-    memset(default_rand.initial, 0, DEFAULT_SEED_LEN);
+    struct Random *r = &default_rand;
+    rb_global_variable(&r->rnd.seed);
+    r->rnd.seed = make_seed_value(r->initial);
+    memset(r->initial, 0, DEFAULT_SEED_LEN);
 }
 
 void
 rb_reset_random_seed(void)
 {
-    uninit_genrand(&default_rand.rnd.mt);
-    default_rand.rnd.seed = INT2FIX(0);
+    struct Random *r = &default_rand;
+    uninit_genrand(&r->rnd.mt);
+    r->rnd.seed = INT2FIX(0);
 }
 
 void
@@ -1153,7 +1182,6 @@
     Init_RandomSeed2();
     rb_define_global_function("srand", rb_f_srand, -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_define_alloc_func(rb_cRandom, random_alloc);

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]