Go to the documentation of this file.
14 #ifndef GRPC_EVENT_ENGINE_MEMORY_ALLOCATOR_H
15 #define GRPC_EVENT_ENGINE_MEMORY_ALLOCATOR_H
23 #include <type_traits>
30 namespace experimental {
41 std::shared_ptr<internal::MemoryAllocatorImpl> allocator)
42 : allocator_(
std::move(allocator)) {}
46 if (allocator_ !=
nullptr) allocator_->Shutdown();
59 auto a = std::move(allocator_);
60 if (a !=
nullptr) a->Shutdown();
69 void Release(
size_t n) {
return allocator_->Release(n); }
85 if (allocator_ !=
nullptr) allocator_->Release(size_);
90 Reservation(std::shared_ptr<internal::MemoryAllocatorImpl> allocator,
92 : allocator_(
std::move(allocator)), size_(size) {}
94 std::shared_ptr<internal::MemoryAllocatorImpl> allocator_;
108 template <
typename T,
typename... Args>
109 typename std::enable_if<std::has_virtual_destructor<T>::value, T*>::type
New(
113 class Wrapper final :
public T {
115 explicit Wrapper(std::shared_ptr<internal::MemoryAllocatorImpl> allocator,
117 : T(std::forward<Args>(args)...), allocator_(std::move(allocator)) {}
118 ~Wrapper()
override { allocator_->Release(
sizeof(*
this)); }
121 const std::shared_ptr<internal::MemoryAllocatorImpl> allocator_;
124 return new Wrapper(allocator_, std::forward<Args>(args)...);
128 template <
typename T,
typename... Args>
130 return std::unique_ptr<T>(New<T>(std::forward<Args>(args)...));
138 return allocator_->MakeSlice(request);
142 template <
typename T>
151 template <
typename U>
156 return underlying_allocator_;
160 underlying_allocator_->
Reserve(n *
sizeof(T));
161 return static_cast<T*
>(::operator
new(n *
sizeof(T)));
164 ::operator
delete(p);
165 underlying_allocator_->
Release(n *
sizeof(T));
177 return allocator_.get();
181 return allocator_.get();
185 std::shared_ptr<internal::MemoryAllocatorImpl> allocator_;
190 template <
typename T>
191 class Vector :
public std::vector<T, MemoryAllocator::Container<T>> {
213 #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:137
const internal::MemoryAllocatorImpl * get_internal_impl_ptr() const
Definition: memory_allocator.h:180
Reservation & operator=(const Reservation &)=delete
MemoryAllocator(std::shared_ptr< internal::MemoryAllocatorImpl > allocator)
Construct a MemoryAllocator given an internal::MemoryAllocatorImpl implementation.
Definition: memory_allocator.h:40
internal::MemoryAllocatorImpl * get_internal_impl_ptr()
Return a pointer to the underlying implementation.
Definition: memory_allocator.h:176
A C++ allocator for containers of T.
Definition: memory_allocator.h:143
Vector(MemoryAllocator *allocator)
Definition: memory_allocator.h:193
~Reservation()
Definition: memory_allocator.h:84
Container(MemoryAllocator *underlying_allocator)
Construct the allocator: underlying_allocator is borrowed, and must outlive this object.
Definition: memory_allocator.h:149
void Release(size_t n)
Release some bytes that were previously reserved.
Definition: memory_allocator.h:69
size_t Reserve(MemoryRequest request)
Reserve bytes from the quota.
Definition: memory_allocator.h:66
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:191
void Reset()
Drop the underlying allocator and make this an empty object.
Definition: memory_allocator.h:58
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
Underlying memory allocation interface.
Definition: memory_allocator_impl.h:35
std::unique_ptr< T > MakeUnique(Args &&... args)
Construct a unique_ptr immediately.
Definition: memory_allocator.h:129
Reservation request - how much memory do we want to allocate?
Definition: memory_request.h:27
T value_type
Definition: memory_allocator.h:145
void deallocate(T *p, size_t n)
Definition: memory_allocator.h:163
Container(const Container< U > &other)
Definition: memory_allocator.h:152
A grpc_slice s, if initialized, represents the byte range s.bytes[0..s.length-1].
Definition: slice_type.h:63
Definition: memory_allocator.h:198
MemoryAllocator * underlying_allocator() const
Definition: memory_allocator.h:155
Definition: async_unary_call.h:405
Definition: endpoint_config.h:24
Definition: memory_allocator.h:35
virtual ~MemoryAllocatorFactory()=default
An automatic releasing reservation of memory.
Definition: memory_allocator.h:77
MemoryAllocator & operator=(const MemoryAllocator &)=delete
MemoryAllocator()
Definition: memory_allocator.h:44
T * allocate(size_t n)
Definition: memory_allocator.h:159
Reservation MakeReservation(MemoryRequest request)
Reserve bytes from the quota and automatically release them when Reservation is destroyed.
Definition: memory_allocator.h:100
~MemoryAllocator()
Definition: memory_allocator.h:45