mirror of
git://sourceware.org/git/lvm2.git
synced 2024-12-21 13:34:40 +03:00
lib: Share lvm_even_rand for random numbers.
This commit is contained in:
parent
e7b8e0a10c
commit
12ddaa5f10
@ -1,5 +1,6 @@
|
||||
Version 2.02.106 -
|
||||
====================================
|
||||
Export lvm_even_rand() for controlled provision of random numbers.
|
||||
Add lvmthin man page to section 7.
|
||||
Extend internal validation of lv names size is less then 128 chars.
|
||||
Fail in resume for lvrename will result in failing command.
|
||||
|
@ -1057,29 +1057,6 @@ uint64_t extents_from_size(struct cmd_context *cmd, uint64_t size,
|
||||
return size / extent_size;
|
||||
}
|
||||
|
||||
/*
|
||||
* Return random integer in [0,max) interval
|
||||
*
|
||||
* The loop rejects numbers that come from an "incomplete" slice of the
|
||||
* RAND_MAX space (considering the number space [0, RAND_MAX] is divided
|
||||
* into some "max"-sized slices and at most a single smaller slice,
|
||||
* between [n*max, RAND_MAX] for suitable n -- numbers from this last slice
|
||||
* are discarded because they could distort the distribution in favour of
|
||||
* smaller numbers.
|
||||
*/
|
||||
static unsigned _even_rand( unsigned *seed, unsigned max )
|
||||
{
|
||||
unsigned r, ret;
|
||||
|
||||
/* make sure distribution is even */
|
||||
do {
|
||||
r = (unsigned) rand_r( seed );
|
||||
ret = r % max;
|
||||
} while ( r - ret > RAND_MAX - max );
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static dm_bitset_t _bitset_with_random_bits(struct dm_pool *mem, uint32_t num_bits,
|
||||
uint32_t num_set_bits, unsigned *seed)
|
||||
{
|
||||
@ -1102,7 +1079,7 @@ static dm_bitset_t _bitset_with_random_bits(struct dm_pool *mem, uint32_t num_bi
|
||||
/* Perform loop num_set_bits times, selecting one bit each time */
|
||||
while (i++ < num_bits) {
|
||||
/* Select a random bit between 0 and (i-1) inclusive. */
|
||||
bit_selected = _even_rand(seed, i);
|
||||
bit_selected = lvm_even_rand(seed, i);
|
||||
|
||||
/*
|
||||
* If the bit was already set, set the new bit that became
|
||||
|
@ -118,3 +118,24 @@ int read_urandom(void *buf, size_t len)
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Return random integer in [0,max) interval
|
||||
*
|
||||
* The loop rejects numbers that come from an "incomplete" slice of the
|
||||
* RAND_MAX space. Considering the number space [0, RAND_MAX] is divided
|
||||
* into some "max"-sized slices and at most a single smaller slice,
|
||||
* between [n*max, RAND_MAX] for suitable n, numbers from this last slice
|
||||
* are discarded because they could distort the distribution in favour of
|
||||
* smaller numbers.
|
||||
*/
|
||||
unsigned lvm_even_rand(unsigned *seed, unsigned max)
|
||||
{
|
||||
unsigned r, ret;
|
||||
|
||||
do {
|
||||
r = (unsigned) rand_r(seed);
|
||||
ret = r % max;
|
||||
} while (r - ret > RAND_MAX - max);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
@ -32,6 +32,11 @@ int lvm_getpagesize(void);
|
||||
*/
|
||||
int read_urandom(void *buf, size_t len);
|
||||
|
||||
/*
|
||||
* Return random integer in [0,max) interval
|
||||
*/
|
||||
unsigned lvm_even_rand(unsigned *seed, unsigned max);
|
||||
|
||||
# ifndef HAVE_SIGINTERRUPT
|
||||
# define siginterrupt(sig, flag) \
|
||||
do { \
|
||||
|
Loading…
Reference in New Issue
Block a user