From 95e4ed03bffba9e11cd11b2bd601edd38a1e75d8 Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Sat, 27 Dec 2014 11:45:59 +0100 Subject: [PATCH] Removes rand() use from the raid module. --- cmdline/util.c | 5 ----- cmdline/util.h | 2 +- raid/memory.c | 10 +++++++--- raid/memory.h | 4 ++-- raid/test.c | 8 ++++---- 5 files changed, 14 insertions(+), 15 deletions(-) diff --git a/cmdline/util.c b/cmdline/util.c index be9ed7d3..ecee1887 100644 --- a/cmdline/util.c +++ b/cmdline/util.c @@ -1588,11 +1588,6 @@ void** malloc_nofail_vector_align(int nd, int n, size_t size, void** freeptr) return ptr; } -void mrand_vector(int n, size_t size, void** vv) -{ - raid_mrand_vector(n, size, vv); -} - void mtest_vector(int n, size_t size, void** vv) { if (raid_mtest_vector(n, size, vv) != 0) { diff --git a/cmdline/util.h b/cmdline/util.h index 6eea1dc9..23552efb 100644 --- a/cmdline/util.h +++ b/cmdline/util.h @@ -485,7 +485,7 @@ void** malloc_nofail_vector_align(int nd, int n, size_t size, void** freeptr); /** * Fills the memory vector with random data. */ -void mrand_vector(int n, size_t size, void** vv); +void mrand_vector(unsigned seed, int n, size_t size, void** vv); /** * Tests the memory vector for RAM problems. diff --git a/raid/memory.c b/raid/memory.c index bd98e01c..dcbcf086 100644 --- a/raid/memory.c +++ b/raid/memory.c @@ -68,15 +68,19 @@ void **raid_malloc_vector(int nd, int n, size_t size, void **freeptr) return v; } -void raid_mrand_vector(int n, size_t size, void **vv) +void raid_mrand_vector(unsigned seed, int n, size_t size, void **vv) { unsigned char **v = (unsigned char**)vv; int i; size_t j; for (i = 0; i < n; ++i) - for (j = 0; j < size; ++j) - v[i][j] = rand(); + for (j = 0; j < size; ++j) { + /* basic C99/C11 linear congruential generator */ + seed = seed * 1103515245U + 12345U; + + v[i][j] = seed >> 16; + } } int raid_mtest_vector(int n, size_t size, void **vv) diff --git a/raid/memory.h b/raid/memory.h index ae262caa..6deb45a8 100644 --- a/raid/memory.h +++ b/raid/memory.h @@ -70,9 +70,9 @@ void *raid_malloc_align(size_t size, void **freeptr); void **raid_malloc_vector(int nd, int n, size_t size, void **freeptr); /** - * Fills the memory vector with random data. + * Fills the memory vector with pseudo-random data based on the specified seed. */ -void raid_mrand_vector(int n, size_t size, void **vv); +void raid_mrand_vector(unsigned seed, int n, size_t size, void **vv); /** * Tests the memory vector for RAM problems. diff --git a/raid/test.c b/raid/test.c index 16e07501..4c196acb 100644 --- a/raid/test.c +++ b/raid/test.c @@ -176,8 +176,8 @@ int raid_test_rec(int mode, int nd, size_t size) waste = v[nv - 1]; - /* fill data disk with random */ - raid_mrand_vector(nd, size, v); + /* fill with pseudo-random data with the arbitrary seed "1" */ + raid_mrand_vector(1, nd, size, v); /* setup recov functions */ for (i = 0; i < np; ++i) { @@ -304,8 +304,8 @@ int raid_test_par(int mode, int nd, size_t size) if (raid_mtest_vector(nv, size, v) != 0) goto bail; - /* fill with random */ - raid_mrand_vector(nv, size, v); + /* fill with pseudo-random data with the arbitrary seed "2" */ + raid_mrand_vector(2, nv, size, v); /* compute the parity */ raid_gen_ref(nd, np, size, v);