channel_stack_builder_impl.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2016 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 
20 
22 
23 #include <string.h>
24 
25 #include <vector>
26 
27 #include "absl/status/status.h"
28 
30 #include <grpc/support/alloc.h>
31 
38 
39 namespace grpc_core {
40 
43  auto* stack = mutable_stack();
44 
45  // calculate the size of the channel stack
46  size_t channel_stack_size =
47  grpc_channel_stack_size(stack->data(), stack->size());
48 
49  // allocate memory
50  auto* channel_stack =
51  static_cast<grpc_channel_stack*>(gpr_zalloc(channel_stack_size));
52 
53  ChannelArgs final_args = channel_args();
54  if (transport() != nullptr) {
55  static const grpc_arg_pointer_vtable vtable = {
56  // copy
57  [](void* p) { return p; },
58  // destroy
59  [](void*) {},
60  // cmp
61  [](void* a, void* b) { return QsortCompare(a, b); },
62  };
63  final_args = final_args.Set(GRPC_ARG_TRANSPORT,
65  }
66 
67  // and initialize it
68  const grpc_channel_args* c_args = final_args.ToC();
70  1,
71  [](void* p, grpc_error_handle) {
72  auto* stk = static_cast<grpc_channel_stack*>(p);
74  gpr_free(stk);
75  },
76  channel_stack, stack->data(), stack->size(), c_args, name(),
77  channel_stack);
79 
80  if (!GRPC_ERROR_IS_NONE(error)) {
81  grpc_channel_stack_destroy(channel_stack);
82  gpr_free(channel_stack);
85  return status;
86  }
87 
88  // run post-initialization functions
89  for (size_t i = 0; i < stack->size(); i++) {
90  auto* elem = grpc_channel_stack_element(channel_stack, i);
91  elem->filter->post_init_channel_elem(channel_stack, elem);
92  }
93 
94  return RefCountedPtr<grpc_channel_stack>(channel_stack);
95 }
96 
97 } // namespace grpc_core
vtable
static const grpc_transport_vtable vtable
Definition: binder_transport.cc:680
grpc_channel_stack
Definition: channel_stack.h:202
grpc_channel_stack_element
grpc_channel_element * grpc_channel_stack_element(grpc_channel_stack *channel_stack, size_t index)
Definition: channel_stack.cc:78
grpc_core
Definition: call_metric_recorder.h:31
string.h
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
useful.h
grpc_core::ChannelStackBuilder::name
const char * name() const
Definition: channel_stack_builder.h:47
elem
Timer elem
Definition: event_engine/iomgr_event_engine/timer_heap_test.cc:109
error
grpc_error_handle error
Definition: retry_filter.cc:499
status
absl::Status status
Definition: rls.cc:251
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
grpc_channel_stack_destroy
void grpc_channel_stack_destroy(grpc_channel_stack *stack)
Definition: channel_stack.cc:166
grpc_arg_pointer_vtable
Definition: grpc_types.h:85
grpc_channel_args
Definition: grpc_types.h:132
gpr_zalloc
GPRAPI void * gpr_zalloc(size_t size)
Definition: alloc.cc:40
grpc_types.h
grpc_core::RefCountedPtr< grpc_channel_stack >
channel_stack.h
grpc_core::ChannelStackBuilder::channel_args
const ChannelArgs & channel_args() const
Definition: channel_stack_builder.h:69
grpc_core::ChannelStackBuilder::mutable_stack
std::vector< const grpc_channel_filter * > * mutable_stack()
Definition: channel_stack_builder.h:72
grpc_core::ChannelArgs::ToC
const grpc_channel_args * ToC() const
Definition: channel_args.cc:94
grpc_channel_args_destroy
void grpc_channel_args_destroy(grpc_channel_args *a)
Definition: channel_args.cc:360
stack
NodeStack stack
Definition: cord_rep_btree.cc:356
error.h
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
grpc_core::ChannelArgs::Set
GRPC_MUST_USE_RESULT ChannelArgs Set(absl::string_view name, Value value) const
Definition: channel_args.cc:113
grpc_channel_stack_size
size_t grpc_channel_stack_size(const grpc_channel_filter **filters, size_t filter_count)
Definition: channel_stack.cc:51
grpc_core::ChannelStackBuilder::transport
grpc_transport * transport() const
Definition: channel_stack_builder.h:63
grpc_core::QsortCompare
int QsortCompare(const T &a, const T &b)
Definition: useful.h:95
grpc_core::ChannelArgs::Pointer
Definition: channel_args.h:113
alloc.h
grpc_channel_stack_init
grpc_error_handle grpc_channel_stack_init(int initial_refs, grpc_iomgr_cb_func destroy, void *destroy_arg, const grpc_channel_filter **filters, size_t filter_count, const grpc_channel_args *channel_args, const char *name, grpc_channel_stack *stack)
Definition: channel_stack.cc:105
grpc_core::ChannelStackBuilderImpl::Build
absl::StatusOr< RefCountedPtr< grpc_channel_stack > > Build() override
Definition: channel_stack_builder_impl.cc:42
GRPC_ERROR_UNREF
#define GRPC_ERROR_UNREF(err)
Definition: error.h:262
grpc_core::ChannelArgs
Definition: channel_args.h:111
transport.h
channel_args.h
channel_stack_builder_impl.h
absl::StatusOr
Definition: abseil-cpp/absl/status/statusor.h:187
grpc_error
Definition: error_internal.h:42
grpc_error_to_absl_status
absl::Status grpc_error_to_absl_status(grpc_error_handle error)
Definition: error_utils.cc:156
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
error_utils.h
GRPC_ERROR_IS_NONE
#define GRPC_ERROR_IS_NONE(err)
Definition: error.h:241
GRPC_ARG_TRANSPORT
#define GRPC_ARG_TRANSPORT
Definition: transport.h:71
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:44