GRPC C++  1.62.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 
19 #include <stdlib.h> // for abort()
20 
21 #include <algorithm>
22 #include <memory>
23 #include <type_traits>
24 #include <vector>
25 
27 #include <grpc/slice.h>
28 
29 namespace grpc_event_engine {
30 namespace experimental {
31 
32 // Tracks memory allocated by one system.
33 // Is effectively a thin wrapper/smart pointer for a MemoryAllocatorImpl,
34 // providing a convenient and stable API.
36  public:
40  explicit MemoryAllocator(
41  std::shared_ptr<internal::MemoryAllocatorImpl> allocator)
42  : allocator_(std::move(allocator)) {}
43  // Construct an invalid MemoryAllocator.
44  MemoryAllocator() : allocator_(nullptr) {}
46  if (allocator_ != nullptr) allocator_->Shutdown();
47  }
48 
49  MemoryAllocator(const MemoryAllocator&) = delete;
50  MemoryAllocator& operator=(const MemoryAllocator&) = delete;
51 
52  MemoryAllocator(MemoryAllocator&&) = default;
54 
58  void Reset() {
59  auto a = std::move(allocator_);
60  if (a != nullptr) a->Shutdown();
61  }
62 
66  size_t Reserve(MemoryRequest request) { return allocator_->Reserve(request); }
67 
69  void Release(size_t n) { return allocator_->Release(n); }
70 
71  //
72  // The remainder of this type are helper functions implemented in terms of
73  // Reserve/Release.
74  //
75 
77  class Reservation {
78  public:
79  Reservation() = default;
80  Reservation(const Reservation&) = delete;
81  Reservation& operator=(const Reservation&) = delete;
82  Reservation(Reservation&&) = default;
83  Reservation& operator=(Reservation&&) = default;
85  if (allocator_ != nullptr) allocator_->Release(size_);
86  }
87 
88  private:
89  friend class MemoryAllocator;
90  Reservation(std::shared_ptr<internal::MemoryAllocatorImpl> allocator,
91  size_t size)
92  : allocator_(std::move(allocator)), size_(size) {}
93 
94  std::shared_ptr<internal::MemoryAllocatorImpl> allocator_;
95  size_t size_ = 0;
96  };
97 
101  return Reservation(allocator_, Reserve(request));
102  }
103 
108  template <typename T, typename... Args>
109  typename std::enable_if<std::has_virtual_destructor<T>::value, T*>::type New(
110  Args&&... args) {
111  // Wrap T such that when it's destroyed, we can release memory back to the
112  // allocator.
113  class Wrapper final : public T {
114  public:
115  explicit Wrapper(std::shared_ptr<internal::MemoryAllocatorImpl> allocator,
116  Args&&... args)
117  : T(std::forward<Args>(args)...), allocator_(std::move(allocator)) {}
118  ~Wrapper() override { allocator_->Release(sizeof(*this)); }
119 
120  private:
121  const std::shared_ptr<internal::MemoryAllocatorImpl> allocator_;
122  };
123  Reserve(sizeof(Wrapper));
124  return new Wrapper(allocator_, std::forward<Args>(args)...);
125  }
126 
128  template <typename T, typename... Args>
129  std::unique_ptr<T> MakeUnique(Args&&... args) {
130  return std::unique_ptr<T>(New<T>(std::forward<Args>(args)...));
131  }
132 
138  return allocator_->MakeSlice(request);
139  }
140 
142  template <typename T>
143  class Container {
144  public:
145  using value_type = T;
146 
150  : underlying_allocator_(underlying_allocator) {}
151  template <typename U>
152  explicit Container(const Container<U>& other)
153  : underlying_allocator_(other.underlying_allocator()) {}
154 
156  return underlying_allocator_;
157  }
158 
159  T* allocate(size_t n) {
160  underlying_allocator_->Reserve(n * sizeof(T));
161  return static_cast<T*>(::operator new(n * sizeof(T)));
162  }
163  void deallocate(T* p, size_t n) {
164  ::operator delete(p);
165  underlying_allocator_->Release(n * sizeof(T));
166  }
167 
168  private:
169  MemoryAllocator* underlying_allocator_;
170  };
171 
172  protected:
177  return allocator_.get();
178  }
179 
181  return allocator_.get();
182  }
183 
184  private:
185  std::shared_ptr<internal::MemoryAllocatorImpl> allocator_;
186 };
187 
188 // Wrapper type around std::vector to make initialization against a
189 // MemoryAllocator based container allocator easy.
190 template <typename T>
191 class Vector : public std::vector<T, MemoryAllocator::Container<T>> {
192  public:
193  explicit Vector(MemoryAllocator* allocator)
194  : std::vector<T, MemoryAllocator::Container<T>>(
195  MemoryAllocator::Container<T>(allocator)) {}
196 };
197 
199  public:
200  virtual ~MemoryAllocatorFactory() = default;
207  virtual MemoryAllocator CreateMemoryAllocator(absl::string_view name) = 0;
208 };
209 
210 } // namespace experimental
211 } // namespace grpc_event_engine
212 
213 #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:137
grpc_event_engine::experimental::MemoryAllocator::get_internal_impl_ptr
const internal::MemoryAllocatorImpl * get_internal_impl_ptr() const
Definition: memory_allocator.h:180
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:40
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:176
grpc_event_engine::experimental::MemoryAllocator::Container
A C++ allocator for containers of T.
Definition: memory_allocator.h:143
grpc_event_engine::experimental::Vector::Vector
Vector(MemoryAllocator *allocator)
Definition: memory_allocator.h:193
grpc_event_engine::experimental::MemoryAllocator::Reservation::~Reservation
~Reservation()
Definition: memory_allocator.h:84
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:149
grpc_event_engine::experimental::MemoryAllocator::Release
void Release(size_t n)
Release some bytes that were previously reserved.
Definition: memory_allocator.h:69
grpc_event_engine::experimental::MemoryAllocator::Reserve
size_t Reserve(MemoryRequest request)
Reserve bytes from the quota.
Definition: memory_allocator.h:66
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:191
grpc_event_engine::experimental::MemoryAllocator::Reset
void Reset()
Drop the underlying allocator and make this an empty object.
Definition: memory_allocator.h:58
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:109
grpc_event_engine::experimental::internal::MemoryAllocatorImpl
Underlying memory allocation interface.
Definition: memory_allocator_impl.h:35
grpc_event_engine::experimental::MemoryAllocator::MakeUnique
std::unique_ptr< T > MakeUnique(Args &&... args)
Construct a unique_ptr immediately.
Definition: memory_allocator.h:129
grpc_event_engine::experimental::MemoryRequest
Reservation request - how much memory do we want to allocate?
Definition: memory_request.h:27
memory_allocator_impl.h
grpc_event_engine::experimental::MemoryAllocator::Container::value_type
T value_type
Definition: memory_allocator.h:145
grpc_event_engine::experimental::MemoryAllocator::Container::deallocate
void deallocate(T *p, size_t n)
Definition: memory_allocator.h:163
grpc_event_engine::experimental::MemoryAllocator::Container::Container
Container(const Container< U > &other)
Definition: memory_allocator.h:152
grpc_slice
A grpc_slice s, if initialized, represents the byte range s.bytes[0..s.length-1].
Definition: slice_type.h:63
grpc_event_engine::experimental::MemoryAllocatorFactory
Definition: memory_allocator.h:198
grpc_event_engine::experimental::MemoryAllocator::Container::underlying_allocator
MemoryAllocator * underlying_allocator() const
Definition: memory_allocator.h:155
slice.h
std
Definition: async_unary_call.h:405
grpc_event_engine
Definition: endpoint_config.h:24
grpc_event_engine::experimental::MemoryAllocator
Definition: memory_allocator.h:35
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:77
grpc_event_engine::experimental::MemoryAllocator::operator=
MemoryAllocator & operator=(const MemoryAllocator &)=delete
grpc_event_engine::experimental::MemoryAllocator::MemoryAllocator
MemoryAllocator()
Definition: memory_allocator.h:44
grpc_event_engine::experimental::MemoryAllocator::Container::allocate
T * allocate(size_t n)
Definition: memory_allocator.h:159
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:100
grpc_event_engine::experimental::MemoryAllocator::~MemoryAllocator
~MemoryAllocator()
Definition: memory_allocator.h:45
port_platform.h