blob: bf14e72b0f1f05495c669de4414509c2a3c8d2c8 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * 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
Craig Tiller9533d042016-03-25 17:11:06 -070034#include "src/core/lib/transport/metadata.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080035
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>
Craig Tiller0cb803d2016-03-02 22:17:24 -080046
Craig Tiller9533d042016-03-25 17:11:06 -070047#include "src/core/lib/iomgr/iomgr_internal.h"
48#include "src/core/lib/profiling/timers.h"
49#include "src/core/lib/support/murmur_hash.h"
50#include "src/core/lib/support/string.h"
Craig Tiller9533d042016-03-25 17:11:06 -070051#include "src/core/lib/transport/static_metadata.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080052
Craig Tillerf82ddc42016-04-05 17:15:07 -070053gpr_slice (*grpc_chttp2_base64_encode_and_huffman_compress)(gpr_slice input);
54
Craig Tiller70b080d2015-11-19 08:04:48 -080055/* There are two kinds of mdelem and mdstr instances.
56 * Static instances are declared in static_metadata.{h,c} and
57 * are initialized by grpc_mdctx_global_init().
58 * Dynamic instances are stored in hash tables on grpc_mdctx, and are backed
59 * by internal_string and internal_element structures.
60 * Internal helper functions here-in (is_mdstr_static, is_mdelem_static) are
61 * used to determine which kind of element a pointer refers to.
62 */
63
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080064#define INITIAL_STRTAB_CAPACITY 4
65#define INITIAL_MDTAB_CAPACITY 4
66
Craig Tiller1a65a232015-07-06 10:22:32 -070067#ifdef GRPC_METADATA_REFCOUNT_DEBUG
68#define DEBUG_ARGS , const char *file, int line
69#define FWD_DEBUG_ARGS , file, line
Craig Tillerb2b42612015-11-20 12:02:17 -080070#define REF_MD_LOCKED(shard, s) ref_md_locked((shard), (s), __FILE__, __LINE__)
Craig Tiller1a65a232015-07-06 10:22:32 -070071#else
72#define DEBUG_ARGS
73#define FWD_DEBUG_ARGS
Craig Tillerb2b42612015-11-20 12:02:17 -080074#define REF_MD_LOCKED(shard, s) ref_md_locked((shard), (s))
Craig Tiller1a65a232015-07-06 10:22:32 -070075#endif
76
Craig Tillerb2b42612015-11-20 12:02:17 -080077#define TABLE_IDX(hash, log2_shards, capacity) \
78 (((hash) >> (log2_shards)) % (capacity))
79#define SHARD_IDX(hash, log2_shards) ((hash) & ((1 << (log2_shards)) - 1))
80
Craig Tiller8344daa2015-10-09 18:10:57 -070081typedef void (*destroy_user_data_func)(void *user_data);
82
Craig Tiller70b080d2015-11-19 08:04:48 -080083/* Shadow structure for grpc_mdstr for non-static values */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080084typedef struct internal_string {
85 /* must be byte compatible with grpc_mdstr */
86 gpr_slice slice;
Craig Tiller7536af02015-12-22 13:49:30 -080087 uint32_t hash;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080088
89 /* private only data */
Craig Tillerb2b42612015-11-20 12:02:17 -080090 gpr_atm refcnt;
91
Craig Tiller7536af02015-12-22 13:49:30 -080092 uint8_t has_base64_and_huffman_encoded;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080093 gpr_slice_refcount refcount;
94
ctiller430c4992014-12-11 09:15:41 -080095 gpr_slice base64_and_huffman;
96
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080097 struct internal_string *bucket_next;
98} internal_string;
99
Craig Tiller70b080d2015-11-19 08:04:48 -0800100/* Shadow structure for grpc_mdelem for non-static elements */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800101typedef struct internal_metadata {
102 /* must be byte compatible with grpc_mdelem */
103 internal_string *key;
104 internal_string *value;
105
Craig Tillerb2b42612015-11-20 12:02:17 -0800106 /* private only data */
Craig Tiller9fa41b92015-04-10 15:08:03 -0700107 gpr_atm refcnt;
108
Craig Tiller83901532015-07-10 14:02:45 -0700109 gpr_mu mu_user_data;
Craig Tiller8344daa2015-10-09 18:10:57 -0700110 gpr_atm destroy_user_data;
111 gpr_atm user_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800112
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800113 struct internal_metadata *bucket_next;
114} internal_metadata;
115
Craig Tillerb2b42612015-11-20 12:02:17 -0800116typedef struct strtab_shard {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800117 gpr_mu mu;
Craig Tillerb2b42612015-11-20 12:02:17 -0800118 internal_string **strs;
119 size_t count;
120 size_t capacity;
121} strtab_shard;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800122
Craig Tillerb2b42612015-11-20 12:02:17 -0800123typedef struct mdtab_shard {
124 gpr_mu mu;
125 internal_metadata **elems;
126 size_t count;
127 size_t capacity;
128 size_t free;
129} mdtab_shard;
Craig Tiller0e72ede2015-11-19 07:48:53 -0800130
Craig Tillerb2b42612015-11-20 12:02:17 -0800131#define LOG2_STRTAB_SHARD_COUNT 5
132#define LOG2_MDTAB_SHARD_COUNT 4
133#define STRTAB_SHARD_COUNT ((size_t)(1 << LOG2_STRTAB_SHARD_COUNT))
134#define MDTAB_SHARD_COUNT ((size_t)(1 << LOG2_MDTAB_SHARD_COUNT))
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800135
Craig Tillerb2b42612015-11-20 12:02:17 -0800136/* hash seed: decided at initialization time */
Craig Tiller7536af02015-12-22 13:49:30 -0800137static uint32_t g_hash_seed;
Craig Tillere62bf982015-12-02 17:11:49 -0800138static int g_forced_hash_seed = 0;
Craig Tillerebdef9d2015-11-19 17:09:49 -0800139
Craig Tillerb2b42612015-11-20 12:02:17 -0800140/* linearly probed hash tables for static element lookup */
141static grpc_mdstr *g_static_strtab[GRPC_STATIC_MDSTR_COUNT * 2];
142static grpc_mdelem *g_static_mdtab[GRPC_STATIC_MDELEM_COUNT * 2];
143static size_t g_static_strtab_maxprobe;
144static size_t g_static_mdtab_maxprobe;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800145
Craig Tillerb2b42612015-11-20 12:02:17 -0800146static strtab_shard g_strtab_shard[STRTAB_SHARD_COUNT];
147static mdtab_shard g_mdtab_shard[MDTAB_SHARD_COUNT];
148
Craig Tillerb2b42612015-11-20 12:02:17 -0800149static void gc_mdtab(mdtab_shard *shard);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800150
Craig Tiller7536af02015-12-22 13:49:30 -0800151void grpc_test_only_set_metadata_hash_seed(uint32_t seed) {
Craig Tillere62bf982015-12-02 17:11:49 -0800152 g_hash_seed = seed;
153 g_forced_hash_seed = 1;
154}
155
Craig Tiller0e72ede2015-11-19 07:48:53 -0800156void grpc_mdctx_global_init(void) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800157 size_t i, j;
Craig Tillere62bf982015-12-02 17:11:49 -0800158 if (!g_forced_hash_seed) {
Craig Tiller7536af02015-12-22 13:49:30 -0800159 g_hash_seed = (uint32_t)gpr_now(GPR_CLOCK_REALTIME).tv_nsec;
Craig Tillere62bf982015-12-02 17:11:49 -0800160 }
Craig Tillerb2b42612015-11-20 12:02:17 -0800161 g_static_strtab_maxprobe = 0;
162 g_static_mdtab_maxprobe = 0;
163 /* build static tables */
164 memset(g_static_mdtab, 0, sizeof(g_static_mdtab));
165 memset(g_static_strtab, 0, sizeof(g_static_strtab));
Craig Tiller0e72ede2015-11-19 07:48:53 -0800166 for (i = 0; i < GRPC_STATIC_MDSTR_COUNT; i++) {
167 grpc_mdstr *elem = &grpc_static_mdstr_table[i];
168 const char *str = grpc_static_metadata_strings[i];
Craig Tiller7536af02015-12-22 13:49:30 -0800169 uint32_t hash = gpr_murmur_hash3(str, strlen(str), g_hash_seed);
Craig Tillerb774be42015-11-19 07:56:13 -0800170 *(gpr_slice *)&elem->slice = gpr_slice_from_static_string(str);
Craig Tiller7536af02015-12-22 13:49:30 -0800171 *(uint32_t *)&elem->hash = hash;
Craig Tillerb2b42612015-11-20 12:02:17 -0800172 for (j = 0;; j++) {
173 size_t idx = (hash + j) % GPR_ARRAY_SIZE(g_static_strtab);
174 if (g_static_strtab[idx] == NULL) {
175 g_static_strtab[idx] = &grpc_static_mdstr_table[i];
176 break;
177 }
178 }
179 if (j > g_static_strtab_maxprobe) {
180 g_static_strtab_maxprobe = j;
181 }
Craig Tiller0e72ede2015-11-19 07:48:53 -0800182 }
183 for (i = 0; i < GRPC_STATIC_MDELEM_COUNT; i++) {
184 grpc_mdelem *elem = &grpc_static_mdelem_table[i];
Craig Tillerebdef9d2015-11-19 17:09:49 -0800185 grpc_mdstr *key =
186 &grpc_static_mdstr_table[grpc_static_metadata_elem_indices[2 * i + 0]];
187 grpc_mdstr *value =
188 &grpc_static_mdstr_table[grpc_static_metadata_elem_indices[2 * i + 1]];
Craig Tiller7536af02015-12-22 13:49:30 -0800189 uint32_t hash = GRPC_MDSTR_KV_HASH(key->hash, value->hash);
Craig Tillerb774be42015-11-19 07:56:13 -0800190 *(grpc_mdstr **)&elem->key = key;
191 *(grpc_mdstr **)&elem->value = value;
Craig Tillerb2b42612015-11-20 12:02:17 -0800192 for (j = 0;; j++) {
193 size_t idx = (hash + j) % GPR_ARRAY_SIZE(g_static_mdtab);
194 if (g_static_mdtab[idx] == NULL) {
195 g_static_mdtab[idx] = elem;
196 break;
197 }
198 }
199 if (j > g_static_mdtab_maxprobe) {
200 g_static_mdtab_maxprobe = j;
201 }
202 }
203 /* initialize shards */
204 for (i = 0; i < STRTAB_SHARD_COUNT; i++) {
205 strtab_shard *shard = &g_strtab_shard[i];
206 gpr_mu_init(&shard->mu);
207 shard->count = 0;
208 shard->capacity = INITIAL_STRTAB_CAPACITY;
209 shard->strs = gpr_malloc(sizeof(*shard->strs) * shard->capacity);
210 memset(shard->strs, 0, sizeof(*shard->strs) * shard->capacity);
211 }
212 for (i = 0; i < MDTAB_SHARD_COUNT; i++) {
213 mdtab_shard *shard = &g_mdtab_shard[i];
214 gpr_mu_init(&shard->mu);
215 shard->count = 0;
216 shard->free = 0;
217 shard->capacity = INITIAL_MDTAB_CAPACITY;
218 shard->elems = gpr_malloc(sizeof(*shard->elems) * shard->capacity);
219 memset(shard->elems, 0, sizeof(*shard->elems) * shard->capacity);
Craig Tiller0e72ede2015-11-19 07:48:53 -0800220 }
221}
222
Craig Tillerb2b42612015-11-20 12:02:17 -0800223void grpc_mdctx_global_shutdown(void) {
224 size_t i;
225 for (i = 0; i < MDTAB_SHARD_COUNT; i++) {
226 mdtab_shard *shard = &g_mdtab_shard[i];
227 gpr_mu_destroy(&shard->mu);
Craig Tiller34075442015-11-23 16:16:54 -0800228 gc_mdtab(shard);
229 /* TODO(ctiller): GPR_ASSERT(shard->count == 0); */
230 if (shard->count != 0) {
yang-gd88e1d82015-12-02 13:23:33 -0800231 gpr_log(GPR_DEBUG, "WARNING: %d metadata elements were leaked",
232 shard->count);
Craig Tiller0cb803d2016-03-02 22:17:24 -0800233 if (grpc_iomgr_abort_on_leaks()) {
234 abort();
235 }
Craig Tiller34075442015-11-23 16:16:54 -0800236 }
Craig Tillerb2b42612015-11-20 12:02:17 -0800237 gpr_free(shard->elems);
238 }
239 for (i = 0; i < STRTAB_SHARD_COUNT; i++) {
240 strtab_shard *shard = &g_strtab_shard[i];
241 gpr_mu_destroy(&shard->mu);
Craig Tiller34075442015-11-23 16:16:54 -0800242 /* TODO(ctiller): GPR_ASSERT(shard->count == 0); */
243 if (shard->count != 0) {
yang-gd88e1d82015-12-02 13:23:33 -0800244 gpr_log(GPR_DEBUG, "WARNING: %d metadata strings were leaked",
245 shard->count);
Craig Tiller0cb803d2016-03-02 22:17:24 -0800246 if (grpc_iomgr_abort_on_leaks()) {
247 abort();
248 }
Craig Tiller34075442015-11-23 16:16:54 -0800249 }
Craig Tillerb2b42612015-11-20 12:02:17 -0800250 gpr_free(shard->strs);
251 }
252}
Craig Tiller0e72ede2015-11-19 07:48:53 -0800253
254static int is_mdstr_static(grpc_mdstr *s) {
Craig Tillerb774be42015-11-19 07:56:13 -0800255 return s >= &grpc_static_mdstr_table[0] &&
256 s < &grpc_static_mdstr_table[GRPC_STATIC_MDSTR_COUNT];
Craig Tiller0e72ede2015-11-19 07:48:53 -0800257}
258
259static int is_mdelem_static(grpc_mdelem *e) {
Craig Tillerb774be42015-11-19 07:56:13 -0800260 return e >= &grpc_static_mdelem_table[0] &&
261 e < &grpc_static_mdelem_table[GRPC_STATIC_MDELEM_COUNT];
Craig Tiller0e72ede2015-11-19 07:48:53 -0800262}
263
Craig Tillerb2b42612015-11-20 12:02:17 -0800264static void ref_md_locked(mdtab_shard *shard,
265 internal_metadata *md DEBUG_ARGS) {
Craig Tiller1a65a232015-07-06 10:22:32 -0700266#ifdef GRPC_METADATA_REFCOUNT_DEBUG
267 gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,
268 "ELM REF:%p:%d->%d: '%s' = '%s'", md,
269 gpr_atm_no_barrier_load(&md->refcnt),
270 gpr_atm_no_barrier_load(&md->refcnt) + 1,
271 grpc_mdstr_as_c_string((grpc_mdstr *)md->key),
272 grpc_mdstr_as_c_string((grpc_mdstr *)md->value));
273#endif
Craig Tillerb8e82672015-10-10 13:52:47 -0700274 if (0 == gpr_atm_no_barrier_fetch_add(&md->refcnt, 2)) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800275 shard->free--;
Craig Tillerb8e82672015-10-10 13:52:47 -0700276 } else {
277 GPR_ASSERT(1 != gpr_atm_no_barrier_fetch_add(&md->refcnt, -1));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800278 }
279}
280
Craig Tillerb2b42612015-11-20 12:02:17 -0800281static void grow_strtab(strtab_shard *shard) {
282 size_t capacity = shard->capacity * 2;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800283 size_t i;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800284 internal_string **strtab;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800285 internal_string *s, *next;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800286
287 GPR_TIMER_BEGIN("grow_strtab", 0);
288
289 strtab = gpr_malloc(sizeof(internal_string *) * capacity);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800290 memset(strtab, 0, sizeof(internal_string *) * capacity);
291
Craig Tillerb2b42612015-11-20 12:02:17 -0800292 for (i = 0; i < shard->capacity; i++) {
293 for (s = shard->strs[i]; s; s = next) {
294 size_t idx = TABLE_IDX(s->hash, LOG2_STRTAB_SHARD_COUNT, capacity);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800295 next = s->bucket_next;
Craig Tillerb2b42612015-11-20 12:02:17 -0800296 s->bucket_next = strtab[idx];
297 strtab[idx] = s;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800298 }
299 }
300
Craig Tillerb2b42612015-11-20 12:02:17 -0800301 gpr_free(shard->strs);
302 shard->strs = strtab;
303 shard->capacity = capacity;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800304
305 GPR_TIMER_END("grow_strtab", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800306}
307
Craig Tillerb2b42612015-11-20 12:02:17 -0800308static void internal_destroy_string(strtab_shard *shard, internal_string *is) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800309 internal_string **prev_next;
310 internal_string *cur;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800311 GPR_TIMER_BEGIN("internal_destroy_string", 0);
ctiller430c4992014-12-11 09:15:41 -0800312 if (is->has_base64_and_huffman_encoded) {
313 gpr_slice_unref(is->base64_and_huffman);
314 }
Craig Tillerb2b42612015-11-20 12:02:17 -0800315 for (prev_next = &shard->strs[TABLE_IDX(is->hash, LOG2_STRTAB_SHARD_COUNT,
316 shard->capacity)],
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800317 cur = *prev_next;
318 cur != is; prev_next = &cur->bucket_next, cur = cur->bucket_next)
319 ;
320 *prev_next = cur->bucket_next;
Craig Tillerb2b42612015-11-20 12:02:17 -0800321 shard->count--;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800322 gpr_free(is);
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800323 GPR_TIMER_END("internal_destroy_string", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800324}
325
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800326static void slice_ref(void *p) {
327 internal_string *is =
328 (internal_string *)((char *)p - offsetof(internal_string, refcount));
Craig Tillerb2b42612015-11-20 12:02:17 -0800329 GRPC_MDSTR_REF((grpc_mdstr *)(is));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800330}
331
332static void slice_unref(void *p) {
333 internal_string *is =
334 (internal_string *)((char *)p - offsetof(internal_string, refcount));
Craig Tillerb2b42612015-11-20 12:02:17 -0800335 GRPC_MDSTR_UNREF((grpc_mdstr *)(is));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800336}
337
Craig Tillerb2b42612015-11-20 12:02:17 -0800338grpc_mdstr *grpc_mdstr_from_string(const char *str) {
Craig Tiller7536af02015-12-22 13:49:30 -0800339 return grpc_mdstr_from_buffer((const uint8_t *)str, strlen(str));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800340}
341
Craig Tillerb2b42612015-11-20 12:02:17 -0800342grpc_mdstr *grpc_mdstr_from_slice(gpr_slice slice) {
343 grpc_mdstr *result = grpc_mdstr_from_buffer(GPR_SLICE_START_PTR(slice),
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800344 GPR_SLICE_LENGTH(slice));
345 gpr_slice_unref(slice);
346 return result;
347}
348
Craig Tiller7536af02015-12-22 13:49:30 -0800349grpc_mdstr *grpc_mdstr_from_buffer(const uint8_t *buf, size_t length) {
350 uint32_t hash = gpr_murmur_hash3(buf, length, g_hash_seed);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800351 internal_string *s;
Craig Tillerb2b42612015-11-20 12:02:17 -0800352 strtab_shard *shard =
353 &g_strtab_shard[SHARD_IDX(hash, LOG2_STRTAB_SHARD_COUNT)];
Craig Tiller0e72ede2015-11-19 07:48:53 -0800354 size_t i;
Craig Tillerb2b42612015-11-20 12:02:17 -0800355 size_t idx;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800356
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800357 GPR_TIMER_BEGIN("grpc_mdstr_from_buffer", 0);
Craig Tiller0e72ede2015-11-19 07:48:53 -0800358
359 /* search for a static string */
Craig Tillerb2b42612015-11-20 12:02:17 -0800360 for (i = 0; i <= g_static_strtab_maxprobe; i++) {
361 grpc_mdstr *ss;
362 idx = (hash + i) % GPR_ARRAY_SIZE(g_static_strtab);
363 ss = g_static_strtab[idx];
364 if (ss == NULL) break;
365 if (ss->hash == hash && GPR_SLICE_LENGTH(ss->slice) == length &&
366 0 == memcmp(buf, GPR_SLICE_START_PTR(ss->slice), length)) {
Craig Tiller07718b82015-11-21 14:06:45 -0800367 GPR_TIMER_END("grpc_mdstr_from_buffer", 0);
Craig Tillerb2b42612015-11-20 12:02:17 -0800368 return ss;
Craig Tiller0e72ede2015-11-19 07:48:53 -0800369 }
370 }
371
Craig Tillerb2b42612015-11-20 12:02:17 -0800372 gpr_mu_lock(&shard->mu);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800373
374 /* search for an existing string */
Craig Tillerb2b42612015-11-20 12:02:17 -0800375 idx = TABLE_IDX(hash, LOG2_STRTAB_SHARD_COUNT, shard->capacity);
376 for (s = shard->strs[idx]; s; s = s->bucket_next) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800377 if (s->hash == hash && GPR_SLICE_LENGTH(s->slice) == length &&
378 0 == memcmp(buf, GPR_SLICE_START_PTR(s->slice), length)) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800379 GRPC_MDSTR_REF((grpc_mdstr *)s);
380 gpr_mu_unlock(&shard->mu);
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800381 GPR_TIMER_END("grpc_mdstr_from_buffer", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800382 return (grpc_mdstr *)s;
383 }
384 }
385
386 /* not found: create a new string */
387 if (length + 1 < GPR_SLICE_INLINED_SIZE) {
388 /* string data goes directly into the slice */
389 s = gpr_malloc(sizeof(internal_string));
Craig Tillerb2b42612015-11-20 12:02:17 -0800390 gpr_atm_rel_store(&s->refcnt, 2);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800391 s->slice.refcount = NULL;
392 memcpy(s->slice.data.inlined.bytes, buf, length);
393 s->slice.data.inlined.bytes[length] = 0;
Craig Tiller7536af02015-12-22 13:49:30 -0800394 s->slice.data.inlined.length = (uint8_t)length;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800395 } else {
396 /* string data goes after the internal_string header, and we +1 for null
397 terminator */
398 s = gpr_malloc(sizeof(internal_string) + length + 1);
Craig Tillerb2b42612015-11-20 12:02:17 -0800399 gpr_atm_rel_store(&s->refcnt, 2);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800400 s->refcount.ref = slice_ref;
401 s->refcount.unref = slice_unref;
402 s->slice.refcount = &s->refcount;
Craig Tiller7536af02015-12-22 13:49:30 -0800403 s->slice.data.refcounted.bytes = (uint8_t *)(s + 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800404 s->slice.data.refcounted.length = length;
405 memcpy(s->slice.data.refcounted.bytes, buf, length);
406 /* add a null terminator for cheap c string conversion when desired */
407 s->slice.data.refcounted.bytes[length] = 0;
408 }
ctiller430c4992014-12-11 09:15:41 -0800409 s->has_base64_and_huffman_encoded = 0;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800410 s->hash = hash;
Craig Tillerb2b42612015-11-20 12:02:17 -0800411 s->bucket_next = shard->strs[idx];
412 shard->strs[idx] = s;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800413
Craig Tillerb2b42612015-11-20 12:02:17 -0800414 shard->count++;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800415
Craig Tillerb2b42612015-11-20 12:02:17 -0800416 if (shard->count > shard->capacity * 2) {
417 grow_strtab(shard);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800418 }
419
Craig Tillerb2b42612015-11-20 12:02:17 -0800420 gpr_mu_unlock(&shard->mu);
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800421 GPR_TIMER_END("grpc_mdstr_from_buffer", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800422
423 return (grpc_mdstr *)s;
424}
425
Craig Tillerb2b42612015-11-20 12:02:17 -0800426static void gc_mdtab(mdtab_shard *shard) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800427 size_t i;
428 internal_metadata **prev_next;
429 internal_metadata *md, *next;
430
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800431 GPR_TIMER_BEGIN("gc_mdtab", 0);
Craig Tillerb2b42612015-11-20 12:02:17 -0800432 for (i = 0; i < shard->capacity; i++) {
433 prev_next = &shard->elems[i];
434 for (md = shard->elems[i]; md; md = next) {
Craig Tiller8344daa2015-10-09 18:10:57 -0700435 void *user_data = (void *)gpr_atm_no_barrier_load(&md->user_data);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800436 next = md->bucket_next;
Craig Tiller9fa41b92015-04-10 15:08:03 -0700437 if (gpr_atm_acq_load(&md->refcnt) == 0) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800438 GRPC_MDSTR_UNREF((grpc_mdstr *)md->key);
439 GRPC_MDSTR_UNREF((grpc_mdstr *)md->value);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800440 if (md->user_data) {
Craig Tiller8344daa2015-10-09 18:10:57 -0700441 ((destroy_user_data_func)gpr_atm_no_barrier_load(
442 &md->destroy_user_data))(user_data);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800443 }
444 gpr_free(md);
445 *prev_next = next;
Craig Tillerb2b42612015-11-20 12:02:17 -0800446 shard->free--;
447 shard->count--;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800448 } else {
449 prev_next = &md->bucket_next;
450 }
451 }
452 }
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800453 GPR_TIMER_END("gc_mdtab", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800454}
455
Craig Tillerb2b42612015-11-20 12:02:17 -0800456static void grow_mdtab(mdtab_shard *shard) {
457 size_t capacity = shard->capacity * 2;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800458 size_t i;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800459 internal_metadata **mdtab;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800460 internal_metadata *md, *next;
Craig Tiller7536af02015-12-22 13:49:30 -0800461 uint32_t hash;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800462
463 GPR_TIMER_BEGIN("grow_mdtab", 0);
464
465 mdtab = gpr_malloc(sizeof(internal_metadata *) * capacity);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800466 memset(mdtab, 0, sizeof(internal_metadata *) * capacity);
467
Craig Tillerb2b42612015-11-20 12:02:17 -0800468 for (i = 0; i < shard->capacity; i++) {
469 for (md = shard->elems[i]; md; md = next) {
470 size_t idx;
ctillerfb93d192014-12-15 10:40:05 -0800471 hash = GRPC_MDSTR_KV_HASH(md->key->hash, md->value->hash);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800472 next = md->bucket_next;
Craig Tillerb2b42612015-11-20 12:02:17 -0800473 idx = TABLE_IDX(hash, LOG2_MDTAB_SHARD_COUNT, capacity);
474 md->bucket_next = mdtab[idx];
475 mdtab[idx] = md;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800476 }
477 }
478
Craig Tillerb2b42612015-11-20 12:02:17 -0800479 gpr_free(shard->elems);
480 shard->elems = mdtab;
481 shard->capacity = capacity;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800482
483 GPR_TIMER_END("grow_mdtab", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800484}
485
Craig Tillerb2b42612015-11-20 12:02:17 -0800486static void rehash_mdtab(mdtab_shard *shard) {
487 if (shard->free > shard->capacity / 4) {
488 gc_mdtab(shard);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800489 } else {
Craig Tillerb2b42612015-11-20 12:02:17 -0800490 grow_mdtab(shard);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800491 }
492}
493
Craig Tillerb2b42612015-11-20 12:02:17 -0800494grpc_mdelem *grpc_mdelem_from_metadata_strings(grpc_mdstr *mkey,
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800495 grpc_mdstr *mvalue) {
496 internal_string *key = (internal_string *)mkey;
497 internal_string *value = (internal_string *)mvalue;
Craig Tiller7536af02015-12-22 13:49:30 -0800498 uint32_t hash = GRPC_MDSTR_KV_HASH(mkey->hash, mvalue->hash);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800499 internal_metadata *md;
Craig Tillerb2b42612015-11-20 12:02:17 -0800500 mdtab_shard *shard = &g_mdtab_shard[SHARD_IDX(hash, LOG2_MDTAB_SHARD_COUNT)];
Craig Tiller0e72ede2015-11-19 07:48:53 -0800501 size_t i;
Craig Tillerb2b42612015-11-20 12:02:17 -0800502 size_t idx;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800503
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800504 GPR_TIMER_BEGIN("grpc_mdelem_from_metadata_strings", 0);
505
Craig Tiller0e72ede2015-11-19 07:48:53 -0800506 if (is_mdstr_static(mkey) && is_mdstr_static(mvalue)) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800507 for (i = 0; i <= g_static_mdtab_maxprobe; i++) {
508 grpc_mdelem *smd;
509 idx = (hash + i) % GPR_ARRAY_SIZE(g_static_mdtab);
510 smd = g_static_mdtab[idx];
511 if (smd == NULL) break;
512 if (smd->key == mkey && smd->value == mvalue) {
Craig Tiller07718b82015-11-21 14:06:45 -0800513 GPR_TIMER_END("grpc_mdelem_from_metadata_strings", 0);
Craig Tillerb2b42612015-11-20 12:02:17 -0800514 return smd;
Craig Tiller0e72ede2015-11-19 07:48:53 -0800515 }
516 }
517 }
518
Craig Tillerb2b42612015-11-20 12:02:17 -0800519 gpr_mu_lock(&shard->mu);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800520
Craig Tillerb2b42612015-11-20 12:02:17 -0800521 idx = TABLE_IDX(hash, LOG2_MDTAB_SHARD_COUNT, shard->capacity);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800522 /* search for an existing pair */
Craig Tillerb2b42612015-11-20 12:02:17 -0800523 for (md = shard->elems[idx]; md; md = md->bucket_next) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800524 if (md->key == key && md->value == value) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800525 REF_MD_LOCKED(shard, md);
526 GRPC_MDSTR_UNREF((grpc_mdstr *)key);
527 GRPC_MDSTR_UNREF((grpc_mdstr *)value);
528 gpr_mu_unlock(&shard->mu);
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800529 GPR_TIMER_END("grpc_mdelem_from_metadata_strings", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800530 return (grpc_mdelem *)md;
531 }
532 }
533
534 /* not found: create a new pair */
535 md = gpr_malloc(sizeof(internal_metadata));
Craig Tiller63bda562015-10-09 17:40:19 -0700536 gpr_atm_rel_store(&md->refcnt, 2);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800537 md->key = key;
538 md->value = value;
Craig Tiller8344daa2015-10-09 18:10:57 -0700539 md->user_data = 0;
540 md->destroy_user_data = 0;
Craig Tillerb2b42612015-11-20 12:02:17 -0800541 md->bucket_next = shard->elems[idx];
542 shard->elems[idx] = md;
Craig Tiller83901532015-07-10 14:02:45 -0700543 gpr_mu_init(&md->mu_user_data);
Craig Tiller1a65a232015-07-06 10:22:32 -0700544#ifdef GRPC_METADATA_REFCOUNT_DEBUG
545 gpr_log(GPR_DEBUG, "ELM NEW:%p:%d: '%s' = '%s'", md,
546 gpr_atm_no_barrier_load(&md->refcnt),
547 grpc_mdstr_as_c_string((grpc_mdstr *)md->key),
548 grpc_mdstr_as_c_string((grpc_mdstr *)md->value));
549#endif
Craig Tillerb2b42612015-11-20 12:02:17 -0800550 shard->count++;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800551
Craig Tillerb2b42612015-11-20 12:02:17 -0800552 if (shard->count > shard->capacity * 2) {
553 rehash_mdtab(shard);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800554 }
555
Craig Tillerb2b42612015-11-20 12:02:17 -0800556 gpr_mu_unlock(&shard->mu);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800557
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800558 GPR_TIMER_END("grpc_mdelem_from_metadata_strings", 0);
559
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800560 return (grpc_mdelem *)md;
561}
562
Craig Tillerb2b42612015-11-20 12:02:17 -0800563grpc_mdelem *grpc_mdelem_from_strings(const char *key, const char *value) {
564 return grpc_mdelem_from_metadata_strings(grpc_mdstr_from_string(key),
565 grpc_mdstr_from_string(value));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800566}
567
Craig Tillerb2b42612015-11-20 12:02:17 -0800568grpc_mdelem *grpc_mdelem_from_slices(gpr_slice key, gpr_slice value) {
569 return grpc_mdelem_from_metadata_strings(grpc_mdstr_from_slice(key),
570 grpc_mdstr_from_slice(value));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800571}
572
Craig Tillerb2b42612015-11-20 12:02:17 -0800573grpc_mdelem *grpc_mdelem_from_string_and_buffer(const char *key,
Craig Tiller7536af02015-12-22 13:49:30 -0800574 const uint8_t *value,
Craig Tiller4dbdd6a2015-09-25 15:12:16 -0700575 size_t value_length) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800576 return grpc_mdelem_from_metadata_strings(
Craig Tillerb2b42612015-11-20 12:02:17 -0800577 grpc_mdstr_from_string(key), grpc_mdstr_from_buffer(value, value_length));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800578}
579
Craig Tiller1a65a232015-07-06 10:22:32 -0700580grpc_mdelem *grpc_mdelem_ref(grpc_mdelem *gmd DEBUG_ARGS) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800581 internal_metadata *md = (internal_metadata *)gmd;
Craig Tiller0e72ede2015-11-19 07:48:53 -0800582 if (is_mdelem_static(gmd)) return gmd;
Craig Tiller1a65a232015-07-06 10:22:32 -0700583#ifdef GRPC_METADATA_REFCOUNT_DEBUG
584 gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,
585 "ELM REF:%p:%d->%d: '%s' = '%s'", md,
586 gpr_atm_no_barrier_load(&md->refcnt),
587 gpr_atm_no_barrier_load(&md->refcnt) + 1,
588 grpc_mdstr_as_c_string((grpc_mdstr *)md->key),
589 grpc_mdstr_as_c_string((grpc_mdstr *)md->value));
590#endif
Craig Tiller9fa41b92015-04-10 15:08:03 -0700591 /* we can assume the ref count is >= 1 as the application is calling
592 this function - meaning that no adjustment to mdtab_free is necessary,
593 simplifying the logic here to be just an atomic increment */
594 /* use C assert to have this removed in opt builds */
Craig Tillerb8e82672015-10-10 13:52:47 -0700595 assert(gpr_atm_no_barrier_load(&md->refcnt) >= 2);
Craig Tiller9fa41b92015-04-10 15:08:03 -0700596 gpr_atm_no_barrier_fetch_add(&md->refcnt, 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800597 return gmd;
598}
599
Craig Tiller1a65a232015-07-06 10:22:32 -0700600void grpc_mdelem_unref(grpc_mdelem *gmd DEBUG_ARGS) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800601 internal_metadata *md = (internal_metadata *)gmd;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800602 if (!md) return;
Craig Tiller0e72ede2015-11-19 07:48:53 -0800603 if (is_mdelem_static(gmd)) return;
Craig Tiller1a65a232015-07-06 10:22:32 -0700604#ifdef GRPC_METADATA_REFCOUNT_DEBUG
605 gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,
606 "ELM UNREF:%p:%d->%d: '%s' = '%s'", md,
607 gpr_atm_no_barrier_load(&md->refcnt),
608 gpr_atm_no_barrier_load(&md->refcnt) - 1,
609 grpc_mdstr_as_c_string((grpc_mdstr *)md->key),
610 grpc_mdstr_as_c_string((grpc_mdstr *)md->value));
611#endif
Craig Tiller63bda562015-10-09 17:40:19 -0700612 if (2 == gpr_atm_full_fetch_add(&md->refcnt, -1)) {
Craig Tiller7536af02015-12-22 13:49:30 -0800613 uint32_t hash = GRPC_MDSTR_KV_HASH(md->key->hash, md->value->hash);
Craig Tillerb2b42612015-11-20 12:02:17 -0800614 mdtab_shard *shard =
615 &g_mdtab_shard[SHARD_IDX(hash, LOG2_MDTAB_SHARD_COUNT)];
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800616 GPR_TIMER_BEGIN("grpc_mdelem_unref.to_zero", 0);
Craig Tillerb2b42612015-11-20 12:02:17 -0800617 gpr_mu_lock(&shard->mu);
Craig Tillerb8e82672015-10-10 13:52:47 -0700618 if (1 == gpr_atm_no_barrier_load(&md->refcnt)) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800619 shard->free++;
Craig Tillerb8e82672015-10-10 13:52:47 -0700620 gpr_atm_no_barrier_store(&md->refcnt, 0);
621 }
Craig Tillerb2b42612015-11-20 12:02:17 -0800622 gpr_mu_unlock(&shard->mu);
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800623 GPR_TIMER_END("grpc_mdelem_unref.to_zero", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800624 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800625}
626
627const char *grpc_mdstr_as_c_string(grpc_mdstr *s) {
628 return (const char *)GPR_SLICE_START_PTR(s->slice);
629}
630
Craig Tiller1a65a232015-07-06 10:22:32 -0700631grpc_mdstr *grpc_mdstr_ref(grpc_mdstr *gs DEBUG_ARGS) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800632 internal_string *s = (internal_string *)gs;
Craig Tiller0e72ede2015-11-19 07:48:53 -0800633 if (is_mdstr_static(gs)) return gs;
Craig Tillerb2b42612015-11-20 12:02:17 -0800634 GPR_ASSERT(gpr_atm_full_fetch_add(&s->refcnt, 1) != 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800635 return gs;
636}
637
Craig Tiller1a65a232015-07-06 10:22:32 -0700638void grpc_mdstr_unref(grpc_mdstr *gs DEBUG_ARGS) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800639 internal_string *s = (internal_string *)gs;
Craig Tiller0e72ede2015-11-19 07:48:53 -0800640 if (is_mdstr_static(gs)) return;
Craig Tillerb2b42612015-11-20 12:02:17 -0800641 if (2 == gpr_atm_full_fetch_add(&s->refcnt, -1)) {
642 strtab_shard *shard =
643 &g_strtab_shard[SHARD_IDX(s->hash, LOG2_STRTAB_SHARD_COUNT)];
644 gpr_mu_lock(&shard->mu);
645 if (1 == gpr_atm_no_barrier_load(&s->refcnt)) {
646 internal_destroy_string(shard, s);
647 }
648 gpr_mu_unlock(&shard->mu);
649 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800650}
651
Craig Tiller8344daa2015-10-09 18:10:57 -0700652void *grpc_mdelem_get_user_data(grpc_mdelem *md, void (*destroy_func)(void *)) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800653 internal_metadata *im = (internal_metadata *)md;
Craig Tiller83901532015-07-10 14:02:45 -0700654 void *result;
Craig Tillerb2b42612015-11-20 12:02:17 -0800655 if (is_mdelem_static(md)) {
656 return (void *)grpc_static_mdelem_user_data[md - grpc_static_mdelem_table];
657 }
Craig Tiller8344daa2015-10-09 18:10:57 -0700658 if (gpr_atm_acq_load(&im->destroy_user_data) == (gpr_atm)destroy_func) {
659 return (void *)gpr_atm_no_barrier_load(&im->user_data);
660 } else {
661 return NULL;
662 }
Craig Tiller83901532015-07-10 14:02:45 -0700663 return result;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800664}
665
666void grpc_mdelem_set_user_data(grpc_mdelem *md, void (*destroy_func)(void *),
667 void *user_data) {
668 internal_metadata *im = (internal_metadata *)md;
Craig Tillerebdef9d2015-11-19 17:09:49 -0800669 GPR_ASSERT(!is_mdelem_static(md));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800670 GPR_ASSERT((user_data == NULL) == (destroy_func == NULL));
Craig Tiller83901532015-07-10 14:02:45 -0700671 gpr_mu_lock(&im->mu_user_data);
Craig Tiller8344daa2015-10-09 18:10:57 -0700672 if (gpr_atm_no_barrier_load(&im->destroy_user_data)) {
Craig Tiller83901532015-07-10 14:02:45 -0700673 /* user data can only be set once */
674 gpr_mu_unlock(&im->mu_user_data);
675 if (destroy_func != NULL) {
676 destroy_func(user_data);
677 }
678 return;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800679 }
Craig Tiller8344daa2015-10-09 18:10:57 -0700680 gpr_atm_no_barrier_store(&im->user_data, (gpr_atm)user_data);
681 gpr_atm_rel_store(&im->destroy_user_data, (gpr_atm)destroy_func);
Craig Tiller83901532015-07-10 14:02:45 -0700682 gpr_mu_unlock(&im->mu_user_data);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800683}
ctiller430c4992014-12-11 09:15:41 -0800684
685gpr_slice grpc_mdstr_as_base64_encoded_and_huffman_compressed(grpc_mdstr *gs) {
686 internal_string *s = (internal_string *)gs;
687 gpr_slice slice;
Craig Tillerb2b42612015-11-20 12:02:17 -0800688 strtab_shard *shard =
689 &g_strtab_shard[SHARD_IDX(s->hash, LOG2_STRTAB_SHARD_COUNT)];
690 gpr_mu_lock(&shard->mu);
ctiller430c4992014-12-11 09:15:41 -0800691 if (!s->has_base64_and_huffman_encoded) {
692 s->base64_and_huffman =
693 grpc_chttp2_base64_encode_and_huffman_compress(s->slice);
ctiller33023c42014-12-12 16:28:33 -0800694 s->has_base64_and_huffman_encoded = 1;
ctiller430c4992014-12-11 09:15:41 -0800695 }
696 slice = s->base64_and_huffman;
Craig Tillerb2b42612015-11-20 12:02:17 -0800697 gpr_mu_unlock(&shard->mu);
ctiller430c4992014-12-11 09:15:41 -0800698 return slice;
David Garcia Quintas2bf574f2016-01-14 15:27:08 -0800699}