diff options
author | bbudge@chromium.org <bbudge@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-02-28 21:35:44 +0000 |
---|---|---|
committer | bbudge@chromium.org <bbudge@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-02-28 21:35:44 +0000 |
commit | 380d832903da7ecd90f22bc93e128e52366575d7 (patch) | |
tree | a70683637eb82a57daefc701fe5a1b076a518e9c /base/rand_util_nacl.cc | |
parent | 3aa8b49dc9d29a5528753962c0760e34f8d63832 (diff) | |
download | chromium_src-380d832903da7ecd90f22bc93e128e52366575d7.zip chromium_src-380d832903da7ecd90f22bc93e128e52366575d7.tar.gz chromium_src-380d832903da7ecd90f22bc93e128e52366575d7.tar.bz2 |
Make base::Logging compile with the NaCl toolchain.
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/9474034
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@124025 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/rand_util_nacl.cc')
-rw-r--r-- | base/rand_util_nacl.cc | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/base/rand_util_nacl.cc b/base/rand_util_nacl.cc new file mode 100644 index 0000000..e885465 --- /dev/null +++ b/base/rand_util_nacl.cc @@ -0,0 +1,54 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/rand_util.h" +#include "base/rand_util_c.h" + +#include "base/lazy_instance.h" +#include "base/logging.h" + +// TODO(bbudge) Replace this with a proper system header file when NaCl +// provides one. +#include "native_client/src/untrusted/irt/irt.h" + +namespace { + +// Create a wrapper class so we can cache the NaCl random number interface. +class URandomInterface { + public: + URandomInterface() { + size_t result = nacl_interface_query(NACL_IRT_RANDOM_v0_1, + &interface_, + sizeof(interface_)); + DCHECK_EQ(result, sizeof(interface_)) << "Can't get random interface."; + } + + uint64 get_random_bytes() const { + size_t nbytes; + uint64 result; + int error = interface_.get_random_bytes(&result, + sizeof(result), + &nbytes); + DCHECK_EQ(error, 0); + DCHECK_EQ(nbytes, sizeof(result)); + return result; + } + + private: + struct nacl_irt_random interface_; +}; + +base::LazyInstance<URandomInterface> g_urandom_interface = + LAZY_INSTANCE_INITIALIZER; + +} // namespace + +namespace base { + +uint64 RandUint64() { + return g_urandom_interface.Pointer()->get_random_bytes(); +} + +} // namespace base + |