Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===-------------------------- random.cpp --------------------------------===// |
| 2 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Marshall Clow | b8dd5ca | 2013-10-09 21:49:03 +0000 | [diff] [blame] | 10 | #if defined(_WIN32) |
| 11 | // Must be defined before including stdlib.h to enable rand_s(). |
| 12 | #define _CRT_RAND_S |
| 13 | #include <stdio.h> |
| 14 | #endif |
| 15 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 16 | #include "random" |
| 17 | #include "system_error" |
| 18 | |
David Chisnall | 997e454 | 2012-02-29 13:05:08 +0000 | [diff] [blame] | 19 | #ifdef __sun__ |
| 20 | #define rename solaris_headers_are_broken |
| 21 | #endif |
Yaron Keren | 81241a9 | 2013-11-18 21:30:19 +0000 | [diff] [blame] | 22 | #if !defined(_WIN32) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 23 | #include <fcntl.h> |
| 24 | #include <unistd.h> |
Yaron Keren | 81241a9 | 2013-11-18 21:30:19 +0000 | [diff] [blame] | 25 | #endif // defined(_WIN32) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 26 | #include <errno.h> |
| 27 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 28 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 29 | |
Marshall Clow | b8dd5ca | 2013-10-09 21:49:03 +0000 | [diff] [blame] | 30 | #if defined(_WIN32) |
| 31 | random_device::random_device(const string&) |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | random_device::~random_device() |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | unsigned |
| 40 | random_device::operator()() |
| 41 | { |
| 42 | unsigned r; |
| 43 | errno_t err = rand_s(&r); |
| 44 | if (err) |
| 45 | __throw_system_error(err, "random_device rand_s failed."); |
| 46 | return r; |
| 47 | } |
| 48 | #else |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 49 | random_device::random_device(const string& __token) |
| 50 | : __f_(open(__token.c_str(), O_RDONLY)) |
| 51 | { |
David Majnemer | 8db32cc | 2014-06-03 02:21:37 +0000 | [diff] [blame] | 52 | if (__f_ < 0) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 53 | __throw_system_error(errno, ("random_device failed to open " + __token).c_str()); |
| 54 | } |
| 55 | |
| 56 | random_device::~random_device() |
| 57 | { |
| 58 | close(__f_); |
| 59 | } |
| 60 | |
| 61 | unsigned |
| 62 | random_device::operator()() |
| 63 | { |
| 64 | unsigned r; |
David Majnemer | 4d9f97b | 2014-06-03 02:40:39 +0000 | [diff] [blame] | 65 | size_t n = sizeof(r); |
| 66 | char* p = reinterpret_cast<char*>(&r); |
| 67 | while (n > 0) |
| 68 | { |
| 69 | ssize_t s = read(__f_, p, n); |
| 70 | if (s == 0) |
| 71 | __throw_system_error(ENODATA, "random_device got EOF"); |
| 72 | if (s == -1) |
| 73 | { |
| 74 | if (errno != EINTR) |
| 75 | __throw_system_error(errno, "random_device got an unexpected error"); |
| 76 | continue; |
| 77 | } |
| 78 | n -= static_cast<size_t>(s); |
| 79 | p += static_cast<size_t>(s); |
| 80 | } |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 81 | return r; |
| 82 | } |
Marshall Clow | b8dd5ca | 2013-10-09 21:49:03 +0000 | [diff] [blame] | 83 | #endif // defined(_WIN32) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 84 | |
| 85 | double |
Howard Hinnant | c83960a | 2012-07-20 21:44:27 +0000 | [diff] [blame] | 86 | random_device::entropy() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 87 | { |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | _LIBCPP_END_NAMESPACE_STD |