core_codegen_interface.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. *
  3. * Copyright 2015 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_CORE_CODEGEN_INTERFACE_H
  19. #define GRPCPP_IMPL_CODEGEN_CORE_CODEGEN_INTERFACE_H
  20. // IWYU pragma: private
  21. #include <grpc/impl/codegen/byte_buffer.h>
  22. #include <grpc/impl/codegen/byte_buffer_reader.h>
  23. #include <grpc/impl/codegen/grpc_types.h>
  24. #include <grpc/impl/codegen/sync.h>
  25. #include <grpcpp/impl/codegen/config.h>
  26. #include <grpcpp/impl/codegen/status.h>
  27. namespace grpc {
  28. /// Interface between the codegen library and the minimal subset of core
  29. /// features required by the generated code.
  30. ///
  31. /// All undocumented methods are simply forwarding the call to their namesakes.
  32. /// Please refer to their corresponding documentation for details.
  33. ///
  34. /// \warning This interface should be considered internal and private.
  35. class CoreCodegenInterface {
  36. public:
  37. virtual ~CoreCodegenInterface() = default;
  38. /// Upon a failed assertion, log the error.
  39. virtual void assert_fail(const char* failed_assertion, const char* file,
  40. int line) = 0;
  41. virtual const grpc_completion_queue_factory*
  42. grpc_completion_queue_factory_lookup(
  43. const grpc_completion_queue_attributes* attributes) = 0;
  44. virtual grpc_completion_queue* grpc_completion_queue_create(
  45. const grpc_completion_queue_factory* factory,
  46. const grpc_completion_queue_attributes* attributes, void* reserved) = 0;
  47. virtual grpc_completion_queue* grpc_completion_queue_create_for_next(
  48. void* reserved) = 0;
  49. virtual grpc_completion_queue* grpc_completion_queue_create_for_pluck(
  50. void* reserved) = 0;
  51. virtual void grpc_completion_queue_shutdown(grpc_completion_queue* cq) = 0;
  52. virtual void grpc_completion_queue_destroy(grpc_completion_queue* cq) = 0;
  53. virtual grpc_event grpc_completion_queue_pluck(grpc_completion_queue* cq,
  54. void* tag,
  55. gpr_timespec deadline,
  56. void* reserved) = 0;
  57. virtual void* gpr_malloc(size_t size) = 0;
  58. virtual void gpr_free(void* p) = 0;
  59. // These are only to be used to fix edge cases involving grpc_init and
  60. // grpc_shutdown. Calling grpc_init from the codegen interface before
  61. // the real grpc_init is called will cause a crash, so if you use this
  62. // function, ensure that it is not the first call to grpc_init.
  63. virtual void grpc_init() = 0;
  64. virtual void grpc_shutdown() = 0;
  65. virtual void gpr_mu_init(gpr_mu* mu) = 0;
  66. virtual void gpr_mu_destroy(gpr_mu* mu) = 0;
  67. virtual void gpr_mu_lock(gpr_mu* mu) = 0;
  68. virtual void gpr_mu_unlock(gpr_mu* mu) = 0;
  69. virtual void gpr_cv_init(gpr_cv* cv) = 0;
  70. virtual void gpr_cv_destroy(gpr_cv* cv) = 0;
  71. virtual int gpr_cv_wait(gpr_cv* cv, gpr_mu* mu,
  72. gpr_timespec abs_deadline) = 0;
  73. virtual void gpr_cv_signal(gpr_cv* cv) = 0;
  74. virtual void gpr_cv_broadcast(gpr_cv* cv) = 0;
  75. virtual grpc_byte_buffer* grpc_byte_buffer_copy(grpc_byte_buffer* bb) = 0;
  76. virtual void grpc_byte_buffer_destroy(grpc_byte_buffer* bb) = 0;
  77. virtual size_t grpc_byte_buffer_length(grpc_byte_buffer* bb)
  78. GRPC_MUST_USE_RESULT = 0;
  79. virtual int grpc_byte_buffer_reader_init(grpc_byte_buffer_reader* reader,
  80. grpc_byte_buffer* buffer)
  81. GRPC_MUST_USE_RESULT = 0;
  82. virtual void grpc_byte_buffer_reader_destroy(
  83. grpc_byte_buffer_reader* reader) = 0;
  84. virtual int grpc_byte_buffer_reader_next(grpc_byte_buffer_reader* reader,
  85. grpc_slice* slice) = 0;
  86. virtual int grpc_byte_buffer_reader_peek(grpc_byte_buffer_reader* reader,
  87. grpc_slice** slice) = 0;
  88. virtual grpc_byte_buffer* grpc_raw_byte_buffer_create(grpc_slice* slice,
  89. size_t nslices) = 0;
  90. virtual grpc_slice grpc_slice_new_with_user_data(void* p, size_t len,
  91. void (*destroy)(void*),
  92. void* user_data) = 0;
  93. virtual grpc_slice grpc_slice_new_with_len(void* p, size_t len,
  94. void (*destroy)(void*,
  95. size_t)) = 0;
  96. virtual grpc_call_error grpc_call_start_batch(grpc_call* call,
  97. const grpc_op* ops, size_t nops,
  98. void* tag, void* reserved) = 0;
  99. virtual grpc_call_error grpc_call_cancel_with_status(grpc_call* call,
  100. grpc_status_code status,
  101. const char* description,
  102. void* reserved) = 0;
  103. virtual int grpc_call_failed_before_recv_message(const grpc_call* c) = 0;
  104. virtual void grpc_call_ref(grpc_call* call) = 0;
  105. virtual void grpc_call_unref(grpc_call* call) = 0;
  106. virtual void* grpc_call_arena_alloc(grpc_call* call, size_t length) = 0;
  107. virtual const char* grpc_call_error_to_string(grpc_call_error error) = 0;
  108. virtual grpc_slice grpc_empty_slice() = 0;
  109. virtual grpc_slice grpc_slice_malloc(size_t length) = 0;
  110. virtual void grpc_slice_unref(grpc_slice slice) = 0;
  111. virtual grpc_slice grpc_slice_ref(grpc_slice slice) = 0;
  112. virtual grpc_slice grpc_slice_split_tail(grpc_slice* s, size_t split) = 0;
  113. virtual grpc_slice grpc_slice_split_head(grpc_slice* s, size_t split) = 0;
  114. virtual grpc_slice grpc_slice_sub(grpc_slice s, size_t begin, size_t end) = 0;
  115. virtual void grpc_slice_buffer_add(grpc_slice_buffer* sb,
  116. grpc_slice slice) = 0;
  117. virtual void grpc_slice_buffer_pop(grpc_slice_buffer* sb) = 0;
  118. virtual grpc_slice grpc_slice_from_static_buffer(const void* buffer,
  119. size_t length) = 0;
  120. virtual grpc_slice grpc_slice_from_copied_buffer(const void* buffer,
  121. size_t length) = 0;
  122. virtual void grpc_metadata_array_init(grpc_metadata_array* array) = 0;
  123. virtual void grpc_metadata_array_destroy(grpc_metadata_array* array) = 0;
  124. virtual const Status& ok() = 0;
  125. virtual const Status& cancelled() = 0;
  126. virtual gpr_timespec gpr_inf_future(gpr_clock_type type) = 0;
  127. virtual gpr_timespec gpr_time_0(gpr_clock_type type) = 0;
  128. };
  129. extern CoreCodegenInterface* g_core_codegen_interface;
  130. /// Codegen specific version of \a GPR_ASSERT.
  131. #define GPR_CODEGEN_ASSERT(x) \
  132. do { \
  133. if (GPR_UNLIKELY(!(x))) { \
  134. grpc::g_core_codegen_interface->assert_fail(#x, __FILE__, __LINE__); \
  135. } \
  136. } while (0)
  137. /// Codegen specific version of \a GPR_DEBUG_ASSERT.
  138. #ifndef NDEBUG
  139. #define GPR_CODEGEN_DEBUG_ASSERT(x) GPR_CODEGEN_ASSERT(x)
  140. #else
  141. #define GPR_CODEGEN_DEBUG_ASSERT(x) \
  142. do { \
  143. } while (0)
  144. #endif
  145. } // namespace grpc
  146. #endif // GRPCPP_IMPL_CODEGEN_CORE_CODEGEN_INTERFACE_H