Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 1 | // Copyright 2018 The Abseil Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #ifndef ABSL_CONTAINER_INTERNAL_COUNTING_ALLOCATOR_H_ |
| 16 | #define ABSL_CONTAINER_INTERNAL_COUNTING_ALLOCATOR_H_ |
| 17 | |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 18 | #include <cstdint> |
| 19 | #include <memory> |
| 20 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 21 | #include "absl/base/config.h" |
| 22 | |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 23 | namespace absl { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 24 | ABSL_NAMESPACE_BEGIN |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 25 | namespace container_internal { |
| 26 | |
| 27 | // This is a stateful allocator, but the state lives outside of the |
| 28 | // allocator (in whatever test is using the allocator). This is odd |
| 29 | // but helps in tests where the allocator is propagated into nested |
| 30 | // containers - that chain of allocators uses the same state and is |
| 31 | // thus easier to query for aggregate allocation information. |
| 32 | template <typename T> |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 33 | class CountingAllocator { |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 34 | public: |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 35 | using Allocator = std::allocator<T>; |
| 36 | using AllocatorTraits = std::allocator_traits<Allocator>; |
| 37 | using value_type = typename AllocatorTraits::value_type; |
| 38 | using pointer = typename AllocatorTraits::pointer; |
| 39 | using const_pointer = typename AllocatorTraits::const_pointer; |
| 40 | using size_type = typename AllocatorTraits::size_type; |
| 41 | using difference_type = typename AllocatorTraits::difference_type; |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 42 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 43 | CountingAllocator() = default; |
| 44 | explicit CountingAllocator(int64_t* bytes_used) : bytes_used_(bytes_used) {} |
| 45 | CountingAllocator(int64_t* bytes_used, int64_t* instance_count) |
| 46 | : bytes_used_(bytes_used), instance_count_(instance_count) {} |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 47 | |
| 48 | template <typename U> |
| 49 | CountingAllocator(const CountingAllocator<U>& x) |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 50 | : bytes_used_(x.bytes_used_), instance_count_(x.instance_count_) {} |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 51 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 52 | pointer allocate( |
| 53 | size_type n, |
| 54 | typename AllocatorTraits::const_void_pointer hint = nullptr) { |
| 55 | Allocator allocator; |
| 56 | pointer ptr = AllocatorTraits::allocate(allocator, n, hint); |
| 57 | if (bytes_used_ != nullptr) { |
| 58 | *bytes_used_ += n * sizeof(T); |
| 59 | } |
| 60 | return ptr; |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void deallocate(pointer p, size_type n) { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 64 | Allocator allocator; |
| 65 | AllocatorTraits::deallocate(allocator, p, n); |
| 66 | if (bytes_used_ != nullptr) { |
| 67 | *bytes_used_ -= n * sizeof(T); |
| 68 | } |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 71 | template <typename U, typename... Args> |
| 72 | void construct(U* p, Args&&... args) { |
| 73 | Allocator allocator; |
| 74 | AllocatorTraits::construct(allocator, p, std::forward<Args>(args)...); |
| 75 | if (instance_count_ != nullptr) { |
| 76 | *instance_count_ += 1; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | template <typename U> |
| 81 | void destroy(U* p) { |
| 82 | Allocator allocator; |
| 83 | AllocatorTraits::destroy(allocator, p); |
| 84 | if (instance_count_ != nullptr) { |
| 85 | *instance_count_ -= 1; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | template <typename U> |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 90 | class rebind { |
| 91 | public: |
| 92 | using other = CountingAllocator<U>; |
| 93 | }; |
| 94 | |
| 95 | friend bool operator==(const CountingAllocator& a, |
| 96 | const CountingAllocator& b) { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 97 | return a.bytes_used_ == b.bytes_used_ && |
| 98 | a.instance_count_ == b.instance_count_; |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | friend bool operator!=(const CountingAllocator& a, |
| 102 | const CountingAllocator& b) { |
| 103 | return !(a == b); |
| 104 | } |
| 105 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 106 | int64_t* bytes_used_ = nullptr; |
| 107 | int64_t* instance_count_ = nullptr; |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | } // namespace container_internal |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 111 | ABSL_NAMESPACE_END |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 112 | } // namespace absl |
| 113 | |
| 114 | #endif // ABSL_CONTAINER_INTERNAL_COUNTING_ALLOCATOR_H_ |