[libcxx] r192325 - patch by Yaron: Uses rand_s() from stdlib.h (when building for Windows)

Marshall Clow mclow.lists at gmail.com
Wed Oct 9 14:49:03 PDT 2013


Author: marshall
Date: Wed Oct  9 16:49:03 2013
New Revision: 192325

URL: http://llvm.org/viewvc/llvm-project?rev=192325&view=rev
Log:
patch by Yaron: Uses rand_s() from stdlib.h (when building for Windows)

Modified:
    libcxx/trunk/include/random
    libcxx/trunk/src/random.cpp

Modified: libcxx/trunk/include/random
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/random?rev=192325&r1=192324&r2=192325&view=diff
==============================================================================
--- libcxx/trunk/include/random (original)
+++ libcxx/trunk/include/random Wed Oct  9 16:49:03 2013
@@ -3475,7 +3475,9 @@ typedef shuffle_order_engine<minstd_rand
 
 class _LIBCPP_TYPE_VIS random_device
 {
+#if !defined(_WIN32)
     int __f_;
+#endif // defined(_WIN32)
 public:
     // types
     typedef unsigned result_type;

Modified: libcxx/trunk/src/random.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/src/random.cpp?rev=192325&r1=192324&r2=192325&view=diff
==============================================================================
--- libcxx/trunk/src/random.cpp (original)
+++ libcxx/trunk/src/random.cpp Wed Oct  9 16:49:03 2013
@@ -7,6 +7,12 @@
 //
 //===----------------------------------------------------------------------===//
 
+#if defined(_WIN32)
+// Must be defined before including stdlib.h to enable rand_s().
+#define _CRT_RAND_S
+#include <stdio.h>
+#endif
+
 #include "random"
 #include "system_error"
 
@@ -19,6 +25,25 @@
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
+#if defined(_WIN32)
+random_device::random_device(const string&)
+{
+}
+
+random_device::~random_device()
+{
+}
+
+unsigned
+random_device::operator()()
+{
+    unsigned r;
+    errno_t err = rand_s(&r);
+    if (err)
+        __throw_system_error(err, "random_device rand_s failed.");
+    return r;
+}
+#else
 random_device::random_device(const string& __token)
     : __f_(open(__token.c_str(), O_RDONLY))
 {
@@ -38,6 +63,7 @@ random_device::operator()()
     read(__f_, &r, sizeof(r));
     return r;
 }
+#endif // defined(_WIN32)
 
 double
 random_device::entropy() const _NOEXCEPT





More information about the cfe-commits mailing list