diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2022-03-04 12:53:38 +0100 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2022-03-08 14:08:15 +0000 |
commit | 27c38d2cc17e4ccfe9e77a8fc25e37ce7a0dfef6 (patch) | |
tree | c8814bb51cbbb960c5d4001e6e94326bd011fa63 | |
parent | 3501323d99ab82023d449a847196c31196b70662 (diff) | |
download | qtwebengine-chromium-27c38d2cc17e4ccfe9e77a8fc25e37ce7a0dfef6.tar.gz |
[Revert] base: Provide a way to customize fallback allocator for StackVector
Reverted patch reviewed on: https://chromium-review.googlesource.com/c/chromium/src/+/3310901
Change-Id: I3ef4f7bf3779ad10b1e0686f02ffa2bd9a1f95c1
Reviewed-by: Peter Varga <pvarga@inf.u-szeged.hu>
-rw-r--r-- | chromium/base/containers/stack_container.h | 64 | ||||
-rw-r--r-- | chromium/base/containers/stack_container_unittest.cc | 43 |
2 files changed, 30 insertions, 77 deletions
diff --git a/chromium/base/containers/stack_container.h b/chromium/base/containers/stack_container.h index de30d882abf..9aa94a14e9c 100644 --- a/chromium/base/containers/stack_container.h +++ b/chromium/base/containers/stack_container.h @@ -32,12 +32,11 @@ namespace base { // be sure to reserve() in the container up to the stack buffer size. Otherwise // the container will allocate a small array which will "use up" the stack // buffer. -template <typename T, size_t stack_capacity, typename FallbackAllocator> -class StackAllocator : public FallbackAllocator { +template<typename T, size_t stack_capacity> +class StackAllocator : public std::allocator<T> { public: - using pointer = typename std::allocator_traits<FallbackAllocator>::pointer; - using size_type = - typename std::allocator_traits<FallbackAllocator>::size_type; + typedef typename std::allocator<T>::pointer pointer; + typedef typename std::allocator<T>::size_type size_type; // Backing store for the allocator. The container owner is responsible for // maintaining this for as long as any containers using this allocator are @@ -70,13 +69,13 @@ class StackAllocator : public FallbackAllocator { // Used by containers when they want to refer to an allocator of type U. template<typename U> struct rebind { - typedef StackAllocator<U, stack_capacity, FallbackAllocator> other; + typedef StackAllocator<U, stack_capacity> other; }; // For the straight up copy c-tor, we can share storage. - StackAllocator( - const StackAllocator<T, stack_capacity, FallbackAllocator>& rhs) - : source_(rhs.source_) {} + StackAllocator(const StackAllocator<T, stack_capacity>& rhs) + : std::allocator<T>(), source_(rhs.source_) { + } // ISO C++ requires the following constructor to be defined, // and std::vector in VC++2008SP1 Release fails with an error @@ -87,8 +86,8 @@ class StackAllocator : public FallbackAllocator { // for Us. // TODO: If we were fancy pants, perhaps we could share storage // iff sizeof(T) == sizeof(U). - template <typename U, size_t other_capacity, typename FA> - StackAllocator(const StackAllocator<U, other_capacity, FA>& other) + template <typename U, size_t other_capacity> + StackAllocator(const StackAllocator<U, other_capacity>& other) : source_(nullptr) {} // This constructor must exist. It creates a default allocator that doesn't @@ -108,7 +107,7 @@ class StackAllocator : public FallbackAllocator { source_->used_stack_buffer_ = true; return source_->stack_buffer(); } else { - return std::allocator_traits<FallbackAllocator>::allocate(*this, n); + return std::allocator<T>::allocate(n); } } @@ -118,7 +117,7 @@ class StackAllocator : public FallbackAllocator { if (source_ && p == source_->stack_buffer()) source_->used_stack_buffer_ = false; else - std::allocator_traits<FallbackAllocator>::deallocate(*this, p, n); + std::allocator<T>::deallocate(p, n); } private: @@ -138,12 +137,12 @@ class StackAllocator : public FallbackAllocator { // WATCH OUT: the ContainerType MUST use the proper StackAllocator for this // type. This object is really intended to be used only internally. You'll want // to use the wrappers below for different types. -template <typename TContainerType, int stack_capacity> +template<typename TContainerType, int stack_capacity> class StackContainer { public: - using ContainerType = TContainerType; - using ContainedType = typename ContainerType::value_type; - using Allocator = typename ContainerType::allocator_type; + typedef TContainerType ContainerType; + typedef typename ContainerType::value_type ContainedType; + typedef StackAllocator<ContainedType, stack_capacity> Allocator; // Allocator must be constructed before the container! StackContainer() : allocator_(&stack_data_), container_(allocator_) { @@ -179,7 +178,7 @@ class StackContainer { protected: typename Allocator::Source stack_data_; - NO_UNIQUE_ADDRESS Allocator allocator_; + Allocator allocator_; ContainerType container_; }; @@ -215,34 +214,29 @@ auto end(const StackContainer<TContainerType, stack_capacity>& stack_container) // StackVector<int, 16> foo; // foo->push_back(22); // we have overloaded operator-> // foo[0] = 10; // as well as operator[] -template <typename T, - size_t stack_capacity, - typename FallbackAllocator = std::allocator<T>> -class StackVector - : public StackContainer< - std::vector<T, StackAllocator<T, stack_capacity, FallbackAllocator>>, - stack_capacity> { +template<typename T, size_t stack_capacity> +class StackVector : public StackContainer< + std::vector<T, StackAllocator<T, stack_capacity> >, + stack_capacity> { public: - StackVector() - : StackContainer< - std::vector<T, - StackAllocator<T, stack_capacity, FallbackAllocator>>, - stack_capacity>() {} + StackVector() : StackContainer< + std::vector<T, StackAllocator<T, stack_capacity> >, + stack_capacity>() { + } // We need to put this in STL containers sometimes, which requires a copy // constructor. We can't call the regular copy constructor because that will // take the stack buffer from the original. Here, we create an empty object // and make a stack buffer of its own. - StackVector(const StackVector<T, stack_capacity, FallbackAllocator>& other) + StackVector(const StackVector<T, stack_capacity>& other) : StackContainer< - std::vector<T, - StackAllocator<T, stack_capacity, FallbackAllocator>>, + std::vector<T, StackAllocator<T, stack_capacity> >, stack_capacity>() { this->container().assign(other->begin(), other->end()); } - StackVector<T, stack_capacity, FallbackAllocator>& operator=( - const StackVector<T, stack_capacity, FallbackAllocator>& other) { + StackVector<T, stack_capacity>& operator=( + const StackVector<T, stack_capacity>& other) { this->container().assign(other->begin(), other->end()); return *this; } diff --git a/chromium/base/containers/stack_container_unittest.cc b/chromium/base/containers/stack_container_unittest.cc index a57ee294dc0..110d958f5f3 100644 --- a/chromium/base/containers/stack_container_unittest.cc +++ b/chromium/base/containers/stack_container_unittest.cc @@ -71,8 +71,7 @@ TEST(StackContainer, Vector) { // Copying the small vector to another should use the same allocator and use // the now-unused stack buffer. GENERALLY CALLERS SHOULD NOT DO THIS since // they have to get the template types just right and it can cause errors. - std::vector<int, StackAllocator<int, stack_size, std::allocator<int>>> other( - vect.container()); + std::vector<int, StackAllocator<int, stack_size> > other(vect.container()); EXPECT_EQ(stack_buffer, &other.front()); EXPECT_TRUE(vect.stack_data().used_stack_buffer_); for (int i = 0; i < stack_size; i++) @@ -172,44 +171,4 @@ TEST(StackContainer, Iteration) { CheckStackVectorElements(vect, {8}); } -namespace { -struct Allocator : std::allocator<int> { - using Base = std::allocator<int>; - - int* allocate(size_t n) { - ++allocated; - return Base::allocate(n); - } - void deallocate(int* p, size_t n) { - ++deallocated; - Base::deallocate(p, n); - } - - static int allocated; - static int deallocated; -}; - -int Allocator::allocated = 0; -int Allocator::deallocated = 0; -} // namespace - -TEST(StackContainer, CustomAllocator) { - StackVector<int, 2, Allocator> v; - - EXPECT_EQ(0, Allocator::allocated); - EXPECT_EQ(0, Allocator::deallocated); - - v->push_back(1); - v->push_back(1); - EXPECT_EQ(0, Allocator::allocated); - v->push_back(1); - EXPECT_EQ(1, Allocator::allocated); - - EXPECT_EQ(0, Allocator::deallocated); - v->clear(); - // shrink_to_fit() makes sure to destroy empty backing store. - v->shrink_to_fit(); - EXPECT_EQ(1, Allocator::deallocated); -} - } // namespace base |