message_allocator.h 2.6 KB

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