From: Francesco Chemolli Date: Tue, 25 Aug 2015 14:36:54 +0000 (+0200) Subject: Portability fix: detect c++11 random support and implement fallbacks if not available X-Git-Tag: SQUID_4_0_1~100 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=8ed8fa4019c31401dc6120e1381463fb5090c491;p=thirdparty%2Fsquid.git Portability fix: detect c++11 random support and implement fallbacks if not available --- diff --git a/acinclude/ax_cxx_0x_types.m4 b/acinclude/ax_cxx_0x_types.m4 index 6707805a03..270d378e2f 100644 --- a/acinclude/ax_cxx_0x_types.m4 +++ b/acinclude/ax_cxx_0x_types.m4 @@ -55,23 +55,28 @@ AC_DEFUN([AX_CXX_TYPE_UNIQUE_PTR],[ AC_DEFUN([AX_CXX_TYPE_UNIFORM_DISTRIBUTIONS],[ AC_REQUIRE([AC_PROG_CXX]) AC_LANG_PUSH([C++]) - AC_MSG_CHECKING([whether std::uniform_int_distribution is supported]) - AC_TRY_COMPILE([#include ],[std::uniform_int_distribution c;], [ - HAVE_UNIFORM_INT_DISTRIBUTION=yes - AC_MSG_RESULT(yes)], [ - HAVE_UNIFORM_INT_DISTRIBUTION=no - AC_MSG_RESULT(no)]) - if test "x$HAVE_UNIFORM_INT_DISTRIBUTION" = xno; then - AC_DEFINE(uniform_int_distributon, tr1::uniform_int, [Leave undefined if std::uniform_int_distribution is supported]) - fi - AC_MSG_CHECKING([whether std::uniform_real_distribution is supported]) - AC_TRY_COMPILE([#include ],[std::uniform_real_distribution c;], [ - HAVE_UNIFORM_REAL_DISTRIBUTION=yes - AC_MSG_RESULT(yes)], [ - HAVE_UNIFORM_REAL_DISTRIBUTION=no - AC_MSG_RESULT(no)]) - if test "x$HAVE_UNIFORM_REAL_DISTRIBUTION" = xno; then - AC_DEFINE(uniform_real_distributon, tr1::uniform_real, [Leave undefined if std::uniform_real_distribution is supported]) - fi + AC_CHECK_HEADERS(tr1/random) + AC_CACHE_CHECK([whether std::uniform_int_distribution is supported], + [squid_cv_std_uniform_int_distribution_works],[ + AC_TRY_COMPILE([#include ],[std::uniform_int_distribution c;], + [squid_cv_std_uniform_int_distribution_works=yes], + [squid_cv_std_uniform_int_distribution_works=no]) + ]) + SQUID_DEFINE_BOOL([HAVE_STD_UNIFORM_INT_DISTRIBUTION], + [$squid_cv_std_uniform_int_distribution_works], + [Define if c++11 std::uniform_int_distribution is supported]) + + AC_CACHE_CHECK([whether std::uniform_real_distribution is supported], + [squid_cv_std_uniform_real_distribution_works],[ + AC_REQUIRE([AC_PROG_CXX]) + AC_LANG_PUSH([C++]) + AC_TRY_COMPILE([#include ],[std::uniform_real_distribution c;], + [squid_cv_std_uniform_real_distribution_works=yes], + [squid_cv_std_uniform_real_distribution_works=no]) + ]) + SQUID_DEFINE_BOOL([HAVE_STD_UNIFORM_REAL_DISTRIBUTION], + [$squid_cv_std_uniform_real_distribution_works], + [Define if c++11 std::uniform_real_distribution is supported]) + AC_LANG_POP ]) diff --git a/compat/types.h b/compat/types.h index 6de19f5e94..c04777d9a4 100644 --- a/compat/types.h +++ b/compat/types.h @@ -45,6 +45,12 @@ #include #endif +#if __cplusplus && HAVE_TR1_RANDOM +#if !HAVE_STD_UNIFORM_INT_DISTRIBUTION && !HAVE_STD_UNIFORM_REAL_DISTRIBUTION +#include +#endif +#endif + /******************************************************/ /* Typedefs for missing entries on a system */ /******************************************************/ @@ -160,5 +166,20 @@ typedef long mtyp_t; #define NULL 0 #endif +/***********************************************************/ +/* uniform_int_distribution backward compatibility wrapper */ +/***********************************************************/ +#if HAVE_STD_UNIFORM_INT_DISTRIBUTION +#define xuniform_int_distribution std::uniform_int_distribution +#else +#define xuniform_int_distribution std::tr1::uniform_int +#endif + +#if HAVE_STD_UNIFORM_REAL_DISTRIBUTION +#define xuniform_real_distribution std::uniform_real_distribution +#else +#define xuniform_real_distribution std::tr1::uniform_real +#endif + #endif /* SQUID_TYPES_H */ diff --git a/helpers/basic_auth/RADIUS/basic_radius_auth.cc b/helpers/basic_auth/RADIUS/basic_radius_auth.cc index ed3d2eefc0..76751a7523 100644 --- a/helpers/basic_auth/RADIUS/basic_radius_auth.cc +++ b/helpers/basic_auth/RADIUS/basic_radius_auth.cc @@ -207,7 +207,7 @@ static void random_vector(char *aVector) { static std::mt19937 mt(time(0)); - static std::uniform_int_distribution dist; + static xuniform_int_distribution dist; for (int i = 0; i < AUTH_VECTOR_LEN; ++i) aVector[i] = static_cast(dist(mt) & 0xFF); diff --git a/lib/hash.cc b/lib/hash.cc index 7a6c68e48d..adbb550713 100644 --- a/lib/hash.cc +++ b/lib/hash.cc @@ -344,7 +344,7 @@ main(void) printf("done creating hash table: %d\n", hid); std::mt19937 mt; - std::uniform_int_distribution<> dist(0,16); + xuniform_int_distribution<> dist(0,16); while (fgets(buf, BUFSIZ, stdin)) { buf[strlen(buf) - 1] = '\0'; diff --git a/lib/ntlmauth/ntlmauth.cc b/lib/ntlmauth/ntlmauth.cc index 52f1757133..b3c652849f 100644 --- a/lib/ntlmauth/ntlmauth.cc +++ b/lib/ntlmauth/ntlmauth.cc @@ -185,7 +185,7 @@ void ntlm_make_nonce(char *nonce) { static std::mt19937 mt(time(0)); - static std::uniform_int_distribution dist; + static xuniform_int_distribution dist; for (int i = 0; i < NTLM_NONCE_LEN; ++i) nonce[i] = static_cast(dist(mt) & 0xFF); diff --git a/src/acl/Random.cc b/src/acl/Random.cc index 22b21ed036..00794619bc 100644 --- a/src/acl/Random.cc +++ b/src/acl/Random.cc @@ -108,7 +108,7 @@ ACLRandom::match(ACLChecklist *) // actually matching whether the random value is above // or below the configured threshold ratio. static std::mt19937 mt; - static std::uniform_real_distribution<> dist(0, 1); + static xuniform_real_distribution<> dist(0, 1); const double random = dist(mt); diff --git a/src/auth/digest/Config.cc b/src/auth/digest/Config.cc index 955f670228..011cfca6ae 100644 --- a/src/auth/digest/Config.cc +++ b/src/auth/digest/Config.cc @@ -159,7 +159,7 @@ authenticateDigestNonceNew(void) // NP: this will likely produce the same randomness sequences for each worker // since they should all start within the 1-second resolution of seed value. static std::mt19937 mt(static_cast(getCurrentTime() & 0xFFFFFFFF)); - static std::uniform_int_distribution newRandomData; + static xuniform_int_distribution newRandomData; /* create a new nonce */ newnonce->nc = 0; diff --git a/src/event.cc b/src/event.cc index eae7fd0641..b2978c4216 100644 --- a/src/event.cc +++ b/src/event.cc @@ -120,7 +120,7 @@ eventAddIsh(const char *name, EVH * func, void *arg, double delta_ish, int weigh // relative to each other to prevent waves of synchronised activity. static std::mt19937 rng; auto third = (delta_ish/3.0); - std::uniform_real_distribution<> thirdIsh(delta_ish - third, delta_ish + third); + xuniform_real_distribution<> thirdIsh(delta_ish - third, delta_ish + third); delta_ish = thirdIsh(rng); } diff --git a/src/fs/ufs/UFSSwapDir.cc b/src/fs/ufs/UFSSwapDir.cc index 75db77f99b..eb03a0531f 100644 --- a/src/fs/ufs/UFSSwapDir.cc +++ b/src/fs/ufs/UFSSwapDir.cc @@ -1044,7 +1044,7 @@ Fs::Ufs::UFSSwapDir::CleanEvent(void *) * swap directories */ std::mt19937 mt(static_cast(getCurrentTime() & 0xFFFFFFFF)); - std::uniform_int_distribution<> dist(0, j); + xuniform_int_distribution<> dist(0, j); swap_index = dist(mt); } diff --git a/src/tests/SBufFindTest.cc b/src/tests/SBufFindTest.cc index a5245e5d82..a7fd35bca0 100644 --- a/src/tests/SBufFindTest.cc +++ b/src/tests/SBufFindTest.cc @@ -385,7 +385,7 @@ SBufFindTest::RandomSBuf(const int length) // sizeof() counts the terminating zero at the end of characters // and the distribution is an 'inclusive' value range, so -2 // TODO: add \0 character (needs reporting adjustments to print it as \0) - static std::uniform_int_distribution dist(0, sizeof(characters)-2); + static xuniform_int_distribution dist(0, sizeof(characters)-2); SBuf buf; buf.reserveCapacity(length); diff --git a/test-suite/hash.c b/test-suite/hash.c index 52eaa33f0f..1d03e62de9 100644 --- a/test-suite/hash.c +++ b/test-suite/hash.c @@ -355,7 +355,7 @@ main(void) printf("done creating hash table: %d\n", hid); std::mt19937 mt; - std::uniform_int_distribution<> dist(0,16); + xuniform_int_distribution<> dist(0,16); while (fgets(buf, BUFSIZ, stdin)) { buf[strlen(buf) - 1] = '\0'; diff --git a/test-suite/splay.cc b/test-suite/splay.cc index 10d1996fa1..a04f9fe6ec 100644 --- a/test-suite/splay.cc +++ b/test-suite/splay.cc @@ -130,7 +130,7 @@ int main(int argc, char *argv[]) { std::mt19937 generator; - std::uniform_int_distribution distribution; + xuniform_int_distribution distribution; auto nextRandom = std::bind (distribution, generator); {