tvl-depot/absl/random/internal/nonsecure_base.h
Abseil Team 12bc53e031 Export of internal Abseil changes
--
c99f979ad34f155fbeeea69b88bdc7458d89a21c by Derek Mauro <dmauro@google.com>:

Remove a floating point division by zero test.

This isn't testing behavior related to the library, and MSVC warns
about it in opt mode.

PiperOrigin-RevId: 285220804

--
68b015491f0dbf1ab547994673281abd1f34cd4b by Gennadiy Rozental <rogeeff@google.com>:

This CL introduces following changes to the class FlagImpl:
* We eliminate the CommandLineFlagLocks struct. Instead callback guard and callback function are combined into a single CallbackData struct, while primary data lock is stored separately.
* CallbackData member of class FlagImpl is initially set to be nullptr and is only allocated and initialized when a flag's callback is being set. For most flags we do not pay for the extra space and extra absl::Mutex now.
* Primary data guard is stored in data_guard_ data member. This is a properly aligned character buffer of necessary size. During initialization of the flag we construct absl::Mutex in this space using placement new call.
* We now avoid extra value copy after successful attempt to parse value out of string. Instead we swap flag's current value with tentative value we just produced.

PiperOrigin-RevId: 285132636

--
ed45d118fb818969eb13094cf7827c885dfc562c by Tom Manshreck <shreck@google.com>:

Change null-term* (and nul-term*) to NUL-term* in comments

PiperOrigin-RevId: 285036610

--
729619017944db895ce8d6d29c1995aa2e5628a5 by Derek Mauro <dmauro@google.com>:

Use the Posix implementation of thread identity on MinGW.
Some versions of MinGW suffer from thread_local bugs.

PiperOrigin-RevId: 285022920

--
39a25493503c76885bc3254c28f66a251c5b5bb0 by Greg Falcon <gfalcon@google.com>:

Implementation detail change.

Add further ABSL_NAMESPACE_BEGIN and _END annotation macros to files in Abseil.

PiperOrigin-RevId: 285012012
GitOrigin-RevId: c99f979ad34f155fbeeea69b88bdc7458d89a21c
Change-Id: I4c85d3704e45d11a9ac50d562f39640a6adbedc1
2019-12-12 15:37:13 -05:00

150 lines
4.9 KiB
C++

// Copyright 2017 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef ABSL_RANDOM_INTERNAL_NONSECURE_BASE_H_
#define ABSL_RANDOM_INTERNAL_NONSECURE_BASE_H_
#include <algorithm>
#include <cstdint>
#include <iostream>
#include <iterator>
#include <random>
#include <string>
#include <type_traits>
#include <vector>
#include "absl/base/macros.h"
#include "absl/meta/type_traits.h"
#include "absl/random/internal/pool_urbg.h"
#include "absl/random/internal/salted_seed_seq.h"
#include "absl/random/internal/seed_material.h"
#include "absl/types/optional.h"
#include "absl/types/span.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace random_internal {
// Each instance of NonsecureURBGBase<URBG> will be seeded by variates produced
// by a thread-unique URBG-instance.
template <typename URBG>
class NonsecureURBGBase {
public:
using result_type = typename URBG::result_type;
// Default constructor
NonsecureURBGBase() : urbg_(ConstructURBG()) {}
// Copy disallowed, move allowed.
NonsecureURBGBase(const NonsecureURBGBase&) = delete;
NonsecureURBGBase& operator=(const NonsecureURBGBase&) = delete;
NonsecureURBGBase(NonsecureURBGBase&&) = default;
NonsecureURBGBase& operator=(NonsecureURBGBase&&) = default;
// Constructor using a seed
template <class SSeq, typename = typename absl::enable_if_t<
!std::is_same<SSeq, NonsecureURBGBase>::value>>
explicit NonsecureURBGBase(SSeq&& seq)
: urbg_(ConstructURBG(std::forward<SSeq>(seq))) {}
// Note: on MSVC, min() or max() can be interpreted as MIN() or MAX(), so we
// enclose min() or max() in parens as (min)() and (max)().
// Additionally, clang-format requires no space before this construction.
// NonsecureURBGBase::min()
static constexpr result_type(min)() { return (URBG::min)(); }
// NonsecureURBGBase::max()
static constexpr result_type(max)() { return (URBG::max)(); }
// NonsecureURBGBase::operator()()
result_type operator()() { return urbg_(); }
// NonsecureURBGBase::discard()
void discard(unsigned long long values) { // NOLINT(runtime/int)
urbg_.discard(values);
}
bool operator==(const NonsecureURBGBase& other) const {
return urbg_ == other.urbg_;
}
bool operator!=(const NonsecureURBGBase& other) const {
return !(urbg_ == other.urbg_);
}
private:
// Seeder is a custom seed sequence type where generate() fills the provided
// buffer via the RandenPool entropy source.
struct Seeder {
using result_type = uint32_t;
size_t size() { return 0; }
template <typename OutIterator>
void param(OutIterator) const {}
template <typename RandomAccessIterator>
void generate(RandomAccessIterator begin, RandomAccessIterator end) {
if (begin != end) {
// begin, end must be random access iterators assignable from uint32_t.
generate_impl(
std::integral_constant<bool, sizeof(*begin) == sizeof(uint32_t)>{},
begin, end);
}
}
// Commonly, generate is invoked with a pointer to a buffer which
// can be cast to a uint32_t.
template <typename RandomAccessIterator>
void generate_impl(std::integral_constant<bool, true>,
RandomAccessIterator begin, RandomAccessIterator end) {
auto buffer = absl::MakeSpan(begin, end);
auto target = absl::MakeSpan(reinterpret_cast<uint32_t*>(buffer.data()),
buffer.size());
RandenPool<uint32_t>::Fill(target);
}
// The non-uint32_t case should be uncommon, and involves an extra copy,
// filling the uint32_t buffer and then mixing into the output.
template <typename RandomAccessIterator>
void generate_impl(std::integral_constant<bool, false>,
RandomAccessIterator begin, RandomAccessIterator end) {
const size_t n = std::distance(begin, end);
absl::InlinedVector<uint32_t, 8> data(n, 0);
RandenPool<uint32_t>::Fill(absl::MakeSpan(data.begin(), data.end()));
std::copy(std::begin(data), std::end(data), begin);
}
};
static URBG ConstructURBG() {
Seeder seeder;
return URBG(seeder);
}
template <typename SSeq>
static URBG ConstructURBG(SSeq&& seq) { // NOLINT(runtime/references)
auto salted_seq =
random_internal::MakeSaltedSeedSeq(std::forward<SSeq>(seq));
return URBG(salted_seq);
}
URBG urbg_;
};
} // namespace random_internal
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_RANDOM_INTERNAL_NONSECURE_BASE_H_