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  if (allocator_ != nullptr) allocator_->Shutdown();
60  allocator_.reset();
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 
102  }
103 
108  template <typename T, typename... Args>
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 
140  template <typename T>
141  class Container {
142  public:
143  using value_type = T;
144 
149  template <typename U>
150  explicit Container(const Container<U>& other)
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:
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;
206 };
207 
208 } // namespace experimental
209 } // namespace grpc_event_engine
210 
211 #endif // GRPC_EVENT_ENGINE_MEMORY_ALLOCATOR_H
grpc_event_engine::experimental::MemoryAllocator::Reserve
size_t Reserve(MemoryRequest request)
Definition: memory_allocator.h:66
grpc_event_engine::experimental::MemoryAllocator::MemoryAllocator
MemoryAllocator()
Definition: memory_allocator.h:44
grpc_event_engine::experimental::MemoryAllocator
Definition: memory_allocator.h:35
slice.h
grpc_event_engine::experimental::MemoryAllocator::Reservation::allocator_
std::shared_ptr< internal::MemoryAllocatorImpl > allocator_
Definition: memory_allocator.h:94
grpc_event_engine::experimental::MemoryAllocator::Container::allocate
T * allocate(size_t n)
Definition: memory_allocator.h:157
benchmark.request
request
Definition: benchmark.py:77
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
grpc_event_engine::experimental::MemoryAllocator::Reservation::operator=
Reservation & operator=(const Reservation &)=delete
setup.name
name
Definition: setup.py:542
grpc_event_engine::experimental::MemoryAllocator::Container::Container
Container(const Container< U > &other)
Definition: memory_allocator.h:150
grpc_event_engine::experimental::MemoryAllocator::Reservation::Reservation
Reservation(std::shared_ptr< internal::MemoryAllocatorImpl > allocator, size_t size)
Definition: memory_allocator.h:90
grpc_event_engine::experimental::MemoryAllocator::Reservation::~Reservation
~Reservation()
Definition: memory_allocator.h:84
grpc_event_engine::experimental::MemoryAllocator::MakeUnique
std::unique_ptr< T > MakeUnique(Args &&... args)
Construct a unique_ptr immediately.
Definition: memory_allocator.h:129
T
#define T(upbtypeconst, upbtype, ctype, default_value)
grpc_event_engine::experimental::MemoryAllocator::MemoryAllocator
MemoryAllocator(std::shared_ptr< internal::MemoryAllocatorImpl > allocator)
Definition: memory_allocator.h:40
grpc_event_engine::experimental::MemoryAllocator::~MemoryAllocator
~MemoryAllocator()
Definition: memory_allocator.h:45
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
memory_allocator_impl.h
grpc_event_engine::experimental::MemoryAllocator::MakeSlice
grpc_slice MakeSlice(MemoryRequest request)
Definition: memory_allocator.cc:61
hpack_encoder_fixtures::Args
Args({0, 16384})
grpc_event_engine::experimental::MemoryAllocator::Reservation::size_
size_t size_
Definition: memory_allocator.h:95
grpc_event_engine::experimental::MemoryAllocatorFactory::CreateMemoryAllocator
virtual MemoryAllocator CreateMemoryAllocator(absl::string_view name)=0
grpc_event_engine::experimental::MemoryAllocator::Container::deallocate
void deallocate(T *p, size_t n)
Definition: memory_allocator.h:161
grpc_event_engine::experimental::MemoryAllocator::operator=
MemoryAllocator & operator=(const MemoryAllocator &)=delete
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::allocator_
std::shared_ptr< internal::MemoryAllocatorImpl > allocator_
Definition: memory_allocator.h:183
grpc_event_engine::experimental::MemoryAllocator::get_internal_impl_ptr
internal::MemoryAllocatorImpl * get_internal_impl_ptr()
Definition: memory_allocator.h:174
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
grpc_event_engine::experimental::MemoryAllocator::Reservation
An automatic releasing reservation of memory.
Definition: memory_allocator.h:77
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
grpc_event_engine::experimental::Vector
Definition: memory_allocator.h:189
value
const char * value
Definition: hpack_parser_table.cc:165
grpc_event_engine::experimental::MemoryAllocator::MakeReservation
Reservation MakeReservation(MemoryRequest request)
Definition: memory_allocator.h:100
grpc_event_engine::experimental::MemoryAllocatorFactory::~MemoryAllocatorFactory
virtual ~MemoryAllocatorFactory()=default
grpc_event_engine::experimental::MemoryAllocator::Container
A C++ allocator for containers of T.
Definition: memory_allocator.h:141
port_platform.h
grpc_event_engine::experimental::MemoryAllocator::Container::underlying_allocator
MemoryAllocator * underlying_allocator() const
Definition: memory_allocator.h:153
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
grpc_event_engine
Definition: endpoint_config.h:24
grpc_event_engine::experimental::MemoryAllocatorFactory
Definition: memory_allocator.h:196
grpc_event_engine::experimental::MemoryAllocator::Reservation::Reservation
Reservation()=default
grpc_event_engine::experimental::MemoryAllocator::Container::value_type
T value_type
Definition: memory_allocator.h:143
grpc_event_engine::experimental::MemoryAllocator::Container::Container
Container(MemoryAllocator *underlying_allocator)
Definition: memory_allocator.h:147
grpc_event_engine::experimental::MemoryAllocator::Container::underlying_allocator_
MemoryAllocator * underlying_allocator_
Definition: memory_allocator.h:167
grpc_event_engine::experimental::MemoryAllocator::Reset
void Reset()
Definition: memory_allocator.h:58
grpc_event_engine::experimental::MemoryAllocator::New
std::enable_if< std::has_virtual_destructor< T >::value, T * >::type New(Args &&... args)
Definition: memory_allocator.h:109
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
grpc_event_engine::experimental::internal::MemoryAllocatorImpl
Definition: memory_allocator_impl.h:35
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
grpc_event_engine::experimental::Vector::Vector
Vector(MemoryAllocator *allocator)
Definition: memory_allocator.h:191
grpc_event_engine::experimental::MemoryRequest
Reservation request - how much memory do we want to allocate?
Definition: memory_request.h:27
grpc_event_engine::experimental::MemoryAllocator::Release
void Release(size_t n)
Release some bytes that were previously reserved.
Definition: memory_allocator.h:69


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:35