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


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:37