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 <cassert>
19 #include <cstdint>
20 #include <memory>
21 
22 namespace absl {
23 namespace container_internal {
24 
25 // This is a stateful allocator, but the state lives outside of the
26 // allocator (in whatever test is using the allocator). This is odd
27 // but helps in tests where the allocator is propagated into nested
28 // containers - that chain of allocators uses the same state and is
29 // thus easier to query for aggregate allocation information.
30 template <typename T>
31 class CountingAllocator : public std::allocator<T> {
32  public:
33  using Alloc = std::allocator<T>;
34  using pointer = typename Alloc::pointer;
35  using size_type = typename Alloc::size_type;
36 
38  explicit CountingAllocator(int64_t* b) : bytes_used_(b) {}
39 
40  template <typename U>
42  : Alloc(x), bytes_used_(x.bytes_used_) {}
43 
45  std::allocator<void>::const_pointer hint = nullptr) {
46  assert(bytes_used_ != nullptr);
47  *bytes_used_ += n * sizeof(T);
48  return Alloc::allocate(n, hint);
49  }
50 
52  Alloc::deallocate(p, n);
53  assert(bytes_used_ != nullptr);
54  *bytes_used_ -= n * sizeof(T);
55  }
56 
57  template<typename U>
58  class rebind {
59  public:
61  };
62 
63  friend bool operator==(const CountingAllocator& a,
64  const CountingAllocator& b) {
65  return a.bytes_used_ == b.bytes_used_;
66  }
67 
68  friend bool operator!=(const CountingAllocator& a,
69  const CountingAllocator& b) {
70  return !(a == b);
71  }
72 
73  int64_t* bytes_used_;
74 };
75 
76 } // namespace container_internal
77 } // namespace absl
78 
79 #endif // ABSL_CONTAINER_INTERNAL_COUNTING_ALLOCATOR_H_
friend bool operator==(const CountingAllocator &a, const CountingAllocator &b)
Definition: algorithm.h:29
friend bool operator!=(const CountingAllocator &a, const CountingAllocator &b)
pointer allocate(size_type n, std::allocator< void >::const_pointer hint=nullptr)
uint64_t b
Definition: layout_test.cc:50
CountingAllocator(const CountingAllocator< U > &x)


abseil_cpp
Author(s):
autogenerated on Tue Jun 18 2019 19:44:35