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
19 #ifndef GRPC_CORE_LIB_TRANSPORT_METADATA_BATCH_H
20 #define GRPC_CORE_LIB_TRANSPORT_METADATA_BATCH_H
21
22 #include <grpc/support/port_platform.h>
23
24 #include <stdbool.h>
25
26 #include <grpc/grpc.h>
27 #include <grpc/slice.h>
28 #include <grpc/support/time.h>
29 #include "src/core/lib/iomgr/exec_ctx.h"
30 #include "src/core/lib/transport/metadata.h"
31 #include "src/core/lib/transport/static_metadata.h"
32
33 typedef struct grpc_linked_mdelem {
grpc_linked_mdelemgrpc_linked_mdelem34 grpc_linked_mdelem() {}
35
36 grpc_mdelem md;
37 struct grpc_linked_mdelem* next = nullptr;
38 struct grpc_linked_mdelem* prev = nullptr;
39 void* reserved;
40 } grpc_linked_mdelem;
41
42 typedef struct grpc_mdelem_list {
43 size_t count;
44 size_t default_count; // Number of default keys.
45 grpc_linked_mdelem* head;
46 grpc_linked_mdelem* tail;
47 } grpc_mdelem_list;
48
49 typedef struct grpc_metadata_batch {
50 /** Metadata elements in this batch */
51 grpc_mdelem_list list;
52 grpc_metadata_batch_callouts idx;
53 /** Used to calculate grpc-timeout at the point of sending,
54 or GRPC_MILLIS_INF_FUTURE if this batch does not need to send a
55 grpc-timeout */
56 grpc_millis deadline;
57 } grpc_metadata_batch;
58
59 void grpc_metadata_batch_init(grpc_metadata_batch* batch);
60 void grpc_metadata_batch_destroy(grpc_metadata_batch* batch);
61 void grpc_metadata_batch_clear(grpc_metadata_batch* batch);
62 bool grpc_metadata_batch_is_empty(grpc_metadata_batch* batch);
63
64 /* Returns the transport size of the batch. */
65 size_t grpc_metadata_batch_size(grpc_metadata_batch* batch);
66
67 /** Remove \a storage from the batch, unreffing the mdelem contained */
68 void grpc_metadata_batch_remove(grpc_metadata_batch* batch,
69 grpc_linked_mdelem* storage);
70 void grpc_metadata_batch_remove(grpc_metadata_batch* batch,
71 grpc_metadata_batch_callouts_index idx);
72
73 /** Substitute a new mdelem for an old value */
74 grpc_error* grpc_metadata_batch_substitute(grpc_metadata_batch* batch,
75 grpc_linked_mdelem* storage,
76 grpc_mdelem new_mdelem);
77
78 void grpc_metadata_batch_set_value(grpc_linked_mdelem* storage,
79 const grpc_slice& value);
80
81 /** Add \a storage to the beginning of \a batch. storage->md is
82 assumed to be valid.
83 \a storage is owned by the caller and must survive for the
84 lifetime of batch. This usually means it should be around
85 for the lifetime of the call. */
86 grpc_error* grpc_metadata_batch_link_head(grpc_metadata_batch* batch,
87 grpc_linked_mdelem* storage)
88 GRPC_MUST_USE_RESULT;
89 grpc_error* grpc_metadata_batch_link_head(
90 grpc_metadata_batch* batch, grpc_linked_mdelem* storage,
91 grpc_metadata_batch_callouts_index idx) GRPC_MUST_USE_RESULT;
92
93 /** Add \a storage to the end of \a batch. storage->md is
94 assumed to be valid.
95 \a storage is owned by the caller and must survive for the
96 lifetime of batch. This usually means it should be around
97 for the lifetime of the call. */
98 grpc_error* grpc_metadata_batch_link_tail(grpc_metadata_batch* batch,
99 grpc_linked_mdelem* storage)
100 GRPC_MUST_USE_RESULT;
101 grpc_error* grpc_metadata_batch_link_tail(
102 grpc_metadata_batch* batch, grpc_linked_mdelem* storage,
103 grpc_metadata_batch_callouts_index idx) GRPC_MUST_USE_RESULT;
104
105 /** Add \a elem_to_add as the first element in \a batch, using
106 \a storage as backing storage for the linked list element.
107 \a storage is owned by the caller and must survive for the
108 lifetime of batch. This usually means it should be around
109 for the lifetime of the call.
110 Takes ownership of \a elem_to_add */
111 grpc_error* grpc_metadata_batch_add_head(
112 grpc_metadata_batch* batch, grpc_linked_mdelem* storage,
113 grpc_mdelem elem_to_add) GRPC_MUST_USE_RESULT;
114
115 // TODO(arjunroy, roth): Remove redundant methods.
116 // add/link_head/tail are almost identical.
grpc_metadata_batch_add_head(grpc_metadata_batch * batch,grpc_linked_mdelem * storage,grpc_metadata_batch_callouts_index idx)117 inline grpc_error* GRPC_MUST_USE_RESULT grpc_metadata_batch_add_head(
118 grpc_metadata_batch* batch, grpc_linked_mdelem* storage,
119 grpc_metadata_batch_callouts_index idx) {
120 return grpc_metadata_batch_link_head(batch, storage, idx);
121 }
122
grpc_metadata_batch_add_head(grpc_metadata_batch * batch,grpc_linked_mdelem * storage,grpc_mdelem elem_to_add,grpc_metadata_batch_callouts_index idx)123 inline grpc_error* GRPC_MUST_USE_RESULT grpc_metadata_batch_add_head(
124 grpc_metadata_batch* batch, grpc_linked_mdelem* storage,
125 grpc_mdelem elem_to_add, grpc_metadata_batch_callouts_index idx) {
126 GPR_DEBUG_ASSERT(!GRPC_MDISNULL(elem_to_add));
127 storage->md = elem_to_add;
128 return grpc_metadata_batch_add_head(batch, storage, idx);
129 }
130
131 /** Add \a elem_to_add as the last element in \a batch, using
132 \a storage as backing storage for the linked list element.
133 \a storage is owned by the caller and must survive for the
134 lifetime of batch. This usually means it should be around
135 for the lifetime of the call.
136 Takes ownership of \a elem_to_add */
137 grpc_error* grpc_metadata_batch_add_tail(
138 grpc_metadata_batch* batch, grpc_linked_mdelem* storage,
139 grpc_mdelem elem_to_add) GRPC_MUST_USE_RESULT;
140
grpc_metadata_batch_add_tail(grpc_metadata_batch * batch,grpc_linked_mdelem * storage,grpc_metadata_batch_callouts_index idx)141 inline grpc_error* GRPC_MUST_USE_RESULT grpc_metadata_batch_add_tail(
142 grpc_metadata_batch* batch, grpc_linked_mdelem* storage,
143 grpc_metadata_batch_callouts_index idx) {
144 return grpc_metadata_batch_link_tail(batch, storage, idx);
145 }
146
grpc_metadata_batch_add_tail(grpc_metadata_batch * batch,grpc_linked_mdelem * storage,grpc_mdelem elem_to_add,grpc_metadata_batch_callouts_index idx)147 inline grpc_error* GRPC_MUST_USE_RESULT grpc_metadata_batch_add_tail(
148 grpc_metadata_batch* batch, grpc_linked_mdelem* storage,
149 grpc_mdelem elem_to_add, grpc_metadata_batch_callouts_index idx) {
150 GPR_DEBUG_ASSERT(!GRPC_MDISNULL(elem_to_add));
151 storage->md = elem_to_add;
152 return grpc_metadata_batch_add_tail(batch, storage, idx);
153 }
154
155 grpc_error* grpc_attach_md_to_error(grpc_error* src, grpc_mdelem md);
156
157 struct grpc_filtered_mdelem {
158 grpc_error* error;
159 grpc_mdelem md;
160 };
161 #define GRPC_FILTERED_ERROR(error) \
162 { (error), GRPC_MDNULL }
163 #define GRPC_FILTERED_MDELEM(md) \
164 { GRPC_ERROR_NONE, (md) }
165 #define GRPC_FILTERED_REMOVE() \
166 { GRPC_ERROR_NONE, GRPC_MDNULL }
167
168 typedef grpc_filtered_mdelem (*grpc_metadata_batch_filter_func)(
169 void* user_data, grpc_mdelem elem);
170 grpc_error* grpc_metadata_batch_filter(
171 grpc_metadata_batch* batch, grpc_metadata_batch_filter_func func,
172 void* user_data, const char* composite_error_string) GRPC_MUST_USE_RESULT;
173
174 #ifndef NDEBUG
175 void grpc_metadata_batch_assert_ok(grpc_metadata_batch* batch);
176 #else
177 #define grpc_metadata_batch_assert_ok(batch) \
178 do { \
179 } while (0)
180 #endif
181
182 /// Copies \a src to \a dst. \a storage must point to an array of
183 /// \a grpc_linked_mdelem structs of at least the same size as \a src.
184 void grpc_metadata_batch_copy(grpc_metadata_batch* src,
185 grpc_metadata_batch* dst,
186 grpc_linked_mdelem* storage);
187
188 void grpc_metadata_batch_move(grpc_metadata_batch* src,
189 grpc_metadata_batch* dst);
190
191 #endif /* GRPC_CORE_LIB_TRANSPORT_METADATA_BATCH_H */
192