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 | |
| 10 | #include "random" |
| 11 | #include "system_error" |
| 12 | |
David Chisnall | 997e454 | 2012-02-29 13:05:08 +0000 | [diff] [blame] | 13 | #ifdef __sun__ |
| 14 | #define rename solaris_headers_are_broken |
| 15 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 16 | #include <fcntl.h> |
| 17 | #include <unistd.h> |
| 18 | #include <errno.h> |
| 19 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 20 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 21 | |
| 22 | random_device::random_device(const string& __token) |
| 23 | : __f_(open(__token.c_str(), O_RDONLY)) |
| 24 | { |
| 25 | if (__f_ <= 0) |
| 26 | __throw_system_error(errno, ("random_device failed to open " + __token).c_str()); |
| 27 | } |
| 28 | |
| 29 | random_device::~random_device() |
| 30 | { |
| 31 | close(__f_); |
| 32 | } |
| 33 | |
| 34 | unsigned |
| 35 | random_device::operator()() |
| 36 | { |
| 37 | unsigned r; |
| 38 | read(__f_, &r, sizeof(r)); |
| 39 | return r; |
| 40 | } |
| 41 | |
| 42 | double |
Howard Hinnant | c83960a | 2012-07-20 21:44:27 +0000 | [diff] [blame^] | 43 | random_device::entropy() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 44 | { |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | _LIBCPP_END_NAMESPACE_STD |