| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "vm/random.h" | 5 #include "vm/random.h" |
| 6 #include "vm/dart.h" |
| 6 #include "vm/flags.h" | 7 #include "vm/flags.h" |
| 7 #include "vm/isolate.h" | 8 #include "vm/os.h" |
| 8 | 9 |
| 9 namespace dart { | 10 namespace dart { |
| 10 | 11 |
| 11 DEFINE_FLAG(int, random_seed, 0, "Override the random seed for debugging."); | 12 DEFINE_FLAG(int, random_seed, 0, "Override the random seed for debugging."); |
| 12 | 13 |
| 13 Random::Random() { | 14 Random::Random() { |
| 14 uint64_t seed = FLAG_random_seed; | 15 uint64_t seed = FLAG_random_seed; |
| 15 if (seed == 0) { | 16 if (seed == 0) { |
| 16 Dart_EntropySource callback = Isolate::entropy_source_callback(); | 17 Dart_EntropySource callback = Dart::entropy_source_callback(); |
| 17 if (callback != NULL) { | 18 if (callback != NULL) { |
| 18 if (!callback(reinterpret_cast<uint8_t*>(&seed), sizeof(seed))) { | 19 if (!callback(reinterpret_cast<uint8_t*>(&seed), sizeof(seed))) { |
| 19 // Callback failed. Reset the seed to 0. | 20 // Callback failed. Reset the seed to 0. |
| 20 seed = 0; | 21 seed = 0; |
| 21 } | 22 } |
| 22 } | 23 } |
| 23 } | 24 } |
| 24 if (seed == 0) { | 25 if (seed == 0) { |
| 25 // We did not get a seed so far. As a fallback we do use the current time. | 26 // We did not get a seed so far. As a fallback we do use the current time. |
| 26 seed = OS::GetCurrentTimeMicros(); | 27 seed = OS::GetCurrentTimeMicros(); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 } | 63 } |
| 63 | 64 |
| 64 | 65 |
| 65 uint32_t Random::NextUInt32() { | 66 uint32_t Random::NextUInt32() { |
| 66 const uint64_t MASK_32 = 0xffffffff; | 67 const uint64_t MASK_32 = 0xffffffff; |
| 67 NextState(); | 68 NextState(); |
| 68 return static_cast<uint32_t>(_state & MASK_32); | 69 return static_cast<uint32_t>(_state & MASK_32); |
| 69 } | 70 } |
| 70 | 71 |
| 71 } // namespace dart | 72 } // namespace dart |
| OLD | NEW |