]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Portability fix: detect c++11 random support and implement fallbacks if not available
authorFrancesco Chemolli <kinkie@squid-cache.org>
Tue, 25 Aug 2015 14:36:54 +0000 (16:36 +0200)
committerFrancesco Chemolli <kinkie@squid-cache.org>
Tue, 25 Aug 2015 14:36:54 +0000 (16:36 +0200)
12 files changed:
acinclude/ax_cxx_0x_types.m4
compat/types.h
helpers/basic_auth/RADIUS/basic_radius_auth.cc
lib/hash.cc
lib/ntlmauth/ntlmauth.cc
src/acl/Random.cc
src/auth/digest/Config.cc
src/event.cc
src/fs/ufs/UFSSwapDir.cc
src/tests/SBufFindTest.cc
test-suite/hash.c
test-suite/splay.cc

index 6707805a03507f1561fcb0128dcbd76b8bdf28cf..270d378e2f1e3bdc753864a567aec0a5a476e265 100644 (file)
@@ -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<T> is supported])
-  AC_TRY_COMPILE([#include <random>],[std::uniform_int_distribution<int> 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<T> is supported])
-  fi
-  AC_MSG_CHECKING([whether std::uniform_real_distribution<T> is supported])
-  AC_TRY_COMPILE([#include <random>],[std::uniform_real_distribution<double> 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<T> is supported])
-  fi
+  AC_CHECK_HEADERS(tr1/random)
+  AC_CACHE_CHECK([whether std::uniform_int_distribution<T> is supported],
+                 [squid_cv_std_uniform_int_distribution_works],[
+    AC_TRY_COMPILE([#include <random>],[std::uniform_int_distribution<int> 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<T> is supported],
+                 [squid_cv_std_uniform_real_distribution_works],[
+    AC_REQUIRE([AC_PROG_CXX])
+    AC_LANG_PUSH([C++])
+    AC_TRY_COMPILE([#include <random>],[std::uniform_real_distribution<double> 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
 ])
index 6de19f5e94d98fae9fb705474b1a77a93a72a2f2..c04777d9a47e900f4123a82d95fe6e4e7da0e260 100644 (file)
 #include <netinet/in_systm.h>
 #endif
 
+#if __cplusplus && HAVE_TR1_RANDOM
+#if !HAVE_STD_UNIFORM_INT_DISTRIBUTION && !HAVE_STD_UNIFORM_REAL_DISTRIBUTION
+#include <tr1/random>
+#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 */
 
index ed3d2eefc0ffd9d94713dc0a820001ecb04c751c..76751a752378c2adc5abb4a8e46e5aef0721ad66 100644 (file)
@@ -207,7 +207,7 @@ static void
 random_vector(char *aVector)
 {
     static std::mt19937 mt(time(0));
-    static std::uniform_int_distribution<uint8_t> dist;
+    static xuniform_int_distribution<uint8_t> dist;
 
     for (int i = 0; i < AUTH_VECTOR_LEN; ++i)
         aVector[i] = static_cast<char>(dist(mt) & 0xFF);
index 7a6c68e48d22513d73d4dd001b3aa268ded863bf..adbb5507137e93c66df6d7804d2338c6b24bba60 100644 (file)
@@ -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';
index 52f1757133736399f1c1b90f0da38c42314d32d6..b3c652849f9119ccd9719b7b034d3b476d9ac45e 100644 (file)
@@ -185,7 +185,7 @@ void
 ntlm_make_nonce(char *nonce)
 {
     static std::mt19937 mt(time(0));
-    static std::uniform_int_distribution<uint8_t> dist;
+    static xuniform_int_distribution<uint8_t> dist;
 
     for (int i = 0; i < NTLM_NONCE_LEN; ++i)
         nonce[i] = static_cast<char>(dist(mt) & 0xFF);
index 22b21ed036ffa936f5bc5dc8eec6be819aab65e2..00794619bc5fab36a2677e8df9ef57f389871801 100644 (file)
@@ -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);
 
index 955f6702280493753de793a164a8d57a70e4f914..011cfca6ae9fd77298739a790e240c6fa7803dc4 100644 (file)
@@ -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<uint32_t>(getCurrentTime() & 0xFFFFFFFF));
-    static std::uniform_int_distribution<uint32_t> newRandomData;
+    static xuniform_int_distribution<uint32_t> newRandomData;
 
     /* create a new nonce */
     newnonce->nc = 0;
index eae7fd0641797ce371e4bf06e038b2dea65f1e49..b2978c4216aaeb3596e6ecb78c825eeea592144d 100644 (file)
@@ -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);
     }
 
index 75db77f99bf9f64763b97c7535a90e4557d56886..eb03a0531fa1a5725e1226fe768ec11f85419680 100644 (file)
@@ -1044,7 +1044,7 @@ Fs::Ufs::UFSSwapDir::CleanEvent(void *)
          * swap directories
          */
         std::mt19937 mt(static_cast<uint32_t>(getCurrentTime() & 0xFFFFFFFF));
-        std::uniform_int_distribution<> dist(0, j);
+        xuniform_int_distribution<> dist(0, j);
         swap_index = dist(mt);
     }
 
index a5245e5d82697b09a769e6bfc0ce0fb02b6c9161..a7fd35bca09958641610aa5edcf15cf5b6452c51 100644 (file)
@@ -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<uint8_t> dist(0, sizeof(characters)-2);
+    static xuniform_int_distribution<uint8_t> dist(0, sizeof(characters)-2);
 
     SBuf buf;
     buf.reserveCapacity(length);
index 52eaa33f0f094da8e8b3362df79ba1faeb0850d7..1d03e62de9730ce810ddf1285af0ca9651529a55 100644 (file)
@@ -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';
index 10d1996fa19a00bf81f208efba63aca5d8ad081f..a04f9fe6ec391d15f94538b01757fbb2ff1a8526 100644 (file)
@@ -130,7 +130,7 @@ int
 main(int argc, char *argv[])
 {
     std::mt19937 generator;
-    std::uniform_int_distribution<int> distribution;
+    xuniform_int_distribution<int> distribution;
     auto nextRandom = std::bind (distribution, generator);
 
     {