blob: a76d1ad3a52f5f8a0b5432055fbd403a164d8f6e [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;
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;
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 */
134static gpr_uint32 g_hash_seed;
Craig Tillerebdef9d2015-11-19 17:09:49 -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];
144
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 }
167 }
168 if (j > g_static_strtab_maxprobe) {
169 g_static_strtab_maxprobe = j;
170 }
Craig Tiller0e72ede2015-11-19 07:48:53 -0800171 }
172 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 }
210}
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) {
220 gpr_log(GPR_DEBUG, "WARNING: %d metadata elements were leaked", shard->count);
221 }
Craig Tillerb2b42612015-11-20 12:02:17 -0800222 gpr_free(shard->elems);
223 }
224 for (i = 0; i < STRTAB_SHARD_COUNT; i++) {
225 strtab_shard *shard = &g_strtab_shard[i];
226 gpr_mu_destroy(&shard->mu);
Craig Tiller34075442015-11-23 16:16:54 -0800227 /* TODO(ctiller): GPR_ASSERT(shard->count == 0); */
228 if (shard->count != 0) {
229 gpr_log(GPR_DEBUG, "WARNING: %d metadata strings were leaked", shard->count);
230 }
Craig Tillerb2b42612015-11-20 12:02:17 -0800231 gpr_free(shard->strs);
232 }
233}
Craig Tiller0e72ede2015-11-19 07:48:53 -0800234
235static int is_mdstr_static(grpc_mdstr *s) {
Craig Tillerb774be42015-11-19 07:56:13 -0800236 return s >= &grpc_static_mdstr_table[0] &&
237 s < &grpc_static_mdstr_table[GRPC_STATIC_MDSTR_COUNT];
Craig Tiller0e72ede2015-11-19 07:48:53 -0800238}
239
240static int is_mdelem_static(grpc_mdelem *e) {
Craig Tillerb774be42015-11-19 07:56:13 -0800241 return e >= &grpc_static_mdelem_table[0] &&
242 e < &grpc_static_mdelem_table[GRPC_STATIC_MDELEM_COUNT];
Craig Tiller0e72ede2015-11-19 07:48:53 -0800243}
244
Craig Tillerb2b42612015-11-20 12:02:17 -0800245static void ref_md_locked(mdtab_shard *shard,
246 internal_metadata *md DEBUG_ARGS) {
Craig Tiller1a65a232015-07-06 10:22:32 -0700247#ifdef GRPC_METADATA_REFCOUNT_DEBUG
248 gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,
249 "ELM REF:%p:%d->%d: '%s' = '%s'", md,
250 gpr_atm_no_barrier_load(&md->refcnt),
251 gpr_atm_no_barrier_load(&md->refcnt) + 1,
252 grpc_mdstr_as_c_string((grpc_mdstr *)md->key),
253 grpc_mdstr_as_c_string((grpc_mdstr *)md->value));
254#endif
Craig Tillerb8e82672015-10-10 13:52:47 -0700255 if (0 == gpr_atm_no_barrier_fetch_add(&md->refcnt, 2)) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800256 shard->free--;
Craig Tillerb8e82672015-10-10 13:52:47 -0700257 } else {
258 GPR_ASSERT(1 != gpr_atm_no_barrier_fetch_add(&md->refcnt, -1));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800259 }
260}
261
Craig Tillerb2b42612015-11-20 12:02:17 -0800262static void grow_strtab(strtab_shard *shard) {
263 size_t capacity = shard->capacity * 2;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800264 size_t i;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800265 internal_string **strtab;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800266 internal_string *s, *next;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800267
268 GPR_TIMER_BEGIN("grow_strtab", 0);
269
270 strtab = gpr_malloc(sizeof(internal_string *) * capacity);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800271 memset(strtab, 0, sizeof(internal_string *) * capacity);
272
Craig Tillerb2b42612015-11-20 12:02:17 -0800273 for (i = 0; i < shard->capacity; i++) {
274 for (s = shard->strs[i]; s; s = next) {
275 size_t idx = TABLE_IDX(s->hash, LOG2_STRTAB_SHARD_COUNT, capacity);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800276 next = s->bucket_next;
Craig Tillerb2b42612015-11-20 12:02:17 -0800277 s->bucket_next = strtab[idx];
278 strtab[idx] = s;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800279 }
280 }
281
Craig Tillerb2b42612015-11-20 12:02:17 -0800282 gpr_free(shard->strs);
283 shard->strs = strtab;
284 shard->capacity = capacity;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800285
286 GPR_TIMER_END("grow_strtab", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800287}
288
Craig Tillerb2b42612015-11-20 12:02:17 -0800289static void internal_destroy_string(strtab_shard *shard, internal_string *is) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800290 internal_string **prev_next;
291 internal_string *cur;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800292 GPR_TIMER_BEGIN("internal_destroy_string", 0);
ctiller430c4992014-12-11 09:15:41 -0800293 if (is->has_base64_and_huffman_encoded) {
294 gpr_slice_unref(is->base64_and_huffman);
295 }
Craig Tillerb2b42612015-11-20 12:02:17 -0800296 for (prev_next = &shard->strs[TABLE_IDX(is->hash, LOG2_STRTAB_SHARD_COUNT,
297 shard->capacity)],
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800298 cur = *prev_next;
299 cur != is; prev_next = &cur->bucket_next, cur = cur->bucket_next)
300 ;
301 *prev_next = cur->bucket_next;
Craig Tillerb2b42612015-11-20 12:02:17 -0800302 shard->count--;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800303 gpr_free(is);
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800304 GPR_TIMER_END("internal_destroy_string", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800305}
306
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800307static void slice_ref(void *p) {
308 internal_string *is =
309 (internal_string *)((char *)p - offsetof(internal_string, refcount));
Craig Tillerb2b42612015-11-20 12:02:17 -0800310 GRPC_MDSTR_REF((grpc_mdstr *)(is));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800311}
312
313static void slice_unref(void *p) {
314 internal_string *is =
315 (internal_string *)((char *)p - offsetof(internal_string, refcount));
Craig Tillerb2b42612015-11-20 12:02:17 -0800316 GRPC_MDSTR_UNREF((grpc_mdstr *)(is));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800317}
318
Craig Tillerb2b42612015-11-20 12:02:17 -0800319grpc_mdstr *grpc_mdstr_from_string(const char *str) {
320 return grpc_mdstr_from_buffer((const gpr_uint8 *)str, strlen(str));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800321}
322
Craig Tillerb2b42612015-11-20 12:02:17 -0800323grpc_mdstr *grpc_mdstr_from_slice(gpr_slice slice) {
324 grpc_mdstr *result = grpc_mdstr_from_buffer(GPR_SLICE_START_PTR(slice),
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800325 GPR_SLICE_LENGTH(slice));
326 gpr_slice_unref(slice);
327 return result;
328}
329
Craig Tillerb2b42612015-11-20 12:02:17 -0800330grpc_mdstr *grpc_mdstr_from_buffer(const gpr_uint8 *buf, size_t length) {
331 gpr_uint32 hash = gpr_murmur_hash3(buf, length, g_hash_seed);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800332 internal_string *s;
Craig Tillerb2b42612015-11-20 12:02:17 -0800333 strtab_shard *shard =
334 &g_strtab_shard[SHARD_IDX(hash, LOG2_STRTAB_SHARD_COUNT)];
Craig Tiller0e72ede2015-11-19 07:48:53 -0800335 size_t i;
Craig Tillerb2b42612015-11-20 12:02:17 -0800336 size_t idx;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800337
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800338 GPR_TIMER_BEGIN("grpc_mdstr_from_buffer", 0);
Craig Tiller0e72ede2015-11-19 07:48:53 -0800339
340 /* search for a static string */
Craig Tillerb2b42612015-11-20 12:02:17 -0800341 for (i = 0; i <= g_static_strtab_maxprobe; i++) {
342 grpc_mdstr *ss;
343 idx = (hash + i) % GPR_ARRAY_SIZE(g_static_strtab);
344 ss = g_static_strtab[idx];
345 if (ss == NULL) break;
346 if (ss->hash == hash && GPR_SLICE_LENGTH(ss->slice) == length &&
347 0 == memcmp(buf, GPR_SLICE_START_PTR(ss->slice), length)) {
Craig Tiller07718b82015-11-21 14:06:45 -0800348 GPR_TIMER_END("grpc_mdstr_from_buffer", 0);
Craig Tillerb2b42612015-11-20 12:02:17 -0800349 return ss;
Craig Tiller0e72ede2015-11-19 07:48:53 -0800350 }
351 }
352
Craig Tillerb2b42612015-11-20 12:02:17 -0800353 gpr_mu_lock(&shard->mu);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800354
355 /* search for an existing string */
Craig Tillerb2b42612015-11-20 12:02:17 -0800356 idx = TABLE_IDX(hash, LOG2_STRTAB_SHARD_COUNT, shard->capacity);
357 for (s = shard->strs[idx]; s; s = s->bucket_next) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800358 if (s->hash == hash && GPR_SLICE_LENGTH(s->slice) == length &&
359 0 == memcmp(buf, GPR_SLICE_START_PTR(s->slice), length)) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800360 GRPC_MDSTR_REF((grpc_mdstr *)s);
361 gpr_mu_unlock(&shard->mu);
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800362 GPR_TIMER_END("grpc_mdstr_from_buffer", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800363 return (grpc_mdstr *)s;
364 }
365 }
366
367 /* not found: create a new string */
368 if (length + 1 < GPR_SLICE_INLINED_SIZE) {
369 /* string data goes directly into the slice */
370 s = gpr_malloc(sizeof(internal_string));
Craig Tillerb2b42612015-11-20 12:02:17 -0800371 gpr_atm_rel_store(&s->refcnt, 2);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800372 s->slice.refcount = NULL;
373 memcpy(s->slice.data.inlined.bytes, buf, length);
374 s->slice.data.inlined.bytes[length] = 0;
Craig Tiller6a6b36c2015-09-10 16:00:22 -0700375 s->slice.data.inlined.length = (gpr_uint8)length;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800376 } else {
377 /* string data goes after the internal_string header, and we +1 for null
378 terminator */
379 s = gpr_malloc(sizeof(internal_string) + length + 1);
Craig Tillerb2b42612015-11-20 12:02:17 -0800380 gpr_atm_rel_store(&s->refcnt, 2);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800381 s->refcount.ref = slice_ref;
382 s->refcount.unref = slice_unref;
383 s->slice.refcount = &s->refcount;
384 s->slice.data.refcounted.bytes = (gpr_uint8 *)(s + 1);
385 s->slice.data.refcounted.length = length;
386 memcpy(s->slice.data.refcounted.bytes, buf, length);
387 /* add a null terminator for cheap c string conversion when desired */
388 s->slice.data.refcounted.bytes[length] = 0;
389 }
ctiller430c4992014-12-11 09:15:41 -0800390 s->has_base64_and_huffman_encoded = 0;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800391 s->hash = hash;
Craig Tillerb2b42612015-11-20 12:02:17 -0800392 s->bucket_next = shard->strs[idx];
393 shard->strs[idx] = s;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800394
Craig Tillerb2b42612015-11-20 12:02:17 -0800395 shard->count++;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800396
Craig Tillerb2b42612015-11-20 12:02:17 -0800397 if (shard->count > shard->capacity * 2) {
398 grow_strtab(shard);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800399 }
400
Craig Tillerb2b42612015-11-20 12:02:17 -0800401 gpr_mu_unlock(&shard->mu);
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800402 GPR_TIMER_END("grpc_mdstr_from_buffer", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800403
404 return (grpc_mdstr *)s;
405}
406
Craig Tillerb2b42612015-11-20 12:02:17 -0800407static void gc_mdtab(mdtab_shard *shard) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800408 size_t i;
409 internal_metadata **prev_next;
410 internal_metadata *md, *next;
411
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800412 GPR_TIMER_BEGIN("gc_mdtab", 0);
Craig Tillerb2b42612015-11-20 12:02:17 -0800413 for (i = 0; i < shard->capacity; i++) {
414 prev_next = &shard->elems[i];
415 for (md = shard->elems[i]; md; md = next) {
Craig Tiller8344daa2015-10-09 18:10:57 -0700416 void *user_data = (void *)gpr_atm_no_barrier_load(&md->user_data);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800417 next = md->bucket_next;
Craig Tiller9fa41b92015-04-10 15:08:03 -0700418 if (gpr_atm_acq_load(&md->refcnt) == 0) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800419 GRPC_MDSTR_UNREF((grpc_mdstr *)md->key);
420 GRPC_MDSTR_UNREF((grpc_mdstr *)md->value);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800421 if (md->user_data) {
Craig Tiller8344daa2015-10-09 18:10:57 -0700422 ((destroy_user_data_func)gpr_atm_no_barrier_load(
423 &md->destroy_user_data))(user_data);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800424 }
425 gpr_free(md);
426 *prev_next = next;
Craig Tillerb2b42612015-11-20 12:02:17 -0800427 shard->free--;
428 shard->count--;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800429 } else {
430 prev_next = &md->bucket_next;
431 }
432 }
433 }
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800434 GPR_TIMER_END("gc_mdtab", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800435}
436
Craig Tillerb2b42612015-11-20 12:02:17 -0800437static void grow_mdtab(mdtab_shard *shard) {
438 size_t capacity = shard->capacity * 2;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800439 size_t i;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800440 internal_metadata **mdtab;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800441 internal_metadata *md, *next;
442 gpr_uint32 hash;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800443
444 GPR_TIMER_BEGIN("grow_mdtab", 0);
445
446 mdtab = gpr_malloc(sizeof(internal_metadata *) * capacity);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800447 memset(mdtab, 0, sizeof(internal_metadata *) * capacity);
448
Craig Tillerb2b42612015-11-20 12:02:17 -0800449 for (i = 0; i < shard->capacity; i++) {
450 for (md = shard->elems[i]; md; md = next) {
451 size_t idx;
ctillerfb93d192014-12-15 10:40:05 -0800452 hash = GRPC_MDSTR_KV_HASH(md->key->hash, md->value->hash);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800453 next = md->bucket_next;
Craig Tillerb2b42612015-11-20 12:02:17 -0800454 idx = TABLE_IDX(hash, LOG2_MDTAB_SHARD_COUNT, capacity);
455 md->bucket_next = mdtab[idx];
456 mdtab[idx] = md;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800457 }
458 }
459
Craig Tillerb2b42612015-11-20 12:02:17 -0800460 gpr_free(shard->elems);
461 shard->elems = mdtab;
462 shard->capacity = capacity;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800463
464 GPR_TIMER_END("grow_mdtab", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800465}
466
Craig Tillerb2b42612015-11-20 12:02:17 -0800467static void rehash_mdtab(mdtab_shard *shard) {
468 if (shard->free > shard->capacity / 4) {
469 gc_mdtab(shard);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800470 } else {
Craig Tillerb2b42612015-11-20 12:02:17 -0800471 grow_mdtab(shard);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800472 }
473}
474
Craig Tillerb2b42612015-11-20 12:02:17 -0800475grpc_mdelem *grpc_mdelem_from_metadata_strings(grpc_mdstr *mkey,
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800476 grpc_mdstr *mvalue) {
477 internal_string *key = (internal_string *)mkey;
478 internal_string *value = (internal_string *)mvalue;
ctillerfb93d192014-12-15 10:40:05 -0800479 gpr_uint32 hash = GRPC_MDSTR_KV_HASH(mkey->hash, mvalue->hash);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800480 internal_metadata *md;
Craig Tillerb2b42612015-11-20 12:02:17 -0800481 mdtab_shard *shard = &g_mdtab_shard[SHARD_IDX(hash, LOG2_MDTAB_SHARD_COUNT)];
Craig Tiller0e72ede2015-11-19 07:48:53 -0800482 size_t i;
Craig Tillerb2b42612015-11-20 12:02:17 -0800483 size_t idx;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800484
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800485 GPR_TIMER_BEGIN("grpc_mdelem_from_metadata_strings", 0);
486
Craig Tiller0e72ede2015-11-19 07:48:53 -0800487 if (is_mdstr_static(mkey) && is_mdstr_static(mvalue)) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800488 for (i = 0; i <= g_static_mdtab_maxprobe; i++) {
489 grpc_mdelem *smd;
490 idx = (hash + i) % GPR_ARRAY_SIZE(g_static_mdtab);
491 smd = g_static_mdtab[idx];
492 if (smd == NULL) break;
493 if (smd->key == mkey && smd->value == mvalue) {
Craig Tiller07718b82015-11-21 14:06:45 -0800494 GPR_TIMER_END("grpc_mdelem_from_metadata_strings", 0);
Craig Tillerb2b42612015-11-20 12:02:17 -0800495 return smd;
Craig Tiller0e72ede2015-11-19 07:48:53 -0800496 }
497 }
498 }
499
Craig Tillerb2b42612015-11-20 12:02:17 -0800500 gpr_mu_lock(&shard->mu);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800501
Craig Tillerb2b42612015-11-20 12:02:17 -0800502 idx = TABLE_IDX(hash, LOG2_MDTAB_SHARD_COUNT, shard->capacity);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800503 /* search for an existing pair */
Craig Tillerb2b42612015-11-20 12:02:17 -0800504 for (md = shard->elems[idx]; md; md = md->bucket_next) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800505 if (md->key == key && md->value == value) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800506 REF_MD_LOCKED(shard, md);
507 GRPC_MDSTR_UNREF((grpc_mdstr *)key);
508 GRPC_MDSTR_UNREF((grpc_mdstr *)value);
509 gpr_mu_unlock(&shard->mu);
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800510 GPR_TIMER_END("grpc_mdelem_from_metadata_strings", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800511 return (grpc_mdelem *)md;
512 }
513 }
514
515 /* not found: create a new pair */
516 md = gpr_malloc(sizeof(internal_metadata));
Craig Tiller63bda562015-10-09 17:40:19 -0700517 gpr_atm_rel_store(&md->refcnt, 2);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800518 md->key = key;
519 md->value = value;
Craig Tiller8344daa2015-10-09 18:10:57 -0700520 md->user_data = 0;
521 md->destroy_user_data = 0;
Craig Tillerb2b42612015-11-20 12:02:17 -0800522 md->bucket_next = shard->elems[idx];
523 shard->elems[idx] = md;
Craig Tiller83901532015-07-10 14:02:45 -0700524 gpr_mu_init(&md->mu_user_data);
Craig Tiller1a65a232015-07-06 10:22:32 -0700525#ifdef GRPC_METADATA_REFCOUNT_DEBUG
526 gpr_log(GPR_DEBUG, "ELM NEW:%p:%d: '%s' = '%s'", md,
527 gpr_atm_no_barrier_load(&md->refcnt),
528 grpc_mdstr_as_c_string((grpc_mdstr *)md->key),
529 grpc_mdstr_as_c_string((grpc_mdstr *)md->value));
530#endif
Craig Tillerb2b42612015-11-20 12:02:17 -0800531 shard->count++;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800532
Craig Tillerb2b42612015-11-20 12:02:17 -0800533 if (shard->count > shard->capacity * 2) {
534 rehash_mdtab(shard);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800535 }
536
Craig Tillerb2b42612015-11-20 12:02:17 -0800537 gpr_mu_unlock(&shard->mu);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800538
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800539 GPR_TIMER_END("grpc_mdelem_from_metadata_strings", 0);
540
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800541 return (grpc_mdelem *)md;
542}
543
Craig Tillerb2b42612015-11-20 12:02:17 -0800544grpc_mdelem *grpc_mdelem_from_strings(const char *key, const char *value) {
545 return grpc_mdelem_from_metadata_strings(grpc_mdstr_from_string(key),
546 grpc_mdstr_from_string(value));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800547}
548
Craig Tillerb2b42612015-11-20 12:02:17 -0800549grpc_mdelem *grpc_mdelem_from_slices(gpr_slice key, gpr_slice value) {
550 return grpc_mdelem_from_metadata_strings(grpc_mdstr_from_slice(key),
551 grpc_mdstr_from_slice(value));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800552}
553
Craig Tillerb2b42612015-11-20 12:02:17 -0800554grpc_mdelem *grpc_mdelem_from_string_and_buffer(const char *key,
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800555 const gpr_uint8 *value,
Craig Tiller4dbdd6a2015-09-25 15:12:16 -0700556 size_t value_length) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800557 return grpc_mdelem_from_metadata_strings(
Craig Tillerb2b42612015-11-20 12:02:17 -0800558 grpc_mdstr_from_string(key), grpc_mdstr_from_buffer(value, value_length));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800559}
560
Craig Tiller1a65a232015-07-06 10:22:32 -0700561grpc_mdelem *grpc_mdelem_ref(grpc_mdelem *gmd DEBUG_ARGS) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800562 internal_metadata *md = (internal_metadata *)gmd;
Craig Tiller0e72ede2015-11-19 07:48:53 -0800563 if (is_mdelem_static(gmd)) return gmd;
Craig Tiller1a65a232015-07-06 10:22:32 -0700564#ifdef GRPC_METADATA_REFCOUNT_DEBUG
565 gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,
566 "ELM REF:%p:%d->%d: '%s' = '%s'", md,
567 gpr_atm_no_barrier_load(&md->refcnt),
568 gpr_atm_no_barrier_load(&md->refcnt) + 1,
569 grpc_mdstr_as_c_string((grpc_mdstr *)md->key),
570 grpc_mdstr_as_c_string((grpc_mdstr *)md->value));
571#endif
Craig Tiller9fa41b92015-04-10 15:08:03 -0700572 /* we can assume the ref count is >= 1 as the application is calling
573 this function - meaning that no adjustment to mdtab_free is necessary,
574 simplifying the logic here to be just an atomic increment */
575 /* use C assert to have this removed in opt builds */
Craig Tillerb8e82672015-10-10 13:52:47 -0700576 assert(gpr_atm_no_barrier_load(&md->refcnt) >= 2);
Craig Tiller9fa41b92015-04-10 15:08:03 -0700577 gpr_atm_no_barrier_fetch_add(&md->refcnt, 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800578 return gmd;
579}
580
Craig Tiller1a65a232015-07-06 10:22:32 -0700581void grpc_mdelem_unref(grpc_mdelem *gmd DEBUG_ARGS) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800582 internal_metadata *md = (internal_metadata *)gmd;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800583 if (!md) return;
Craig Tiller0e72ede2015-11-19 07:48:53 -0800584 if (is_mdelem_static(gmd)) return;
Craig Tiller1a65a232015-07-06 10:22:32 -0700585#ifdef GRPC_METADATA_REFCOUNT_DEBUG
586 gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,
587 "ELM UNREF:%p:%d->%d: '%s' = '%s'", md,
588 gpr_atm_no_barrier_load(&md->refcnt),
589 gpr_atm_no_barrier_load(&md->refcnt) - 1,
590 grpc_mdstr_as_c_string((grpc_mdstr *)md->key),
591 grpc_mdstr_as_c_string((grpc_mdstr *)md->value));
592#endif
Craig Tiller63bda562015-10-09 17:40:19 -0700593 if (2 == gpr_atm_full_fetch_add(&md->refcnt, -1)) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800594 gpr_uint32 hash = GRPC_MDSTR_KV_HASH(md->key->hash, md->value->hash);
595 mdtab_shard *shard =
596 &g_mdtab_shard[SHARD_IDX(hash, LOG2_MDTAB_SHARD_COUNT)];
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800597 GPR_TIMER_BEGIN("grpc_mdelem_unref.to_zero", 0);
Craig Tillerb2b42612015-11-20 12:02:17 -0800598 gpr_mu_lock(&shard->mu);
Craig Tillerb8e82672015-10-10 13:52:47 -0700599 if (1 == gpr_atm_no_barrier_load(&md->refcnt)) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800600 shard->free++;
Craig Tillerb8e82672015-10-10 13:52:47 -0700601 gpr_atm_no_barrier_store(&md->refcnt, 0);
602 }
Craig Tillerb2b42612015-11-20 12:02:17 -0800603 gpr_mu_unlock(&shard->mu);
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800604 GPR_TIMER_END("grpc_mdelem_unref.to_zero", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800605 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800606}
607
608const char *grpc_mdstr_as_c_string(grpc_mdstr *s) {
609 return (const char *)GPR_SLICE_START_PTR(s->slice);
610}
611
Craig Tiller1a65a232015-07-06 10:22:32 -0700612grpc_mdstr *grpc_mdstr_ref(grpc_mdstr *gs DEBUG_ARGS) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800613 internal_string *s = (internal_string *)gs;
Craig Tiller0e72ede2015-11-19 07:48:53 -0800614 if (is_mdstr_static(gs)) return gs;
Craig Tillerb2b42612015-11-20 12:02:17 -0800615 GPR_ASSERT(gpr_atm_full_fetch_add(&s->refcnt, 1) != 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800616 return gs;
617}
618
Craig Tiller1a65a232015-07-06 10:22:32 -0700619void grpc_mdstr_unref(grpc_mdstr *gs DEBUG_ARGS) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800620 internal_string *s = (internal_string *)gs;
Craig Tiller0e72ede2015-11-19 07:48:53 -0800621 if (is_mdstr_static(gs)) return;
Craig Tillerb2b42612015-11-20 12:02:17 -0800622 if (2 == gpr_atm_full_fetch_add(&s->refcnt, -1)) {
623 strtab_shard *shard =
624 &g_strtab_shard[SHARD_IDX(s->hash, LOG2_STRTAB_SHARD_COUNT)];
625 gpr_mu_lock(&shard->mu);
626 if (1 == gpr_atm_no_barrier_load(&s->refcnt)) {
627 internal_destroy_string(shard, s);
628 }
629 gpr_mu_unlock(&shard->mu);
630 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800631}
632
Craig Tiller8344daa2015-10-09 18:10:57 -0700633void *grpc_mdelem_get_user_data(grpc_mdelem *md, void (*destroy_func)(void *)) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800634 internal_metadata *im = (internal_metadata *)md;
Craig Tiller83901532015-07-10 14:02:45 -0700635 void *result;
Craig Tillerb2b42612015-11-20 12:02:17 -0800636 if (is_mdelem_static(md)) {
637 return (void *)grpc_static_mdelem_user_data[md - grpc_static_mdelem_table];
638 }
Craig Tiller8344daa2015-10-09 18:10:57 -0700639 if (gpr_atm_acq_load(&im->destroy_user_data) == (gpr_atm)destroy_func) {
640 return (void *)gpr_atm_no_barrier_load(&im->user_data);
641 } else {
642 return NULL;
643 }
Craig Tiller83901532015-07-10 14:02:45 -0700644 return result;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800645}
646
647void grpc_mdelem_set_user_data(grpc_mdelem *md, void (*destroy_func)(void *),
648 void *user_data) {
649 internal_metadata *im = (internal_metadata *)md;
Craig Tillerebdef9d2015-11-19 17:09:49 -0800650 GPR_ASSERT(!is_mdelem_static(md));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800651 GPR_ASSERT((user_data == NULL) == (destroy_func == NULL));
Craig Tiller83901532015-07-10 14:02:45 -0700652 gpr_mu_lock(&im->mu_user_data);
Craig Tiller8344daa2015-10-09 18:10:57 -0700653 if (gpr_atm_no_barrier_load(&im->destroy_user_data)) {
Craig Tiller83901532015-07-10 14:02:45 -0700654 /* user data can only be set once */
655 gpr_mu_unlock(&im->mu_user_data);
656 if (destroy_func != NULL) {
657 destroy_func(user_data);
658 }
659 return;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800660 }
Craig Tiller8344daa2015-10-09 18:10:57 -0700661 gpr_atm_no_barrier_store(&im->user_data, (gpr_atm)user_data);
662 gpr_atm_rel_store(&im->destroy_user_data, (gpr_atm)destroy_func);
Craig Tiller83901532015-07-10 14:02:45 -0700663 gpr_mu_unlock(&im->mu_user_data);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800664}
ctiller430c4992014-12-11 09:15:41 -0800665
666gpr_slice grpc_mdstr_as_base64_encoded_and_huffman_compressed(grpc_mdstr *gs) {
667 internal_string *s = (internal_string *)gs;
668 gpr_slice slice;
Craig Tillerb2b42612015-11-20 12:02:17 -0800669 strtab_shard *shard =
670 &g_strtab_shard[SHARD_IDX(s->hash, LOG2_STRTAB_SHARD_COUNT)];
671 gpr_mu_lock(&shard->mu);
ctiller430c4992014-12-11 09:15:41 -0800672 if (!s->has_base64_and_huffman_encoded) {
673 s->base64_and_huffman =
674 grpc_chttp2_base64_encode_and_huffman_compress(s->slice);
ctiller33023c42014-12-12 16:28:33 -0800675 s->has_base64_and_huffman_encoded = 1;
ctiller430c4992014-12-11 09:15:41 -0800676 }
677 slice = s->base64_and_huffman;
Craig Tillerb2b42612015-11-20 12:02:17 -0800678 gpr_mu_unlock(&shard->mu);
ctiller430c4992014-12-11 09:15:41 -0800679 return slice;
Craig Tiller190d3602015-02-18 09:23:38 -0800680}
Craig Tillerfe0104a2015-04-14 09:19:12 -0700681
Craig Tiller49772e02015-08-21 08:08:37 -0700682static int conforms_to(grpc_mdstr *s, const gpr_uint8 *legal_bits) {
Craig Tillerb96d0012015-05-06 15:33:23 -0700683 const gpr_uint8 *p = GPR_SLICE_START_PTR(s->slice);
684 const gpr_uint8 *e = GPR_SLICE_END_PTR(s->slice);
685 for (; p != e; p++) {
Craig Tiller49772e02015-08-21 08:08:37 -0700686 int idx = *p;
687 int byte = idx / 8;
688 int bit = idx % 8;
689 if ((legal_bits[byte] & (1 << bit)) == 0) return 0;
Craig Tillerb96d0012015-05-06 15:33:23 -0700690 }
691 return 1;
692}
693
Craig Tiller49772e02015-08-21 08:08:37 -0700694int grpc_mdstr_is_legal_header(grpc_mdstr *s) {
695 static const gpr_uint8 legal_header_bits[256 / 8] = {
Craig Tiller80aa2802015-08-21 08:50:51 -0700696 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0xff, 0x03, 0x00, 0x00, 0x00,
697 0x80, 0xfe, 0xff, 0xff, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Craig Tiller49772e02015-08-21 08:08:37 -0700698 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Craig Tiller49772e02015-08-21 08:08:37 -0700699 return conforms_to(s, legal_header_bits);
700}
701
702int grpc_mdstr_is_legal_nonbin_header(grpc_mdstr *s) {
703 static const gpr_uint8 legal_header_bits[256 / 8] = {
Craig Tiller240b7db2015-08-27 15:35:32 -0700704 0x00, 0x00, 0x00, 0x00, 0xff, 0xef, 0xff, 0xff, 0xff, 0xff, 0xff,
Craig Tiller49772e02015-08-21 08:08:37 -0700705 0xff, 0xff, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
706 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
707 return conforms_to(s, legal_header_bits);
708}
709
Craig Tillerb96d0012015-05-06 15:33:23 -0700710int grpc_mdstr_is_bin_suffixed(grpc_mdstr *s) {
711 /* TODO(ctiller): consider caching this */
712 return grpc_is_binary_header((const char *)GPR_SLICE_START_PTR(s->slice),
713 GPR_SLICE_LENGTH(s->slice));
714}