tvl-depot/absl/container/internal/inlined_vector.h
Abseil Team eab2078b53 Export of internal Abseil changes.
--
8b7c3bc2fb69608e9b2389b1be0b0de840a4c59d by Derek Mauro <dmauro@google.com>:

Set correct flags for clang-cl.
https://github.com/abseil/abseil-cpp/pull/278

clang-cl produce binaries with MSVC ABI and wants to be as
flag-compatible with pure MSVC as possible, so this leads to all sorts
of weird cases.

clang-cl alias /Wall as clang's -Weverything which is way too verbose,
so it needs /W3 like pure MSVC.

clang-cl only understand GCC style warning flags (-W[no]blah) and just
silent drop MSVC style warning flags (/wd[num]).

clang-cl needs MSVC define flags since it is consuming the same header
files as pure MSVC.

CMake set CMAKE_CXX_COMPILER_ID as Clang when clang-cl is detected, so
need extra if (MSVC) to differentiate it.

We are not doing clang-cl specialization in Bazel as currently there
is no reliable way to detect clang-cl in Bazel..

Other changes:
Add ABSL_ prefix to variable names to avoid name collision in CMake.

PiperOrigin-RevId: 239841297

--
add96c3fc067d5c7b6f016d2ba74725a443a185e by CJ Johnson <johnsoncj@google.com>:

Eventually Storage will need to refer to the type `absl::InlinedVector<...>*`. This can be done via a forward declaration. However, doing so would move the defaulted allocator template parameter to the forward declaration and thus inside an internal file. Instead of doing that, this change gives Storage access to the template and it's parameters so the complete type can be formed without including it.

PiperOrigin-RevId: 239811298

--
b5f5279f1b13b09cae5c745597d64ea1efab146b by CJ Johnson <johnsoncj@google.com>:

Simplify/cleanup the benchmark tests for InlinedVector

PiperOrigin-RevId: 239805767

--
f5991e51b43b13a0ae95025474071f5039a33d27 by Matt Calabrese <calabrese@google.com>:

Update the internal-only IsSwappable traits to be nested inside of namespace absl so that the script to add inline namespaces for LTS releases works with the implementation.

PiperOrigin-RevId: 239622024

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

Mutex: fix tsan annotations

This fixes 2 bugs:
1. We call cond directly in Mutex::AwaitCommon without using EvalConditionAnnotated. As the result we call into user code ignoring synchronization, miss synchronization and report false positives later. Use EvalConditionAnnotated to call cond as we should.

2. We call Mutex invariant ignoring synchronization. Result is the same: we miss synchronization and report false positive races later. Reuse EvalConditionAnnotated to call mutex invariant too.

PiperOrigin-RevId: 239583878

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

Clarify how to obtain the same behavior as std::unordered_map::erase if need be.

PiperOrigin-RevId: 239549513

--
6e76e68ed084fd1247981dbb92677ce8e563b0ec by Jon Cohen <cohenjon@google.com>:

Avoid the -S -B form of `cmake` since it's only supported starting in CMake 3.13

PiperOrigin-RevId: 239473143
GitOrigin-RevId: 8b7c3bc2fb69608e9b2389b1be0b0de840a4c59d
Change-Id: Ib6d356fa1a7435260273df991e65df4149bd5861
2019-03-22 15:30:00 -04:00

126 lines
4.3 KiB
C++

// Copyright 2019 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_CONTAINER_INTERNAL_INLINED_VECTOR_INTERNAL_H_
#define ABSL_CONTAINER_INTERNAL_INLINED_VECTOR_INTERNAL_H_
#include <cstddef>
#include <iterator>
#include <memory>
#include "absl/meta/type_traits.h"
namespace absl {
namespace inlined_vector_internal {
template <typename InlinedVector>
class Storage;
template <template <typename, size_t, typename> class InlinedVector, typename T,
size_t N, typename A>
class Storage<InlinedVector<T, N, A>> {
public:
using allocator_type = A;
using value_type = typename allocator_type::value_type;
using pointer = typename allocator_type::pointer;
using const_pointer = typename allocator_type::const_pointer;
using reference = typename allocator_type::reference;
using const_reference = typename allocator_type::const_reference;
using rvalue_reference = typename allocator_type::value_type&&;
using size_type = typename allocator_type::size_type;
using difference_type = typename allocator_type::difference_type;
using iterator = pointer;
using const_iterator = const_pointer;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
explicit Storage(const allocator_type& a) : allocator_and_tag_(a) {}
// TODO(johnsoncj): Make the below types and members private after migration
// Holds whether the vector is allocated or not in the lowest bit and the size
// in the high bits:
// `size_ = (size << 1) | is_allocated;`
class Tag {
size_type size_;
public:
Tag() : size_(0) {}
size_type size() const { return size_ / 2; }
void add_size(size_type n) { size_ += n * 2; }
void set_inline_size(size_type n) { size_ = n * 2; }
void set_allocated_size(size_type n) { size_ = (n * 2) + 1; }
bool allocated() const { return size_ % 2; }
};
// Derives from `allocator_type` to use the empty base class optimization.
// If the `allocator_type` is stateless, we can store our instance for free.
class AllocatorAndTag : private allocator_type {
Tag tag_;
public:
explicit AllocatorAndTag(const allocator_type& a) : allocator_type(a) {}
Tag& tag() { return tag_; }
const Tag& tag() const { return tag_; }
allocator_type& allocator() { return *this; }
const allocator_type& allocator() const { return *this; }
};
class Allocation {
size_type capacity_;
pointer buffer_;
public:
Allocation(allocator_type& a, size_type capacity)
: capacity_(capacity), buffer_(Create(a, capacity)) {}
void Dealloc(allocator_type& a) {
std::allocator_traits<allocator_type>::deallocate(a, buffer_, capacity_);
}
size_type capacity() const { return capacity_; }
const_pointer buffer() const { return buffer_; }
pointer buffer() { return buffer_; }
static pointer Create(allocator_type& a, size_type n) {
return std::allocator_traits<allocator_type>::allocate(a, n);
}
};
// Stores either the inlined or allocated representation
union Rep {
using ValueTypeBuffer =
absl::aligned_storage_t<sizeof(value_type), alignof(value_type)>;
using AllocationBuffer =
absl::aligned_storage_t<sizeof(Allocation), alignof(Allocation)>;
// Structs wrap the buffers to perform indirection that solves a bizarre
// compilation error on Visual Studio (all known versions).
struct InlinedRep {
ValueTypeBuffer inlined[N];
};
struct AllocatedRep {
AllocationBuffer allocation;
};
InlinedRep inlined_storage;
AllocatedRep allocation_storage;
};
AllocatorAndTag allocator_and_tag_;
Rep rep_;
};
} // namespace inlined_vector_internal
} // namespace absl
#endif // ABSL_CONTAINER_INTERNAL_INLINED_VECTOR_INTERNAL_H_