Go to the documentation of this file.
14 #ifndef GRPC_EVENT_ENGINE_MEMORY_ALLOCATOR_H
15 #define GRPC_EVENT_ENGINE_MEMORY_ALLOCATOR_H
21 #include <type_traits>
29 namespace experimental {
40 std::shared_ptr<internal::MemoryAllocatorImpl> allocator)
41 : allocator_(std::move(allocator)) {}
45 if (allocator_ !=
nullptr) allocator_->Shutdown();
58 auto a = std::move(allocator_);
59 if (a !=
nullptr) a->Shutdown();
68 void Release(
size_t n) {
return allocator_->Release(n); }
84 if (allocator_ !=
nullptr) allocator_->Release(size_);
89 Reservation(std::shared_ptr<internal::MemoryAllocatorImpl> allocator,
91 : allocator_(std::move(allocator)), size_(size) {}
93 std::shared_ptr<internal::MemoryAllocatorImpl> allocator_;
107 template <
typename T,
typename... Args>
108 typename std::enable_if<std::has_virtual_destructor<T>::value, T*>::type
New(
112 class Wrapper final :
public T {
114 explicit Wrapper(std::shared_ptr<internal::MemoryAllocatorImpl> allocator,
116 : T(std::forward<Args>(args)...), allocator_(std::move(allocator)) {}
117 ~Wrapper()
override { allocator_->Release(
sizeof(*
this)); }
120 const std::shared_ptr<internal::MemoryAllocatorImpl> allocator_;
123 return new Wrapper(allocator_, std::forward<Args>(args)...);
127 template <
typename T,
typename... Args>
129 return std::unique_ptr<T>(New<T>(std::forward<Args>(args)...));
137 return allocator_->MakeSlice(request);
141 template <
typename T>
150 template <
typename U>
155 return underlying_allocator_;
159 underlying_allocator_->
Reserve(n *
sizeof(T));
160 return static_cast<T*
>(::operator
new(n *
sizeof(T)));
163 ::operator
delete(p);
164 underlying_allocator_->
Release(n *
sizeof(T));
176 return allocator_.get();
180 return allocator_.get();
184 std::shared_ptr<internal::MemoryAllocatorImpl> allocator_;
189 template <
typename T>
190 class Vector :
public std::vector<T, MemoryAllocator::Container<T>> {
212 #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:136
const internal::MemoryAllocatorImpl * get_internal_impl_ptr() const
Definition: memory_allocator.h:179
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:175
A C++ allocator for containers of T.
Definition: memory_allocator.h:142
Vector(MemoryAllocator *allocator)
Definition: memory_allocator.h:192
~Reservation()
Definition: memory_allocator.h:83
Container(MemoryAllocator *underlying_allocator)
Construct the allocator: underlying_allocator is borrowed, and must outlive this object.
Definition: memory_allocator.h:148
void Release(size_t n)
Release some bytes that were previously reserved.
Definition: memory_allocator.h:68
size_t Reserve(MemoryRequest request)
Reserve bytes from the quota.
Definition: memory_allocator.h:65
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:190
void Reset()
Drop the underlying allocator and make this an empty object.
Definition: memory_allocator.h:57
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
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:128
Reservation request - how much memory do we want to allocate?
Definition: memory_request.h:27
T value_type
Definition: memory_allocator.h:144
void deallocate(T *p, size_t n)
Definition: memory_allocator.h:162
Container(const Container< U > &other)
Definition: memory_allocator.h:151
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:197
MemoryAllocator * underlying_allocator() const
Definition: memory_allocator.h:154
Definition: endpoint_config.h:24
Definition: memory_allocator.h:34
virtual ~MemoryAllocatorFactory()=default
An automatic releasing reservation of memory.
Definition: memory_allocator.h:76
MemoryAllocator & operator=(const MemoryAllocator &)=delete
MemoryAllocator()
Definition: memory_allocator.h:43
T * allocate(size_t n)
Definition: memory_allocator.h:158
Reservation MakeReservation(MemoryRequest request)
Reserve bytes from the quota and automatically release them when Reservation is destroyed.
Definition: memory_allocator.h:99
~MemoryAllocator()
Definition: memory_allocator.h:44