abseil-cpp/absl/container/internal/counting_allocator.h
Go to the documentation of this file.
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 
18 #include <cstdint>
19 #include <memory>
20 
21 #include "absl/base/config.h"
22 
23 namespace absl {
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>
34  public:
35  using Allocator = std::allocator<T>;
36  using AllocatorTraits = std::allocator_traits<Allocator>;
38  using pointer = typename AllocatorTraits::pointer;
40  using size_type = typename AllocatorTraits::size_type;
41  using difference_type = typename AllocatorTraits::difference_type;
42 
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) {}
47 
48  template <typename U>
51 
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;
61  }
62 
64  Allocator allocator;
65  AllocatorTraits::deallocate(allocator, p, n);
66  if (bytes_used_ != nullptr) {
67  *bytes_used_ -= n * sizeof(T);
68  }
69  }
70 
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  // Ignore GCC warning bug.
84 #if ABSL_INTERNAL_HAVE_MIN_GNUC_VERSION(12, 0)
85 #pragma GCC diagnostic push
86 #pragma GCC diagnostic ignored "-Wuse-after-free"
87 #endif
88  AllocatorTraits::destroy(allocator, p);
89 #if ABSL_INTERNAL_HAVE_MIN_GNUC_VERSION(12, 0)
90 #pragma GCC diagnostic pop
91 #endif
92  if (instance_count_ != nullptr) {
93  *instance_count_ -= 1;
94  }
95  }
96 
97  template <typename U>
98  class rebind {
99  public:
101  };
102 
103  friend bool operator==(const CountingAllocator& a,
104  const CountingAllocator& b) {
105  return a.bytes_used_ == b.bytes_used_ &&
106  a.instance_count_ == b.instance_count_;
107  }
108 
109  friend bool operator!=(const CountingAllocator& a,
110  const CountingAllocator& b) {
111  return !(a == b);
112  }
113 
114  int64_t* bytes_used_ = nullptr;
116 };
117 
118 } // namespace container_internal
120 } // namespace absl
121 
122 #endif // ABSL_CONTAINER_INTERNAL_COUNTING_ALLOCATOR_H_
ptr
char * ptr
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:45
absl::container_internal::CountingAllocator::allocate
pointer allocate(size_type n, typename AllocatorTraits::const_void_pointer hint=nullptr)
Definition: abseil-cpp/absl/container/internal/counting_allocator.h:52
absl::container_internal::CountingAllocator::construct
void construct(U *p, Args &&... args)
Definition: abseil-cpp/absl/container/internal/counting_allocator.h:72
absl::container_internal::CountingAllocator::deallocate
void deallocate(pointer p, size_type n)
Definition: abseil-cpp/absl/container/internal/counting_allocator.h:63
absl::container_internal::CountingAllocator::rebind
Definition: abseil-cpp/absl/container/internal/counting_allocator.h:98
absl::container_internal::CountingAllocator::AllocatorTraits
std::allocator_traits< Allocator > AllocatorTraits
Definition: abseil-cpp/absl/container/internal/counting_allocator.h:36
absl::container_internal::CountingAllocator::bytes_used_
int64_t * bytes_used_
Definition: abseil-cpp/absl/container/internal/counting_allocator.h:114
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
T
#define T(upbtypeconst, upbtype, ctype, default_value)
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
hpack_encoder_fixtures::Args
Args({0, 16384})
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
absl::container_internal::CountingAllocator::CountingAllocator
CountingAllocator(int64_t *bytes_used, int64_t *instance_count)
Definition: abseil-cpp/absl/container/internal/counting_allocator.h:45
absl::container_internal::CountingAllocator::const_pointer
typename AllocatorTraits::const_pointer const_pointer
Definition: abseil-cpp/absl/container/internal/counting_allocator.h:39
absl::container_internal::CountingAllocator::destroy
void destroy(U *p)
Definition: abseil-cpp/absl/container/internal/counting_allocator.h:81
absl::compare_internal::value_type
int8_t value_type
Definition: abseil-cpp/absl/types/compare.h:45
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
absl::container_internal::CountingAllocator::CountingAllocator
CountingAllocator(int64_t *bytes_used)
Definition: abseil-cpp/absl/container/internal/counting_allocator.h:44
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
absl::container_internal::CountingAllocator::instance_count_
int64_t * instance_count_
Definition: abseil-cpp/absl/container/internal/counting_allocator.h:115
absl::container_internal::CountingAllocator::difference_type
typename AllocatorTraits::difference_type difference_type
Definition: abseil-cpp/absl/container/internal/counting_allocator.h:41
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
absl::container_internal::CountingAllocator::value_type
typename AllocatorTraits::value_type value_type
Definition: abseil-cpp/absl/container/internal/counting_allocator.h:37
absl::container_internal::CountingAllocator::Allocator
std::allocator< T > Allocator
Definition: abseil-cpp/absl/container/internal/counting_allocator.h:35
construct
static std::function< void(void *, Slot *, Slot)> construct
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:41
absl::container_internal::CountingAllocator::operator==
friend bool operator==(const CountingAllocator &a, const CountingAllocator &b)
Definition: abseil-cpp/absl/container/internal/counting_allocator.h:103
absl::container_internal::CountingAllocator
Definition: abseil-cpp/absl/container/internal/counting_allocator.h:33
absl::container_internal::CountingAllocator::CountingAllocator
CountingAllocator(const CountingAllocator< U > &x)
Definition: abseil-cpp/absl/container/internal/counting_allocator.h:49
absl::container_internal::CountingAllocator::pointer
typename AllocatorTraits::pointer pointer
Definition: abseil-cpp/absl/container/internal/counting_allocator.h:38
absl::container_internal::CountingAllocator::size_type
typename AllocatorTraits::size_type size_type
Definition: abseil-cpp/absl/container/internal/counting_allocator.h:40
absl::container_internal::CountingAllocator::operator!=
friend bool operator!=(const CountingAllocator &a, const CountingAllocator &b)
Definition: abseil-cpp/absl/container/internal/counting_allocator.h:109
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
absl::container_internal::CountingAllocator::CountingAllocator
CountingAllocator()=default
destroy
static std::function< void(void *, Slot *)> destroy
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:42
const_pointer
const typedef T * const_pointer
Definition: cxa_demangle.cpp:4833


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:04