diff --git a/include/grpcpp/completion_queue.h b/include/grpcpp/completion_queue.h index 43a90e1b63b..4f26e2750c4 100644 --- a/include/grpcpp/completion_queue.h +++ b/include/grpcpp/completion_queue.h @@ -36,6 +36,7 @@ #include #include +#include #include #include #include @@ -180,9 +181,8 @@ class CompletionQueue : private grpc::internal::GrpcLibrary { // TIMEOUT - we passed infinity time => queue has been shutdown, return // false. // GOT_EVENT - we actually got an event, return true. - return (AsyncNextInternal(tag, ok, - grpc::g_core_codegen_interface->gpr_inf_future( - GPR_CLOCK_REALTIME)) == GOT_EVENT); + return (AsyncNextInternal(tag, ok, gpr_inf_future(GPR_CLOCK_REALTIME)) == + GOT_EVENT); } /// Read from the queue, blocking up to \a deadline (or the queue's shutdown). @@ -243,7 +243,7 @@ class CompletionQueue : private grpc::internal::GrpcLibrary { /// instance. /// /// \warning Remember that the returned instance is owned. No transfer of - /// owership is performed. + /// ownership is performed. grpc_completion_queue* cq() { return cq_; } protected: @@ -319,8 +319,7 @@ class CompletionQueue : private grpc::internal::GrpcLibrary { /// Wraps \a grpc_completion_queue_pluck. /// \warning Must not be mixed with calls to \a Next. bool Pluck(grpc::internal::CompletionQueueTag* tag) { - auto deadline = - grpc::g_core_codegen_interface->gpr_inf_future(GPR_CLOCK_REALTIME); + auto deadline = gpr_inf_future(GPR_CLOCK_REALTIME); while (true) { auto ev = grpc_completion_queue_pluck(cq_, tag, deadline, nullptr); bool ok = ev.success != 0; @@ -341,8 +340,7 @@ class CompletionQueue : private grpc::internal::GrpcLibrary { /// timeout. i.e: /// TryPluck(tag, gpr_time_0(GPR_CLOCK_REALTIME)) void TryPluck(grpc::internal::CompletionQueueTag* tag) { - auto deadline = - grpc::g_core_codegen_interface->gpr_time_0(GPR_CLOCK_REALTIME); + auto deadline = gpr_time_0(GPR_CLOCK_REALTIME); auto ev = grpc_completion_queue_pluck(cq_, tag, deadline, nullptr); if (ev.type == GRPC_QUEUE_TIMEOUT) return; bool ok = ev.success != 0; @@ -373,7 +371,7 @@ class CompletionQueue : private grpc::internal::GrpcLibrary { /// queue should not really shutdown until all avalanching operations have /// been finalized. Note that we maintain the requirement that an avalanche /// registration must take place before CQ shutdown (which must be maintained - /// elsehwere) + /// elsewhere) void InitialAvalanching() { gpr_atm_rel_store(&avalanches_in_flight_, gpr_atm{1}); } diff --git a/include/grpcpp/impl/call_op_set.h b/include/grpcpp/impl/call_op_set.h index 5a19e1bd145..372f7c1c500 100644 --- a/include/grpcpp/impl/call_op_set.h +++ b/include/grpcpp/impl/call_op_set.h @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -68,9 +69,8 @@ inline grpc_metadata* FillMetadataArray( metadata_array[i].value = SliceReferencingString(iter->second); } if (!optional_error_details.empty()) { - metadata_array[i].key = - g_core_codegen_interface->grpc_slice_from_static_buffer( - kBinaryErrorDetailsKey, sizeof(kBinaryErrorDetailsKey) - 1); + metadata_array[i].key = grpc_slice_from_static_buffer( + kBinaryErrorDetailsKey, sizeof(kBinaryErrorDetailsKey) - 1); metadata_array[i].value = SliceReferencingString(optional_error_details); } return metadata_array; @@ -778,7 +778,7 @@ class CallOpClientRecvStatus { client_context_ = context; metadata_map_ = &client_context_->trailing_metadata_; recv_status_ = status; - error_message_ = g_core_codegen_interface->grpc_empty_slice(); + error_message_ = grpc_empty_slice(); } protected: @@ -814,7 +814,7 @@ class CallOpClientRecvStatus { } // TODO(soheil): Find callers that set debug string even for status OK, // and fix them. - g_core_codegen_interface->grpc_slice_unref(error_message_); + grpc_slice_unref(error_message_); } void SetInterceptionHookPoint( diff --git a/include/grpcpp/impl/codegen/core_codegen.h b/include/grpcpp/impl/codegen/core_codegen.h index 4fe893fca32..494e78da5d3 100644 --- a/include/grpcpp/impl/codegen/core_codegen.h +++ b/include/grpcpp/impl/codegen/core_codegen.h @@ -33,27 +33,6 @@ namespace grpc { /// Implementation of the core codegen interface. class CoreCodegen final : public CoreCodegenInterface { private: - grpc_slice grpc_empty_slice() override; - grpc_slice grpc_slice_malloc(size_t length) override; - void grpc_slice_unref(grpc_slice slice) override; - grpc_slice grpc_slice_ref(grpc_slice slice) override; - grpc_slice grpc_slice_split_tail(grpc_slice* s, size_t split) override; - grpc_slice grpc_slice_split_head(grpc_slice* s, size_t split) override; - grpc_slice grpc_slice_sub(grpc_slice s, size_t begin, size_t end) override; - void grpc_slice_buffer_add(grpc_slice_buffer* sb, grpc_slice slice) override; - void grpc_slice_buffer_pop(grpc_slice_buffer* sb) override; - void grpc_slice_buffer_add_indexed(grpc_slice_buffer* sb, - grpc_slice slice) override; - grpc_slice grpc_slice_from_static_buffer(const void* buffer, - size_t length) override; - grpc_slice grpc_slice_from_copied_buffer(const void* buffer, - size_t length) override; - void grpc_metadata_array_init(grpc_metadata_array* array) override; - void grpc_metadata_array_destroy(grpc_metadata_array* array) override; - - gpr_timespec gpr_inf_future(gpr_clock_type type) override; - gpr_timespec gpr_time_0(gpr_clock_type type) override; - const Status& ok() override; const Status& cancelled() override; diff --git a/include/grpcpp/impl/codegen/core_codegen_interface.h b/include/grpcpp/impl/codegen/core_codegen_interface.h index e05cbd47e01..ec597edfc85 100644 --- a/include/grpcpp/impl/codegen/core_codegen_interface.h +++ b/include/grpcpp/impl/codegen/core_codegen_interface.h @@ -21,12 +21,12 @@ // IWYU pragma: private -#include -#include +#include +#include #include -#include -#include -#include +#include +#include +#include namespace grpc { @@ -45,31 +45,8 @@ class CoreCodegenInterface { virtual void assert_fail(const char* failed_assertion, const char* file, int line) = 0; - virtual grpc_slice grpc_empty_slice() = 0; - virtual grpc_slice grpc_slice_malloc(size_t length) = 0; - virtual void grpc_slice_unref(grpc_slice slice) = 0; - virtual grpc_slice grpc_slice_ref(grpc_slice slice) = 0; - virtual grpc_slice grpc_slice_split_tail(grpc_slice* s, size_t split) = 0; - virtual grpc_slice grpc_slice_split_head(grpc_slice* s, size_t split) = 0; - virtual grpc_slice grpc_slice_sub(grpc_slice s, size_t begin, size_t end) = 0; - virtual void grpc_slice_buffer_add(grpc_slice_buffer* sb, - grpc_slice slice) = 0; - virtual void grpc_slice_buffer_add_indexed(grpc_slice_buffer* sb, - grpc_slice slice) = 0; - virtual void grpc_slice_buffer_pop(grpc_slice_buffer* sb) = 0; - virtual grpc_slice grpc_slice_from_static_buffer(const void* buffer, - size_t length) = 0; - virtual grpc_slice grpc_slice_from_copied_buffer(const void* buffer, - size_t length) = 0; - - virtual void grpc_metadata_array_init(grpc_metadata_array* array) = 0; - virtual void grpc_metadata_array_destroy(grpc_metadata_array* array) = 0; - virtual const Status& ok() = 0; virtual const Status& cancelled() = 0; - - virtual gpr_timespec gpr_inf_future(gpr_clock_type type) = 0; - virtual gpr_timespec gpr_time_0(gpr_clock_type type) = 0; }; extern CoreCodegenInterface* g_core_codegen_interface; diff --git a/include/grpcpp/impl/metadata_map.h b/include/grpcpp/impl/metadata_map.h index f5478bbd0d4..5aeec99f538 100644 --- a/include/grpcpp/impl/metadata_map.h +++ b/include/grpcpp/impl/metadata_map.h @@ -21,6 +21,7 @@ #include +#include #include #include @@ -81,9 +82,7 @@ class MetadataMap { grpc_metadata_array arr_; std::multimap map_; - void Destroy() { - g_core_codegen_interface->grpc_metadata_array_destroy(&arr_); - } + void Destroy() { grpc_metadata_array_destroy(&arr_); } void Setup() { memset(&arr_, 0, sizeof(arr_)); } diff --git a/include/grpcpp/impl/sync.h b/include/grpcpp/impl/sync.h index 1e293971e41..766cb9f653a 100644 --- a/include/grpcpp/impl/sync.h +++ b/include/grpcpp/impl/sync.h @@ -31,6 +31,7 @@ #include #include +#include #include // The core library is not accessible in C++ codegen headers, and vice versa. @@ -127,8 +128,7 @@ class CondVar { void SignalAll() { gpr_cv_broadcast(&cv_); } void Wait(Mutex* mu) { - gpr_cv_wait(&cv_, &mu->mu_, - g_core_codegen_interface->gpr_inf_future(GPR_CLOCK_REALTIME)); + gpr_cv_wait(&cv_, &mu->mu_, gpr_inf_future(GPR_CLOCK_REALTIME)); } private: diff --git a/include/grpcpp/server_interface.h b/include/grpcpp/server_interface.h index 1fec93444c7..e1a8a8f6790 100644 --- a/include/grpcpp/server_interface.h +++ b/include/grpcpp/server_interface.h @@ -23,6 +23,7 @@ #include #include +#include #include #include #include @@ -103,10 +104,7 @@ class ServerInterface : public internal::CallHook { /// All completion queue associated with the server (for example, for async /// serving) must be shutdown *after* this method has returned: /// See \a ServerBuilder::AddCompletionQueue for details. - void Shutdown() { - ShutdownInternal( - g_core_codegen_interface->gpr_inf_future(GPR_CLOCK_MONOTONIC)); - } + void Shutdown() { ShutdownInternal(gpr_inf_future(GPR_CLOCK_MONOTONIC)); } /// Block waiting for all work to complete. /// diff --git a/include/grpcpp/support/proto_buffer_writer.h b/include/grpcpp/support/proto_buffer_writer.h index f10dcaca4dd..6b4897db69b 100644 --- a/include/grpcpp/support/proto_buffer_writer.h +++ b/include/grpcpp/support/proto_buffer_writer.h @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -72,7 +73,7 @@ class ProtoBufferWriter : public grpc::protobuf::io::ZeroCopyOutputStream { ~ProtoBufferWriter() override { if (have_backup_) { - g_core_codegen_interface->grpc_slice_unref(backup_slice_); + grpc_slice_unref(backup_slice_); } } @@ -99,10 +100,9 @@ class ProtoBufferWriter : public grpc::protobuf::io::ZeroCopyOutputStream { // But make sure the allocated slice is not inlined. size_t allocate_length = remain > static_cast(block_size_) ? block_size_ : remain; - slice_ = g_core_codegen_interface->grpc_slice_malloc( - allocate_length > GRPC_SLICE_INLINED_SIZE - ? allocate_length - : GRPC_SLICE_INLINED_SIZE + 1); + slice_ = grpc_slice_malloc(allocate_length > GRPC_SLICE_INLINED_SIZE + ? allocate_length + : GRPC_SLICE_INLINED_SIZE + 1); } *data = GRPC_SLICE_START_PTR(slice_); // On win x64, int is only 32bit @@ -112,8 +112,7 @@ class ProtoBufferWriter : public grpc::protobuf::io::ZeroCopyOutputStream { // previous slice. Therefore, use grpc_slice_buffer_add_indexed method to // ensure the slice gets added at a separate index. It can then be kept // around and popped later in the BackUp function. - g_core_codegen_interface->grpc_slice_buffer_add_indexed(slice_buffer_, - slice_); + grpc_slice_buffer_add_indexed(slice_buffer_, slice_); return true; } @@ -133,13 +132,13 @@ class ProtoBufferWriter : public grpc::protobuf::io::ZeroCopyOutputStream { /// 3. Add the needed part back to the slice buffer /// 4. Mark that we still have the remaining part (for later use/unref) GPR_CODEGEN_ASSERT(count <= static_cast(GRPC_SLICE_LENGTH(slice_))); - g_core_codegen_interface->grpc_slice_buffer_pop(slice_buffer_); + grpc_slice_buffer_pop(slice_buffer_); if (static_cast(count) == GRPC_SLICE_LENGTH(slice_)) { backup_slice_ = slice_; } else { - backup_slice_ = g_core_codegen_interface->grpc_slice_split_tail( - &slice_, GRPC_SLICE_LENGTH(slice_) - count); - g_core_codegen_interface->grpc_slice_buffer_add(slice_buffer_, slice_); + backup_slice_ = + grpc_slice_split_tail(&slice_, GRPC_SLICE_LENGTH(slice_) - count); + grpc_slice_buffer_add(slice_buffer_, slice_); } // It's dangerous to keep an inlined grpc_slice as the backup slice, since // on a following Next() call, a reference will be returned to this slice diff --git a/include/grpcpp/support/slice.h b/include/grpcpp/support/slice.h index 0686845e68f..582e3c259d2 100644 --- a/include/grpcpp/support/slice.h +++ b/include/grpcpp/support/slice.h @@ -34,48 +34,44 @@ namespace grpc { class Slice final { public: /// Construct an empty slice. - Slice() : slice_(g_core_codegen_interface->grpc_empty_slice()) {} + Slice() : slice_(grpc_empty_slice()) {} /// Destructor - drops one reference. - ~Slice() { g_core_codegen_interface->grpc_slice_unref(slice_); } + ~Slice() { grpc_slice_unref(slice_); } enum AddRef { ADD_REF }; /// Construct a slice from \a slice, adding a reference. - Slice(grpc_slice slice, AddRef) - : slice_(g_core_codegen_interface->grpc_slice_ref(slice)) {} + Slice(grpc_slice slice, AddRef) : slice_(grpc_slice_ref(slice)) {} enum StealRef { STEAL_REF }; /// Construct a slice from \a slice, stealing a reference. Slice(grpc_slice slice, StealRef) : slice_(slice) {} /// Allocate a slice of specified size - explicit Slice(size_t len) - : slice_(g_core_codegen_interface->grpc_slice_malloc(len)) {} + explicit Slice(size_t len) : slice_(grpc_slice_malloc(len)) {} /// Construct a slice from a copied buffer Slice(const void* buf, size_t len) - : slice_(g_core_codegen_interface->grpc_slice_from_copied_buffer( - reinterpret_cast(buf), len)) {} + : slice_(grpc_slice_from_copied_buffer(reinterpret_cast(buf), + len)) {} /// Construct a slice from a copied string /* NOLINTNEXTLINE(google-explicit-constructor) */ Slice(const std::string& str) - : slice_(g_core_codegen_interface->grpc_slice_from_copied_buffer( - str.c_str(), str.length())) {} + : slice_(grpc_slice_from_copied_buffer(str.c_str(), str.length())) {} enum StaticSlice { STATIC_SLICE }; /// Construct a slice from a static buffer Slice(const void* buf, size_t len, StaticSlice) - : slice_(g_core_codegen_interface->grpc_slice_from_static_buffer( - reinterpret_cast(buf), len)) {} + : slice_(grpc_slice_from_static_buffer(reinterpret_cast(buf), + len)) {} /// Copy constructor, adds a reference. - Slice(const Slice& other) - : slice_(g_core_codegen_interface->grpc_slice_ref(other.slice_)) {} + Slice(const Slice& other) : slice_(grpc_slice_ref(other.slice_)) {} /// Move constructor, steals a reference. Slice(Slice&& other) noexcept : slice_(other.slice_) { - other.slice_ = g_core_codegen_interface->grpc_empty_slice(); + other.slice_ = grpc_empty_slice(); } /// Assignment, reference count is unchanged. @@ -111,14 +107,11 @@ class Slice final { /// Returns a substring of the `slice` as another slice. Slice sub(size_t begin, size_t end) const { - return Slice(g_core_codegen_interface->grpc_slice_sub(slice_, begin, end), - STEAL_REF); + return Slice(grpc_slice_sub(slice_, begin, end), STEAL_REF); } /// Raw C slice. Caller needs to call grpc_slice_unref when done. - grpc_slice c_slice() const { - return g_core_codegen_interface->grpc_slice_ref(slice_); - } + grpc_slice c_slice() const { return grpc_slice_ref(slice_); } private: friend class ByteBuffer; @@ -138,13 +131,11 @@ inline std::string StringFromCopiedSlice(grpc_slice slice) { } inline grpc_slice SliceReferencingString(const std::string& str) { - return g_core_codegen_interface->grpc_slice_from_static_buffer(str.data(), - str.length()); + return grpc_slice_from_static_buffer(str.data(), str.length()); } inline grpc_slice SliceFromCopiedString(const std::string& str) { - return g_core_codegen_interface->grpc_slice_from_copied_buffer(str.data(), - str.length()); + return grpc_slice_from_copied_buffer(str.data(), str.length()); } } // namespace grpc diff --git a/src/cpp/client/channel_cc.cc b/src/cpp/client/channel_cc.cc index 08a91d57d70..c6373e631cf 100644 --- a/src/cpp/client/channel_cc.cc +++ b/src/cpp/client/channel_cc.cc @@ -74,7 +74,7 @@ Channel::~Channel() { namespace { inline grpc_slice SliceFromArray(const char* arr, size_t len) { - return g_core_codegen_interface->grpc_slice_from_copied_buffer(arr, len); + return grpc_slice_from_copied_buffer(arr, len); } std::string GetChannelInfoField(grpc_channel* channel, diff --git a/src/cpp/common/core_codegen.cc b/src/cpp/common/core_codegen.cc index e4960b73063..9bd101a07c7 100644 --- a/src/cpp/common/core_codegen.cc +++ b/src/cpp/common/core_codegen.cc @@ -34,77 +34,10 @@ namespace grpc { -grpc_slice CoreCodegen::grpc_empty_slice() { return ::grpc_empty_slice(); } - -grpc_slice CoreCodegen::grpc_slice_malloc(size_t length) { - return ::grpc_slice_malloc(length); -} - -void CoreCodegen::grpc_slice_unref(grpc_slice slice) { - ::grpc_slice_unref(slice); -} - -grpc_slice CoreCodegen::grpc_slice_ref(grpc_slice slice) { - return ::grpc_slice_ref(slice); -} - -grpc_slice CoreCodegen::grpc_slice_split_tail(grpc_slice* s, size_t split) { - return ::grpc_slice_split_tail(s, split); -} - -grpc_slice CoreCodegen::grpc_slice_split_head(grpc_slice* s, size_t split) { - return ::grpc_slice_split_head(s, split); -} - -grpc_slice CoreCodegen::grpc_slice_sub(grpc_slice s, size_t begin, size_t end) { - return ::grpc_slice_sub(s, begin, end); -} - -grpc_slice CoreCodegen::grpc_slice_from_static_buffer(const void* buffer, - size_t length) { - return ::grpc_slice_from_static_buffer(buffer, length); -} - -grpc_slice CoreCodegen::grpc_slice_from_copied_buffer(const void* buffer, - size_t length) { - return ::grpc_slice_from_copied_buffer(static_cast(buffer), - length); -} - -void CoreCodegen::grpc_slice_buffer_add(grpc_slice_buffer* sb, - grpc_slice slice) { - ::grpc_slice_buffer_add(sb, slice); -} - -void CoreCodegen::grpc_slice_buffer_add_indexed(grpc_slice_buffer* sb, - grpc_slice slice) { - ::grpc_slice_buffer_add_indexed(sb, slice); -} - -void CoreCodegen::grpc_slice_buffer_pop(grpc_slice_buffer* sb) { - ::grpc_slice_buffer_pop(sb); -} - -void CoreCodegen::grpc_metadata_array_init(grpc_metadata_array* array) { - ::grpc_metadata_array_init(array); -} - -void CoreCodegen::grpc_metadata_array_destroy(grpc_metadata_array* array) { - ::grpc_metadata_array_destroy(array); -} - const Status& CoreCodegen::ok() { return grpc::Status::OK; } const Status& CoreCodegen::cancelled() { return grpc::Status::CANCELLED; } -gpr_timespec CoreCodegen::gpr_inf_future(gpr_clock_type type) { - return ::gpr_inf_future(type); -} - -gpr_timespec CoreCodegen::gpr_time_0(gpr_clock_type type) { - return ::gpr_time_0(type); -} - void CoreCodegen::assert_fail(const char* failed_assertion, const char* file, int line) { gpr_log(file, line, GPR_LOG_SEVERITY_ERROR, "assertion failed: %s", diff --git a/test/cpp/microbenchmarks/bm_byte_buffer.cc b/test/cpp/microbenchmarks/bm_byte_buffer.cc index 811043f45a5..76c220aaf3c 100644 --- a/test/cpp/microbenchmarks/bm_byte_buffer.cc +++ b/test/cpp/microbenchmarks/bm_byte_buffer.cc @@ -23,6 +23,7 @@ #include #include +#include #include #include @@ -56,8 +57,7 @@ static void BM_ByteBufferReader_Next(benchmark::State& state) { std::vector slices; for (int i = 0; i < num_slices; ++i) { std::unique_ptr buf(new char[kSliceSize]); - slices.emplace_back(g_core_codegen_interface->grpc_slice_from_copied_buffer( - buf.get(), kSliceSize)); + slices.emplace_back(grpc_slice_from_copied_buffer(buf.get(), kSliceSize)); } grpc_byte_buffer* bb = grpc_raw_byte_buffer_create(slices.data(), num_slices); grpc_byte_buffer_reader reader; @@ -74,7 +74,7 @@ static void BM_ByteBufferReader_Next(benchmark::State& state) { grpc_byte_buffer_reader_destroy(&reader); grpc_byte_buffer_destroy(bb); for (auto& slice : slices) { - g_core_codegen_interface->grpc_slice_unref(slice); + grpc_slice_unref(slice); } } BENCHMARK(BM_ByteBufferReader_Next)->Ranges({{64 * 1024, 1024 * 1024}}); @@ -85,8 +85,7 @@ static void BM_ByteBufferReader_Peek(benchmark::State& state) { std::vector slices; for (int i = 0; i < num_slices; ++i) { std::unique_ptr buf(new char[kSliceSize]); - slices.emplace_back(g_core_codegen_interface->grpc_slice_from_copied_buffer( - buf.get(), kSliceSize)); + slices.emplace_back(grpc_slice_from_copied_buffer(buf.get(), kSliceSize)); } grpc_byte_buffer* bb = grpc_raw_byte_buffer_create(slices.data(), num_slices); grpc_byte_buffer_reader reader; @@ -103,7 +102,7 @@ static void BM_ByteBufferReader_Peek(benchmark::State& state) { grpc_byte_buffer_reader_destroy(&reader); grpc_byte_buffer_destroy(bb); for (auto& slice : slices) { - g_core_codegen_interface->grpc_slice_unref(slice); + grpc_slice_unref(slice); } } BENCHMARK(BM_ByteBufferReader_Peek)->Ranges({{64 * 1024, 1024 * 1024}});