Go to the documentation of this file.
14 #ifndef GRPC_EVENT_ENGINE_MEMORY_ALLOCATOR_H
15 #define GRPC_EVENT_ENGINE_MEMORY_ALLOCATOR_H
24 #include <type_traits>
28 namespace experimental {
39 std::shared_ptr<internal::MemoryAllocatorImpl> allocator)
40 : allocator_(std::move(allocator)) {}
44 if (allocator_ !=
nullptr) allocator_->Shutdown();
57 auto a = std::move(allocator_);
58 if (a !=
nullptr) a->Shutdown();
67 void Release(
size_t n) {
return allocator_->Release(n); }
83 if (allocator_ !=
nullptr) allocator_->Release(size_);
88 Reservation(std::shared_ptr<internal::MemoryAllocatorImpl> allocator,
90 : allocator_(std::move(allocator)), size_(size) {}
92 std::shared_ptr<internal::MemoryAllocatorImpl> allocator_;
106 template <
typename T,
typename... Args>
107 typename std::enable_if<std::has_virtual_destructor<T>::value, T*>::type
New(
111 class Wrapper final :
public T {
113 explicit Wrapper(std::shared_ptr<internal::MemoryAllocatorImpl> allocator,
115 : T(std::forward<Args>(args)...), allocator_(std::move(allocator)) {}
116 ~Wrapper()
override { allocator_->Release(
sizeof(*
this)); }
119 const std::shared_ptr<internal::MemoryAllocatorImpl> allocator_;
122 return new Wrapper(allocator_, std::forward<Args>(args)...);
126 template <
typename T,
typename... Args>
128 return std::unique_ptr<T>(New<T>(std::forward<Args>(args)...));
136 return allocator_->MakeSlice(request);
140 template <
typename T>
149 template <
typename U>
154 return underlying_allocator_;
158 underlying_allocator_->
Reserve(n *
sizeof(T));
159 return static_cast<T*
>(::operator
new(n *
sizeof(T)));
162 ::operator
delete(p);
163 underlying_allocator_->
Release(n *
sizeof(T));
175 return allocator_.get();
179 return allocator_.get();
183 std::shared_ptr<internal::MemoryAllocatorImpl> allocator_;
188 template <
typename T>
189 class Vector :
public std::vector<T, MemoryAllocator::Container<T>> {
211 #endif // GRPC_EVENT_ENGINE_MEMORY_ALLOCATOR_H
grpc_slice MakeSlice(MemoryRequest request)
Allocate a slice, using MemoryRequest to size the number of returned bytes.
Definition: memory_allocator.h:135
const internal::MemoryAllocatorImpl * get_internal_impl_ptr() const
Definition: memory_allocator.h:178
Reservation & operator=(const Reservation &)=delete
MemoryAllocator(std::shared_ptr< internal::MemoryAllocatorImpl > allocator)
Construct a MemoryAllocator given an internal::MemoryAllocatorImpl implementation.
Definition: memory_allocator.h:38
internal::MemoryAllocatorImpl * get_internal_impl_ptr()
Return a pointer to the underlying implementation.
Definition: memory_allocator.h:174
A C++ allocator for containers of T.
Definition: memory_allocator.h:141
Vector(MemoryAllocator *allocator)
Definition: memory_allocator.h:191
~Reservation()
Definition: memory_allocator.h:82
Container(MemoryAllocator *underlying_allocator)
Construct the allocator: underlying_allocator is borrowed, and must outlive this object.
Definition: memory_allocator.h:147
void Release(size_t n)
Release some bytes that were previously reserved.
Definition: memory_allocator.h:67
size_t Reserve(MemoryRequest request)
Reserve bytes from the quota.
Definition: memory_allocator.h:64
virtual MemoryAllocator CreateMemoryAllocator(absl::string_view name)=0
On Endpoint creation, call CreateMemoryAllocator to create a new allocator for the endpoint.
Definition: memory_allocator.h:189
void Reset()
Drop the underlying allocator and make this an empty object.
Definition: memory_allocator.h:56
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
Underlying memory allocation interface.
Definition: memory_allocator_impl.h:34
std::unique_ptr< T > MakeUnique(Args &&... args)
Construct a unique_ptr immediately.
Definition: memory_allocator.h:127
Reservation request - how much memory do we want to allocate?
Definition: memory_request.h:26
T value_type
Definition: memory_allocator.h:143
void deallocate(T *p, size_t n)
Definition: memory_allocator.h:161
Container(const Container< U > &other)
Definition: memory_allocator.h:150
A grpc_slice s, if initialized, represents the byte range s.bytes[0..s.length-1].
Definition: slice_type.h:62
Definition: memory_allocator.h:196
MemoryAllocator * underlying_allocator() const
Definition: memory_allocator.h:153
Definition: endpoint_config.h:24
Definition: memory_allocator.h:33
virtual ~MemoryAllocatorFactory()=default
An automatic releasing reservation of memory.
Definition: memory_allocator.h:75
MemoryAllocator & operator=(const MemoryAllocator &)=delete
MemoryAllocator()
Definition: memory_allocator.h:42
T * allocate(size_t n)
Definition: memory_allocator.h:157
Reservation MakeReservation(MemoryRequest request)
Reserve bytes from the quota and automatically release them when Reservation is destroyed.
Definition: memory_allocator.h:98
~MemoryAllocator()
Definition: memory_allocator.h:43