GRPC Core  44.2.0
memory_allocator.h
Go to the documentation of this file.
1 // Copyright 2021 The gRPC 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 // http://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 #ifndef GRPC_EVENT_ENGINE_MEMORY_ALLOCATOR_H
15 #define GRPC_EVENT_ENGINE_MEMORY_ALLOCATOR_H
16 
18 #include <grpc/slice.h>
20 #include <stdlib.h> // for abort()
21 
22 #include <algorithm>
23 #include <memory>
24 #include <type_traits>
25 #include <vector>
26 
27 namespace grpc_event_engine {
28 namespace experimental {
29 
30 // Tracks memory allocated by one system.
31 // Is effectively a thin wrapper/smart pointer for a MemoryAllocatorImpl,
32 // providing a convenient and stable API.
34  public:
38  explicit MemoryAllocator(
39  std::shared_ptr<internal::MemoryAllocatorImpl> allocator)
40  : allocator_(std::move(allocator)) {}
41  // Construct an invalid MemoryAllocator.
42  MemoryAllocator() : allocator_(nullptr) {}
44  if (allocator_ != nullptr) allocator_->Shutdown();
45  }
46 
47  MemoryAllocator(const MemoryAllocator&) = delete;
48  MemoryAllocator& operator=(const MemoryAllocator&) = delete;
49 
50  MemoryAllocator(MemoryAllocator&&) = default;
52 
56  void Reset() {
57  auto a = std::move(allocator_);
58  if (a != nullptr) a->Shutdown();
59  }
60 
64  size_t Reserve(MemoryRequest request) { return allocator_->Reserve(request); }
65 
67  void Release(size_t n) { return allocator_->Release(n); }
68 
69  //
70  // The remainder of this type are helper functions implemented in terms of
71  // Reserve/Release.
72  //
73 
75  class Reservation {
76  public:
77  Reservation() = default;
78  Reservation(const Reservation&) = delete;
79  Reservation& operator=(const Reservation&) = delete;
80  Reservation(Reservation&&) = default;
81  Reservation& operator=(Reservation&&) = default;
83  if (allocator_ != nullptr) allocator_->Release(size_);
84  }
85 
86  private:
87  friend class MemoryAllocator;
88  Reservation(std::shared_ptr<internal::MemoryAllocatorImpl> allocator,
89  size_t size)
90  : allocator_(std::move(allocator)), size_(size) {}
91 
92  std::shared_ptr<internal::MemoryAllocatorImpl> allocator_;
93  size_t size_ = 0;
94  };
95 
99  return Reservation(allocator_, Reserve(request));
100  }
101 
106  template <typename T, typename... Args>
107  typename std::enable_if<std::has_virtual_destructor<T>::value, T*>::type New(
108  Args&&... args) {
109  // Wrap T such that when it's destroyed, we can release memory back to the
110  // allocator.
111  class Wrapper final : public T {
112  public:
113  explicit Wrapper(std::shared_ptr<internal::MemoryAllocatorImpl> allocator,
114  Args&&... args)
115  : T(std::forward<Args>(args)...), allocator_(std::move(allocator)) {}
116  ~Wrapper() override { allocator_->Release(sizeof(*this)); }
117 
118  private:
119  const std::shared_ptr<internal::MemoryAllocatorImpl> allocator_;
120  };
121  Reserve(sizeof(Wrapper));
122  return new Wrapper(allocator_, std::forward<Args>(args)...);
123  }
124 
126  template <typename T, typename... Args>
127  std::unique_ptr<T> MakeUnique(Args&&... args) {
128  return std::unique_ptr<T>(New<T>(std::forward<Args>(args)...));
129  }
130 
136  return allocator_->MakeSlice(request);
137  }
138 
140  template <typename T>
141  class Container {
142  public:
143  using value_type = T;
144 
148  : underlying_allocator_(underlying_allocator) {}
149  template <typename U>
150  explicit Container(const Container<U>& other)
151  : underlying_allocator_(other.underlying_allocator()) {}
152 
154  return underlying_allocator_;
155  }
156 
157  T* allocate(size_t n) {
158  underlying_allocator_->Reserve(n * sizeof(T));
159  return static_cast<T*>(::operator new(n * sizeof(T)));
160  }
161  void deallocate(T* p, size_t n) {
162  ::operator delete(p);
163  underlying_allocator_->Release(n * sizeof(T));
164  }
165 
166  private:
167  MemoryAllocator* underlying_allocator_;
168  };
169 
170  protected:
175  return allocator_.get();
176  }
177 
179  return allocator_.get();
180  }
181 
182  private:
183  std::shared_ptr<internal::MemoryAllocatorImpl> allocator_;
184 };
185 
186 // Wrapper type around std::vector to make initialization against a
187 // MemoryAllocator based container allocator easy.
188 template <typename T>
189 class Vector : public std::vector<T, MemoryAllocator::Container<T>> {
190  public:
191  explicit Vector(MemoryAllocator* allocator)
192  : std::vector<T, MemoryAllocator::Container<T>>(
193  MemoryAllocator::Container<T>(allocator)) {}
194 };
195 
197  public:
198  virtual ~MemoryAllocatorFactory() = default;
205  virtual MemoryAllocator CreateMemoryAllocator(absl::string_view name) = 0;
206 };
207 
208 } // namespace experimental
209 } // namespace grpc_event_engine
210 
211 #endif // GRPC_EVENT_ENGINE_MEMORY_ALLOCATOR_H
grpc_event_engine::experimental::MemoryAllocator::MakeSlice
grpc_slice MakeSlice(MemoryRequest request)
Allocate a slice, using MemoryRequest to size the number of returned bytes.
Definition: memory_allocator.h:135
grpc_event_engine::experimental::MemoryAllocator::get_internal_impl_ptr
const internal::MemoryAllocatorImpl * get_internal_impl_ptr() const
Definition: memory_allocator.h:178
grpc_event_engine::experimental::MemoryAllocator::Reservation::operator=
Reservation & operator=(const Reservation &)=delete
grpc_event_engine::experimental::MemoryAllocator::MemoryAllocator
MemoryAllocator(std::shared_ptr< internal::MemoryAllocatorImpl > allocator)
Construct a MemoryAllocator given an internal::MemoryAllocatorImpl implementation.
Definition: memory_allocator.h:38
grpc_event_engine::experimental::MemoryAllocator::get_internal_impl_ptr
internal::MemoryAllocatorImpl * get_internal_impl_ptr()
Return a pointer to the underlying implementation.
Definition: memory_allocator.h:174
grpc_event_engine::experimental::MemoryAllocator::Container
A C++ allocator for containers of T.
Definition: memory_allocator.h:141
grpc_event_engine::experimental::Vector::Vector
Vector(MemoryAllocator *allocator)
Definition: memory_allocator.h:191
grpc_event_engine::experimental::MemoryAllocator::Reservation::~Reservation
~Reservation()
Definition: memory_allocator.h:82
grpc_event_engine::experimental::MemoryAllocator::Container::Container
Container(MemoryAllocator *underlying_allocator)
Construct the allocator: underlying_allocator is borrowed, and must outlive this object.
Definition: memory_allocator.h:147
grpc_event_engine::experimental::MemoryAllocator::Release
void Release(size_t n)
Release some bytes that were previously reserved.
Definition: memory_allocator.h:67
grpc_event_engine::experimental::MemoryAllocator::Reserve
size_t Reserve(MemoryRequest request)
Reserve bytes from the quota.
Definition: memory_allocator.h:64
grpc_event_engine::experimental::MemoryAllocatorFactory::CreateMemoryAllocator
virtual MemoryAllocator CreateMemoryAllocator(absl::string_view name)=0
On Endpoint creation, call CreateMemoryAllocator to create a new allocator for the endpoint.
grpc_event_engine::experimental::Vector
Definition: memory_allocator.h:189
grpc_event_engine::experimental::MemoryAllocator::Reset
void Reset()
Drop the underlying allocator and make this an empty object.
Definition: memory_allocator.h:56
grpc_event_engine::experimental::MemoryAllocator::Reservation::Reservation
Reservation()=default
grpc_event_engine::experimental::MemoryAllocator::New
std::enable_if< std::has_virtual_destructor< T >::value, T * >::type New(Args &&... args)
Allocate a new object of type T, with constructor arguments.
Definition: memory_allocator.h:107
grpc_event_engine::experimental::internal::MemoryAllocatorImpl
Underlying memory allocation interface.
Definition: memory_allocator_impl.h:34
grpc_event_engine::experimental::MemoryAllocator::MakeUnique
std::unique_ptr< T > MakeUnique(Args &&... args)
Construct a unique_ptr immediately.
Definition: memory_allocator.h:127
grpc_event_engine::experimental::MemoryRequest
Reservation request - how much memory do we want to allocate?
Definition: memory_request.h:26
memory_allocator_impl.h
grpc_event_engine::experimental::MemoryAllocator::Container::value_type
T value_type
Definition: memory_allocator.h:143
grpc_event_engine::experimental::MemoryAllocator::Container::deallocate
void deallocate(T *p, size_t n)
Definition: memory_allocator.h:161
grpc_event_engine::experimental::MemoryAllocator::Container::Container
Container(const Container< U > &other)
Definition: memory_allocator.h:150
grpc_slice
A grpc_slice s, if initialized, represents the byte range s.bytes[0..s.length-1].
Definition: slice_type.h:62
grpc_event_engine::experimental::MemoryAllocatorFactory
Definition: memory_allocator.h:196
grpc_event_engine::experimental::MemoryAllocator::Container::underlying_allocator
MemoryAllocator * underlying_allocator() const
Definition: memory_allocator.h:153
grpc_event_engine
Definition: endpoint_config.h:24
grpc_event_engine::experimental::MemoryAllocator
Definition: memory_allocator.h:33
grpc_event_engine::experimental::MemoryAllocatorFactory::~MemoryAllocatorFactory
virtual ~MemoryAllocatorFactory()=default
grpc_event_engine::experimental::MemoryAllocator::Reservation
An automatic releasing reservation of memory.
Definition: memory_allocator.h:75
grpc_event_engine::experimental::MemoryAllocator::operator=
MemoryAllocator & operator=(const MemoryAllocator &)=delete
grpc_event_engine::experimental::MemoryAllocator::MemoryAllocator
MemoryAllocator()
Definition: memory_allocator.h:42
slice.h
grpc_event_engine::experimental::MemoryAllocator::Container::allocate
T * allocate(size_t n)
Definition: memory_allocator.h:157
grpc_event_engine::experimental::MemoryAllocator::MakeReservation
Reservation MakeReservation(MemoryRequest request)
Reserve bytes from the quota and automatically release them when Reservation is destroyed.
Definition: memory_allocator.h:98
grpc_event_engine::experimental::MemoryAllocator::~MemoryAllocator
~MemoryAllocator()
Definition: memory_allocator.h:43
port_platform.h