From: Wouter Wijngaards Date: Fri, 23 Feb 2007 16:40:58 +0000 (+0000) Subject: Random nrs. X-Git-Tag: release-0.1~27 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7ccdfe8f7cb9cb50d58138b9c2db69a6e0860f77;p=thirdparty%2Funbound.git Random nrs. git-svn-id: file:///svn/unbound/trunk@144 be551aaa-1e26-0410-a405-d3ace91eadb9 --- diff --git a/doc/Changelog b/doc/Changelog index 5ffabd124..d566e5026 100644 --- a/doc/Changelog +++ b/doc/Changelog @@ -6,6 +6,7 @@ - config file added interface:, chroot: and username:. - config file: directory, logfile, pidfile. And they work too. - will daemonize by default now. Use -d to stay in the foreground. + - got BSD random[256 state] code, made it threadsafe. util/random. 22 February 2007: Wouter - Have a config file. Removed commandline options, moved to config. diff --git a/util/random.c b/util/random.c index 76ae5a6a4..3e19da876 100644 --- a/util/random.c +++ b/util/random.c @@ -50,9 +50,11 @@ #include #ifndef ULONG_MAX +/** in case its not defined */ #define ULONG_MAX ((unsigned long)(~0L)) /* 0xFFFFFFFF for 32-bits */ #endif #ifndef LONG_MAX +/** in case its not defined */ #define LONG_MAX ((long)(ULONG_MAX >> 1)) /* 0x7FFFFFFF for 32-bits*/ #endif @@ -98,41 +100,57 @@ the polynomial (actually a trinomial) that the R.N.G. is based on, and separation between the two lower order coefficients of the trinomial. */ -/* Linear congruential. */ +/** Linear congruential. */ #define TYPE_0 0 +/** the break */ #define BREAK_0 8 +/** the degree */ #define DEG_0 0 +/** the sep */ #define SEP_0 0 -/* x**7 + x**3 + 1. */ +/** x**7 + x**3 + 1. */ #define TYPE_1 1 +/** the break */ #define BREAK_1 32 +/** the degree */ #define DEG_1 7 +/** the sep */ #define SEP_1 3 -/* x**15 + x + 1. */ +/** x**15 + x + 1. */ #define TYPE_2 2 +/** the break */ #define BREAK_2 64 +/** the degree */ #define DEG_2 15 +/** the sep */ #define SEP_2 1 -/* x**31 + x**3 + 1. */ +/** x**31 + x**3 + 1. */ #define TYPE_3 3 +/** the break */ #define BREAK_3 128 +/** the degree */ #define DEG_3 31 +/** the sep */ #define SEP_3 3 -/* x**63 + x + 1. */ +/** x**63 + x + 1. */ #define TYPE_4 4 +/** the break */ #define BREAK_4 256 +/** the degree */ #define DEG_4 63 +/** the sep */ #define SEP_4 1 /* Array versions of the above information to make code run faster. Relies on fact that TYPE_i == i. */ -#define MAX_TYPES 5 /* Max number of types above. */ +/** Max number of types above. */ +#define MAX_TYPES 5 /* static int degrees[MAX_TYPES] = { DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 }; @@ -207,10 +225,14 @@ static long int *end_ptr = &randtbl[sizeof(randtbl) / sizeof(randtbl[0])]; information a given number of times to get rid of any initial dependencies introduced by the L.C.R.N.G. Note that the initialization of randtbl[] for default usage relies on values produced by this routine. */ +/** init state. + * @param s: state to init. + * @param x: seed. + */ static void ub_srandom (struct ub_randstate* s, unsigned int x) { - s->state[0] = x; + s->state[0] = (long int)x; if (s->rand_type != TYPE_0) { register long int i; @@ -219,7 +241,7 @@ ub_srandom (struct ub_randstate* s, unsigned int x) s->fptr = &s->state[s->rand_sep]; s->rptr = &s->state[0]; for (i = 0; i < 10 * s->rand_deg; ++i) - ub_random(s); + (void)ub_random(s); } } @@ -332,3 +354,8 @@ ub_random (struct ub_randstate* s) } } + +void ub_randfree(struct ub_randstate* state) +{ + free(state->state); +} diff --git a/util/random.h b/util/random.h index ef3e0499c..b0b3d1e3b 100644 --- a/util/random.h +++ b/util/random.h @@ -45,12 +45,19 @@ * random state structure. */ struct ub_randstate { + /** state array, malloced */ long int* state; + /** front ptr */ long int* fptr; + /** rear ptr */ long int* rptr; + /** rng type */ int rand_type; + /** rng degree */ int rand_deg; + /** rng sep */ int rand_sep; + /** rng end ptr */ long int* end_ptr; }; @@ -72,4 +79,10 @@ int ub_initstate(unsigned int seed, struct ub_randstate* state, */ long int ub_random(struct ub_randstate* state); +/** + * Delete the random state. + * @param state: to delete. + */ +void ub_randfree(struct ub_randstate* state); + #endif /* UTIL_RANDOM_H */