blob: 7605f099914bf8d4105967c96b4fff48ec036fc5 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
murgatroid9940809212016-01-13 17:45:30 -08003 * Copyright 2015-2016, 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"
51#include "src/core/lib/transport/chttp2/bin_encoder.h"
52#include "src/core/lib/transport/static_metadata.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080053
Craig Tiller70b080d2015-11-19 08:04:48 -080054/* There are two kinds of mdelem and mdstr instances.
55 * Static instances are declared in static_metadata.{h,c} and
56 * are initialized by grpc_mdctx_global_init().
57 * Dynamic instances are stored in hash tables on grpc_mdctx, and are backed
58 * by internal_string and internal_element structures.
59 * Internal helper functions here-in (is_mdstr_static, is_mdelem_static) are
60 * used to determine which kind of element a pointer refers to.
61 */
62
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080063#define INITIAL_STRTAB_CAPACITY 4
64#define INITIAL_MDTAB_CAPACITY 4
65
Craig Tiller1a65a232015-07-06 10:22:32 -070066#ifdef GRPC_METADATA_REFCOUNT_DEBUG
67#define DEBUG_ARGS , const char *file, int line
68#define FWD_DEBUG_ARGS , file, line
Craig Tillerb2b42612015-11-20 12:02:17 -080069#define REF_MD_LOCKED(shard, s) ref_md_locked((shard), (s), __FILE__, __LINE__)
Craig Tiller1a65a232015-07-06 10:22:32 -070070#else
71#define DEBUG_ARGS
72#define FWD_DEBUG_ARGS
Craig Tillerb2b42612015-11-20 12:02:17 -080073#define REF_MD_LOCKED(shard, s) ref_md_locked((shard), (s))
Craig Tiller1a65a232015-07-06 10:22:32 -070074#endif
75
Craig Tillerb2b42612015-11-20 12:02:17 -080076#define TABLE_IDX(hash, log2_shards, capacity) \
77 (((hash) >> (log2_shards)) % (capacity))
78#define SHARD_IDX(hash, log2_shards) ((hash) & ((1 << (log2_shards)) - 1))
79
Craig Tiller8344daa2015-10-09 18:10:57 -070080typedef void (*destroy_user_data_func)(void *user_data);
81
Craig Tiller70b080d2015-11-19 08:04:48 -080082/* Shadow structure for grpc_mdstr for non-static values */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080083typedef struct internal_string {
84 /* must be byte compatible with grpc_mdstr */
85 gpr_slice slice;
Craig Tiller7536af02015-12-22 13:49:30 -080086 uint32_t hash;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080087
88 /* private only data */
Craig Tillerb2b42612015-11-20 12:02:17 -080089 gpr_atm refcnt;
90
Craig Tiller7536af02015-12-22 13:49:30 -080091 uint8_t has_base64_and_huffman_encoded;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080092 gpr_slice_refcount refcount;
93
ctiller430c4992014-12-11 09:15:41 -080094 gpr_slice base64_and_huffman;
95
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080096 struct internal_string *bucket_next;
97} internal_string;
98
Craig Tiller70b080d2015-11-19 08:04:48 -080099/* Shadow structure for grpc_mdelem for non-static elements */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800100typedef struct internal_metadata {
101 /* must be byte compatible with grpc_mdelem */
102 internal_string *key;
103 internal_string *value;
104
Craig Tillerb2b42612015-11-20 12:02:17 -0800105 /* private only data */
Craig Tiller9fa41b92015-04-10 15:08:03 -0700106 gpr_atm refcnt;
107
Craig Tiller83901532015-07-10 14:02:45 -0700108 gpr_mu mu_user_data;
Craig Tiller8344daa2015-10-09 18:10:57 -0700109 gpr_atm destroy_user_data;
110 gpr_atm user_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800111
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800112 struct internal_metadata *bucket_next;
113} internal_metadata;
114
Craig Tillerb2b42612015-11-20 12:02:17 -0800115typedef struct strtab_shard {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800116 gpr_mu mu;
Craig Tillerb2b42612015-11-20 12:02:17 -0800117 internal_string **strs;
118 size_t count;
119 size_t capacity;
120} strtab_shard;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800121
Craig Tillerb2b42612015-11-20 12:02:17 -0800122typedef struct mdtab_shard {
123 gpr_mu mu;
124 internal_metadata **elems;
125 size_t count;
126 size_t capacity;
127 size_t free;
128} mdtab_shard;
Craig Tiller0e72ede2015-11-19 07:48:53 -0800129
Craig Tillerb2b42612015-11-20 12:02:17 -0800130#define LOG2_STRTAB_SHARD_COUNT 5
131#define LOG2_MDTAB_SHARD_COUNT 4
132#define STRTAB_SHARD_COUNT ((size_t)(1 << LOG2_STRTAB_SHARD_COUNT))
133#define MDTAB_SHARD_COUNT ((size_t)(1 << LOG2_MDTAB_SHARD_COUNT))
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800134
Craig Tillerb2b42612015-11-20 12:02:17 -0800135/* hash seed: decided at initialization time */
Craig Tiller7536af02015-12-22 13:49:30 -0800136static uint32_t g_hash_seed;
Craig Tillere62bf982015-12-02 17:11:49 -0800137static int g_forced_hash_seed = 0;
Craig Tillerebdef9d2015-11-19 17:09:49 -0800138
Craig Tillerb2b42612015-11-20 12:02:17 -0800139/* linearly probed hash tables for static element lookup */
140static grpc_mdstr *g_static_strtab[GRPC_STATIC_MDSTR_COUNT * 2];
141static grpc_mdelem *g_static_mdtab[GRPC_STATIC_MDELEM_COUNT * 2];
142static size_t g_static_strtab_maxprobe;
143static size_t g_static_mdtab_maxprobe;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800144
Craig Tillerb2b42612015-11-20 12:02:17 -0800145static strtab_shard g_strtab_shard[STRTAB_SHARD_COUNT];
146static mdtab_shard g_mdtab_shard[MDTAB_SHARD_COUNT];
147
Craig Tillerb2b42612015-11-20 12:02:17 -0800148static void gc_mdtab(mdtab_shard *shard);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800149
Craig Tiller7536af02015-12-22 13:49:30 -0800150void grpc_test_only_set_metadata_hash_seed(uint32_t seed) {
Craig Tillere62bf982015-12-02 17:11:49 -0800151 g_hash_seed = seed;
152 g_forced_hash_seed = 1;
153}
154
Craig Tiller0e72ede2015-11-19 07:48:53 -0800155void grpc_mdctx_global_init(void) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800156 size_t i, j;
Craig Tillere62bf982015-12-02 17:11:49 -0800157 if (!g_forced_hash_seed) {
Craig Tiller7536af02015-12-22 13:49:30 -0800158 g_hash_seed = (uint32_t)gpr_now(GPR_CLOCK_REALTIME).tv_nsec;
Craig Tillere62bf982015-12-02 17:11:49 -0800159 }
Craig Tillerb2b42612015-11-20 12:02:17 -0800160 g_static_strtab_maxprobe = 0;
161 g_static_mdtab_maxprobe = 0;
162 /* build static tables */
163 memset(g_static_mdtab, 0, sizeof(g_static_mdtab));
164 memset(g_static_strtab, 0, sizeof(g_static_strtab));
Craig Tiller0e72ede2015-11-19 07:48:53 -0800165 for (i = 0; i < GRPC_STATIC_MDSTR_COUNT; i++) {
166 grpc_mdstr *elem = &grpc_static_mdstr_table[i];
167 const char *str = grpc_static_metadata_strings[i];
Craig Tiller7536af02015-12-22 13:49:30 -0800168 uint32_t hash = gpr_murmur_hash3(str, strlen(str), g_hash_seed);
Craig Tillerb774be42015-11-19 07:56:13 -0800169 *(gpr_slice *)&elem->slice = gpr_slice_from_static_string(str);
Craig Tiller7536af02015-12-22 13:49:30 -0800170 *(uint32_t *)&elem->hash = hash;
Craig Tillerb2b42612015-11-20 12:02:17 -0800171 for (j = 0;; j++) {
172 size_t idx = (hash + j) % GPR_ARRAY_SIZE(g_static_strtab);
173 if (g_static_strtab[idx] == NULL) {
174 g_static_strtab[idx] = &grpc_static_mdstr_table[i];
175 break;
176 }
177 }
178 if (j > g_static_strtab_maxprobe) {
179 g_static_strtab_maxprobe = j;
180 }
Craig Tiller0e72ede2015-11-19 07:48:53 -0800181 }
182 for (i = 0; i < GRPC_STATIC_MDELEM_COUNT; i++) {
183 grpc_mdelem *elem = &grpc_static_mdelem_table[i];
Craig Tillerebdef9d2015-11-19 17:09:49 -0800184 grpc_mdstr *key =
185 &grpc_static_mdstr_table[grpc_static_metadata_elem_indices[2 * i + 0]];
186 grpc_mdstr *value =
187 &grpc_static_mdstr_table[grpc_static_metadata_elem_indices[2 * i + 1]];
Craig Tiller7536af02015-12-22 13:49:30 -0800188 uint32_t hash = GRPC_MDSTR_KV_HASH(key->hash, value->hash);
Craig Tillerb774be42015-11-19 07:56:13 -0800189 *(grpc_mdstr **)&elem->key = key;
190 *(grpc_mdstr **)&elem->value = value;
Craig Tillerb2b42612015-11-20 12:02:17 -0800191 for (j = 0;; j++) {
192 size_t idx = (hash + j) % GPR_ARRAY_SIZE(g_static_mdtab);
193 if (g_static_mdtab[idx] == NULL) {
194 g_static_mdtab[idx] = elem;
195 break;
196 }
197 }
198 if (j > g_static_mdtab_maxprobe) {
199 g_static_mdtab_maxprobe = j;
200 }
201 }
202 /* initialize shards */
203 for (i = 0; i < STRTAB_SHARD_COUNT; i++) {
204 strtab_shard *shard = &g_strtab_shard[i];
205 gpr_mu_init(&shard->mu);
206 shard->count = 0;
207 shard->capacity = INITIAL_STRTAB_CAPACITY;
208 shard->strs = gpr_malloc(sizeof(*shard->strs) * shard->capacity);
209 memset(shard->strs, 0, sizeof(*shard->strs) * shard->capacity);
210 }
211 for (i = 0; i < MDTAB_SHARD_COUNT; i++) {
212 mdtab_shard *shard = &g_mdtab_shard[i];
213 gpr_mu_init(&shard->mu);
214 shard->count = 0;
215 shard->free = 0;
216 shard->capacity = INITIAL_MDTAB_CAPACITY;
217 shard->elems = gpr_malloc(sizeof(*shard->elems) * shard->capacity);
218 memset(shard->elems, 0, sizeof(*shard->elems) * shard->capacity);
Craig Tiller0e72ede2015-11-19 07:48:53 -0800219 }
220}
221
Craig Tillerb2b42612015-11-20 12:02:17 -0800222void grpc_mdctx_global_shutdown(void) {
223 size_t i;
224 for (i = 0; i < MDTAB_SHARD_COUNT; i++) {
225 mdtab_shard *shard = &g_mdtab_shard[i];
226 gpr_mu_destroy(&shard->mu);
Craig Tiller34075442015-11-23 16:16:54 -0800227 gc_mdtab(shard);
228 /* TODO(ctiller): GPR_ASSERT(shard->count == 0); */
229 if (shard->count != 0) {
yang-gd88e1d82015-12-02 13:23:33 -0800230 gpr_log(GPR_DEBUG, "WARNING: %d metadata elements were leaked",
231 shard->count);
Craig Tiller0cb803d2016-03-02 22:17:24 -0800232 if (grpc_iomgr_abort_on_leaks()) {
233 abort();
234 }
Craig Tiller34075442015-11-23 16:16:54 -0800235 }
Craig Tillerb2b42612015-11-20 12:02:17 -0800236 gpr_free(shard->elems);
237 }
238 for (i = 0; i < STRTAB_SHARD_COUNT; i++) {
239 strtab_shard *shard = &g_strtab_shard[i];
240 gpr_mu_destroy(&shard->mu);
Craig Tiller34075442015-11-23 16:16:54 -0800241 /* TODO(ctiller): GPR_ASSERT(shard->count == 0); */
242 if (shard->count != 0) {
yang-gd88e1d82015-12-02 13:23:33 -0800243 gpr_log(GPR_DEBUG, "WARNING: %d metadata strings were leaked",
244 shard->count);
Craig Tiller0cb803d2016-03-02 22:17:24 -0800245 if (grpc_iomgr_abort_on_leaks()) {
246 abort();
247 }
Craig Tiller34075442015-11-23 16:16:54 -0800248 }
Craig Tillerb2b42612015-11-20 12:02:17 -0800249 gpr_free(shard->strs);
250 }
251}
Craig Tiller0e72ede2015-11-19 07:48:53 -0800252
253static int is_mdstr_static(grpc_mdstr *s) {
Craig Tillerb774be42015-11-19 07:56:13 -0800254 return s >= &grpc_static_mdstr_table[0] &&
255 s < &grpc_static_mdstr_table[GRPC_STATIC_MDSTR_COUNT];
Craig Tiller0e72ede2015-11-19 07:48:53 -0800256}
257
258static int is_mdelem_static(grpc_mdelem *e) {
Craig Tillerb774be42015-11-19 07:56:13 -0800259 return e >= &grpc_static_mdelem_table[0] &&
260 e < &grpc_static_mdelem_table[GRPC_STATIC_MDELEM_COUNT];
Craig Tiller0e72ede2015-11-19 07:48:53 -0800261}
262
Craig Tillerb2b42612015-11-20 12:02:17 -0800263static void ref_md_locked(mdtab_shard *shard,
264 internal_metadata *md DEBUG_ARGS) {
Craig Tiller1a65a232015-07-06 10:22:32 -0700265#ifdef GRPC_METADATA_REFCOUNT_DEBUG
266 gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,
267 "ELM REF:%p:%d->%d: '%s' = '%s'", md,
268 gpr_atm_no_barrier_load(&md->refcnt),
269 gpr_atm_no_barrier_load(&md->refcnt) + 1,
270 grpc_mdstr_as_c_string((grpc_mdstr *)md->key),
271 grpc_mdstr_as_c_string((grpc_mdstr *)md->value));
272#endif
Craig Tillerb8e82672015-10-10 13:52:47 -0700273 if (0 == gpr_atm_no_barrier_fetch_add(&md->refcnt, 2)) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800274 shard->free--;
Craig Tillerb8e82672015-10-10 13:52:47 -0700275 } else {
276 GPR_ASSERT(1 != gpr_atm_no_barrier_fetch_add(&md->refcnt, -1));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800277 }
278}
279
Craig Tillerb2b42612015-11-20 12:02:17 -0800280static void grow_strtab(strtab_shard *shard) {
281 size_t capacity = shard->capacity * 2;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800282 size_t i;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800283 internal_string **strtab;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800284 internal_string *s, *next;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800285
286 GPR_TIMER_BEGIN("grow_strtab", 0);
287
288 strtab = gpr_malloc(sizeof(internal_string *) * capacity);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800289 memset(strtab, 0, sizeof(internal_string *) * capacity);
290
Craig Tillerb2b42612015-11-20 12:02:17 -0800291 for (i = 0; i < shard->capacity; i++) {
292 for (s = shard->strs[i]; s; s = next) {
293 size_t idx = TABLE_IDX(s->hash, LOG2_STRTAB_SHARD_COUNT, capacity);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800294 next = s->bucket_next;
Craig Tillerb2b42612015-11-20 12:02:17 -0800295 s->bucket_next = strtab[idx];
296 strtab[idx] = s;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800297 }
298 }
299
Craig Tillerb2b42612015-11-20 12:02:17 -0800300 gpr_free(shard->strs);
301 shard->strs = strtab;
302 shard->capacity = capacity;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800303
304 GPR_TIMER_END("grow_strtab", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800305}
306
Craig Tillerb2b42612015-11-20 12:02:17 -0800307static void internal_destroy_string(strtab_shard *shard, internal_string *is) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800308 internal_string **prev_next;
309 internal_string *cur;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800310 GPR_TIMER_BEGIN("internal_destroy_string", 0);
ctiller430c4992014-12-11 09:15:41 -0800311 if (is->has_base64_and_huffman_encoded) {
312 gpr_slice_unref(is->base64_and_huffman);
313 }
Craig Tillerb2b42612015-11-20 12:02:17 -0800314 for (prev_next = &shard->strs[TABLE_IDX(is->hash, LOG2_STRTAB_SHARD_COUNT,
315 shard->capacity)],
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800316 cur = *prev_next;
317 cur != is; prev_next = &cur->bucket_next, cur = cur->bucket_next)
318 ;
319 *prev_next = cur->bucket_next;
Craig Tillerb2b42612015-11-20 12:02:17 -0800320 shard->count--;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800321 gpr_free(is);
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800322 GPR_TIMER_END("internal_destroy_string", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800323}
324
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800325static void slice_ref(void *p) {
326 internal_string *is =
327 (internal_string *)((char *)p - offsetof(internal_string, refcount));
Craig Tillerb2b42612015-11-20 12:02:17 -0800328 GRPC_MDSTR_REF((grpc_mdstr *)(is));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800329}
330
331static void slice_unref(void *p) {
332 internal_string *is =
333 (internal_string *)((char *)p - offsetof(internal_string, refcount));
Craig Tillerb2b42612015-11-20 12:02:17 -0800334 GRPC_MDSTR_UNREF((grpc_mdstr *)(is));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800335}
336
Craig Tillerb2b42612015-11-20 12:02:17 -0800337grpc_mdstr *grpc_mdstr_from_string(const char *str) {
Craig Tiller7536af02015-12-22 13:49:30 -0800338 return grpc_mdstr_from_buffer((const uint8_t *)str, strlen(str));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800339}
340
Craig Tillerb2b42612015-11-20 12:02:17 -0800341grpc_mdstr *grpc_mdstr_from_slice(gpr_slice slice) {
342 grpc_mdstr *result = grpc_mdstr_from_buffer(GPR_SLICE_START_PTR(slice),
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800343 GPR_SLICE_LENGTH(slice));
344 gpr_slice_unref(slice);
345 return result;
346}
347
Craig Tiller7536af02015-12-22 13:49:30 -0800348grpc_mdstr *grpc_mdstr_from_buffer(const uint8_t *buf, size_t length) {
349 uint32_t hash = gpr_murmur_hash3(buf, length, g_hash_seed);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800350 internal_string *s;
Craig Tillerb2b42612015-11-20 12:02:17 -0800351 strtab_shard *shard =
352 &g_strtab_shard[SHARD_IDX(hash, LOG2_STRTAB_SHARD_COUNT)];
Craig Tiller0e72ede2015-11-19 07:48:53 -0800353 size_t i;
Craig Tillerb2b42612015-11-20 12:02:17 -0800354 size_t idx;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800355
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800356 GPR_TIMER_BEGIN("grpc_mdstr_from_buffer", 0);
Craig Tiller0e72ede2015-11-19 07:48:53 -0800357
358 /* search for a static string */
Craig Tillerb2b42612015-11-20 12:02:17 -0800359 for (i = 0; i <= g_static_strtab_maxprobe; i++) {
360 grpc_mdstr *ss;
361 idx = (hash + i) % GPR_ARRAY_SIZE(g_static_strtab);
362 ss = g_static_strtab[idx];
363 if (ss == NULL) break;
364 if (ss->hash == hash && GPR_SLICE_LENGTH(ss->slice) == length &&
365 0 == memcmp(buf, GPR_SLICE_START_PTR(ss->slice), length)) {
Craig Tiller07718b82015-11-21 14:06:45 -0800366 GPR_TIMER_END("grpc_mdstr_from_buffer", 0);
Craig Tillerb2b42612015-11-20 12:02:17 -0800367 return ss;
Craig Tiller0e72ede2015-11-19 07:48:53 -0800368 }
369 }
370
Craig Tillerb2b42612015-11-20 12:02:17 -0800371 gpr_mu_lock(&shard->mu);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800372
373 /* search for an existing string */
Craig Tillerb2b42612015-11-20 12:02:17 -0800374 idx = TABLE_IDX(hash, LOG2_STRTAB_SHARD_COUNT, shard->capacity);
375 for (s = shard->strs[idx]; s; s = s->bucket_next) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800376 if (s->hash == hash && GPR_SLICE_LENGTH(s->slice) == length &&
377 0 == memcmp(buf, GPR_SLICE_START_PTR(s->slice), length)) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800378 GRPC_MDSTR_REF((grpc_mdstr *)s);
379 gpr_mu_unlock(&shard->mu);
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800380 GPR_TIMER_END("grpc_mdstr_from_buffer", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800381 return (grpc_mdstr *)s;
382 }
383 }
384
385 /* not found: create a new string */
386 if (length + 1 < GPR_SLICE_INLINED_SIZE) {
387 /* string data goes directly into the slice */
388 s = gpr_malloc(sizeof(internal_string));
Craig Tillerb2b42612015-11-20 12:02:17 -0800389 gpr_atm_rel_store(&s->refcnt, 2);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800390 s->slice.refcount = NULL;
391 memcpy(s->slice.data.inlined.bytes, buf, length);
392 s->slice.data.inlined.bytes[length] = 0;
Craig Tiller7536af02015-12-22 13:49:30 -0800393 s->slice.data.inlined.length = (uint8_t)length;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800394 } else {
395 /* string data goes after the internal_string header, and we +1 for null
396 terminator */
397 s = gpr_malloc(sizeof(internal_string) + length + 1);
Craig Tillerb2b42612015-11-20 12:02:17 -0800398 gpr_atm_rel_store(&s->refcnt, 2);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800399 s->refcount.ref = slice_ref;
400 s->refcount.unref = slice_unref;
401 s->slice.refcount = &s->refcount;
Craig Tiller7536af02015-12-22 13:49:30 -0800402 s->slice.data.refcounted.bytes = (uint8_t *)(s + 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800403 s->slice.data.refcounted.length = length;
404 memcpy(s->slice.data.refcounted.bytes, buf, length);
405 /* add a null terminator for cheap c string conversion when desired */
406 s->slice.data.refcounted.bytes[length] = 0;
407 }
ctiller430c4992014-12-11 09:15:41 -0800408 s->has_base64_and_huffman_encoded = 0;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800409 s->hash = hash;
Craig Tillerb2b42612015-11-20 12:02:17 -0800410 s->bucket_next = shard->strs[idx];
411 shard->strs[idx] = s;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800412
Craig Tillerb2b42612015-11-20 12:02:17 -0800413 shard->count++;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800414
Craig Tillerb2b42612015-11-20 12:02:17 -0800415 if (shard->count > shard->capacity * 2) {
416 grow_strtab(shard);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800417 }
418
Craig Tillerb2b42612015-11-20 12:02:17 -0800419 gpr_mu_unlock(&shard->mu);
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800420 GPR_TIMER_END("grpc_mdstr_from_buffer", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800421
422 return (grpc_mdstr *)s;
423}
424
Craig Tillerb2b42612015-11-20 12:02:17 -0800425static void gc_mdtab(mdtab_shard *shard) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800426 size_t i;
427 internal_metadata **prev_next;
428 internal_metadata *md, *next;
429
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800430 GPR_TIMER_BEGIN("gc_mdtab", 0);
Craig Tillerb2b42612015-11-20 12:02:17 -0800431 for (i = 0; i < shard->capacity; i++) {
432 prev_next = &shard->elems[i];
433 for (md = shard->elems[i]; md; md = next) {
Craig Tiller8344daa2015-10-09 18:10:57 -0700434 void *user_data = (void *)gpr_atm_no_barrier_load(&md->user_data);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800435 next = md->bucket_next;
Craig Tiller9fa41b92015-04-10 15:08:03 -0700436 if (gpr_atm_acq_load(&md->refcnt) == 0) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800437 GRPC_MDSTR_UNREF((grpc_mdstr *)md->key);
438 GRPC_MDSTR_UNREF((grpc_mdstr *)md->value);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800439 if (md->user_data) {
Craig Tiller8344daa2015-10-09 18:10:57 -0700440 ((destroy_user_data_func)gpr_atm_no_barrier_load(
441 &md->destroy_user_data))(user_data);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800442 }
443 gpr_free(md);
444 *prev_next = next;
Craig Tillerb2b42612015-11-20 12:02:17 -0800445 shard->free--;
446 shard->count--;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800447 } else {
448 prev_next = &md->bucket_next;
449 }
450 }
451 }
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800452 GPR_TIMER_END("gc_mdtab", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800453}
454
Craig Tillerb2b42612015-11-20 12:02:17 -0800455static void grow_mdtab(mdtab_shard *shard) {
456 size_t capacity = shard->capacity * 2;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800457 size_t i;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800458 internal_metadata **mdtab;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800459 internal_metadata *md, *next;
Craig Tiller7536af02015-12-22 13:49:30 -0800460 uint32_t hash;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800461
462 GPR_TIMER_BEGIN("grow_mdtab", 0);
463
464 mdtab = gpr_malloc(sizeof(internal_metadata *) * capacity);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800465 memset(mdtab, 0, sizeof(internal_metadata *) * capacity);
466
Craig Tillerb2b42612015-11-20 12:02:17 -0800467 for (i = 0; i < shard->capacity; i++) {
468 for (md = shard->elems[i]; md; md = next) {
469 size_t idx;
ctillerfb93d192014-12-15 10:40:05 -0800470 hash = GRPC_MDSTR_KV_HASH(md->key->hash, md->value->hash);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800471 next = md->bucket_next;
Craig Tillerb2b42612015-11-20 12:02:17 -0800472 idx = TABLE_IDX(hash, LOG2_MDTAB_SHARD_COUNT, capacity);
473 md->bucket_next = mdtab[idx];
474 mdtab[idx] = md;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800475 }
476 }
477
Craig Tillerb2b42612015-11-20 12:02:17 -0800478 gpr_free(shard->elems);
479 shard->elems = mdtab;
480 shard->capacity = capacity;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800481
482 GPR_TIMER_END("grow_mdtab", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800483}
484
Craig Tillerb2b42612015-11-20 12:02:17 -0800485static void rehash_mdtab(mdtab_shard *shard) {
486 if (shard->free > shard->capacity / 4) {
487 gc_mdtab(shard);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800488 } else {
Craig Tillerb2b42612015-11-20 12:02:17 -0800489 grow_mdtab(shard);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800490 }
491}
492
Craig Tillerb2b42612015-11-20 12:02:17 -0800493grpc_mdelem *grpc_mdelem_from_metadata_strings(grpc_mdstr *mkey,
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800494 grpc_mdstr *mvalue) {
495 internal_string *key = (internal_string *)mkey;
496 internal_string *value = (internal_string *)mvalue;
Craig Tiller7536af02015-12-22 13:49:30 -0800497 uint32_t hash = GRPC_MDSTR_KV_HASH(mkey->hash, mvalue->hash);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800498 internal_metadata *md;
Craig Tillerb2b42612015-11-20 12:02:17 -0800499 mdtab_shard *shard = &g_mdtab_shard[SHARD_IDX(hash, LOG2_MDTAB_SHARD_COUNT)];
Craig Tiller0e72ede2015-11-19 07:48:53 -0800500 size_t i;
Craig Tillerb2b42612015-11-20 12:02:17 -0800501 size_t idx;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800502
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800503 GPR_TIMER_BEGIN("grpc_mdelem_from_metadata_strings", 0);
504
Craig Tiller0e72ede2015-11-19 07:48:53 -0800505 if (is_mdstr_static(mkey) && is_mdstr_static(mvalue)) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800506 for (i = 0; i <= g_static_mdtab_maxprobe; i++) {
507 grpc_mdelem *smd;
508 idx = (hash + i) % GPR_ARRAY_SIZE(g_static_mdtab);
509 smd = g_static_mdtab[idx];
510 if (smd == NULL) break;
511 if (smd->key == mkey && smd->value == mvalue) {
Craig Tiller07718b82015-11-21 14:06:45 -0800512 GPR_TIMER_END("grpc_mdelem_from_metadata_strings", 0);
Craig Tillerb2b42612015-11-20 12:02:17 -0800513 return smd;
Craig Tiller0e72ede2015-11-19 07:48:53 -0800514 }
515 }
516 }
517
Craig Tillerb2b42612015-11-20 12:02:17 -0800518 gpr_mu_lock(&shard->mu);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800519
Craig Tillerb2b42612015-11-20 12:02:17 -0800520 idx = TABLE_IDX(hash, LOG2_MDTAB_SHARD_COUNT, shard->capacity);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800521 /* search for an existing pair */
Craig Tillerb2b42612015-11-20 12:02:17 -0800522 for (md = shard->elems[idx]; md; md = md->bucket_next) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800523 if (md->key == key && md->value == value) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800524 REF_MD_LOCKED(shard, md);
525 GRPC_MDSTR_UNREF((grpc_mdstr *)key);
526 GRPC_MDSTR_UNREF((grpc_mdstr *)value);
527 gpr_mu_unlock(&shard->mu);
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800528 GPR_TIMER_END("grpc_mdelem_from_metadata_strings", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800529 return (grpc_mdelem *)md;
530 }
531 }
532
533 /* not found: create a new pair */
534 md = gpr_malloc(sizeof(internal_metadata));
Craig Tiller63bda562015-10-09 17:40:19 -0700535 gpr_atm_rel_store(&md->refcnt, 2);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800536 md->key = key;
537 md->value = value;
Craig Tiller8344daa2015-10-09 18:10:57 -0700538 md->user_data = 0;
539 md->destroy_user_data = 0;
Craig Tillerb2b42612015-11-20 12:02:17 -0800540 md->bucket_next = shard->elems[idx];
541 shard->elems[idx] = md;
Craig Tiller83901532015-07-10 14:02:45 -0700542 gpr_mu_init(&md->mu_user_data);
Craig Tiller1a65a232015-07-06 10:22:32 -0700543#ifdef GRPC_METADATA_REFCOUNT_DEBUG
544 gpr_log(GPR_DEBUG, "ELM NEW:%p:%d: '%s' = '%s'", md,
545 gpr_atm_no_barrier_load(&md->refcnt),
546 grpc_mdstr_as_c_string((grpc_mdstr *)md->key),
547 grpc_mdstr_as_c_string((grpc_mdstr *)md->value));
548#endif
Craig Tillerb2b42612015-11-20 12:02:17 -0800549 shard->count++;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800550
Craig Tillerb2b42612015-11-20 12:02:17 -0800551 if (shard->count > shard->capacity * 2) {
552 rehash_mdtab(shard);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800553 }
554
Craig Tillerb2b42612015-11-20 12:02:17 -0800555 gpr_mu_unlock(&shard->mu);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800556
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800557 GPR_TIMER_END("grpc_mdelem_from_metadata_strings", 0);
558
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800559 return (grpc_mdelem *)md;
560}
561
Craig Tillerb2b42612015-11-20 12:02:17 -0800562grpc_mdelem *grpc_mdelem_from_strings(const char *key, const char *value) {
563 return grpc_mdelem_from_metadata_strings(grpc_mdstr_from_string(key),
564 grpc_mdstr_from_string(value));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800565}
566
Craig Tillerb2b42612015-11-20 12:02:17 -0800567grpc_mdelem *grpc_mdelem_from_slices(gpr_slice key, gpr_slice value) {
568 return grpc_mdelem_from_metadata_strings(grpc_mdstr_from_slice(key),
569 grpc_mdstr_from_slice(value));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800570}
571
Craig Tillerb2b42612015-11-20 12:02:17 -0800572grpc_mdelem *grpc_mdelem_from_string_and_buffer(const char *key,
Craig Tiller7536af02015-12-22 13:49:30 -0800573 const uint8_t *value,
Craig Tiller4dbdd6a2015-09-25 15:12:16 -0700574 size_t value_length) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800575 return grpc_mdelem_from_metadata_strings(
Craig Tillerb2b42612015-11-20 12:02:17 -0800576 grpc_mdstr_from_string(key), grpc_mdstr_from_buffer(value, value_length));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800577}
578
Craig Tiller1a65a232015-07-06 10:22:32 -0700579grpc_mdelem *grpc_mdelem_ref(grpc_mdelem *gmd DEBUG_ARGS) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800580 internal_metadata *md = (internal_metadata *)gmd;
Craig Tiller0e72ede2015-11-19 07:48:53 -0800581 if (is_mdelem_static(gmd)) return gmd;
Craig Tiller1a65a232015-07-06 10:22:32 -0700582#ifdef GRPC_METADATA_REFCOUNT_DEBUG
583 gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,
584 "ELM REF:%p:%d->%d: '%s' = '%s'", md,
585 gpr_atm_no_barrier_load(&md->refcnt),
586 gpr_atm_no_barrier_load(&md->refcnt) + 1,
587 grpc_mdstr_as_c_string((grpc_mdstr *)md->key),
588 grpc_mdstr_as_c_string((grpc_mdstr *)md->value));
589#endif
Craig Tiller9fa41b92015-04-10 15:08:03 -0700590 /* we can assume the ref count is >= 1 as the application is calling
591 this function - meaning that no adjustment to mdtab_free is necessary,
592 simplifying the logic here to be just an atomic increment */
593 /* use C assert to have this removed in opt builds */
Craig Tillerb8e82672015-10-10 13:52:47 -0700594 assert(gpr_atm_no_barrier_load(&md->refcnt) >= 2);
Craig Tiller9fa41b92015-04-10 15:08:03 -0700595 gpr_atm_no_barrier_fetch_add(&md->refcnt, 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800596 return gmd;
597}
598
Craig Tiller1a65a232015-07-06 10:22:32 -0700599void grpc_mdelem_unref(grpc_mdelem *gmd DEBUG_ARGS) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800600 internal_metadata *md = (internal_metadata *)gmd;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800601 if (!md) return;
Craig Tiller0e72ede2015-11-19 07:48:53 -0800602 if (is_mdelem_static(gmd)) return;
Craig Tiller1a65a232015-07-06 10:22:32 -0700603#ifdef GRPC_METADATA_REFCOUNT_DEBUG
604 gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,
605 "ELM UNREF:%p:%d->%d: '%s' = '%s'", md,
606 gpr_atm_no_barrier_load(&md->refcnt),
607 gpr_atm_no_barrier_load(&md->refcnt) - 1,
608 grpc_mdstr_as_c_string((grpc_mdstr *)md->key),
609 grpc_mdstr_as_c_string((grpc_mdstr *)md->value));
610#endif
Craig Tiller63bda562015-10-09 17:40:19 -0700611 if (2 == gpr_atm_full_fetch_add(&md->refcnt, -1)) {
Craig Tiller7536af02015-12-22 13:49:30 -0800612 uint32_t hash = GRPC_MDSTR_KV_HASH(md->key->hash, md->value->hash);
Craig Tillerb2b42612015-11-20 12:02:17 -0800613 mdtab_shard *shard =
614 &g_mdtab_shard[SHARD_IDX(hash, LOG2_MDTAB_SHARD_COUNT)];
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800615 GPR_TIMER_BEGIN("grpc_mdelem_unref.to_zero", 0);
Craig Tillerb2b42612015-11-20 12:02:17 -0800616 gpr_mu_lock(&shard->mu);
Craig Tillerb8e82672015-10-10 13:52:47 -0700617 if (1 == gpr_atm_no_barrier_load(&md->refcnt)) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800618 shard->free++;
Craig Tillerb8e82672015-10-10 13:52:47 -0700619 gpr_atm_no_barrier_store(&md->refcnt, 0);
620 }
Craig Tillerb2b42612015-11-20 12:02:17 -0800621 gpr_mu_unlock(&shard->mu);
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800622 GPR_TIMER_END("grpc_mdelem_unref.to_zero", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800623 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800624}
625
626const char *grpc_mdstr_as_c_string(grpc_mdstr *s) {
627 return (const char *)GPR_SLICE_START_PTR(s->slice);
628}
629
Craig Tiller1a65a232015-07-06 10:22:32 -0700630grpc_mdstr *grpc_mdstr_ref(grpc_mdstr *gs DEBUG_ARGS) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800631 internal_string *s = (internal_string *)gs;
Craig Tiller0e72ede2015-11-19 07:48:53 -0800632 if (is_mdstr_static(gs)) return gs;
Craig Tillerb2b42612015-11-20 12:02:17 -0800633 GPR_ASSERT(gpr_atm_full_fetch_add(&s->refcnt, 1) != 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800634 return gs;
635}
636
Craig Tiller1a65a232015-07-06 10:22:32 -0700637void grpc_mdstr_unref(grpc_mdstr *gs DEBUG_ARGS) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800638 internal_string *s = (internal_string *)gs;
Craig Tiller0e72ede2015-11-19 07:48:53 -0800639 if (is_mdstr_static(gs)) return;
Craig Tillerb2b42612015-11-20 12:02:17 -0800640 if (2 == gpr_atm_full_fetch_add(&s->refcnt, -1)) {
641 strtab_shard *shard =
642 &g_strtab_shard[SHARD_IDX(s->hash, LOG2_STRTAB_SHARD_COUNT)];
643 gpr_mu_lock(&shard->mu);
644 if (1 == gpr_atm_no_barrier_load(&s->refcnt)) {
645 internal_destroy_string(shard, s);
646 }
647 gpr_mu_unlock(&shard->mu);
648 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800649}
650
Craig Tiller8344daa2015-10-09 18:10:57 -0700651void *grpc_mdelem_get_user_data(grpc_mdelem *md, void (*destroy_func)(void *)) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800652 internal_metadata *im = (internal_metadata *)md;
Craig Tiller83901532015-07-10 14:02:45 -0700653 void *result;
Craig Tillerb2b42612015-11-20 12:02:17 -0800654 if (is_mdelem_static(md)) {
655 return (void *)grpc_static_mdelem_user_data[md - grpc_static_mdelem_table];
656 }
Craig Tiller8344daa2015-10-09 18:10:57 -0700657 if (gpr_atm_acq_load(&im->destroy_user_data) == (gpr_atm)destroy_func) {
658 return (void *)gpr_atm_no_barrier_load(&im->user_data);
659 } else {
660 return NULL;
661 }
Craig Tiller83901532015-07-10 14:02:45 -0700662 return result;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800663}
664
665void grpc_mdelem_set_user_data(grpc_mdelem *md, void (*destroy_func)(void *),
666 void *user_data) {
667 internal_metadata *im = (internal_metadata *)md;
Craig Tillerebdef9d2015-11-19 17:09:49 -0800668 GPR_ASSERT(!is_mdelem_static(md));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800669 GPR_ASSERT((user_data == NULL) == (destroy_func == NULL));
Craig Tiller83901532015-07-10 14:02:45 -0700670 gpr_mu_lock(&im->mu_user_data);
Craig Tiller8344daa2015-10-09 18:10:57 -0700671 if (gpr_atm_no_barrier_load(&im->destroy_user_data)) {
Craig Tiller83901532015-07-10 14:02:45 -0700672 /* user data can only be set once */
673 gpr_mu_unlock(&im->mu_user_data);
674 if (destroy_func != NULL) {
675 destroy_func(user_data);
676 }
677 return;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800678 }
Craig Tiller8344daa2015-10-09 18:10:57 -0700679 gpr_atm_no_barrier_store(&im->user_data, (gpr_atm)user_data);
680 gpr_atm_rel_store(&im->destroy_user_data, (gpr_atm)destroy_func);
Craig Tiller83901532015-07-10 14:02:45 -0700681 gpr_mu_unlock(&im->mu_user_data);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800682}
ctiller430c4992014-12-11 09:15:41 -0800683
684gpr_slice grpc_mdstr_as_base64_encoded_and_huffman_compressed(grpc_mdstr *gs) {
685 internal_string *s = (internal_string *)gs;
686 gpr_slice slice;
Craig Tillerb2b42612015-11-20 12:02:17 -0800687 strtab_shard *shard =
688 &g_strtab_shard[SHARD_IDX(s->hash, LOG2_STRTAB_SHARD_COUNT)];
689 gpr_mu_lock(&shard->mu);
ctiller430c4992014-12-11 09:15:41 -0800690 if (!s->has_base64_and_huffman_encoded) {
691 s->base64_and_huffman =
692 grpc_chttp2_base64_encode_and_huffman_compress(s->slice);
ctiller33023c42014-12-12 16:28:33 -0800693 s->has_base64_and_huffman_encoded = 1;
ctiller430c4992014-12-11 09:15:41 -0800694 }
695 slice = s->base64_and_huffman;
Craig Tillerb2b42612015-11-20 12:02:17 -0800696 gpr_mu_unlock(&shard->mu);
ctiller430c4992014-12-11 09:15:41 -0800697 return slice;
David Garcia Quintas2bf574f2016-01-14 15:27:08 -0800698}