Reindent server/core/random_jkiss.c

This commit is contained in:
Johan Wikman
2015-11-30 16:04:18 +02:00
parent 72dd159f98
commit 233f50f3e6
2 changed files with 66 additions and 27 deletions

View File

@ -18,15 +18,15 @@
/** /**
* @file random_jkiss.c - Random number generator for the MariaDB Corporation MaxScale * @file random_jkiss.c - Random number generator for the MariaDB Corporation MaxScale
* *
* See http://www0.cs.ucl.ac.uk/staff/d.jones/GoodPracticeRNG.pdf for discussion of random * See http://www0.cs.ucl.ac.uk/staff/d.jones/GoodPracticeRNG.pdf for discussion of random
* number generators (RNGs). * number generators (RNGs).
* *
* @verbatim * @verbatim
* Revision History * Revision History
* *
* Date Who Description * Date Who Description
* 26/08/15 Martin Brampton Initial implementation * 26/08/15 Martin Brampton Initial implementation
* *
* @endverbatim * @endverbatim
*/ */
@ -53,18 +53,18 @@ static unsigned int random_jkiss_devrand(void);
static void random_init_jkiss(void); static void random_init_jkiss(void);
/*** /***
* *
* Return a pseudo-random number that satisfies major tests for random sequences * Return a pseudo-random number that satisfies major tests for random sequences
* *
* @return uint Random number * @return uint Random number
* *
*/ */
unsigned int unsigned int
random_jkiss(void) random_jkiss(void)
{ {
unsigned long long t; unsigned long long t;
unsigned int result; unsigned int result;
spinlock_acquire(&random_jkiss_spinlock); spinlock_acquire(&random_jkiss_spinlock);
if (!init) if (!init)
{ {
@ -88,18 +88,23 @@ random_jkiss(void)
/* Own code adapted from http://www0.cs.ucl.ac.uk/staff/d.jones/GoodPracticeRNG.pdf */ /* Own code adapted from http://www0.cs.ucl.ac.uk/staff/d.jones/GoodPracticeRNG.pdf */
/*** /***
* *
* Obtain a seed random number from /dev/urandom if available. * Obtain a seed random number from /dev/urandom if available.
* *
* @return uint Random number * @return uint Random number
* *
*/ */
static unsigned int static unsigned int
random_jkiss_devrand(void) random_jkiss_devrand(void)
{ {
int fn; int fn;
unsigned int r; unsigned int r;
if ((fn = open("/dev/urandom", O_RDONLY)) == -1) return 0;
if ((fn = open("/dev/urandom", O_RDONLY)) == -1)
{
return 0;
}
if (read(fn, &r, sizeof(r)) != sizeof(r)) if (read(fn, &r, sizeof(r)) != sizeof(r))
{ {
r = 0; r = 0;
@ -109,23 +114,41 @@ random_jkiss_devrand(void)
} }
/*** /***
* *
* Initialise the generator using /dev/urandom if available, and warm up * Initialise the generator using /dev/urandom if available, and warm up
* with 1000 iterations * with 1000 iterations
* *
*/ */
static void static void
random_init_jkiss(void) random_init_jkiss(void)
{ {
int newrand, i; int newrand, i;
spinlock_acquire(&random_jkiss_spinlock); spinlock_acquire(&random_jkiss_spinlock);
if ((newrand = random_jkiss_devrand()) != 0) x = newrand; if ((newrand = random_jkiss_devrand()) != 0)
if ((newrand = random_jkiss_devrand()) != 0) y = newrand; {
if ((newrand = random_jkiss_devrand()) != 0) z = newrand; x = newrand;
if ((newrand = random_jkiss_devrand()) != 0) }
if ((newrand = random_jkiss_devrand()) != 0)
{
y = newrand;
}
if ((newrand = random_jkiss_devrand()) != 0)
{
z = newrand;
}
if ((newrand = random_jkiss_devrand()) != 0)
{
c = newrand % 698769068 + 1; /* Should be less than 698769069 */ c = newrand % 698769068 + 1; /* Should be less than 698769069 */
}
spinlock_release(&random_jkiss_spinlock); spinlock_release(&random_jkiss_spinlock);
/* "Warm up" our random number generator */ /* "Warm up" our random number generator */
for (i = 0; i < 100; i++) random_jkiss(); for (i = 0; i < 100; i++)
{
random_jkiss();
}
} }

View File

@ -1,22 +1,38 @@
/* #ifndef RANDOM_JKISS_H
#define RANDOM_JKISS_H
/*
* This file is distributed as part of the MariaDB Corporation MaxScale. It is free
* software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation,
* version 2.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Copyright MariaDB Corporation Ab 2013-2014
*/
/*
* File: random_jkiss.h * File: random_jkiss.h
* Author: mbrampton * Author: mbrampton
* *
* Created on 26 August 2015, 15:34 * Created on 26 August 2015, 15:34
*/ */
#ifndef RANDOM_JKISS_H
#define RANDOM_JKISS_H
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
extern unsigned int random_jkiss(void); extern unsigned int random_jkiss(void);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif /* RANDOM_H */ #endif /* RANDOM_H */