blob: ada889983c28d11253e45ca51f581f566598daed [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 */
60
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080061#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;
Craig Tiller7536af02015-12-22 13:49:30 -080084 uint32_t hash;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080085
86 /* private only data */
Craig Tillerb2b42612015-11-20 12:02:17 -080087 gpr_atm refcnt;
88
Craig Tiller7536af02015-12-22 13:49:30 -080089 uint8_t 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;
Craig Tiller0e72ede2015-11-19 07:48:53 -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 */
Craig Tiller7536af02015-12-22 13:49:30 -0800134static uint32_t g_hash_seed;
Craig Tillere62bf982015-12-02 17:11:49 -0800135static int g_forced_hash_seed = 0;
Craig Tillerebdef9d2015-11-19 17:09:49 -0800136
Craig Tillerb2b42612015-11-20 12:02:17 -0800137/* linearly probed hash tables for static element lookup */
138static grpc_mdstr *g_static_strtab[GRPC_STATIC_MDSTR_COUNT * 2];
139static grpc_mdelem *g_static_mdtab[GRPC_STATIC_MDELEM_COUNT * 2];
140static size_t g_static_strtab_maxprobe;
141static size_t g_static_mdtab_maxprobe;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800142
Craig Tillerb2b42612015-11-20 12:02:17 -0800143static strtab_shard g_strtab_shard[STRTAB_SHARD_COUNT];
144static mdtab_shard g_mdtab_shard[MDTAB_SHARD_COUNT];
145
Craig Tillerb2b42612015-11-20 12:02:17 -0800146static void gc_mdtab(mdtab_shard *shard);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800147
Craig Tiller7536af02015-12-22 13:49:30 -0800148void grpc_test_only_set_metadata_hash_seed(uint32_t seed) {
Craig Tillere62bf982015-12-02 17:11:49 -0800149 g_hash_seed = seed;
150 g_forced_hash_seed = 1;
151}
152
Craig Tiller0e72ede2015-11-19 07:48:53 -0800153void grpc_mdctx_global_init(void) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800154 size_t i, j;
Craig Tillere62bf982015-12-02 17:11:49 -0800155 if (!g_forced_hash_seed) {
Craig Tiller7536af02015-12-22 13:49:30 -0800156 g_hash_seed = (uint32_t)gpr_now(GPR_CLOCK_REALTIME).tv_nsec;
Craig Tillere62bf982015-12-02 17:11:49 -0800157 }
Craig Tillerb2b42612015-11-20 12:02:17 -0800158 g_static_strtab_maxprobe = 0;
159 g_static_mdtab_maxprobe = 0;
160 /* build static tables */
161 memset(g_static_mdtab, 0, sizeof(g_static_mdtab));
162 memset(g_static_strtab, 0, sizeof(g_static_strtab));
Craig Tiller0e72ede2015-11-19 07:48:53 -0800163 for (i = 0; i < GRPC_STATIC_MDSTR_COUNT; i++) {
164 grpc_mdstr *elem = &grpc_static_mdstr_table[i];
165 const char *str = grpc_static_metadata_strings[i];
Craig Tiller7536af02015-12-22 13:49:30 -0800166 uint32_t hash = gpr_murmur_hash3(str, strlen(str), g_hash_seed);
Craig Tillerb774be42015-11-19 07:56:13 -0800167 *(gpr_slice *)&elem->slice = gpr_slice_from_static_string(str);
Craig Tiller7536af02015-12-22 13:49:30 -0800168 *(uint32_t *)&elem->hash = hash;
Craig Tillerb2b42612015-11-20 12:02:17 -0800169 for (j = 0;; j++) {
170 size_t idx = (hash + j) % GPR_ARRAY_SIZE(g_static_strtab);
171 if (g_static_strtab[idx] == NULL) {
172 g_static_strtab[idx] = &grpc_static_mdstr_table[i];
173 break;
174 }
175 }
176 if (j > g_static_strtab_maxprobe) {
177 g_static_strtab_maxprobe = j;
178 }
Craig Tiller0e72ede2015-11-19 07:48:53 -0800179 }
180 for (i = 0; i < GRPC_STATIC_MDELEM_COUNT; i++) {
181 grpc_mdelem *elem = &grpc_static_mdelem_table[i];
Craig Tillerebdef9d2015-11-19 17:09:49 -0800182 grpc_mdstr *key =
183 &grpc_static_mdstr_table[grpc_static_metadata_elem_indices[2 * i + 0]];
184 grpc_mdstr *value =
185 &grpc_static_mdstr_table[grpc_static_metadata_elem_indices[2 * i + 1]];
Craig Tiller7536af02015-12-22 13:49:30 -0800186 uint32_t hash = GRPC_MDSTR_KV_HASH(key->hash, value->hash);
Craig Tillerb774be42015-11-19 07:56:13 -0800187 *(grpc_mdstr **)&elem->key = key;
188 *(grpc_mdstr **)&elem->value = value;
Craig Tillerb2b42612015-11-20 12:02:17 -0800189 for (j = 0;; j++) {
190 size_t idx = (hash + j) % GPR_ARRAY_SIZE(g_static_mdtab);
191 if (g_static_mdtab[idx] == NULL) {
192 g_static_mdtab[idx] = elem;
193 break;
194 }
195 }
196 if (j > g_static_mdtab_maxprobe) {
197 g_static_mdtab_maxprobe = j;
198 }
199 }
200 /* initialize shards */
201 for (i = 0; i < STRTAB_SHARD_COUNT; i++) {
202 strtab_shard *shard = &g_strtab_shard[i];
203 gpr_mu_init(&shard->mu);
204 shard->count = 0;
205 shard->capacity = INITIAL_STRTAB_CAPACITY;
206 shard->strs = gpr_malloc(sizeof(*shard->strs) * shard->capacity);
207 memset(shard->strs, 0, sizeof(*shard->strs) * shard->capacity);
208 }
209 for (i = 0; i < MDTAB_SHARD_COUNT; i++) {
210 mdtab_shard *shard = &g_mdtab_shard[i];
211 gpr_mu_init(&shard->mu);
212 shard->count = 0;
213 shard->free = 0;
214 shard->capacity = INITIAL_MDTAB_CAPACITY;
215 shard->elems = gpr_malloc(sizeof(*shard->elems) * shard->capacity);
216 memset(shard->elems, 0, sizeof(*shard->elems) * shard->capacity);
Craig Tiller0e72ede2015-11-19 07:48:53 -0800217 }
218}
219
Craig Tillerb2b42612015-11-20 12:02:17 -0800220void grpc_mdctx_global_shutdown(void) {
221 size_t i;
222 for (i = 0; i < MDTAB_SHARD_COUNT; i++) {
223 mdtab_shard *shard = &g_mdtab_shard[i];
224 gpr_mu_destroy(&shard->mu);
Craig Tiller34075442015-11-23 16:16:54 -0800225 gc_mdtab(shard);
226 /* TODO(ctiller): GPR_ASSERT(shard->count == 0); */
227 if (shard->count != 0) {
yang-gd88e1d82015-12-02 13:23:33 -0800228 gpr_log(GPR_DEBUG, "WARNING: %d metadata elements were leaked",
229 shard->count);
Craig Tiller34075442015-11-23 16:16:54 -0800230 }
Craig Tillerb2b42612015-11-20 12:02:17 -0800231 gpr_free(shard->elems);
232 }
233 for (i = 0; i < STRTAB_SHARD_COUNT; i++) {
234 strtab_shard *shard = &g_strtab_shard[i];
235 gpr_mu_destroy(&shard->mu);
Craig Tiller34075442015-11-23 16:16:54 -0800236 /* TODO(ctiller): GPR_ASSERT(shard->count == 0); */
237 if (shard->count != 0) {
yang-gd88e1d82015-12-02 13:23:33 -0800238 gpr_log(GPR_DEBUG, "WARNING: %d metadata strings were leaked",
239 shard->count);
Craig Tiller34075442015-11-23 16:16:54 -0800240 }
Craig Tillerb2b42612015-11-20 12:02:17 -0800241 gpr_free(shard->strs);
242 }
243}
Craig Tiller0e72ede2015-11-19 07:48:53 -0800244
245static int is_mdstr_static(grpc_mdstr *s) {
Craig Tillerb774be42015-11-19 07:56:13 -0800246 return s >= &grpc_static_mdstr_table[0] &&
247 s < &grpc_static_mdstr_table[GRPC_STATIC_MDSTR_COUNT];
Craig Tiller0e72ede2015-11-19 07:48:53 -0800248}
249
250static int is_mdelem_static(grpc_mdelem *e) {
Craig Tillerb774be42015-11-19 07:56:13 -0800251 return e >= &grpc_static_mdelem_table[0] &&
252 e < &grpc_static_mdelem_table[GRPC_STATIC_MDELEM_COUNT];
Craig Tiller0e72ede2015-11-19 07:48:53 -0800253}
254
Craig Tillerb2b42612015-11-20 12:02:17 -0800255static void ref_md_locked(mdtab_shard *shard,
256 internal_metadata *md DEBUG_ARGS) {
Craig Tiller1a65a232015-07-06 10:22:32 -0700257#ifdef GRPC_METADATA_REFCOUNT_DEBUG
258 gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,
259 "ELM REF:%p:%d->%d: '%s' = '%s'", md,
260 gpr_atm_no_barrier_load(&md->refcnt),
261 gpr_atm_no_barrier_load(&md->refcnt) + 1,
262 grpc_mdstr_as_c_string((grpc_mdstr *)md->key),
263 grpc_mdstr_as_c_string((grpc_mdstr *)md->value));
264#endif
Craig Tillerb8e82672015-10-10 13:52:47 -0700265 if (0 == gpr_atm_no_barrier_fetch_add(&md->refcnt, 2)) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800266 shard->free--;
Craig Tillerb8e82672015-10-10 13:52:47 -0700267 } else {
268 GPR_ASSERT(1 != gpr_atm_no_barrier_fetch_add(&md->refcnt, -1));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800269 }
270}
271
Craig Tillerb2b42612015-11-20 12:02:17 -0800272static void grow_strtab(strtab_shard *shard) {
273 size_t capacity = shard->capacity * 2;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800274 size_t i;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800275 internal_string **strtab;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800276 internal_string *s, *next;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800277
278 GPR_TIMER_BEGIN("grow_strtab", 0);
279
280 strtab = gpr_malloc(sizeof(internal_string *) * capacity);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800281 memset(strtab, 0, sizeof(internal_string *) * capacity);
282
Craig Tillerb2b42612015-11-20 12:02:17 -0800283 for (i = 0; i < shard->capacity; i++) {
284 for (s = shard->strs[i]; s; s = next) {
285 size_t idx = TABLE_IDX(s->hash, LOG2_STRTAB_SHARD_COUNT, capacity);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800286 next = s->bucket_next;
Craig Tillerb2b42612015-11-20 12:02:17 -0800287 s->bucket_next = strtab[idx];
288 strtab[idx] = s;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800289 }
290 }
291
Craig Tillerb2b42612015-11-20 12:02:17 -0800292 gpr_free(shard->strs);
293 shard->strs = strtab;
294 shard->capacity = capacity;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800295
296 GPR_TIMER_END("grow_strtab", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800297}
298
Craig Tillerb2b42612015-11-20 12:02:17 -0800299static void internal_destroy_string(strtab_shard *shard, internal_string *is) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800300 internal_string **prev_next;
301 internal_string *cur;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800302 GPR_TIMER_BEGIN("internal_destroy_string", 0);
ctiller430c4992014-12-11 09:15:41 -0800303 if (is->has_base64_and_huffman_encoded) {
304 gpr_slice_unref(is->base64_and_huffman);
305 }
Craig Tillerb2b42612015-11-20 12:02:17 -0800306 for (prev_next = &shard->strs[TABLE_IDX(is->hash, LOG2_STRTAB_SHARD_COUNT,
307 shard->capacity)],
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800308 cur = *prev_next;
309 cur != is; prev_next = &cur->bucket_next, cur = cur->bucket_next)
310 ;
311 *prev_next = cur->bucket_next;
Craig Tillerb2b42612015-11-20 12:02:17 -0800312 shard->count--;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800313 gpr_free(is);
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800314 GPR_TIMER_END("internal_destroy_string", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800315}
316
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800317static void slice_ref(void *p) {
318 internal_string *is =
319 (internal_string *)((char *)p - offsetof(internal_string, refcount));
Craig Tillerb2b42612015-11-20 12:02:17 -0800320 GRPC_MDSTR_REF((grpc_mdstr *)(is));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800321}
322
323static void slice_unref(void *p) {
324 internal_string *is =
325 (internal_string *)((char *)p - offsetof(internal_string, refcount));
Craig Tillerb2b42612015-11-20 12:02:17 -0800326 GRPC_MDSTR_UNREF((grpc_mdstr *)(is));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800327}
328
Craig Tillerb2b42612015-11-20 12:02:17 -0800329grpc_mdstr *grpc_mdstr_from_string(const char *str) {
Craig Tiller7536af02015-12-22 13:49:30 -0800330 return grpc_mdstr_from_buffer((const uint8_t *)str, strlen(str));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800331}
332
Craig Tillerb2b42612015-11-20 12:02:17 -0800333grpc_mdstr *grpc_mdstr_from_slice(gpr_slice slice) {
334 grpc_mdstr *result = grpc_mdstr_from_buffer(GPR_SLICE_START_PTR(slice),
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800335 GPR_SLICE_LENGTH(slice));
336 gpr_slice_unref(slice);
337 return result;
338}
339
Craig Tiller7536af02015-12-22 13:49:30 -0800340grpc_mdstr *grpc_mdstr_from_buffer(const uint8_t *buf, size_t length) {
341 uint32_t hash = gpr_murmur_hash3(buf, length, g_hash_seed);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800342 internal_string *s;
Craig Tillerb2b42612015-11-20 12:02:17 -0800343 strtab_shard *shard =
344 &g_strtab_shard[SHARD_IDX(hash, LOG2_STRTAB_SHARD_COUNT)];
Craig Tiller0e72ede2015-11-19 07:48:53 -0800345 size_t i;
Craig Tillerb2b42612015-11-20 12:02:17 -0800346 size_t idx;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800347
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800348 GPR_TIMER_BEGIN("grpc_mdstr_from_buffer", 0);
Craig Tiller0e72ede2015-11-19 07:48:53 -0800349
350 /* search for a static string */
Craig Tillerb2b42612015-11-20 12:02:17 -0800351 for (i = 0; i <= g_static_strtab_maxprobe; i++) {
352 grpc_mdstr *ss;
353 idx = (hash + i) % GPR_ARRAY_SIZE(g_static_strtab);
354 ss = g_static_strtab[idx];
355 if (ss == NULL) break;
356 if (ss->hash == hash && GPR_SLICE_LENGTH(ss->slice) == length &&
357 0 == memcmp(buf, GPR_SLICE_START_PTR(ss->slice), length)) {
Craig Tiller07718b82015-11-21 14:06:45 -0800358 GPR_TIMER_END("grpc_mdstr_from_buffer", 0);
Craig Tillerb2b42612015-11-20 12:02:17 -0800359 return ss;
Craig Tiller0e72ede2015-11-19 07:48:53 -0800360 }
361 }
362
Craig Tillerb2b42612015-11-20 12:02:17 -0800363 gpr_mu_lock(&shard->mu);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800364
365 /* search for an existing string */
Craig Tillerb2b42612015-11-20 12:02:17 -0800366 idx = TABLE_IDX(hash, LOG2_STRTAB_SHARD_COUNT, shard->capacity);
367 for (s = shard->strs[idx]; s; s = s->bucket_next) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800368 if (s->hash == hash && GPR_SLICE_LENGTH(s->slice) == length &&
369 0 == memcmp(buf, GPR_SLICE_START_PTR(s->slice), length)) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800370 GRPC_MDSTR_REF((grpc_mdstr *)s);
371 gpr_mu_unlock(&shard->mu);
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800372 GPR_TIMER_END("grpc_mdstr_from_buffer", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800373 return (grpc_mdstr *)s;
374 }
375 }
376
377 /* not found: create a new string */
378 if (length + 1 < GPR_SLICE_INLINED_SIZE) {
379 /* string data goes directly into the slice */
380 s = gpr_malloc(sizeof(internal_string));
Craig Tillerb2b42612015-11-20 12:02:17 -0800381 gpr_atm_rel_store(&s->refcnt, 2);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800382 s->slice.refcount = NULL;
383 memcpy(s->slice.data.inlined.bytes, buf, length);
384 s->slice.data.inlined.bytes[length] = 0;
Craig Tiller7536af02015-12-22 13:49:30 -0800385 s->slice.data.inlined.length = (uint8_t)length;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800386 } else {
387 /* string data goes after the internal_string header, and we +1 for null
388 terminator */
389 s = gpr_malloc(sizeof(internal_string) + length + 1);
Craig Tillerb2b42612015-11-20 12:02:17 -0800390 gpr_atm_rel_store(&s->refcnt, 2);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800391 s->refcount.ref = slice_ref;
392 s->refcount.unref = slice_unref;
393 s->slice.refcount = &s->refcount;
Craig Tiller7536af02015-12-22 13:49:30 -0800394 s->slice.data.refcounted.bytes = (uint8_t *)(s + 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800395 s->slice.data.refcounted.length = length;
396 memcpy(s->slice.data.refcounted.bytes, buf, length);
397 /* add a null terminator for cheap c string conversion when desired */
398 s->slice.data.refcounted.bytes[length] = 0;
399 }
ctiller430c4992014-12-11 09:15:41 -0800400 s->has_base64_and_huffman_encoded = 0;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800401 s->hash = hash;
Craig Tillerb2b42612015-11-20 12:02:17 -0800402 s->bucket_next = shard->strs[idx];
403 shard->strs[idx] = s;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800404
Craig Tillerb2b42612015-11-20 12:02:17 -0800405 shard->count++;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800406
Craig Tillerb2b42612015-11-20 12:02:17 -0800407 if (shard->count > shard->capacity * 2) {
408 grow_strtab(shard);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800409 }
410
Craig Tillerb2b42612015-11-20 12:02:17 -0800411 gpr_mu_unlock(&shard->mu);
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800412 GPR_TIMER_END("grpc_mdstr_from_buffer", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800413
414 return (grpc_mdstr *)s;
415}
416
Craig Tillerb2b42612015-11-20 12:02:17 -0800417static void gc_mdtab(mdtab_shard *shard) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800418 size_t i;
419 internal_metadata **prev_next;
420 internal_metadata *md, *next;
421
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800422 GPR_TIMER_BEGIN("gc_mdtab", 0);
Craig Tillerb2b42612015-11-20 12:02:17 -0800423 for (i = 0; i < shard->capacity; i++) {
424 prev_next = &shard->elems[i];
425 for (md = shard->elems[i]; md; md = next) {
Craig Tiller8344daa2015-10-09 18:10:57 -0700426 void *user_data = (void *)gpr_atm_no_barrier_load(&md->user_data);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800427 next = md->bucket_next;
Craig Tiller9fa41b92015-04-10 15:08:03 -0700428 if (gpr_atm_acq_load(&md->refcnt) == 0) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800429 GRPC_MDSTR_UNREF((grpc_mdstr *)md->key);
430 GRPC_MDSTR_UNREF((grpc_mdstr *)md->value);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800431 if (md->user_data) {
Craig Tiller8344daa2015-10-09 18:10:57 -0700432 ((destroy_user_data_func)gpr_atm_no_barrier_load(
433 &md->destroy_user_data))(user_data);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800434 }
435 gpr_free(md);
436 *prev_next = next;
Craig Tillerb2b42612015-11-20 12:02:17 -0800437 shard->free--;
438 shard->count--;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800439 } else {
440 prev_next = &md->bucket_next;
441 }
442 }
443 }
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800444 GPR_TIMER_END("gc_mdtab", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800445}
446
Craig Tillerb2b42612015-11-20 12:02:17 -0800447static void grow_mdtab(mdtab_shard *shard) {
448 size_t capacity = shard->capacity * 2;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800449 size_t i;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800450 internal_metadata **mdtab;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800451 internal_metadata *md, *next;
Craig Tiller7536af02015-12-22 13:49:30 -0800452 uint32_t hash;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800453
454 GPR_TIMER_BEGIN("grow_mdtab", 0);
455
456 mdtab = gpr_malloc(sizeof(internal_metadata *) * capacity);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800457 memset(mdtab, 0, sizeof(internal_metadata *) * capacity);
458
Craig Tillerb2b42612015-11-20 12:02:17 -0800459 for (i = 0; i < shard->capacity; i++) {
460 for (md = shard->elems[i]; md; md = next) {
461 size_t idx;
ctillerfb93d192014-12-15 10:40:05 -0800462 hash = GRPC_MDSTR_KV_HASH(md->key->hash, md->value->hash);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800463 next = md->bucket_next;
Craig Tillerb2b42612015-11-20 12:02:17 -0800464 idx = TABLE_IDX(hash, LOG2_MDTAB_SHARD_COUNT, capacity);
465 md->bucket_next = mdtab[idx];
466 mdtab[idx] = md;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800467 }
468 }
469
Craig Tillerb2b42612015-11-20 12:02:17 -0800470 gpr_free(shard->elems);
471 shard->elems = mdtab;
472 shard->capacity = capacity;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800473
474 GPR_TIMER_END("grow_mdtab", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800475}
476
Craig Tillerb2b42612015-11-20 12:02:17 -0800477static void rehash_mdtab(mdtab_shard *shard) {
478 if (shard->free > shard->capacity / 4) {
479 gc_mdtab(shard);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800480 } else {
Craig Tillerb2b42612015-11-20 12:02:17 -0800481 grow_mdtab(shard);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800482 }
483}
484
Craig Tillerb2b42612015-11-20 12:02:17 -0800485grpc_mdelem *grpc_mdelem_from_metadata_strings(grpc_mdstr *mkey,
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800486 grpc_mdstr *mvalue) {
487 internal_string *key = (internal_string *)mkey;
488 internal_string *value = (internal_string *)mvalue;
Craig Tiller7536af02015-12-22 13:49:30 -0800489 uint32_t hash = GRPC_MDSTR_KV_HASH(mkey->hash, mvalue->hash);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800490 internal_metadata *md;
Craig Tillerb2b42612015-11-20 12:02:17 -0800491 mdtab_shard *shard = &g_mdtab_shard[SHARD_IDX(hash, LOG2_MDTAB_SHARD_COUNT)];
Craig Tiller0e72ede2015-11-19 07:48:53 -0800492 size_t i;
Craig Tillerb2b42612015-11-20 12:02:17 -0800493 size_t idx;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800494
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800495 GPR_TIMER_BEGIN("grpc_mdelem_from_metadata_strings", 0);
496
Craig Tiller0e72ede2015-11-19 07:48:53 -0800497 if (is_mdstr_static(mkey) && is_mdstr_static(mvalue)) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800498 for (i = 0; i <= g_static_mdtab_maxprobe; i++) {
499 grpc_mdelem *smd;
500 idx = (hash + i) % GPR_ARRAY_SIZE(g_static_mdtab);
501 smd = g_static_mdtab[idx];
502 if (smd == NULL) break;
503 if (smd->key == mkey && smd->value == mvalue) {
Craig Tiller07718b82015-11-21 14:06:45 -0800504 GPR_TIMER_END("grpc_mdelem_from_metadata_strings", 0);
Craig Tillerb2b42612015-11-20 12:02:17 -0800505 return smd;
Craig Tiller0e72ede2015-11-19 07:48:53 -0800506 }
507 }
508 }
509
Craig Tillerb2b42612015-11-20 12:02:17 -0800510 gpr_mu_lock(&shard->mu);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800511
Craig Tillerb2b42612015-11-20 12:02:17 -0800512 idx = TABLE_IDX(hash, LOG2_MDTAB_SHARD_COUNT, shard->capacity);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800513 /* search for an existing pair */
Craig Tillerb2b42612015-11-20 12:02:17 -0800514 for (md = shard->elems[idx]; md; md = md->bucket_next) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800515 if (md->key == key && md->value == value) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800516 REF_MD_LOCKED(shard, md);
517 GRPC_MDSTR_UNREF((grpc_mdstr *)key);
518 GRPC_MDSTR_UNREF((grpc_mdstr *)value);
519 gpr_mu_unlock(&shard->mu);
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800520 GPR_TIMER_END("grpc_mdelem_from_metadata_strings", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800521 return (grpc_mdelem *)md;
522 }
523 }
524
525 /* not found: create a new pair */
526 md = gpr_malloc(sizeof(internal_metadata));
Craig Tiller63bda562015-10-09 17:40:19 -0700527 gpr_atm_rel_store(&md->refcnt, 2);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800528 md->key = key;
529 md->value = value;
Craig Tiller8344daa2015-10-09 18:10:57 -0700530 md->user_data = 0;
531 md->destroy_user_data = 0;
Craig Tillerb2b42612015-11-20 12:02:17 -0800532 md->bucket_next = shard->elems[idx];
533 shard->elems[idx] = md;
Craig Tiller83901532015-07-10 14:02:45 -0700534 gpr_mu_init(&md->mu_user_data);
Craig Tiller1a65a232015-07-06 10:22:32 -0700535#ifdef GRPC_METADATA_REFCOUNT_DEBUG
536 gpr_log(GPR_DEBUG, "ELM NEW:%p:%d: '%s' = '%s'", md,
537 gpr_atm_no_barrier_load(&md->refcnt),
538 grpc_mdstr_as_c_string((grpc_mdstr *)md->key),
539 grpc_mdstr_as_c_string((grpc_mdstr *)md->value));
540#endif
Craig Tillerb2b42612015-11-20 12:02:17 -0800541 shard->count++;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800542
Craig Tillerb2b42612015-11-20 12:02:17 -0800543 if (shard->count > shard->capacity * 2) {
544 rehash_mdtab(shard);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800545 }
546
Craig Tillerb2b42612015-11-20 12:02:17 -0800547 gpr_mu_unlock(&shard->mu);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800548
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800549 GPR_TIMER_END("grpc_mdelem_from_metadata_strings", 0);
550
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800551 return (grpc_mdelem *)md;
552}
553
Craig Tillerb2b42612015-11-20 12:02:17 -0800554grpc_mdelem *grpc_mdelem_from_strings(const char *key, const char *value) {
555 return grpc_mdelem_from_metadata_strings(grpc_mdstr_from_string(key),
556 grpc_mdstr_from_string(value));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800557}
558
Craig Tillerb2b42612015-11-20 12:02:17 -0800559grpc_mdelem *grpc_mdelem_from_slices(gpr_slice key, gpr_slice value) {
560 return grpc_mdelem_from_metadata_strings(grpc_mdstr_from_slice(key),
561 grpc_mdstr_from_slice(value));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800562}
563
Craig Tillerb2b42612015-11-20 12:02:17 -0800564grpc_mdelem *grpc_mdelem_from_string_and_buffer(const char *key,
Craig Tiller7536af02015-12-22 13:49:30 -0800565 const uint8_t *value,
Craig Tiller4dbdd6a2015-09-25 15:12:16 -0700566 size_t value_length) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800567 return grpc_mdelem_from_metadata_strings(
Craig Tillerb2b42612015-11-20 12:02:17 -0800568 grpc_mdstr_from_string(key), grpc_mdstr_from_buffer(value, value_length));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800569}
570
Craig Tiller1a65a232015-07-06 10:22:32 -0700571grpc_mdelem *grpc_mdelem_ref(grpc_mdelem *gmd DEBUG_ARGS) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800572 internal_metadata *md = (internal_metadata *)gmd;
Craig Tiller0e72ede2015-11-19 07:48:53 -0800573 if (is_mdelem_static(gmd)) return gmd;
Craig Tiller1a65a232015-07-06 10:22:32 -0700574#ifdef GRPC_METADATA_REFCOUNT_DEBUG
575 gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,
576 "ELM REF:%p:%d->%d: '%s' = '%s'", md,
577 gpr_atm_no_barrier_load(&md->refcnt),
578 gpr_atm_no_barrier_load(&md->refcnt) + 1,
579 grpc_mdstr_as_c_string((grpc_mdstr *)md->key),
580 grpc_mdstr_as_c_string((grpc_mdstr *)md->value));
581#endif
Craig Tiller9fa41b92015-04-10 15:08:03 -0700582 /* we can assume the ref count is >= 1 as the application is calling
583 this function - meaning that no adjustment to mdtab_free is necessary,
584 simplifying the logic here to be just an atomic increment */
585 /* use C assert to have this removed in opt builds */
Craig Tillerb8e82672015-10-10 13:52:47 -0700586 assert(gpr_atm_no_barrier_load(&md->refcnt) >= 2);
Craig Tiller9fa41b92015-04-10 15:08:03 -0700587 gpr_atm_no_barrier_fetch_add(&md->refcnt, 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800588 return gmd;
589}
590
Craig Tiller1a65a232015-07-06 10:22:32 -0700591void grpc_mdelem_unref(grpc_mdelem *gmd DEBUG_ARGS) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800592 internal_metadata *md = (internal_metadata *)gmd;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800593 if (!md) return;
Craig Tiller0e72ede2015-11-19 07:48:53 -0800594 if (is_mdelem_static(gmd)) return;
Craig Tiller1a65a232015-07-06 10:22:32 -0700595#ifdef GRPC_METADATA_REFCOUNT_DEBUG
596 gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,
597 "ELM UNREF:%p:%d->%d: '%s' = '%s'", md,
598 gpr_atm_no_barrier_load(&md->refcnt),
599 gpr_atm_no_barrier_load(&md->refcnt) - 1,
600 grpc_mdstr_as_c_string((grpc_mdstr *)md->key),
601 grpc_mdstr_as_c_string((grpc_mdstr *)md->value));
602#endif
Craig Tiller63bda562015-10-09 17:40:19 -0700603 if (2 == gpr_atm_full_fetch_add(&md->refcnt, -1)) {
Craig Tiller7536af02015-12-22 13:49:30 -0800604 uint32_t hash = GRPC_MDSTR_KV_HASH(md->key->hash, md->value->hash);
Craig Tillerb2b42612015-11-20 12:02:17 -0800605 mdtab_shard *shard =
606 &g_mdtab_shard[SHARD_IDX(hash, LOG2_MDTAB_SHARD_COUNT)];
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800607 GPR_TIMER_BEGIN("grpc_mdelem_unref.to_zero", 0);
Craig Tillerb2b42612015-11-20 12:02:17 -0800608 gpr_mu_lock(&shard->mu);
Craig Tillerb8e82672015-10-10 13:52:47 -0700609 if (1 == gpr_atm_no_barrier_load(&md->refcnt)) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800610 shard->free++;
Craig Tillerb8e82672015-10-10 13:52:47 -0700611 gpr_atm_no_barrier_store(&md->refcnt, 0);
612 }
Craig Tillerb2b42612015-11-20 12:02:17 -0800613 gpr_mu_unlock(&shard->mu);
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800614 GPR_TIMER_END("grpc_mdelem_unref.to_zero", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800615 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800616}
617
618const char *grpc_mdstr_as_c_string(grpc_mdstr *s) {
619 return (const char *)GPR_SLICE_START_PTR(s->slice);
620}
621
Craig Tiller1a65a232015-07-06 10:22:32 -0700622grpc_mdstr *grpc_mdstr_ref(grpc_mdstr *gs DEBUG_ARGS) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800623 internal_string *s = (internal_string *)gs;
Craig Tiller0e72ede2015-11-19 07:48:53 -0800624 if (is_mdstr_static(gs)) return gs;
Craig Tillerb2b42612015-11-20 12:02:17 -0800625 GPR_ASSERT(gpr_atm_full_fetch_add(&s->refcnt, 1) != 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800626 return gs;
627}
628
Craig Tiller1a65a232015-07-06 10:22:32 -0700629void grpc_mdstr_unref(grpc_mdstr *gs DEBUG_ARGS) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800630 internal_string *s = (internal_string *)gs;
Craig Tiller0e72ede2015-11-19 07:48:53 -0800631 if (is_mdstr_static(gs)) return;
Craig Tillerb2b42612015-11-20 12:02:17 -0800632 if (2 == gpr_atm_full_fetch_add(&s->refcnt, -1)) {
633 strtab_shard *shard =
634 &g_strtab_shard[SHARD_IDX(s->hash, LOG2_STRTAB_SHARD_COUNT)];
635 gpr_mu_lock(&shard->mu);
636 if (1 == gpr_atm_no_barrier_load(&s->refcnt)) {
637 internal_destroy_string(shard, s);
638 }
639 gpr_mu_unlock(&shard->mu);
640 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800641}
642
Craig Tiller8344daa2015-10-09 18:10:57 -0700643void *grpc_mdelem_get_user_data(grpc_mdelem *md, void (*destroy_func)(void *)) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800644 internal_metadata *im = (internal_metadata *)md;
Craig Tiller83901532015-07-10 14:02:45 -0700645 void *result;
Craig Tillerb2b42612015-11-20 12:02:17 -0800646 if (is_mdelem_static(md)) {
647 return (void *)grpc_static_mdelem_user_data[md - grpc_static_mdelem_table];
648 }
Craig Tiller8344daa2015-10-09 18:10:57 -0700649 if (gpr_atm_acq_load(&im->destroy_user_data) == (gpr_atm)destroy_func) {
650 return (void *)gpr_atm_no_barrier_load(&im->user_data);
651 } else {
652 return NULL;
653 }
Craig Tiller83901532015-07-10 14:02:45 -0700654 return result;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800655}
656
657void grpc_mdelem_set_user_data(grpc_mdelem *md, void (*destroy_func)(void *),
658 void *user_data) {
659 internal_metadata *im = (internal_metadata *)md;
Craig Tillerebdef9d2015-11-19 17:09:49 -0800660 GPR_ASSERT(!is_mdelem_static(md));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800661 GPR_ASSERT((user_data == NULL) == (destroy_func == NULL));
Craig Tiller83901532015-07-10 14:02:45 -0700662 gpr_mu_lock(&im->mu_user_data);
Craig Tiller8344daa2015-10-09 18:10:57 -0700663 if (gpr_atm_no_barrier_load(&im->destroy_user_data)) {
Craig Tiller83901532015-07-10 14:02:45 -0700664 /* user data can only be set once */
665 gpr_mu_unlock(&im->mu_user_data);
666 if (destroy_func != NULL) {
667 destroy_func(user_data);
668 }
669 return;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800670 }
Craig Tiller8344daa2015-10-09 18:10:57 -0700671 gpr_atm_no_barrier_store(&im->user_data, (gpr_atm)user_data);
672 gpr_atm_rel_store(&im->destroy_user_data, (gpr_atm)destroy_func);
Craig Tiller83901532015-07-10 14:02:45 -0700673 gpr_mu_unlock(&im->mu_user_data);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800674}
ctiller430c4992014-12-11 09:15:41 -0800675
676gpr_slice grpc_mdstr_as_base64_encoded_and_huffman_compressed(grpc_mdstr *gs) {
677 internal_string *s = (internal_string *)gs;
678 gpr_slice slice;
Craig Tillerb2b42612015-11-20 12:02:17 -0800679 strtab_shard *shard =
680 &g_strtab_shard[SHARD_IDX(s->hash, LOG2_STRTAB_SHARD_COUNT)];
681 gpr_mu_lock(&shard->mu);
ctiller430c4992014-12-11 09:15:41 -0800682 if (!s->has_base64_and_huffman_encoded) {
683 s->base64_and_huffman =
684 grpc_chttp2_base64_encode_and_huffman_compress(s->slice);
ctiller33023c42014-12-12 16:28:33 -0800685 s->has_base64_and_huffman_encoded = 1;
ctiller430c4992014-12-11 09:15:41 -0800686 }
687 slice = s->base64_and_huffman;
Craig Tillerb2b42612015-11-20 12:02:17 -0800688 gpr_mu_unlock(&shard->mu);
ctiller430c4992014-12-11 09:15:41 -0800689 return slice;
Craig Tiller190d3602015-02-18 09:23:38 -0800690}
Craig Tillerfe0104a2015-04-14 09:19:12 -0700691
Craig Tiller7536af02015-12-22 13:49:30 -0800692static int conforms_to(grpc_mdstr *s, const uint8_t *legal_bits) {
693 const uint8_t *p = GPR_SLICE_START_PTR(s->slice);
694 const uint8_t *e = GPR_SLICE_END_PTR(s->slice);
Craig Tillerb96d0012015-05-06 15:33:23 -0700695 for (; p != e; p++) {
Craig Tiller49772e02015-08-21 08:08:37 -0700696 int idx = *p;
697 int byte = idx / 8;
698 int bit = idx % 8;
699 if ((legal_bits[byte] & (1 << bit)) == 0) return 0;
Craig Tillerb96d0012015-05-06 15:33:23 -0700700 }
701 return 1;
702}
703
Craig Tiller49772e02015-08-21 08:08:37 -0700704int grpc_mdstr_is_legal_header(grpc_mdstr *s) {
Craig Tiller7536af02015-12-22 13:49:30 -0800705 static const uint8_t legal_header_bits[256 / 8] = {
Craig Tillera7615892015-12-15 10:41:54 -0800706 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0xff, 0x03, 0x00, 0x00, 0x00,
Craig Tiller80aa2802015-08-21 08:50:51 -0700707 0x80, 0xfe, 0xff, 0xff, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Craig Tiller49772e02015-08-21 08:08:37 -0700708 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Craig Tiller49772e02015-08-21 08:08:37 -0700709 return conforms_to(s, legal_header_bits);
710}
711
712int grpc_mdstr_is_legal_nonbin_header(grpc_mdstr *s) {
Craig Tiller7536af02015-12-22 13:49:30 -0800713 static const uint8_t legal_header_bits[256 / 8] = {
Craig Tiller934e9d72015-12-09 12:58:29 -0800714 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
Craig Tiller49772e02015-08-21 08:08:37 -0700715 0xff, 0xff, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
716 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
717 return conforms_to(s, legal_header_bits);
718}
719
Craig Tillerb96d0012015-05-06 15:33:23 -0700720int grpc_mdstr_is_bin_suffixed(grpc_mdstr *s) {
721 /* TODO(ctiller): consider caching this */
722 return grpc_is_binary_header((const char *)GPR_SLICE_START_PTR(s->slice),
723 GPR_SLICE_LENGTH(s->slice));
724}