blob: 927cf0825579dcf83b3a6d64798eafc8b1d929ff [file] [log] [blame]
Austin Schuh36244a12019-09-21 17:52:38 -07001// 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 Schuh36244a12019-09-21 17:52:38 -070018#include <cstdint>
19#include <memory>
20
Austin Schuhb4691e92020-12-31 12:37:18 -080021#include "absl/base/config.h"
22
Austin Schuh36244a12019-09-21 17:52:38 -070023namespace absl {
Austin Schuhb4691e92020-12-31 12:37:18 -080024ABSL_NAMESPACE_BEGIN
Austin Schuh36244a12019-09-21 17:52:38 -070025namespace 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.
32template <typename T>
Austin Schuhb4691e92020-12-31 12:37:18 -080033class CountingAllocator {
Austin Schuh36244a12019-09-21 17:52:38 -070034 public:
Austin Schuhb4691e92020-12-31 12:37:18 -080035 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 Schuh36244a12019-09-21 17:52:38 -070042
Austin Schuhb4691e92020-12-31 12:37:18 -080043 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 Schuh36244a12019-09-21 17:52:38 -070047
48 template <typename U>
49 CountingAllocator(const CountingAllocator<U>& x)
Austin Schuhb4691e92020-12-31 12:37:18 -080050 : bytes_used_(x.bytes_used_), instance_count_(x.instance_count_) {}
Austin Schuh36244a12019-09-21 17:52:38 -070051
Austin Schuhb4691e92020-12-31 12:37:18 -080052 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 Schuh36244a12019-09-21 17:52:38 -070061 }
62
63 void deallocate(pointer p, size_type n) {
Austin Schuhb4691e92020-12-31 12:37:18 -080064 Allocator allocator;
65 AllocatorTraits::deallocate(allocator, p, n);
66 if (bytes_used_ != nullptr) {
67 *bytes_used_ -= n * sizeof(T);
68 }
Austin Schuh36244a12019-09-21 17:52:38 -070069 }
70
Austin Schuhb4691e92020-12-31 12:37:18 -080071 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 Schuh36244a12019-09-21 17:52:38 -070090 class rebind {
91 public:
92 using other = CountingAllocator<U>;
93 };
94
95 friend bool operator==(const CountingAllocator& a,
96 const CountingAllocator& b) {
Austin Schuhb4691e92020-12-31 12:37:18 -080097 return a.bytes_used_ == b.bytes_used_ &&
98 a.instance_count_ == b.instance_count_;
Austin Schuh36244a12019-09-21 17:52:38 -070099 }
100
101 friend bool operator!=(const CountingAllocator& a,
102 const CountingAllocator& b) {
103 return !(a == b);
104 }
105
Austin Schuhb4691e92020-12-31 12:37:18 -0800106 int64_t* bytes_used_ = nullptr;
107 int64_t* instance_count_ = nullptr;
Austin Schuh36244a12019-09-21 17:52:38 -0700108};
109
110} // namespace container_internal
Austin Schuhb4691e92020-12-31 12:37:18 -0800111ABSL_NAMESPACE_END
Austin Schuh36244a12019-09-21 17:52:38 -0700112} // namespace absl
113
114#endif // ABSL_CONTAINER_INTERNAL_COUNTING_ALLOCATOR_H_