rand/rand.[ch]: Don't dynamically construct the global generator.
authorMark Wooding <mdw@distorted.org.uk>
Mon, 22 Dec 2014 20:32:58 +0000 (20:32 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Sun, 22 Mar 2015 01:02:45 +0000 (01:02 +0000)
Now it always exists, and is statically initialized.  There should be no
observable change.

rand/rand.c
rand/rand.h

index fa6dab8..be39d79 100644 (file)
@@ -37,6 +37,8 @@
 #include "arena.h"
 #include "blowfish-cbc.h"
 #include "paranoia.h"
+
+#define RAND__HACKS
 #include "rand.h"
 #include "rmd160.h"
 #include "rmd160-hmac.h"
 
 static const grand_ops gops;
 
-typedef struct gctx {
+typedef struct rand__gctx {
   grand r;
   rand_pool p;
 } gctx;
 
-static gctx *pool = 0;                 /* Default random pool */
+gctx rand_global = {
+  { &gops },
+  { { 0 }, 0, 0, 0,
+    { 0 }, RAND_SECSZ, 0,
+    { { 0xb7, 0xb0, 0xb4, 0xdb, 0x59, 0x75, 0x49, 0x32,
+       0x1a, 0x8d, 0x4b, 0x86, 0x3a, 0x38, 0xfd, 0x59,
+       0xc1, 0x63, 0x66, 0xd9 }, 64,
+      { 0x91, 0x9a, 0xe6, 0xa1, 0x9d, 0x3a, 0x86, 0xef,
+       0xb2, 0xb9, 0xca, 0xfc, 0x26, 0xf8, 0xb1, 0x04,
+       0x4a, 0x41, 0xc4, 0x7a }, 64 },
+    0 }
+};
 
 /*----- Macros ------------------------------------------------------------*/
 
-#define RAND_RESOLVE(r) do {                                           \
-  if ((r) == RAND_GLOBAL) {                                            \
-    if (!pool)                                                         \
-      pool = (gctx *)rand_create();                                    \
-    (r) = &pool->p;                                                    \
-  }                                                                    \
-} while (0)
+#define RAND_RESOLVE(r)                                                        \
+  do { if ((r) == RAND_GLOBAL) r = &rand_global.p; } while (0)
 
 #define TIMER(r) do {                                                  \
   if ((r)->s && (r)->s->timer)                                         \
@@ -421,21 +429,10 @@ void rand_getgood(rand_pool *r, void *p, size_t sz)
 
 /*----- Generic random number generator interface -------------------------*/
 
-#define GRESOLVE(g, r) do {                                            \
-  if (r != &rand_global)                                               \
-    g = (gctx *)r;                                                     \
-  else {                                                               \
-    if (!pool)                                                         \
-      pool = (gctx *)rand_create();                                    \
-    g = pool;                                                          \
-  }                                                                    \
-} while (0)
-
 static void gdestroy(grand *r)
 {
-  gctx *g;
-  GRESOLVE(g, r);
-  if (g != pool) {
+  gctx *g = (gctx *)r;
+  if (g != &rand_global) {
     BURN(*g);
     S_DESTROY(g);
   }
@@ -443,12 +440,11 @@ static void gdestroy(grand *r)
 
 static int gmisc(grand *r, unsigned op, ...)
 {
-  gctx *g;
+  gctx *g = (gctx *)r;
   va_list ap;
   int rc = 0;
   va_start(ap, op);
 
-  GRESOLVE(g, r);
   switch (op) {
     case GRAND_CHECK:
       switch (va_arg(ap, unsigned)) {
@@ -531,26 +527,23 @@ static int gmisc(grand *r, unsigned op, ...)
 
 static octet gbyte(grand *r)
 {
-  gctx *g;
+  gctx *g = (gctx *)r;
   octet o;
-  GRESOLVE(g, r);
   rand_getgood(&g->p, &o, 1);
   return (o);
 }
 
 static uint32 gword(grand *r)
 {
-  gctx *g;
+  gctx *g = (gctx *)r;
   octet b[4];
-  GRESOLVE(g, r);
   rand_getgood(&g->p, &b, sizeof(b));
   return (LOAD32(b));
 }
 
 static void gfill(grand *r, void *p, size_t sz)
 {
-  gctx *g;
-  GRESOLVE(g, r);
+  gctx *g = (gctx *)r;
   rand_get(&g->p, p, sz);
 }
 
@@ -561,8 +554,6 @@ static const grand_ops gops = {
   gword, gbyte, gword, grand_range, gfill
 };
 
-grand rand_global = { &gops };
-
 /* --- @rand_create@ --- *
  *
  * Arguments:  ---
index a024c0f..389526d 100644 (file)
@@ -290,7 +290,11 @@ enum {
 
 /* --- Default random number generator --- */
 
-extern grand rand_global;
+#ifdef RAND__HACKS
+  extern struct rand__gctx rand_global;
+#else
+  extern grand rand_global;
+#endif
 
 /* --- @rand_create@ --- *
  *