96c1d1780696a304370e2dac651a323e8b6e7e6f
[platform/upstream/grpc.git] / include / grpc / event_engine / slice_allocator.h
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 struct grpc_slice_buffer;
27
28 namespace grpc_event_engine {
29 namespace experimental {
30
31 // TODO(hork): stubbed out here, to be replaced with a real version in next PR.
32 class SliceBuffer {
33  public:
34   SliceBuffer() { abort(); }
35   explicit SliceBuffer(grpc_slice_buffer*) { abort(); }
36 };
37
38 class SliceAllocator {
39  public:
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.
44   ///
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;
52 };
53
54 class SliceAllocatorFactory {
55  public:
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;
61 };
62
63 }  // namespace experimental
64 }  // namespace grpc_event_engine
65
66 #endif  // GRPC_EVENT_ENGINE_SLICE_ALLOCATOR_H