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_SLICE_ALLOCATOR_H 15 #define GRPC_EVENT_ENGINE_SLICE_ALLOCATOR_H 16 17 #include <grpc/support/port_platform.h> 18 19 #include <functional> 20 21 #include "absl/status/status.h" 22 23 // forward-declaring an internal struct, not used publicly. 24 struct grpc_resource_quota; 25 struct grpc_resource_user; 26 27 namespace grpc_event_engine { 28 namespace experimental { 29 30 // TODO(nnoble): forward declared here, needs definition. 31 class SliceBuffer; 32 33 class SliceAllocator { 34 public: 35 // gRPC-internal constructor 36 explicit SliceAllocator(grpc_resource_user* user); 37 // Not copyable 38 SliceAllocator(SliceAllocator& other) = delete; 39 SliceAllocator& operator=(const SliceAllocator& other) = delete; 40 // Moveable 41 SliceAllocator(SliceAllocator&& other) = default; 42 SliceAllocator& operator=(SliceAllocator&& other) = default; 43 ~SliceAllocator(); 44 45 using AllocateCallback = 46 std::function<void(absl::Status, SliceBuffer* buffer)>; 47 // TODO(hork): explain what happens under resource exhaustion. 48 /// Requests \a size bytes from gRPC, and populates \a dest with the allocated 49 /// slices. Ownership of the \a SliceBuffer is not transferred. 50 absl::Status Allocate(size_t size, SliceBuffer* dest, 51 SliceAllocator::AllocateCallback cb); 52 53 private: 54 grpc_resource_user* resource_user_; 55 }; 56 57 class SliceAllocatorFactory { 58 public: 59 // gRPC-internal constructor 60 explicit SliceAllocatorFactory(grpc_resource_quota* quota); 61 // Not copyable 62 SliceAllocatorFactory(SliceAllocatorFactory& other) = delete; 63 SliceAllocatorFactory& operator=(const SliceAllocatorFactory& other) = delete; 64 // Moveable 65 SliceAllocatorFactory(SliceAllocatorFactory&& other) = default; 66 SliceAllocatorFactory& operator=(SliceAllocatorFactory&& other) = default; 67 ~SliceAllocatorFactory(); 68 69 /// On Endpoint creation, call \a CreateSliceAllocator with the name of the 70 /// endpoint peer (a URI string, most likely). Note: \a peer_name must outlive 71 /// the Endpoint. 72 SliceAllocator CreateSliceAllocator(absl::string_view peer_name); 73 74 private: 75 grpc_resource_quota* resource_quota_; 76 }; 77 78 } // namespace experimental 79 } // namespace grpc_event_engine 80 81 #endif // GRPC_EVENT_ENGINE_SLICE_ALLOCATOR_H 82