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>
29 namespace experimental {
40 std::shared_ptr<internal::MemoryAllocatorImpl> allocator)
41 : allocator_(std::move(allocator)) {}
45 if (allocator_ !=
nullptr) allocator_->Shutdown();
54 bool IsValid()
const {
return allocator_ !=
nullptr; }
60 auto a = std::move(allocator_);
61 if (a !=
nullptr) a->Shutdown();
70 void Release(
size_t n) {
return allocator_->Release(n); }
86 if (allocator_ !=
nullptr) allocator_->Release(size_);
91 Reservation(std::shared_ptr<internal::MemoryAllocatorImpl> allocator,
93 : allocator_(std::move(allocator)), size_(size) {}
95 std::shared_ptr<internal::MemoryAllocatorImpl> allocator_;
109 template <
typename T,
typename... Args>
110 typename std::enable_if<std::has_virtual_destructor<T>::value, T*>::type
New(
114 class Wrapper final :
public T {
116 explicit Wrapper(std::shared_ptr<internal::MemoryAllocatorImpl> allocator,
118 : T(std::forward<Args>(args)...), allocator_(std::move(allocator)) {}
119 ~Wrapper()
override { allocator_->Release(
sizeof(*
this)); }
122 const std::shared_ptr<internal::MemoryAllocatorImpl> allocator_;
125 return new Wrapper(allocator_, std::forward<Args>(args)...);
129 template <
typename T,
typename... Args>
131 return std::unique_ptr<T>(New<T>(std::forward<Args>(args)...));
139 return allocator_->MakeSlice(request);
143 template <
typename T>
152 template <
typename U>
157 return underlying_allocator_;
161 underlying_allocator_->
Reserve(n *
sizeof(T));
162 return static_cast<T*
>(::operator
new(n *
sizeof(T)));
165 ::operator
delete(p);
166 underlying_allocator_->
Release(n *
sizeof(T));
178 return allocator_.get();
182 return allocator_.get();
186 std::shared_ptr<internal::MemoryAllocatorImpl> allocator_;
191 template <
typename T>
192 class Vector :
public std::vector<T, MemoryAllocator::Container<T>> {
214 #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:138
const internal::MemoryAllocatorImpl * get_internal_impl_ptr() const
Definition: memory_allocator.h:181
Reservation & operator=(const Reservation &)=delete
MemoryAllocator(std::shared_ptr< internal::MemoryAllocatorImpl > allocator)
Construct a MemoryAllocator given an internal::MemoryAllocatorImpl implementation.
Definition: memory_allocator.h:39
internal::MemoryAllocatorImpl * get_internal_impl_ptr()
Return a pointer to the underlying implementation.
Definition: memory_allocator.h:177
A C++ allocator for containers of T.
Definition: memory_allocator.h:144
Vector(MemoryAllocator *allocator)
Definition: memory_allocator.h:194
~Reservation()
Definition: memory_allocator.h:85
Container(MemoryAllocator *underlying_allocator)
Construct the allocator: underlying_allocator is borrowed, and must outlive this object.
Definition: memory_allocator.h:150
void Release(size_t n)
Release some bytes that were previously reserved.
Definition: memory_allocator.h:70
size_t Reserve(MemoryRequest request)
Reserve bytes from the quota.
Definition: memory_allocator.h:67
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:192
void Reset()
Drop the underlying allocator and make this an empty object.
Definition: memory_allocator.h:59
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:110
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:130
Reservation request - how much memory do we want to allocate?
Definition: memory_request.h:28
T value_type
Definition: memory_allocator.h:146
void deallocate(T *p, size_t n)
Definition: memory_allocator.h:164
bool IsValid() const
Definition: memory_allocator.h:54
Container(const Container< U > &other)
Definition: memory_allocator.h:153
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:199
MemoryAllocator * underlying_allocator() const
Definition: memory_allocator.h:156
Definition: endpoint_config.h:24
Definition: memory_allocator.h:34
virtual ~MemoryAllocatorFactory()=default
An automatic releasing reservation of memory.
Definition: memory_allocator.h:78
MemoryAllocator & operator=(const MemoryAllocator &)=delete
MemoryAllocator()
Definition: memory_allocator.h:43
T * allocate(size_t n)
Definition: memory_allocator.h:160
Reservation MakeReservation(MemoryRequest request)
Reserve bytes from the quota and automatically release them when Reservation is destroyed.
Definition: memory_allocator.h:101
~MemoryAllocator()
Definition: memory_allocator.h:44