Skip to content
This repository has been archived by the owner on Nov 6, 2023. It is now read-only.

Commit

Permalink
Remove functions in class CoreCodegen (grpc#31796)
Browse files Browse the repository at this point in the history
* Update include

* Clean up `grpc_empty_slice()`

* Clean up `grpc_slice_malloc()`

* Clean up `grpc_slice_unref()`

* Clean up `grpc_slice_ref()`

* Clean up `grpc_slice_split_tail()`

* Clean up `grpc_slice_split_head()`

* Clean up `grpc_slice_sub()`

* Clean up `grpc_slice_buffer_add()`

* Clean up `grpc_slice_buffer_add_indexed()`

* Clean up `grpc_slice_buffer_pop()`

* Clean up `grpc_slice_from_static_buffer()`

* Clean up `grpc_slice_from_copied_buffer()`

* Clean up `grpc_metadata_array_init()`

* Clean up `grpc_metadata_array_destroy()`

* Clean up `gpr_inf_future()`

* Clean up `gpr_time_0()`
  • Loading branch information
ralphchung authored Dec 7, 2022
1 parent 14cdae3 commit 4e90f43
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 181 deletions.
16 changes: 7 additions & 9 deletions include/grpcpp/completion_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

#include <grpc/grpc.h>
#include <grpc/support/atm.h>
#include <grpc/support/time.h>
#include <grpcpp/impl/codegen/core_codegen_interface.h>
#include <grpcpp/impl/codegen/rpc_service_method.h>
#include <grpcpp/impl/codegen/status.h>
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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});
}
Expand Down
10 changes: 5 additions & 5 deletions include/grpcpp/impl/call_op_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <grpc/grpc.h>
#include <grpc/impl/codegen/grpc_types.h>
#include <grpc/impl/compression_types.h>
#include <grpc/slice.h>
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
#include <grpcpp/client_context.h>
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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(
Expand Down
21 changes: 0 additions & 21 deletions include/grpcpp/impl/codegen/core_codegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
33 changes: 5 additions & 28 deletions include/grpcpp/impl/codegen/core_codegen_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@

// IWYU pragma: private

#include <grpc/impl/codegen/byte_buffer.h>
#include <grpc/impl/codegen/byte_buffer_reader.h>
#include <grpc/byte_buffer.h>
#include <grpc/byte_buffer_reader.h>
#include <grpc/impl/codegen/grpc_types.h>
#include <grpc/impl/codegen/sync.h>
#include <grpcpp/impl/codegen/config.h>
#include <grpcpp/impl/codegen/status.h>
#include <grpc/support/sync.h>
#include <grpcpp/support/config.h>
#include <grpcpp/support/status.h>

namespace grpc {

Expand All @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions include/grpcpp/impl/metadata_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <map>

#include <grpc/grpc.h>
#include <grpc/support/log.h>
#include <grpcpp/support/slice.h>

Expand Down Expand Up @@ -81,9 +82,7 @@ class MetadataMap {
grpc_metadata_array arr_;
std::multimap<grpc::string_ref, grpc::string_ref> 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_)); }

Expand Down
4 changes: 2 additions & 2 deletions include/grpcpp/impl/sync.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include <grpc/support/log.h>
#include <grpc/support/sync.h>
#include <grpc/support/time.h>
#include <grpcpp/impl/codegen/core_codegen_interface.h>

// The core library is not accessible in C++ codegen headers, and vice versa.
Expand Down Expand Up @@ -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:
Expand Down
6 changes: 2 additions & 4 deletions include/grpcpp/server_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include <grpc/grpc.h>
#include <grpc/impl/codegen/grpc_types.h>
#include <grpc/support/time.h>
#include <grpcpp/impl/call.h>
#include <grpcpp/impl/call_hook.h>
#include <grpcpp/impl/codegen/core_codegen_interface.h>
Expand Down Expand Up @@ -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.
///
Expand Down
21 changes: 10 additions & 11 deletions include/grpcpp/support/proto_buffer_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <grpc/byte_buffer.h>
#include <grpc/impl/codegen/grpc_types.h>
#include <grpc/slice.h>
#include <grpc/slice_buffer.h>
#include <grpcpp/impl/codegen/config_protobuf.h>
#include <grpcpp/impl/codegen/core_codegen_interface.h>
#include <grpcpp/impl/serialization_traits.h>
Expand Down Expand Up @@ -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_);
}
}

Expand All @@ -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<size_t>(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
Expand All @@ -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;
}

Expand All @@ -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<int>(GRPC_SLICE_LENGTH(slice_)));
g_core_codegen_interface->grpc_slice_buffer_pop(slice_buffer_);
grpc_slice_buffer_pop(slice_buffer_);
if (static_cast<size_t>(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
Expand Down
39 changes: 15 additions & 24 deletions include/grpcpp/support/slice.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<const char*>(buf), len)) {}
: slice_(grpc_slice_from_copied_buffer(reinterpret_cast<const char*>(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<const char*>(buf), len)) {}
: slice_(grpc_slice_from_static_buffer(reinterpret_cast<const char*>(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.
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/cpp/client/channel_cc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading

0 comments on commit 4e90f43

Please sign in to comment.