1 // Copyright 2021 The gRPC Authors
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
17 #include <grpc/support/port_platform.h>
21 #include "absl/status/status.h"
23 // forward-declaring an internal struct, not used publicly.
24 struct grpc_resource_quota;
25 struct grpc_resource_user;
26 struct grpc_slice_buffer;
28 namespace grpc_event_engine {
29 namespace experimental {
31 // TODO(hork): stubbed out here, to be replaced with a real version in next PR.
34 SliceBuffer() { abort(); }
35 explicit SliceBuffer(grpc_slice_buffer*) { abort(); }
38 class SliceAllocator {
40 using AllocateCallback = std::function<void(absl::Status)>;
41 virtual ~SliceAllocator() = default;
42 /// Requests \a size bytes from gRPC, and populates \a dest with the allocated
43 /// slices. Ownership of the \a SliceBuffer is not transferred.
45 /// gRPC provides a ResourceQuota system to cap the amount of memory used by
46 /// the library. When a memory limit has been reached, slice allocation is
47 /// interrupted to attempt to reclaim memory from participating gRPC
48 /// internals. When there is sufficient memory available, slice allocation
49 /// proceeds as normal.
50 virtual absl::Status Allocate(size_t size, SliceBuffer* dest,
51 SliceAllocator::AllocateCallback cb) = 0;
54 class SliceAllocatorFactory {
56 virtual ~SliceAllocatorFactory() = default;
57 /// On Endpoint creation, call \a CreateSliceAllocator with the name of the
58 /// endpoint peer (a URI string, most likely).
59 virtual std::unique_ptr<SliceAllocator> CreateSliceAllocator(
60 absl::string_view peer_name) = 0;
63 } // namespace experimental
64 } // namespace grpc_event_engine
66 #endif // GRPC_EVENT_ENGINE_SLICE_ALLOCATOR_H