blob: 56e83a0a75380001d1b18f15436cc87d00076cbe [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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.h"
35
Craig Tiller9fa41b92015-04-10 15:08:03 -070036#include <assert.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080037#include <stddef.h>
38#include <string.h>
39
Craig Tillerebdef9d2015-11-19 17:09:49 -080040#include <grpc/compression.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080041#include <grpc/support/alloc.h>
Craig Tiller9fa41b92015-04-10 15:08:03 -070042#include <grpc/support/atm.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080043#include <grpc/support/log.h>
Craig Tillerebdef9d2015-11-19 17:09:49 -080044#include <grpc/support/string_util.h>
Craig Tiller9d35a1f2015-11-02 14:16:12 -080045#include <grpc/support/time.h>
46#include "src/core/profiling/timers.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080047#include "src/core/support/murmur_hash.h"
Craig Tillerebdef9d2015-11-19 17:09:49 -080048#include "src/core/support/string.h"
ctiller430c4992014-12-11 09:15:41 -080049#include "src/core/transport/chttp2/bin_encoder.h"
Craig Tiller0e72ede2015-11-19 07:48:53 -080050#include "src/core/transport/static_metadata.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080051
Craig Tiller70b080d2015-11-19 08:04:48 -080052/* There are two kinds of mdelem and mdstr instances.
53 * Static instances are declared in static_metadata.{h,c} and
54 * are initialized by grpc_mdctx_global_init().
55 * Dynamic instances are stored in hash tables on grpc_mdctx, and are backed
56 * by internal_string and internal_element structures.
57 * Internal helper functions here-in (is_mdstr_static, is_mdelem_static) are
58 * used to determine which kind of element a pointer refers to.
59 */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080060
61#define INITIAL_STRTAB_CAPACITY 4
62#define INITIAL_MDTAB_CAPACITY 4
63
Craig Tiller1a65a232015-07-06 10:22:32 -070064#ifdef GRPC_METADATA_REFCOUNT_DEBUG
65#define DEBUG_ARGS , const char *file, int line
66#define FWD_DEBUG_ARGS , file, line
Craig Tillerb2b42612015-11-20 12:02:17 -080067#define REF_MD_LOCKED(shard, s) ref_md_locked((shard), (s), __FILE__, __LINE__)
Craig Tiller1a65a232015-07-06 10:22:32 -070068#else
69#define DEBUG_ARGS
70#define FWD_DEBUG_ARGS
Craig Tillerb2b42612015-11-20 12:02:17 -080071#define REF_MD_LOCKED(shard, s) ref_md_locked((shard), (s))
Craig Tiller1a65a232015-07-06 10:22:32 -070072#endif
73
Craig Tillerb2b42612015-11-20 12:02:17 -080074#define TABLE_IDX(hash, log2_shards, capacity) \
75 (((hash) >> (log2_shards)) % (capacity))
76#define SHARD_IDX(hash, log2_shards) ((hash) & ((1 << (log2_shards)) - 1))
77
Craig Tiller8344daa2015-10-09 18:10:57 -070078typedef void (*destroy_user_data_func)(void *user_data);
79
Craig Tiller70b080d2015-11-19 08:04:48 -080080/* Shadow structure for grpc_mdstr for non-static values */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080081typedef struct internal_string {
82 /* must be byte compatible with grpc_mdstr */
83 gpr_slice slice;
84 gpr_uint32 hash;
85
86 /* private only data */
Craig Tillerb2b42612015-11-20 12:02:17 -080087 gpr_atm refcnt;
88
ctiller430c4992014-12-11 09:15:41 -080089 gpr_uint8 has_base64_and_huffman_encoded;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080090 gpr_slice_refcount refcount;
91
ctiller430c4992014-12-11 09:15:41 -080092 gpr_slice base64_and_huffman;
93
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080094 struct internal_string *bucket_next;
95} internal_string;
96
Craig Tiller70b080d2015-11-19 08:04:48 -080097/* Shadow structure for grpc_mdelem for non-static elements */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080098typedef struct internal_metadata {
99 /* must be byte compatible with grpc_mdelem */
100 internal_string *key;
101 internal_string *value;
102
Craig Tillerb2b42612015-11-20 12:02:17 -0800103 /* private only data */
Craig Tiller9fa41b92015-04-10 15:08:03 -0700104 gpr_atm refcnt;
105
Craig Tiller83901532015-07-10 14:02:45 -0700106 gpr_mu mu_user_data;
Craig Tiller8344daa2015-10-09 18:10:57 -0700107 gpr_atm destroy_user_data;
108 gpr_atm user_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800109
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800110 struct internal_metadata *bucket_next;
111} internal_metadata;
112
Craig Tillerb2b42612015-11-20 12:02:17 -0800113typedef struct strtab_shard {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800114 gpr_mu mu;
Craig Tillerb2b42612015-11-20 12:02:17 -0800115 internal_string **strs;
116 size_t count;
117 size_t capacity;
118} strtab_shard;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800119
Craig Tillerb2b42612015-11-20 12:02:17 -0800120typedef struct mdtab_shard {
121 gpr_mu mu;
122 internal_metadata **elems;
123 size_t count;
124 size_t capacity;
125 size_t free;
126} mdtab_shard;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800127
Craig Tillerb2b42612015-11-20 12:02:17 -0800128#define LOG2_STRTAB_SHARD_COUNT 5
129#define LOG2_MDTAB_SHARD_COUNT 4
130#define STRTAB_SHARD_COUNT ((size_t)(1 << LOG2_STRTAB_SHARD_COUNT))
131#define MDTAB_SHARD_COUNT ((size_t)(1 << LOG2_MDTAB_SHARD_COUNT))
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800132
Craig Tillerb2b42612015-11-20 12:02:17 -0800133/* hash seed: decided at initialization time */
134static gpr_uint32 g_hash_seed;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800135
Craig Tillerb2b42612015-11-20 12:02:17 -0800136/* linearly probed hash tables for static element lookup */
137static grpc_mdstr *g_static_strtab[GRPC_STATIC_MDSTR_COUNT * 2];
138static grpc_mdelem *g_static_mdtab[GRPC_STATIC_MDELEM_COUNT * 2];
139static size_t g_static_strtab_maxprobe;
140static size_t g_static_mdtab_maxprobe;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800141
Craig Tillerb2b42612015-11-20 12:02:17 -0800142static strtab_shard g_strtab_shard[STRTAB_SHARD_COUNT];
143static mdtab_shard g_mdtab_shard[MDTAB_SHARD_COUNT];
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800144
Craig Tillerb2b42612015-11-20 12:02:17 -0800145static void gc_mdtab(mdtab_shard *shard);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800146
Craig Tiller0e72ede2015-11-19 07:48:53 -0800147void grpc_mdctx_global_init(void) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800148 size_t i, j;
149 g_hash_seed = (gpr_uint32)gpr_now(GPR_CLOCK_REALTIME).tv_nsec;
150 g_static_strtab_maxprobe = 0;
151 g_static_mdtab_maxprobe = 0;
152 /* build static tables */
153 memset(g_static_mdtab, 0, sizeof(g_static_mdtab));
154 memset(g_static_strtab, 0, sizeof(g_static_strtab));
Craig Tiller0e72ede2015-11-19 07:48:53 -0800155 for (i = 0; i < GRPC_STATIC_MDSTR_COUNT; i++) {
156 grpc_mdstr *elem = &grpc_static_mdstr_table[i];
157 const char *str = grpc_static_metadata_strings[i];
Craig Tillerb2b42612015-11-20 12:02:17 -0800158 gpr_uint32 hash = gpr_murmur_hash3(str, strlen(str), g_hash_seed);
Craig Tillerb774be42015-11-19 07:56:13 -0800159 *(gpr_slice *)&elem->slice = gpr_slice_from_static_string(str);
Craig Tillerb2b42612015-11-20 12:02:17 -0800160 *(gpr_uint32 *)&elem->hash = hash;
161 for (j = 0;; j++) {
162 size_t idx = (hash + j) % GPR_ARRAY_SIZE(g_static_strtab);
163 if (g_static_strtab[idx] == NULL) {
164 g_static_strtab[idx] = &grpc_static_mdstr_table[i];
165 break;
166 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800167 }
Craig Tillerb2b42612015-11-20 12:02:17 -0800168 if (j > g_static_strtab_maxprobe) {
169 g_static_strtab_maxprobe = j;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800170 }
171 }
Craig Tiller0e72ede2015-11-19 07:48:53 -0800172 for (i = 0; i < GRPC_STATIC_MDELEM_COUNT; i++) {
173 grpc_mdelem *elem = &grpc_static_mdelem_table[i];
Craig Tillerebdef9d2015-11-19 17:09:49 -0800174 grpc_mdstr *key =
175 &grpc_static_mdstr_table[grpc_static_metadata_elem_indices[2 * i + 0]];
176 grpc_mdstr *value =
177 &grpc_static_mdstr_table[grpc_static_metadata_elem_indices[2 * i + 1]];
Craig Tillerb2b42612015-11-20 12:02:17 -0800178 gpr_uint32 hash = GRPC_MDSTR_KV_HASH(key->hash, value->hash);
Craig Tillerb774be42015-11-19 07:56:13 -0800179 *(grpc_mdstr **)&elem->key = key;
180 *(grpc_mdstr **)&elem->value = value;
Craig Tillerb2b42612015-11-20 12:02:17 -0800181 for (j = 0;; j++) {
182 size_t idx = (hash + j) % GPR_ARRAY_SIZE(g_static_mdtab);
183 if (g_static_mdtab[idx] == NULL) {
184 g_static_mdtab[idx] = elem;
185 break;
186 }
187 }
188 if (j > g_static_mdtab_maxprobe) {
189 g_static_mdtab_maxprobe = j;
190 }
191 }
192 /* initialize shards */
193 for (i = 0; i < STRTAB_SHARD_COUNT; i++) {
194 strtab_shard *shard = &g_strtab_shard[i];
195 gpr_mu_init(&shard->mu);
196 shard->count = 0;
197 shard->capacity = INITIAL_STRTAB_CAPACITY;
198 shard->strs = gpr_malloc(sizeof(*shard->strs) * shard->capacity);
199 memset(shard->strs, 0, sizeof(*shard->strs) * shard->capacity);
200 }
201 for (i = 0; i < MDTAB_SHARD_COUNT; i++) {
202 mdtab_shard *shard = &g_mdtab_shard[i];
203 gpr_mu_init(&shard->mu);
204 shard->count = 0;
205 shard->free = 0;
206 shard->capacity = INITIAL_MDTAB_CAPACITY;
207 shard->elems = gpr_malloc(sizeof(*shard->elems) * shard->capacity);
208 memset(shard->elems, 0, sizeof(*shard->elems) * shard->capacity);
Craig Tiller0e72ede2015-11-19 07:48:53 -0800209 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800210}
211
Craig Tillerb2b42612015-11-20 12:02:17 -0800212void grpc_mdctx_global_shutdown(void) {
213 size_t i;
214 for (i = 0; i < MDTAB_SHARD_COUNT; i++) {
215 mdtab_shard *shard = &g_mdtab_shard[i];
216 gpr_mu_destroy(&shard->mu);
Craig Tiller34075442015-11-23 16:16:54 -0800217 gc_mdtab(shard);
218 /* TODO(ctiller): GPR_ASSERT(shard->count == 0); */
219 if (shard->count != 0) {
yang-gd88e1d82015-12-02 13:23:33 -0800220 gpr_log(GPR_DEBUG, "WARNING: %d metadata elements were leaked",
221 shard->count);
Craig Tiller34075442015-11-23 16:16:54 -0800222 }
Craig Tillerb2b42612015-11-20 12:02:17 -0800223 gpr_free(shard->elems);
224 }
225 for (i = 0; i < STRTAB_SHARD_COUNT; i++) {
226 strtab_shard *shard = &g_strtab_shard[i];
227 gpr_mu_destroy(&shard->mu);
Craig Tiller34075442015-11-23 16:16:54 -0800228 /* TODO(ctiller): GPR_ASSERT(shard->count == 0); */
229 if (shard->count != 0) {
yang-gd88e1d82015-12-02 13:23:33 -0800230 gpr_log(GPR_DEBUG, "WARNING: %d metadata strings were leaked",
231 shard->count);
Craig Tiller34075442015-11-23 16:16:54 -0800232 }
Craig Tillerb2b42612015-11-20 12:02:17 -0800233 gpr_free(shard->strs);
234 }
235}
Craig Tiller0e72ede2015-11-19 07:48:53 -0800236
237static int is_mdstr_static(grpc_mdstr *s) {
Craig Tillerb774be42015-11-19 07:56:13 -0800238 return s >= &grpc_static_mdstr_table[0] &&
239 s < &grpc_static_mdstr_table[GRPC_STATIC_MDSTR_COUNT];
Craig Tiller0e72ede2015-11-19 07:48:53 -0800240}
241
242static int is_mdelem_static(grpc_mdelem *e) {
Craig Tillerb774be42015-11-19 07:56:13 -0800243 return e >= &grpc_static_mdelem_table[0] &&
244 e < &grpc_static_mdelem_table[GRPC_STATIC_MDELEM_COUNT];
Craig Tiller0e72ede2015-11-19 07:48:53 -0800245}
246
Craig Tillerb2b42612015-11-20 12:02:17 -0800247static void ref_md_locked(mdtab_shard *shard,
248 internal_metadata *md DEBUG_ARGS) {
Craig Tiller1a65a232015-07-06 10:22:32 -0700249#ifdef GRPC_METADATA_REFCOUNT_DEBUG
250 gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,
251 "ELM REF:%p:%d->%d: '%s' = '%s'", md,
252 gpr_atm_no_barrier_load(&md->refcnt),
253 gpr_atm_no_barrier_load(&md->refcnt) + 1,
254 grpc_mdstr_as_c_string((grpc_mdstr *)md->key),
255 grpc_mdstr_as_c_string((grpc_mdstr *)md->value));
256#endif
Craig Tillerb8e82672015-10-10 13:52:47 -0700257 if (0 == gpr_atm_no_barrier_fetch_add(&md->refcnt, 2)) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800258 shard->free--;
Craig Tillerb8e82672015-10-10 13:52:47 -0700259 } else {
260 GPR_ASSERT(1 != gpr_atm_no_barrier_fetch_add(&md->refcnt, -1));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800261 }
262}
263
Craig Tillerb2b42612015-11-20 12:02:17 -0800264static void grow_strtab(strtab_shard *shard) {
265 size_t capacity = shard->capacity * 2;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800266 size_t i;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800267 internal_string **strtab;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800268 internal_string *s, *next;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800269
270 GPR_TIMER_BEGIN("grow_strtab", 0);
271
272 strtab = gpr_malloc(sizeof(internal_string *) * capacity);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800273 memset(strtab, 0, sizeof(internal_string *) * capacity);
274
Craig Tillerb2b42612015-11-20 12:02:17 -0800275 for (i = 0; i < shard->capacity; i++) {
276 for (s = shard->strs[i]; s; s = next) {
277 size_t idx = TABLE_IDX(s->hash, LOG2_STRTAB_SHARD_COUNT, capacity);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800278 next = s->bucket_next;
Craig Tillerb2b42612015-11-20 12:02:17 -0800279 s->bucket_next = strtab[idx];
280 strtab[idx] = s;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800281 }
282 }
283
Craig Tillerb2b42612015-11-20 12:02:17 -0800284 gpr_free(shard->strs);
285 shard->strs = strtab;
286 shard->capacity = capacity;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800287
288 GPR_TIMER_END("grow_strtab", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800289}
290
Craig Tillerb2b42612015-11-20 12:02:17 -0800291static void internal_destroy_string(strtab_shard *shard, internal_string *is) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800292 internal_string **prev_next;
293 internal_string *cur;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800294 GPR_TIMER_BEGIN("internal_destroy_string", 0);
ctiller430c4992014-12-11 09:15:41 -0800295 if (is->has_base64_and_huffman_encoded) {
296 gpr_slice_unref(is->base64_and_huffman);
297 }
Craig Tillerb2b42612015-11-20 12:02:17 -0800298 for (prev_next = &shard->strs[TABLE_IDX(is->hash, LOG2_STRTAB_SHARD_COUNT,
299 shard->capacity)],
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800300 cur = *prev_next;
301 cur != is; prev_next = &cur->bucket_next, cur = cur->bucket_next)
302 ;
303 *prev_next = cur->bucket_next;
Craig Tillerb2b42612015-11-20 12:02:17 -0800304 shard->count--;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800305 gpr_free(is);
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800306 GPR_TIMER_END("internal_destroy_string", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800307}
308
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800309static void slice_ref(void *p) {
310 internal_string *is =
311 (internal_string *)((char *)p - offsetof(internal_string, refcount));
Craig Tillerb2b42612015-11-20 12:02:17 -0800312 GRPC_MDSTR_REF((grpc_mdstr *)(is));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800313}
314
315static void slice_unref(void *p) {
316 internal_string *is =
317 (internal_string *)((char *)p - offsetof(internal_string, refcount));
Craig Tillerb2b42612015-11-20 12:02:17 -0800318 GRPC_MDSTR_UNREF((grpc_mdstr *)(is));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800319}
320
Craig Tillerb2b42612015-11-20 12:02:17 -0800321grpc_mdstr *grpc_mdstr_from_string(const char *str) {
322 return grpc_mdstr_from_buffer((const gpr_uint8 *)str, strlen(str));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800323}
324
Craig Tillerb2b42612015-11-20 12:02:17 -0800325grpc_mdstr *grpc_mdstr_from_slice(gpr_slice slice) {
326 grpc_mdstr *result = grpc_mdstr_from_buffer(GPR_SLICE_START_PTR(slice),
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800327 GPR_SLICE_LENGTH(slice));
328 gpr_slice_unref(slice);
329 return result;
330}
331
Craig Tillerb2b42612015-11-20 12:02:17 -0800332grpc_mdstr *grpc_mdstr_from_buffer(const gpr_uint8 *buf, size_t length) {
333 gpr_uint32 hash = gpr_murmur_hash3(buf, length, g_hash_seed);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800334 internal_string *s;
Craig Tillerb2b42612015-11-20 12:02:17 -0800335 strtab_shard *shard =
336 &g_strtab_shard[SHARD_IDX(hash, LOG2_STRTAB_SHARD_COUNT)];
Craig Tiller0e72ede2015-11-19 07:48:53 -0800337 size_t i;
Craig Tillerb2b42612015-11-20 12:02:17 -0800338 size_t idx;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800339
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800340 GPR_TIMER_BEGIN("grpc_mdstr_from_buffer", 0);
Craig Tiller0e72ede2015-11-19 07:48:53 -0800341
342 /* search for a static string */
Craig Tillerb2b42612015-11-20 12:02:17 -0800343 for (i = 0; i <= g_static_strtab_maxprobe; i++) {
344 grpc_mdstr *ss;
345 idx = (hash + i) % GPR_ARRAY_SIZE(g_static_strtab);
346 ss = g_static_strtab[idx];
347 if (ss == NULL) break;
348 if (ss->hash == hash && GPR_SLICE_LENGTH(ss->slice) == length &&
349 0 == memcmp(buf, GPR_SLICE_START_PTR(ss->slice), length)) {
Craig Tiller07718b82015-11-21 14:06:45 -0800350 GPR_TIMER_END("grpc_mdstr_from_buffer", 0);
Craig Tillerb2b42612015-11-20 12:02:17 -0800351 return ss;
Craig Tiller0e72ede2015-11-19 07:48:53 -0800352 }
353 }
354
Craig Tillerb2b42612015-11-20 12:02:17 -0800355 gpr_mu_lock(&shard->mu);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800356
357 /* search for an existing string */
Craig Tillerb2b42612015-11-20 12:02:17 -0800358 idx = TABLE_IDX(hash, LOG2_STRTAB_SHARD_COUNT, shard->capacity);
359 for (s = shard->strs[idx]; s; s = s->bucket_next) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800360 if (s->hash == hash && GPR_SLICE_LENGTH(s->slice) == length &&
361 0 == memcmp(buf, GPR_SLICE_START_PTR(s->slice), length)) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800362 GRPC_MDSTR_REF((grpc_mdstr *)s);
363 gpr_mu_unlock(&shard->mu);
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800364 GPR_TIMER_END("grpc_mdstr_from_buffer", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800365 return (grpc_mdstr *)s;
366 }
367 }
368
369 /* not found: create a new string */
370 if (length + 1 < GPR_SLICE_INLINED_SIZE) {
371 /* string data goes directly into the slice */
372 s = gpr_malloc(sizeof(internal_string));
Craig Tillerb2b42612015-11-20 12:02:17 -0800373 gpr_atm_rel_store(&s->refcnt, 2);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800374 s->slice.refcount = NULL;
375 memcpy(s->slice.data.inlined.bytes, buf, length);
376 s->slice.data.inlined.bytes[length] = 0;
Craig Tiller6a6b36c2015-09-10 16:00:22 -0700377 s->slice.data.inlined.length = (gpr_uint8)length;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800378 } else {
379 /* string data goes after the internal_string header, and we +1 for null
380 terminator */
381 s = gpr_malloc(sizeof(internal_string) + length + 1);
Craig Tillerb2b42612015-11-20 12:02:17 -0800382 gpr_atm_rel_store(&s->refcnt, 2);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800383 s->refcount.ref = slice_ref;
384 s->refcount.unref = slice_unref;
385 s->slice.refcount = &s->refcount;
386 s->slice.data.refcounted.bytes = (gpr_uint8 *)(s + 1);
387 s->slice.data.refcounted.length = length;
388 memcpy(s->slice.data.refcounted.bytes, buf, length);
389 /* add a null terminator for cheap c string conversion when desired */
390 s->slice.data.refcounted.bytes[length] = 0;
391 }
ctiller430c4992014-12-11 09:15:41 -0800392 s->has_base64_and_huffman_encoded = 0;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800393 s->hash = hash;
Craig Tillerb2b42612015-11-20 12:02:17 -0800394 s->bucket_next = shard->strs[idx];
395 shard->strs[idx] = s;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800396
Craig Tillerb2b42612015-11-20 12:02:17 -0800397 shard->count++;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800398
Craig Tillerb2b42612015-11-20 12:02:17 -0800399 if (shard->count > shard->capacity * 2) {
400 grow_strtab(shard);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800401 }
402
Craig Tillerb2b42612015-11-20 12:02:17 -0800403 gpr_mu_unlock(&shard->mu);
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800404 GPR_TIMER_END("grpc_mdstr_from_buffer", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800405
406 return (grpc_mdstr *)s;
407}
408
Craig Tillerb2b42612015-11-20 12:02:17 -0800409static void gc_mdtab(mdtab_shard *shard) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800410 size_t i;
411 internal_metadata **prev_next;
412 internal_metadata *md, *next;
413
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800414 GPR_TIMER_BEGIN("gc_mdtab", 0);
Craig Tillerb2b42612015-11-20 12:02:17 -0800415 for (i = 0; i < shard->capacity; i++) {
416 prev_next = &shard->elems[i];
417 for (md = shard->elems[i]; md; md = next) {
Craig Tiller8344daa2015-10-09 18:10:57 -0700418 void *user_data = (void *)gpr_atm_no_barrier_load(&md->user_data);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800419 next = md->bucket_next;
Craig Tiller9fa41b92015-04-10 15:08:03 -0700420 if (gpr_atm_acq_load(&md->refcnt) == 0) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800421 GRPC_MDSTR_UNREF((grpc_mdstr *)md->key);
422 GRPC_MDSTR_UNREF((grpc_mdstr *)md->value);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800423 if (md->user_data) {
Craig Tiller8344daa2015-10-09 18:10:57 -0700424 ((destroy_user_data_func)gpr_atm_no_barrier_load(
425 &md->destroy_user_data))(user_data);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800426 }
427 gpr_free(md);
428 *prev_next = next;
Craig Tillerb2b42612015-11-20 12:02:17 -0800429 shard->free--;
430 shard->count--;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800431 } else {
432 prev_next = &md->bucket_next;
433 }
434 }
435 }
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800436 GPR_TIMER_END("gc_mdtab", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800437}
438
Craig Tillerb2b42612015-11-20 12:02:17 -0800439static void grow_mdtab(mdtab_shard *shard) {
440 size_t capacity = shard->capacity * 2;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800441 size_t i;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800442 internal_metadata **mdtab;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800443 internal_metadata *md, *next;
444 gpr_uint32 hash;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800445
446 GPR_TIMER_BEGIN("grow_mdtab", 0);
447
448 mdtab = gpr_malloc(sizeof(internal_metadata *) * capacity);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800449 memset(mdtab, 0, sizeof(internal_metadata *) * capacity);
450
Craig Tillerb2b42612015-11-20 12:02:17 -0800451 for (i = 0; i < shard->capacity; i++) {
452 for (md = shard->elems[i]; md; md = next) {
453 size_t idx;
ctillerfb93d192014-12-15 10:40:05 -0800454 hash = GRPC_MDSTR_KV_HASH(md->key->hash, md->value->hash);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800455 next = md->bucket_next;
Craig Tillerb2b42612015-11-20 12:02:17 -0800456 idx = TABLE_IDX(hash, LOG2_MDTAB_SHARD_COUNT, capacity);
457 md->bucket_next = mdtab[idx];
458 mdtab[idx] = md;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800459 }
460 }
461
Craig Tillerb2b42612015-11-20 12:02:17 -0800462 gpr_free(shard->elems);
463 shard->elems = mdtab;
464 shard->capacity = capacity;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800465
466 GPR_TIMER_END("grow_mdtab", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800467}
468
Craig Tillerb2b42612015-11-20 12:02:17 -0800469static void rehash_mdtab(mdtab_shard *shard) {
470 if (shard->free > shard->capacity / 4) {
471 gc_mdtab(shard);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800472 } else {
Craig Tillerb2b42612015-11-20 12:02:17 -0800473 grow_mdtab(shard);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800474 }
475}
476
Craig Tillerb2b42612015-11-20 12:02:17 -0800477grpc_mdelem *grpc_mdelem_from_metadata_strings(grpc_mdstr *mkey,
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800478 grpc_mdstr *mvalue) {
479 internal_string *key = (internal_string *)mkey;
480 internal_string *value = (internal_string *)mvalue;
ctillerfb93d192014-12-15 10:40:05 -0800481 gpr_uint32 hash = GRPC_MDSTR_KV_HASH(mkey->hash, mvalue->hash);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800482 internal_metadata *md;
Craig Tillerb2b42612015-11-20 12:02:17 -0800483 mdtab_shard *shard = &g_mdtab_shard[SHARD_IDX(hash, LOG2_MDTAB_SHARD_COUNT)];
Craig Tiller0e72ede2015-11-19 07:48:53 -0800484 size_t i;
Craig Tillerb2b42612015-11-20 12:02:17 -0800485 size_t idx;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800486
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800487 GPR_TIMER_BEGIN("grpc_mdelem_from_metadata_strings", 0);
488
Craig Tiller0e72ede2015-11-19 07:48:53 -0800489 if (is_mdstr_static(mkey) && is_mdstr_static(mvalue)) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800490 for (i = 0; i <= g_static_mdtab_maxprobe; i++) {
491 grpc_mdelem *smd;
492 idx = (hash + i) % GPR_ARRAY_SIZE(g_static_mdtab);
493 smd = g_static_mdtab[idx];
494 if (smd == NULL) break;
495 if (smd->key == mkey && smd->value == mvalue) {
Craig Tiller07718b82015-11-21 14:06:45 -0800496 GPR_TIMER_END("grpc_mdelem_from_metadata_strings", 0);
Craig Tillerb2b42612015-11-20 12:02:17 -0800497 return smd;
Craig Tiller0e72ede2015-11-19 07:48:53 -0800498 }
499 }
500 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800501
Craig Tillerb2b42612015-11-20 12:02:17 -0800502 gpr_mu_lock(&shard->mu);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800503
Craig Tillerb2b42612015-11-20 12:02:17 -0800504 idx = TABLE_IDX(hash, LOG2_MDTAB_SHARD_COUNT, shard->capacity);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800505 /* search for an existing pair */
Craig Tillerb2b42612015-11-20 12:02:17 -0800506 for (md = shard->elems[idx]; md; md = md->bucket_next) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800507 if (md->key == key && md->value == value) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800508 REF_MD_LOCKED(shard, md);
509 GRPC_MDSTR_UNREF((grpc_mdstr *)key);
510 GRPC_MDSTR_UNREF((grpc_mdstr *)value);
511 gpr_mu_unlock(&shard->mu);
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800512 GPR_TIMER_END("grpc_mdelem_from_metadata_strings", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800513 return (grpc_mdelem *)md;
514 }
515 }
516
517 /* not found: create a new pair */
518 md = gpr_malloc(sizeof(internal_metadata));
Craig Tiller63bda562015-10-09 17:40:19 -0700519 gpr_atm_rel_store(&md->refcnt, 2);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800520 md->key = key;
521 md->value = value;
Craig Tiller8344daa2015-10-09 18:10:57 -0700522 md->user_data = 0;
523 md->destroy_user_data = 0;
Craig Tillerb2b42612015-11-20 12:02:17 -0800524 md->bucket_next = shard->elems[idx];
525 shard->elems[idx] = md;
Craig Tiller83901532015-07-10 14:02:45 -0700526 gpr_mu_init(&md->mu_user_data);
Craig Tiller1a65a232015-07-06 10:22:32 -0700527#ifdef GRPC_METADATA_REFCOUNT_DEBUG
528 gpr_log(GPR_DEBUG, "ELM NEW:%p:%d: '%s' = '%s'", md,
529 gpr_atm_no_barrier_load(&md->refcnt),
530 grpc_mdstr_as_c_string((grpc_mdstr *)md->key),
531 grpc_mdstr_as_c_string((grpc_mdstr *)md->value));
532#endif
Craig Tillerb2b42612015-11-20 12:02:17 -0800533 shard->count++;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800534
Craig Tillerb2b42612015-11-20 12:02:17 -0800535 if (shard->count > shard->capacity * 2) {
536 rehash_mdtab(shard);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800537 }
538
Craig Tillerb2b42612015-11-20 12:02:17 -0800539 gpr_mu_unlock(&shard->mu);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800540
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800541 GPR_TIMER_END("grpc_mdelem_from_metadata_strings", 0);
542
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800543 return (grpc_mdelem *)md;
544}
545
Craig Tillerb2b42612015-11-20 12:02:17 -0800546grpc_mdelem *grpc_mdelem_from_strings(const char *key, const char *value) {
547 return grpc_mdelem_from_metadata_strings(grpc_mdstr_from_string(key),
548 grpc_mdstr_from_string(value));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800549}
550
Craig Tillerb2b42612015-11-20 12:02:17 -0800551grpc_mdelem *grpc_mdelem_from_slices(gpr_slice key, gpr_slice value) {
552 return grpc_mdelem_from_metadata_strings(grpc_mdstr_from_slice(key),
553 grpc_mdstr_from_slice(value));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800554}
555
Craig Tillerb2b42612015-11-20 12:02:17 -0800556grpc_mdelem *grpc_mdelem_from_string_and_buffer(const char *key,
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800557 const gpr_uint8 *value,
Craig Tiller4dbdd6a2015-09-25 15:12:16 -0700558 size_t value_length) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800559 return grpc_mdelem_from_metadata_strings(
Craig Tillerb2b42612015-11-20 12:02:17 -0800560 grpc_mdstr_from_string(key), grpc_mdstr_from_buffer(value, value_length));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800561}
562
Craig Tiller1a65a232015-07-06 10:22:32 -0700563grpc_mdelem *grpc_mdelem_ref(grpc_mdelem *gmd DEBUG_ARGS) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800564 internal_metadata *md = (internal_metadata *)gmd;
Craig Tiller0e72ede2015-11-19 07:48:53 -0800565 if (is_mdelem_static(gmd)) return gmd;
Craig Tiller1a65a232015-07-06 10:22:32 -0700566#ifdef GRPC_METADATA_REFCOUNT_DEBUG
567 gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,
568 "ELM REF:%p:%d->%d: '%s' = '%s'", md,
569 gpr_atm_no_barrier_load(&md->refcnt),
570 gpr_atm_no_barrier_load(&md->refcnt) + 1,
571 grpc_mdstr_as_c_string((grpc_mdstr *)md->key),
572 grpc_mdstr_as_c_string((grpc_mdstr *)md->value));
573#endif
Craig Tiller9fa41b92015-04-10 15:08:03 -0700574 /* we can assume the ref count is >= 1 as the application is calling
575 this function - meaning that no adjustment to mdtab_free is necessary,
576 simplifying the logic here to be just an atomic increment */
577 /* use C assert to have this removed in opt builds */
Craig Tillerb8e82672015-10-10 13:52:47 -0700578 assert(gpr_atm_no_barrier_load(&md->refcnt) >= 2);
Craig Tiller9fa41b92015-04-10 15:08:03 -0700579 gpr_atm_no_barrier_fetch_add(&md->refcnt, 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800580 return gmd;
581}
582
Craig Tiller1a65a232015-07-06 10:22:32 -0700583void grpc_mdelem_unref(grpc_mdelem *gmd DEBUG_ARGS) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800584 internal_metadata *md = (internal_metadata *)gmd;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800585 if (!md) return;
Craig Tiller0e72ede2015-11-19 07:48:53 -0800586 if (is_mdelem_static(gmd)) return;
Craig Tiller1a65a232015-07-06 10:22:32 -0700587#ifdef GRPC_METADATA_REFCOUNT_DEBUG
588 gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,
589 "ELM UNREF:%p:%d->%d: '%s' = '%s'", md,
590 gpr_atm_no_barrier_load(&md->refcnt),
591 gpr_atm_no_barrier_load(&md->refcnt) - 1,
592 grpc_mdstr_as_c_string((grpc_mdstr *)md->key),
593 grpc_mdstr_as_c_string((grpc_mdstr *)md->value));
594#endif
Craig Tiller63bda562015-10-09 17:40:19 -0700595 if (2 == gpr_atm_full_fetch_add(&md->refcnt, -1)) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800596 gpr_uint32 hash = GRPC_MDSTR_KV_HASH(md->key->hash, md->value->hash);
597 mdtab_shard *shard =
598 &g_mdtab_shard[SHARD_IDX(hash, LOG2_MDTAB_SHARD_COUNT)];
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800599 GPR_TIMER_BEGIN("grpc_mdelem_unref.to_zero", 0);
Craig Tillerb2b42612015-11-20 12:02:17 -0800600 gpr_mu_lock(&shard->mu);
Craig Tillerb8e82672015-10-10 13:52:47 -0700601 if (1 == gpr_atm_no_barrier_load(&md->refcnt)) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800602 shard->free++;
Craig Tillerb8e82672015-10-10 13:52:47 -0700603 gpr_atm_no_barrier_store(&md->refcnt, 0);
604 }
Craig Tillerb2b42612015-11-20 12:02:17 -0800605 gpr_mu_unlock(&shard->mu);
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800606 GPR_TIMER_END("grpc_mdelem_unref.to_zero", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800607 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800608}
609
610const char *grpc_mdstr_as_c_string(grpc_mdstr *s) {
611 return (const char *)GPR_SLICE_START_PTR(s->slice);
612}
613
Craig Tiller1a65a232015-07-06 10:22:32 -0700614grpc_mdstr *grpc_mdstr_ref(grpc_mdstr *gs DEBUG_ARGS) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800615 internal_string *s = (internal_string *)gs;
Craig Tiller0e72ede2015-11-19 07:48:53 -0800616 if (is_mdstr_static(gs)) return gs;
Craig Tillerb2b42612015-11-20 12:02:17 -0800617 GPR_ASSERT(gpr_atm_full_fetch_add(&s->refcnt, 1) != 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800618 return gs;
619}
620
Craig Tiller1a65a232015-07-06 10:22:32 -0700621void grpc_mdstr_unref(grpc_mdstr *gs DEBUG_ARGS) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800622 internal_string *s = (internal_string *)gs;
Craig Tiller0e72ede2015-11-19 07:48:53 -0800623 if (is_mdstr_static(gs)) return;
Craig Tillerb2b42612015-11-20 12:02:17 -0800624 if (2 == gpr_atm_full_fetch_add(&s->refcnt, -1)) {
625 strtab_shard *shard =
626 &g_strtab_shard[SHARD_IDX(s->hash, LOG2_STRTAB_SHARD_COUNT)];
627 gpr_mu_lock(&shard->mu);
628 if (1 == gpr_atm_no_barrier_load(&s->refcnt)) {
629 internal_destroy_string(shard, s);
630 }
631 gpr_mu_unlock(&shard->mu);
632 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800633}
634
Craig Tiller8344daa2015-10-09 18:10:57 -0700635void *grpc_mdelem_get_user_data(grpc_mdelem *md, void (*destroy_func)(void *)) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800636 internal_metadata *im = (internal_metadata *)md;
Craig Tiller83901532015-07-10 14:02:45 -0700637 void *result;
Craig Tillerb2b42612015-11-20 12:02:17 -0800638 if (is_mdelem_static(md)) {
639 return (void *)grpc_static_mdelem_user_data[md - grpc_static_mdelem_table];
640 }
Craig Tiller8344daa2015-10-09 18:10:57 -0700641 if (gpr_atm_acq_load(&im->destroy_user_data) == (gpr_atm)destroy_func) {
642 return (void *)gpr_atm_no_barrier_load(&im->user_data);
643 } else {
644 return NULL;
645 }
Craig Tiller83901532015-07-10 14:02:45 -0700646 return result;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800647}
648
649void grpc_mdelem_set_user_data(grpc_mdelem *md, void (*destroy_func)(void *),
650 void *user_data) {
651 internal_metadata *im = (internal_metadata *)md;
Craig Tillerebdef9d2015-11-19 17:09:49 -0800652 GPR_ASSERT(!is_mdelem_static(md));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800653 GPR_ASSERT((user_data == NULL) == (destroy_func == NULL));
Craig Tiller83901532015-07-10 14:02:45 -0700654 gpr_mu_lock(&im->mu_user_data);
Craig Tiller8344daa2015-10-09 18:10:57 -0700655 if (gpr_atm_no_barrier_load(&im->destroy_user_data)) {
Craig Tiller83901532015-07-10 14:02:45 -0700656 /* user data can only be set once */
657 gpr_mu_unlock(&im->mu_user_data);
658 if (destroy_func != NULL) {
659 destroy_func(user_data);
660 }
661 return;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800662 }
Craig Tiller8344daa2015-10-09 18:10:57 -0700663 gpr_atm_no_barrier_store(&im->user_data, (gpr_atm)user_data);
664 gpr_atm_rel_store(&im->destroy_user_data, (gpr_atm)destroy_func);
Craig Tiller83901532015-07-10 14:02:45 -0700665 gpr_mu_unlock(&im->mu_user_data);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800666}
ctiller430c4992014-12-11 09:15:41 -0800667
668gpr_slice grpc_mdstr_as_base64_encoded_and_huffman_compressed(grpc_mdstr *gs) {
669 internal_string *s = (internal_string *)gs;
670 gpr_slice slice;
Craig Tillerb2b42612015-11-20 12:02:17 -0800671 strtab_shard *shard =
672 &g_strtab_shard[SHARD_IDX(s->hash, LOG2_STRTAB_SHARD_COUNT)];
673 gpr_mu_lock(&shard->mu);
ctiller430c4992014-12-11 09:15:41 -0800674 if (!s->has_base64_and_huffman_encoded) {
675 s->base64_and_huffman =
676 grpc_chttp2_base64_encode_and_huffman_compress(s->slice);
ctiller33023c42014-12-12 16:28:33 -0800677 s->has_base64_and_huffman_encoded = 1;
ctiller430c4992014-12-11 09:15:41 -0800678 }
679 slice = s->base64_and_huffman;
Craig Tillerb2b42612015-11-20 12:02:17 -0800680 gpr_mu_unlock(&shard->mu);
ctiller430c4992014-12-11 09:15:41 -0800681 return slice;
Craig Tiller190d3602015-02-18 09:23:38 -0800682}
Craig Tillerfe0104a2015-04-14 09:19:12 -0700683
Craig Tiller49772e02015-08-21 08:08:37 -0700684static int conforms_to(grpc_mdstr *s, const gpr_uint8 *legal_bits) {
Craig Tillerb96d0012015-05-06 15:33:23 -0700685 const gpr_uint8 *p = GPR_SLICE_START_PTR(s->slice);
686 const gpr_uint8 *e = GPR_SLICE_END_PTR(s->slice);
687 for (; p != e; p++) {
Craig Tiller49772e02015-08-21 08:08:37 -0700688 int idx = *p;
689 int byte = idx / 8;
690 int bit = idx % 8;
691 if ((legal_bits[byte] & (1 << bit)) == 0) return 0;
Craig Tillerb96d0012015-05-06 15:33:23 -0700692 }
693 return 1;
694}
695
Craig Tiller49772e02015-08-21 08:08:37 -0700696int grpc_mdstr_is_legal_header(grpc_mdstr *s) {
697 static const gpr_uint8 legal_header_bits[256 / 8] = {
Craig Tiller80aa2802015-08-21 08:50:51 -0700698 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0xff, 0x03, 0x00, 0x00, 0x00,
699 0x80, 0xfe, 0xff, 0xff, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Craig Tiller49772e02015-08-21 08:08:37 -0700700 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Craig Tiller49772e02015-08-21 08:08:37 -0700701 return conforms_to(s, legal_header_bits);
702}
703
704int grpc_mdstr_is_legal_nonbin_header(grpc_mdstr *s) {
705 static const gpr_uint8 legal_header_bits[256 / 8] = {
Craig Tiller240b7db2015-08-27 15:35:32 -0700706 0x00, 0x00, 0x00, 0x00, 0xff, 0xef, 0xff, 0xff, 0xff, 0xff, 0xff,
Craig Tiller49772e02015-08-21 08:08:37 -0700707 0xff, 0xff, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
708 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
709 return conforms_to(s, legal_header_bits);
710}
711
Craig Tillerb96d0012015-05-06 15:33:23 -0700712int grpc_mdstr_is_bin_suffixed(grpc_mdstr *s) {
713 /* TODO(ctiller): consider caching this */
714 return grpc_is_binary_header((const char *)GPR_SLICE_START_PTR(s->slice),
715 GPR_SLICE_LENGTH(s->slice));
716}