GRPC C++  1.26.0
message_allocator.h
Go to the documentation of this file.
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_IMPL_CODEGEN_MESSAGE_ALLOCATOR_H
20 #define GRPCPP_IMPL_CODEGEN_MESSAGE_ALLOCATOR_H
21 
22 namespace grpc {
23 namespace experimental {
24 
25 // NOTE: This is an API for advanced users who need custom allocators.
26 // Per rpc struct for the allocator. This is the interface to return to user.
28  public:
29  virtual ~RpcAllocatorState() = default;
30  // Optionally deallocate request early to reduce the size of working set.
31  // A custom MessageAllocator needs to be registered to make use of this.
32  // This is not abstract because implementing it is optional.
33  virtual void FreeRequest() {}
34 };
35 
36 // This is the interface returned by the allocator.
37 // grpc library will call the methods to get request/response pointers and to
38 // release the object when it is done.
39 template <typename RequestT, typename ResponseT>
41  public:
42  // Release this object. For example, if the custom allocator's
43  // AllocateMessasge creates an instance of a subclass with new, the Release()
44  // should do a "delete this;".
45  virtual void Release() = 0;
46  RequestT* request() { return request_; }
47  ResponseT* response() { return response_; }
48 
49  protected:
50  void set_request(RequestT* request) { request_ = request; }
51  void set_response(ResponseT* response) { response_ = response; }
52 
53  private:
54  // NOTE: subclasses should set these pointers.
55  RequestT* request_;
56  ResponseT* response_;
57 };
58 
59 // A custom allocator can be set via the generated code to a callback unary
60 // method, such as SetMessageAllocatorFor_Echo(custom_allocator). The allocator
61 // needs to be alive for the lifetime of the server.
62 // Implementations need to be thread-safe.
63 template <typename RequestT, typename ResponseT>
65  public:
66  virtual ~MessageAllocator() = default;
68 };
69 
70 } // namespace experimental
71 } // namespace grpc
72 
73 #endif // GRPCPP_IMPL_CODEGEN_MESSAGE_ALLOCATOR_H
Definition: message_allocator.h:64
void set_response(ResponseT *response)
Definition: message_allocator.h:51
ResponseT * response()
Definition: message_allocator.h:47
RequestT * request()
Definition: message_allocator.h:46
void set_request(RequestT *request)
Definition: message_allocator.h:50
This header provides an object that reads bytes directly from a grpc::ByteBuffer, via the ZeroCopyInp...
Definition: alarm.h:24
Definition: message_allocator.h:40
virtual MessageHolder< RequestT, ResponseT > * AllocateMessages()=0
virtual void FreeRequest()
Definition: message_allocator.h:33
Definition: message_allocator.h:27