Line data Source code
1 : /*
2 : *
3 : * Copyright 2015, Google Inc.
4 : * All rights reserved.
5 : *
6 : * Redistribution and use in source and binary forms, with or without
7 : * modification, are permitted provided that the following conditions are
8 : * met:
9 : *
10 : * * Redistributions of source code must retain the above copyright
11 : * notice, this list of conditions and the following disclaimer.
12 : * * Redistributions in binary form must reproduce the above
13 : * copyright notice, this list of conditions and the following disclaimer
14 : * in the documentation and/or other materials provided with the
15 : * distribution.
16 : * * Neither the name of Google Inc. nor the names of its
17 : * contributors may be used to endorse or promote products derived from
18 : * this software without specific prior written permission.
19 : *
20 : * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 : * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 : * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 : * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 : * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 : * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 : * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 : * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 : * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 : * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 : * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 : *
32 : */
33 :
34 : #include "src/core/transport/metadata_batch.h"
35 :
36 : #include <string.h>
37 :
38 : #include <grpc/support/alloc.h>
39 : #include <grpc/support/log.h>
40 :
41 : #include "src/core/profiling/timers.h"
42 :
43 126826244 : static void assert_valid_list(grpc_mdelem_list *list) {
44 : #ifndef NDEBUG
45 : grpc_linked_mdelem *l;
46 :
47 126826244 : GPR_ASSERT((list->head == NULL) == (list->tail == NULL));
48 253979518 : if (!list->head) return;
49 106968813 : GPR_ASSERT(list->head->prev == NULL);
50 106968813 : GPR_ASSERT(list->tail->next == NULL);
51 106968813 : GPR_ASSERT((list->head == list->tail) == (list->head->next == NULL));
52 :
53 588370269 : for (l = list->head; l; l = l->next) {
54 481074426 : GPR_ASSERT(l->md);
55 481401456 : GPR_ASSERT((l->prev == NULL) == (l == list->head));
56 481401456 : GPR_ASSERT((l->next == NULL) == (l == list->tail));
57 481401456 : if (l->next) GPR_ASSERT(l->next->prev == l);
58 481401456 : if (l->prev) GPR_ASSERT(l->prev->next == l);
59 : }
60 : #endif /* NDEBUG */
61 : }
62 :
63 : #ifndef NDEBUG
64 6612889 : void grpc_metadata_batch_assert_ok(grpc_metadata_batch *batch) {
65 6612889 : assert_valid_list(&batch->list);
66 6608201 : }
67 : #endif /* NDEBUG */
68 :
69 473 : void grpc_metadata_batch_init(grpc_metadata_batch *batch) {
70 473 : batch->list.head = batch->list.tail = NULL;
71 473 : batch->deadline = gpr_inf_future(GPR_CLOCK_REALTIME);
72 473 : }
73 :
74 17733148 : void grpc_metadata_batch_destroy(grpc_metadata_batch *batch) {
75 : grpc_linked_mdelem *l;
76 54609341 : for (l = batch->list.head; l; l = l->next) {
77 36864449 : GRPC_MDELEM_UNREF(l->md);
78 : }
79 17744892 : }
80 :
81 6706633 : void grpc_metadata_batch_add_head(grpc_metadata_batch *batch,
82 : grpc_linked_mdelem *storage,
83 : grpc_mdelem *elem_to_add) {
84 6706633 : GPR_ASSERT(elem_to_add);
85 6706633 : storage->md = elem_to_add;
86 6706633 : grpc_metadata_batch_link_head(batch, storage);
87 6704345 : }
88 :
89 6708586 : static void link_head(grpc_mdelem_list *list, grpc_linked_mdelem *storage) {
90 6708586 : assert_valid_list(list);
91 6706266 : GPR_ASSERT(storage->md);
92 6706266 : storage->prev = NULL;
93 6706266 : storage->next = list->head;
94 6706266 : if (list->head != NULL) {
95 6601524 : list->head->prev = storage;
96 : } else {
97 104742 : list->tail = storage;
98 : }
99 6706266 : list->head = storage;
100 6706266 : assert_valid_list(list);
101 6702966 : }
102 :
103 6708412 : void grpc_metadata_batch_link_head(grpc_metadata_batch *batch,
104 : grpc_linked_mdelem *storage) {
105 6708412 : link_head(&batch->list, storage);
106 6704424 : }
107 :
108 17621353 : void grpc_metadata_batch_add_tail(grpc_metadata_batch *batch,
109 : grpc_linked_mdelem *storage,
110 : grpc_mdelem *elem_to_add) {
111 17621353 : GPR_ASSERT(elem_to_add);
112 17621353 : storage->md = elem_to_add;
113 17621353 : grpc_metadata_batch_link_tail(batch, storage);
114 17613227 : }
115 :
116 17622530 : static void link_tail(grpc_mdelem_list *list, grpc_linked_mdelem *storage) {
117 17622530 : assert_valid_list(list);
118 17636530 : GPR_ASSERT(storage->md);
119 17636530 : storage->prev = list->tail;
120 17636530 : storage->next = NULL;
121 17636530 : storage->reserved = NULL;
122 17636530 : if (list->tail != NULL) {
123 15571087 : list->tail->next = storage;
124 : } else {
125 2065443 : list->head = storage;
126 : }
127 17636530 : list->tail = storage;
128 17636530 : assert_valid_list(list);
129 17611054 : }
130 :
131 17624937 : void grpc_metadata_batch_link_tail(grpc_metadata_batch *batch,
132 : grpc_linked_mdelem *storage) {
133 17624937 : link_tail(&batch->list, storage);
134 17608777 : }
135 :
136 0 : void grpc_metadata_batch_merge(grpc_metadata_batch *target,
137 : grpc_metadata_batch *to_add) {
138 : grpc_linked_mdelem *l;
139 : grpc_linked_mdelem *next;
140 0 : for (l = to_add->list.head; l; l = next) {
141 0 : next = l->next;
142 0 : link_tail(&target->list, l);
143 : }
144 0 : }
145 :
146 0 : void grpc_metadata_batch_move(grpc_metadata_batch *dst,
147 : grpc_metadata_batch *src) {
148 0 : *dst = *src;
149 0 : memset(src, 0, sizeof(grpc_metadata_batch));
150 0 : }
151 :
152 22048516 : void grpc_metadata_batch_filter(grpc_metadata_batch *batch,
153 : grpc_mdelem *(*filter)(void *user_data,
154 : grpc_mdelem *elem),
155 : void *user_data) {
156 : grpc_linked_mdelem *l;
157 : grpc_linked_mdelem *next;
158 :
159 : GPR_TIMER_BEGIN("grpc_metadata_batch_filter", 0);
160 :
161 22048516 : assert_valid_list(&batch->list);
162 96635368 : for (l = batch->list.head; l; l = next) {
163 74597116 : grpc_mdelem *orig = l->md;
164 74597116 : grpc_mdelem *filt = filter(user_data, orig);
165 74818822 : next = l->next;
166 74818822 : if (filt == NULL) {
167 29041165 : if (l->prev) {
168 7574466 : l->prev->next = l->next;
169 : }
170 29041165 : if (l->next) {
171 22272249 : l->next->prev = l->prev;
172 : }
173 29041165 : if (batch->list.head == l) {
174 21514109 : batch->list.head = l->next;
175 : }
176 29041165 : if (batch->list.tail == l) {
177 6837572 : batch->list.tail = l->prev;
178 : }
179 29041165 : assert_valid_list(&batch->list);
180 29058387 : GRPC_MDELEM_UNREF(l->md);
181 45777657 : } else if (filt != orig) {
182 1 : GRPC_MDELEM_UNREF(orig);
183 1 : l->md = filt;
184 : }
185 : }
186 22037361 : assert_valid_list(&batch->list);
187 :
188 : GPR_TIMER_END("grpc_metadata_batch_filter", 0);
189 22037607 : }
190 :
191 182 : static grpc_mdelem *no_metadata_for_you(void *user_data, grpc_mdelem *elem) {
192 182 : return NULL;
193 : }
194 :
195 135 : void grpc_metadata_batch_clear(grpc_metadata_batch *batch) {
196 135 : batch->deadline = gpr_inf_future(GPR_CLOCK_REALTIME);
197 135 : grpc_metadata_batch_filter(batch, no_metadata_for_you, NULL);
198 135 : }
199 :
200 6083488 : int grpc_metadata_batch_is_empty(grpc_metadata_batch *batch) {
201 8355499 : return batch->list.head == NULL &&
202 2271353 : gpr_time_cmp(gpr_inf_future(batch->deadline.clock_type),
203 : batch->deadline) == 0;
204 : }
|