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