GRPC C++  1.66.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_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.
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>
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>
64  public:
65  virtual ~MessageAllocator() = default;
67 };
68 
69 } // namespace grpc
70 
71 #endif // GRPCPP_SUPPORT_MESSAGE_ALLOCATOR_H
grpc::MessageHolder::set_response
void set_response(ResponseT *response)
Definition: message_allocator.h:50
grpc::MessageHolder::set_request
void set_request(RequestT *request)
Definition: message_allocator.h:49
grpc
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm.h:33
grpc::RpcAllocatorState
Definition: message_allocator.h:26
grpc::MessageHolder::response
ResponseT * response()
Definition: message_allocator.h:46
grpc::MessageAllocator
Definition: message_allocator.h:63
grpc::RpcAllocatorState::FreeRequest
virtual void FreeRequest()
Definition: message_allocator.h:32
grpc::RpcAllocatorState::~RpcAllocatorState
virtual ~RpcAllocatorState()=default
grpc::MessageAllocator::AllocateMessages
virtual MessageHolder< RequestT, ResponseT > * AllocateMessages()=0
grpc::MessageHolder::request
RequestT * request()
Definition: message_allocator.h:45
grpc::MessageAllocator::~MessageAllocator
virtual ~MessageAllocator()=default
grpc::MessageHolder
Definition: message_allocator.h:39
grpc::MessageHolder::Release
virtual void Release()=0