Re: [PATCH] btrfs-progs: utils: use better wrappered random generator

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The API does not seem right. It's fine to provide functions for full
int/u32/u64 ranges, but in the cases when we know the range from which
we request the random number, it has to be passed as parameter. Not
doing the % by hand.

> +u32 rand_u32(void)
> +{
> +	struct timeval tv;
> +	unsigned short rand_seed[3];

This could be made static (with thread local storage) so the state does
not get regenerated all the time. Possibly it could be initialize from
some true random source, not time or pid.

> +	long int ret;
> +	int i;
> +
> +	gettimeofday(&tv, 0);
> +	rand_seed[0] = getpid() ^ (tv.tv_sec & 0xFFFF);
> +	rand_seed[1] = getppid() ^ (tv.tv_usec & 0xFFFF);
> +	rand_seed[2] = (tv.tv_sec ^ tv.tv_usec) >> 16;
> +
> +	/* Crank the random number generator a few times */
> +	gettimeofday(&tv, 0);
> +	for (i = (tv.tv_sec ^ tv.tv_sec) ^ 0x1F; i > 0; i--)
> +		nrand48(rand_seed);

This would be then unnecesssray, just draw the number from nrand.

About patch separation: please introduce the new api in one patch, use
in another (ie. drop srand and switch to it).
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Linux Filesystem Development]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux