Export of internal Abseil changes.

--
b01400905d2ba23ec9f4541153532eefcfb0d5f5 by Mark Barolak <mbar@google.com>:

Tweak the comment for WebSafeBase64Unescape to make it match the style of the other comments in escaping.h.

PiperOrigin-RevId: 209596034

--
dd35c55ee538ed458bb32494d87996d6624676df by Abseil Team <absl-team@google.com>:

Migrate FixedArray from allocator pointers to references. Also updates the name to be more generic as nothing about the implementation was specific to FixedArray's StorageElement*

PiperOrigin-RevId: 209438135
GitOrigin-RevId: b01400905d2ba23ec9f4541153532eefcfb0d5f5
Change-Id: I27304e4609708ec24fb19dce1e33215d7e4b5ae9
This commit is contained in:
Abseil Team 2018-08-21 08:05:11 -07:00 committed by Derek Mauro
parent d8cfe9f2a7
commit fefc83638f
3 changed files with 41 additions and 38 deletions

View file

@ -138,8 +138,8 @@ class FixedArray {
explicit FixedArray(size_type n, const allocator_type& a = allocator_type()) explicit FixedArray(size_type n, const allocator_type& a = allocator_type())
: storage_(n, a) { : storage_(n, a) {
if (DefaultConstructorIsNonTrivial()) { if (DefaultConstructorIsNonTrivial()) {
memory_internal::ConstructStorage(storage_.alloc(), storage_.begin(), memory_internal::ConstructRange(storage_.alloc(), storage_.begin(),
storage_.end()); storage_.end());
} }
} }
@ -147,8 +147,8 @@ class FixedArray {
FixedArray(size_type n, const value_type& val, FixedArray(size_type n, const value_type& val,
const allocator_type& a = allocator_type()) const allocator_type& a = allocator_type())
: storage_(n, a) { : storage_(n, a) {
memory_internal::ConstructStorage(storage_.alloc(), storage_.begin(), memory_internal::ConstructRange(storage_.alloc(), storage_.begin(),
storage_.end(), val); storage_.end(), val);
} }
// Creates an array initialized with the size and contents of `init_list`. // Creates an array initialized with the size and contents of `init_list`.
@ -163,13 +163,12 @@ class FixedArray {
FixedArray(Iterator first, Iterator last, FixedArray(Iterator first, Iterator last,
const allocator_type& a = allocator_type()) const allocator_type& a = allocator_type())
: storage_(std::distance(first, last), a) { : storage_(std::distance(first, last), a) {
memory_internal::CopyToStorageFromRange(storage_.alloc(), storage_.begin(), memory_internal::CopyRange(storage_.alloc(), storage_.begin(), first, last);
first, last);
} }
~FixedArray() noexcept { ~FixedArray() noexcept {
for (auto* cur = storage_.begin(); cur != storage_.end(); ++cur) { for (auto* cur = storage_.begin(); cur != storage_.end(); ++cur) {
AllocatorTraits::destroy(*storage_.alloc(), cur); AllocatorTraits::destroy(storage_.alloc(), cur);
} }
} }
@ -446,15 +445,15 @@ class FixedArray {
if (UsingInlinedStorage(size())) { if (UsingInlinedStorage(size())) {
InlinedStorage::AnnotateDestruct(size()); InlinedStorage::AnnotateDestruct(size());
} else { } else {
AllocatorTraits::deallocate(*alloc(), AsValueType(begin()), size()); AllocatorTraits::deallocate(alloc(), AsValueType(begin()), size());
} }
} }
size_type size() const { return size_alloc_.template get<0>(); } size_type size() const { return size_alloc_.template get<0>(); }
StorageElement* begin() const { return data_; } StorageElement* begin() const { return data_; }
StorageElement* end() const { return begin() + size(); } StorageElement* end() const { return begin() + size(); }
allocator_type* alloc() { allocator_type& alloc() {
return std::addressof(size_alloc_.template get<1>()); return size_alloc_.template get<1>();
} }
private: private:
@ -468,7 +467,7 @@ class FixedArray {
return InlinedStorage::data(); return InlinedStorage::data();
} else { } else {
return reinterpret_cast<StorageElement*>( return reinterpret_cast<StorageElement*>(
AllocatorTraits::allocate(*alloc(), size())); AllocatorTraits::allocate(alloc(), size()));
} }
} }

View file

@ -641,55 +641,59 @@ struct default_allocator_is_nothrow : std::false_type {};
#endif #endif
namespace memory_internal { namespace memory_internal {
#ifdef ABSL_HAVE_EXCEPTIONS #ifdef ABSL_HAVE_EXCEPTIONS // ConstructRange
template <typename Allocator, typename StorageElement, typename... Args> template <typename Allocator, typename Iterator, typename... Args>
void ConstructStorage(Allocator* alloc, StorageElement* first, void ConstructRange(Allocator& alloc, Iterator first, Iterator last,
StorageElement* last, const Args&... args) { const Args&... args) {
for (StorageElement* cur = first; cur != last; ++cur) { for (Iterator cur = first; cur != last; ++cur) {
try { try {
std::allocator_traits<Allocator>::construct(*alloc, cur, args...); std::allocator_traits<Allocator>::construct(alloc, cur, args...);
} catch (...) { } catch (...) {
while (cur != first) { while (cur != first) {
--cur; --cur;
std::allocator_traits<Allocator>::destroy(*alloc, cur); std::allocator_traits<Allocator>::destroy(alloc, cur);
} }
throw; throw;
} }
} }
} }
template <typename Allocator, typename StorageElement, typename Iterator> #else // ABSL_HAVE_EXCEPTIONS // ConstructRange
void CopyToStorageFromRange(Allocator* alloc, StorageElement* destination, template <typename Allocator, typename Iterator, typename... Args>
Iterator first, Iterator last) { void ConstructRange(Allocator& alloc, Iterator first, Iterator last,
for (StorageElement* cur = destination; first != last; const Args&... args) {
for (; first != last; ++first) {
std::allocator_traits<Allocator>::construct(alloc, first, args...);
}
}
#endif // ABSL_HAVE_EXCEPTIONS // ConstructRange
#ifdef ABSL_HAVE_EXCEPTIONS // CopyRange
template <typename Allocator, typename Iterator, typename InputIterator>
void CopyRange(Allocator& alloc, Iterator destination, InputIterator first,
InputIterator last) {
for (Iterator cur = destination; first != last;
static_cast<void>(++cur), static_cast<void>(++first)) { static_cast<void>(++cur), static_cast<void>(++first)) {
try { try {
std::allocator_traits<Allocator>::construct(*alloc, cur, *first); std::allocator_traits<Allocator>::construct(alloc, cur, *first);
} catch (...) { } catch (...) {
while (cur != destination) { while (cur != destination) {
--cur; --cur;
std::allocator_traits<Allocator>::destroy(*alloc, cur); std::allocator_traits<Allocator>::destroy(alloc, cur);
} }
throw; throw;
} }
} }
} }
#else // ABSL_HAVE_EXCEPTIONS #else // ABSL_HAVE_EXCEPTIONS // CopyRange
template <typename Allocator, typename StorageElement, typename... Args> template <typename Allocator, typename Iterator, typename InputIterator>
void ConstructStorage(Allocator* alloc, StorageElement* first, void CopyRange(Allocator& alloc, Iterator destination, InputIterator first,
StorageElement* last, const Args&... args) { InputIterator last) {
for (; first != last; ++first) {
std::allocator_traits<Allocator>::construct(*alloc, first, args...);
}
}
template <typename Allocator, typename StorageElement, typename Iterator>
void CopyToStorageFromRange(Allocator* alloc, StorageElement* destination,
Iterator first, Iterator last) {
for (; first != last; for (; first != last;
static_cast<void>(++destination), static_cast<void>(++first)) { static_cast<void>(++destination), static_cast<void>(++first)) {
std::allocator_traits<Allocator>::construct(*alloc, destination, *first); std::allocator_traits<Allocator>::construct(alloc, destination, *first);
} }
} }
#endif // ABSL_HAVE_EXCEPTIONS #endif // ABSL_HAVE_EXCEPTIONS // CopyRange
} // namespace memory_internal } // namespace memory_internal
} // namespace absl } // namespace absl

View file

@ -124,7 +124,7 @@ std::string Utf8SafeCHexEscape(absl::string_view src);
// characters, `dest` is cleared and returns `false`. // characters, `dest` is cleared and returns `false`.
bool Base64Unescape(absl::string_view src, std::string* dest); bool Base64Unescape(absl::string_view src, std::string* dest);
// WebSafeBase64Unescape(absl::string_view, std::string*) // WebSafeBase64Unescape()
// //
// Converts a `src` std::string encoded in Base64 to its binary equivalent, writing // Converts a `src` std::string encoded in Base64 to its binary equivalent, writing
// it to a `dest` buffer, but using '-' instead of '+', and '_' instead of '/'. // it to a `dest` buffer, but using '-' instead of '+', and '_' instead of '/'.