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_INTERNAL_MEMORY_ALLOCATOR_IMPL_H 15 #define GRPC_EVENT_ENGINE_INTERNAL_MEMORY_ALLOCATOR_IMPL_H 16 17 #include <grpc/event_engine/memory_request.h> 18 #include <grpc/slice.h> 19 #include <grpc/support/port_platform.h> 20 21 #include <algorithm> 22 #include <memory> 23 #include <type_traits> 24 #include <vector> 25 26 namespace grpc_event_engine { 27 namespace experimental { 28 29 namespace internal { 30 31 /// Underlying memory allocation interface. 32 /// This is an internal interface, not intended to be used by users. 33 /// Its interface is subject to change at any time. 34 class MemoryAllocatorImpl 35 : public std::enable_shared_from_this<MemoryAllocatorImpl> { 36 public: MemoryAllocatorImpl()37 MemoryAllocatorImpl() {} ~MemoryAllocatorImpl()38 virtual ~MemoryAllocatorImpl() {} 39 40 MemoryAllocatorImpl(const MemoryAllocatorImpl&) = delete; 41 MemoryAllocatorImpl& operator=(const MemoryAllocatorImpl&) = delete; 42 43 /// Reserve bytes from the quota. 44 /// If we enter overcommit, reclamation will begin concurrently. 45 /// Returns the number of bytes reserved. 46 /// If MemoryRequest is invalid, this function will abort. 47 /// If MemoryRequest is valid, this function is infallible, and will always 48 /// succeed at reserving the some number of bytes between request.min() and 49 /// request.max() inclusively. 50 virtual size_t Reserve(MemoryRequest request) = 0; 51 52 /// Allocate a slice, using MemoryRequest to size the number of returned 53 /// bytes. For a variable length request, check the returned slice length to 54 /// verify how much memory was allocated. Takes care of reserving memory for 55 /// any relevant control structures also. 56 virtual grpc_slice MakeSlice(MemoryRequest request) = 0; 57 58 /// Release some bytes that were previously reserved. 59 /// If more bytes are released than were reserved, we will have undefined 60 /// behavior. 61 virtual void Release(size_t n) = 0; 62 63 /// Shutdown this allocator. 64 /// Further usage of Reserve() is undefined behavior. 65 virtual void Shutdown() = 0; 66 }; 67 68 } // namespace internal 69 70 } // namespace experimental 71 } // namespace grpc_event_engine 72 73 #endif // GRPC_EVENT_ENGINE_INTERNAL_MEMORY_ALLOCATOR_IMPL_H 74