ConfigureChecks.cmake | 1 config.h.cmake | 3 -- goo/grandom.cc | 58 ++++++++++++-------------------------------------- goo/grandom.h | 20 +++-------------- 4 files changed, 19 insertions(+), 63 deletions(-)
New commits: commit c46717a70341ac0120579c867d49c250bed4ed52 Author: Adam Reichold <[email protected]> Date: Fri Aug 31 23:05:05 2018 +0200 Use the C++ standard library facilities for thread-safe random number generation avoiding the need for explicit configure checks. diff --git a/ConfigureChecks.cmake b/ConfigureChecks.cmake index 21bad506..6a910394 100644 --- a/ConfigureChecks.cmake +++ b/ConfigureChecks.cmake @@ -29,7 +29,6 @@ check_function_exists(gettimeofday HAVE_GETTIMEOFDAY) check_function_exists(localtime_r HAVE_LOCALTIME_R) check_function_exists(popen HAVE_POPEN) check_function_exists(mkstemp HAVE_MKSTEMP) -check_function_exists(rand_r HAVE_RAND_R) check_function_exists(strcpy_s HAVE_STRCPY_S) check_function_exists(strcat_s HAVE_STRCAT_S) check_function_exists(strtok_r HAVE_STRTOK_R) diff --git a/config.h.cmake b/config.h.cmake index 194ccc41..74355a69 100644 --- a/config.h.cmake +++ b/config.h.cmake @@ -82,9 +82,6 @@ /* Define to 1 if you have the `mkstemp' function. */ #cmakedefine HAVE_MKSTEMP 1 -/* Define to 1 if you have the `rand_r' function. */ -#cmakedefine HAVE_RAND_R 1 - /* Define to 1 if you have the `strcpy_s' function. */ #cmakedefine HAVE_STRCPY_S 1 diff --git a/goo/grandom.cc b/goo/grandom.cc index 3171af85..a9a28a59 100644 --- a/goo/grandom.cc +++ b/goo/grandom.cc @@ -8,63 +8,35 @@ * Copyright (C) 2012 Fabio D'Urso <[email protected]> */ -#include <config.h> #include "grandom.h" -#include "gtypes.h" -#ifdef HAVE_RAND_R // rand_r backend (POSIX) +#include <random> -static GBool initialized = gFalse; - -#include <stdlib.h> -#include <time.h> -static unsigned int seed; - -static void initialize() { - if (!initialized) { - seed = time(nullptr); - initialized = gTrue; - } -} - -void grandom_fill(Guchar *buff, int size) +namespace { - initialize(); - while (size--) - *buff++ = rand_r(&seed) % 256; -} -double grandom_double() +auto& grandom_engine() { - initialize(); - return rand_r(&seed) / (1 + (double)RAND_MAX); + static thread_local std::independent_bits_engine<std::default_random_engine, std::numeric_limits<Guchar>::digits, Guchar> engine{ + std::default_random_engine{ + std::random_device{}() + } + }; + return engine; } -#else // srand+rand backend (unsafe, because it may interfere with the application) - -static GBool initialized = gFalse; - -#include <stdlib.h> -#include <time.h> - -static void initialize() { - if (!initialized) { - srand(time(nullptr)); - initialized = gTrue; - } } void grandom_fill(Guchar *buff, int size) { - initialize(); - while (size--) - *buff++ = rand() % 256; + auto& engine = grandom_engine(); + for (int index = 0; index < size; ++index) { + buff[index] = engine(); + } } double grandom_double() { - initialize(); - return rand() / (1 + (double)RAND_MAX); + auto& engine = grandom_engine(); + return std::generate_canonical<double, std::numeric_limits<double>::digits>(engine); } - -#endif diff --git a/goo/grandom.h b/goo/grandom.h index 45fa791a..b70dd7e2 100644 --- a/goo/grandom.h +++ b/goo/grandom.h @@ -13,22 +13,10 @@ #include "gtypes.h" -#ifdef __cplusplus -extern "C" { -#endif - -/* - * Fills the given buffer with random bytes - */ -extern void grandom_fill(Guchar *buff, int size); +/// Fills the given buffer with random bytes +void grandom_fill(Guchar *buff, int size); -/* - * Returns a random number in [0,1) - */ -extern double grandom_double(); - -#ifdef __cplusplus -} -#endif +/// Returns a random number in [0,1) +double grandom_double(); #endif _______________________________________________ poppler mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/poppler
