• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 #include "src/core/lib/channel/channel_stack_builder_impl.h"
20 
21 #include <grpc/support/alloc.h>
22 #include <grpc/support/port_platform.h>
23 #include <string.h>
24 
25 #include <algorithm>
26 #include <functional>
27 #include <memory>
28 #include <string>
29 #include <utility>
30 #include <vector>
31 
32 #include "absl/base/thread_annotations.h"
33 #include "absl/container/flat_hash_map.h"
34 #include "absl/status/status.h"
35 #include "absl/strings/str_cat.h"
36 #include "src/core/lib/channel/channel_fwd.h"
37 #include "src/core/lib/channel/channel_stack.h"
38 #include "src/core/lib/debug/trace.h"
39 #include "src/core/lib/iomgr/closure.h"
40 #include "src/core/lib/iomgr/error.h"
41 #include "src/core/lib/promise/activity.h"
42 #include "src/core/lib/promise/arena_promise.h"
43 #include "src/core/lib/promise/poll.h"
44 #include "src/core/lib/surface/channel_stack_type.h"
45 #include "src/core/lib/transport/error_utils.h"
46 #include "src/core/lib/transport/metadata_batch.h"
47 #include "src/core/lib/transport/transport.h"
48 #include "src/core/util/no_destruct.h"
49 #include "src/core/util/sync.h"
50 
51 namespace grpc_core {
52 
53 absl::StatusOr<RefCountedPtr<grpc_channel_stack>>
Build()54 ChannelStackBuilderImpl::Build() {
55   std::vector<const grpc_channel_filter*> stack;
56 
57   for (const auto* filter : this->stack()) {
58     stack.push_back(filter);
59   }
60 
61   // calculate the size of the channel stack
62   size_t channel_stack_size =
63       grpc_channel_stack_size(stack.data(), stack.size());
64 
65   // allocate memory
66   auto* channel_stack =
67       static_cast<grpc_channel_stack*>(gpr_zalloc(channel_stack_size));
68 
69   // and initialize it
70   grpc_error_handle error = grpc_channel_stack_init(
71       1,
72       [](void* p, grpc_error_handle) {
73         auto* stk = static_cast<grpc_channel_stack*>(p);
74         grpc_channel_stack_destroy(stk);
75         gpr_free(stk);
76       },
77       channel_stack, stack.data(), stack.size(), channel_args(), name(),
78       channel_stack, old_blackboard_, new_blackboard_);
79 
80   if (!error.ok()) {
81     grpc_channel_stack_destroy(channel_stack);
82     gpr_free(channel_stack);
83     auto status = grpc_error_to_absl_status(error);
84     return status;
85   }
86 
87   // run post-initialization functions
88   for (size_t i = 0; i < stack.size(); i++) {
89     auto* elem = grpc_channel_stack_element(channel_stack, i);
90     elem->filter->post_init_channel_elem(channel_stack, elem);
91   }
92 
93   return RefCountedPtr<grpc_channel_stack>(channel_stack);
94 }
95 
96 }  // namespace grpc_core
97