Debian Codegraph @

include/grpcpp/support/message_allocator.h

raw
1 /*
2 *
3 * Copyright 2019 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #ifndef GRPCPP_SUPPORT_MESSAGE_ALLOCATOR_H
20 #define GRPCPP_SUPPORT_MESSAGE_ALLOCATOR_H
21
22 namespace grpc {
23
24 // NOTE: This is an API for advanced users who need custom allocators.
25 // Per rpc struct for the allocator. This is the interface to return to user.
26 class RpcAllocatorState {
27 public:
28 virtual ~RpcAllocatorState() = default;
29 // Optionally deallocate request early to reduce the size of working set.
30 // A custom MessageAllocator needs to be registered to make use of this.
31 // This is not abstract because implementing it is optional.
32 virtual void FreeRequest() {}
33 };
34
35 // This is the interface returned by the allocator.
36 // grpc library will call the methods to get request/response pointers and to
37 // release the object when it is done.
38 template <typename RequestT, typename ResponseT>
39 class MessageHolder : public RpcAllocatorState {
40 public:
41 // Release this object. For example, if the custom allocator's
42 // AllocateMessasge creates an instance of a subclass with new, the Release()
43 // should do a "delete this;".
44 virtual void Release() = 0;
45 RequestT* request() { return request_; }
46 ResponseT* response() { return response_; }
47
48 protected:
49 void set_request(RequestT* request) { request_ = request; }
50 void set_response(ResponseT* response) { response_ = response; }
51
52 private:
53 // NOTE: subclasses should set these pointers.
54 RequestT* request_;
55 ResponseT* response_;
56 };
57
58 // A custom allocator can be set via the generated code to a callback unary
59 // method, such as SetMessageAllocatorFor_Echo(custom_allocator). The allocator
60 // needs to be alive for the lifetime of the server.
61 // Implementations need to be thread-safe.
62 template <typename RequestT, typename ResponseT>
63 class MessageAllocator {
64 public:
65 virtual ~MessageAllocator() = default;
66 virtual MessageHolder<RequestT, ResponseT>* AllocateMessages() = 0;
67 };
68
69 } // namespace grpc
70
71 #endif // GRPCPP_SUPPORT_MESSAGE_ALLOCATOR_H