blob: 79de54beb595cee45f76c93739d03e7af1d2000a [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>
yang-gcb7a8022016-03-30 14:58:53 -070041#include <grpc/grpc.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080042#include <grpc/support/alloc.h>
Craig Tiller9fa41b92015-04-10 15:08:03 -070043#include <grpc/support/atm.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080044#include <grpc/support/log.h>
Craig Tillerebdef9d2015-11-19 17:09:49 -080045#include <grpc/support/string_util.h>
Craig Tiller9d35a1f2015-11-02 14:16:12 -080046#include <grpc/support/time.h>
Craig Tiller0cb803d2016-03-02 22:17:24 -080047
Craig Tiller9533d042016-03-25 17:11:06 -070048#include "src/core/lib/iomgr/iomgr_internal.h"
49#include "src/core/lib/profiling/timers.h"
50#include "src/core/lib/support/murmur_hash.h"
51#include "src/core/lib/support/string.h"
Craig Tiller9533d042016-03-25 17:11:06 -070052#include "src/core/lib/transport/static_metadata.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080053
Craig Tillerf82ddc42016-04-05 17:15:07 -070054gpr_slice (*grpc_chttp2_base64_encode_and_huffman_compress)(gpr_slice input);
55
Craig Tiller70b080d2015-11-19 08:04:48 -080056/* There are two kinds of mdelem and mdstr instances.
57 * Static instances are declared in static_metadata.{h,c} and
58 * are initialized by grpc_mdctx_global_init().
59 * Dynamic instances are stored in hash tables on grpc_mdctx, and are backed
60 * by internal_string and internal_element structures.
61 * Internal helper functions here-in (is_mdstr_static, is_mdelem_static) are
62 * used to determine which kind of element a pointer refers to.
63 */
64
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080065#define INITIAL_STRTAB_CAPACITY 4
66#define INITIAL_MDTAB_CAPACITY 4
67
Craig Tiller1a65a232015-07-06 10:22:32 -070068#ifdef GRPC_METADATA_REFCOUNT_DEBUG
69#define DEBUG_ARGS , const char *file, int line
70#define FWD_DEBUG_ARGS , file, line
Craig Tillerb2b42612015-11-20 12:02:17 -080071#define REF_MD_LOCKED(shard, s) ref_md_locked((shard), (s), __FILE__, __LINE__)
Craig Tiller1a65a232015-07-06 10:22:32 -070072#else
73#define DEBUG_ARGS
74#define FWD_DEBUG_ARGS
Craig Tillerb2b42612015-11-20 12:02:17 -080075#define REF_MD_LOCKED(shard, s) ref_md_locked((shard), (s))
Craig Tiller1a65a232015-07-06 10:22:32 -070076#endif
77
Craig Tillerb2b42612015-11-20 12:02:17 -080078#define TABLE_IDX(hash, log2_shards, capacity) \
79 (((hash) >> (log2_shards)) % (capacity))
80#define SHARD_IDX(hash, log2_shards) ((hash) & ((1 << (log2_shards)) - 1))
81
Craig Tiller8344daa2015-10-09 18:10:57 -070082typedef void (*destroy_user_data_func)(void *user_data);
83
yang-g4a530b02016-03-31 11:44:44 -070084#define SIZE_IN_DECODER_TABLE_NOT_SET -1
Craig Tiller70b080d2015-11-19 08:04:48 -080085/* Shadow structure for grpc_mdstr for non-static values */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080086typedef struct internal_string {
87 /* must be byte compatible with grpc_mdstr */
88 gpr_slice slice;
Craig Tiller7536af02015-12-22 13:49:30 -080089 uint32_t hash;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080090
91 /* private only data */
Craig Tillerb2b42612015-11-20 12:02:17 -080092 gpr_atm refcnt;
93
Craig Tiller7536af02015-12-22 13:49:30 -080094 uint8_t has_base64_and_huffman_encoded;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080095 gpr_slice_refcount refcount;
96
ctiller430c4992014-12-11 09:15:41 -080097 gpr_slice base64_and_huffman;
98
yang-g4a530b02016-03-31 11:44:44 -070099 gpr_atm size_in_decoder_table;
yang-gcb7a8022016-03-30 14:58:53 -0700100
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800101 struct internal_string *bucket_next;
102} internal_string;
103
Craig Tiller70b080d2015-11-19 08:04:48 -0800104/* Shadow structure for grpc_mdelem for non-static elements */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800105typedef struct internal_metadata {
106 /* must be byte compatible with grpc_mdelem */
107 internal_string *key;
108 internal_string *value;
109
Craig Tillerb2b42612015-11-20 12:02:17 -0800110 /* private only data */
Craig Tiller9fa41b92015-04-10 15:08:03 -0700111 gpr_atm refcnt;
112
Craig Tiller83901532015-07-10 14:02:45 -0700113 gpr_mu mu_user_data;
Craig Tiller8344daa2015-10-09 18:10:57 -0700114 gpr_atm destroy_user_data;
115 gpr_atm user_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800116
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800117 struct internal_metadata *bucket_next;
118} internal_metadata;
119
Craig Tillerb2b42612015-11-20 12:02:17 -0800120typedef struct strtab_shard {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800121 gpr_mu mu;
Craig Tillerb2b42612015-11-20 12:02:17 -0800122 internal_string **strs;
123 size_t count;
124 size_t capacity;
125} strtab_shard;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800126
Craig Tillerb2b42612015-11-20 12:02:17 -0800127typedef struct mdtab_shard {
128 gpr_mu mu;
129 internal_metadata **elems;
130 size_t count;
131 size_t capacity;
Craig Tiller8d8f9a82016-06-03 08:49:11 -0700132 /** Estimate of the number of unreferenced mdelems in the hash table.
133 This will eventually converge to the exact number, but it's instantaneous
134 accuracy is not guaranteed */
135 gpr_atm free_estimate;
Craig Tillerb2b42612015-11-20 12:02:17 -0800136} mdtab_shard;
Craig Tiller0e72ede2015-11-19 07:48:53 -0800137
Craig Tillerb2b42612015-11-20 12:02:17 -0800138#define LOG2_STRTAB_SHARD_COUNT 5
139#define LOG2_MDTAB_SHARD_COUNT 4
140#define STRTAB_SHARD_COUNT ((size_t)(1 << LOG2_STRTAB_SHARD_COUNT))
141#define MDTAB_SHARD_COUNT ((size_t)(1 << LOG2_MDTAB_SHARD_COUNT))
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800142
Craig Tillerb2b42612015-11-20 12:02:17 -0800143/* hash seed: decided at initialization time */
Craig Tiller7536af02015-12-22 13:49:30 -0800144static uint32_t g_hash_seed;
Craig Tillere62bf982015-12-02 17:11:49 -0800145static int g_forced_hash_seed = 0;
Craig Tillerebdef9d2015-11-19 17:09:49 -0800146
Craig Tillerb2b42612015-11-20 12:02:17 -0800147/* linearly probed hash tables for static element lookup */
148static grpc_mdstr *g_static_strtab[GRPC_STATIC_MDSTR_COUNT * 2];
149static grpc_mdelem *g_static_mdtab[GRPC_STATIC_MDELEM_COUNT * 2];
150static size_t g_static_strtab_maxprobe;
151static size_t g_static_mdtab_maxprobe;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800152
Craig Tillerb2b42612015-11-20 12:02:17 -0800153static strtab_shard g_strtab_shard[STRTAB_SHARD_COUNT];
154static mdtab_shard g_mdtab_shard[MDTAB_SHARD_COUNT];
155
Craig Tillerb2b42612015-11-20 12:02:17 -0800156static void gc_mdtab(mdtab_shard *shard);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800157
Craig Tiller7536af02015-12-22 13:49:30 -0800158void grpc_test_only_set_metadata_hash_seed(uint32_t seed) {
Craig Tillere62bf982015-12-02 17:11:49 -0800159 g_hash_seed = seed;
160 g_forced_hash_seed = 1;
161}
162
Craig Tiller0e72ede2015-11-19 07:48:53 -0800163void grpc_mdctx_global_init(void) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800164 size_t i, j;
Craig Tillere62bf982015-12-02 17:11:49 -0800165 if (!g_forced_hash_seed) {
Craig Tiller7536af02015-12-22 13:49:30 -0800166 g_hash_seed = (uint32_t)gpr_now(GPR_CLOCK_REALTIME).tv_nsec;
Craig Tillere62bf982015-12-02 17:11:49 -0800167 }
Craig Tillerb2b42612015-11-20 12:02:17 -0800168 g_static_strtab_maxprobe = 0;
169 g_static_mdtab_maxprobe = 0;
170 /* build static tables */
171 memset(g_static_mdtab, 0, sizeof(g_static_mdtab));
172 memset(g_static_strtab, 0, sizeof(g_static_strtab));
Craig Tiller0e72ede2015-11-19 07:48:53 -0800173 for (i = 0; i < GRPC_STATIC_MDSTR_COUNT; i++) {
174 grpc_mdstr *elem = &grpc_static_mdstr_table[i];
175 const char *str = grpc_static_metadata_strings[i];
Craig Tiller7536af02015-12-22 13:49:30 -0800176 uint32_t hash = gpr_murmur_hash3(str, strlen(str), g_hash_seed);
Craig Tillerb774be42015-11-19 07:56:13 -0800177 *(gpr_slice *)&elem->slice = gpr_slice_from_static_string(str);
Craig Tiller7536af02015-12-22 13:49:30 -0800178 *(uint32_t *)&elem->hash = hash;
Craig Tillerb2b42612015-11-20 12:02:17 -0800179 for (j = 0;; j++) {
180 size_t idx = (hash + j) % GPR_ARRAY_SIZE(g_static_strtab);
181 if (g_static_strtab[idx] == NULL) {
182 g_static_strtab[idx] = &grpc_static_mdstr_table[i];
183 break;
184 }
185 }
186 if (j > g_static_strtab_maxprobe) {
187 g_static_strtab_maxprobe = j;
188 }
Craig Tiller0e72ede2015-11-19 07:48:53 -0800189 }
190 for (i = 0; i < GRPC_STATIC_MDELEM_COUNT; i++) {
191 grpc_mdelem *elem = &grpc_static_mdelem_table[i];
Craig Tillerebdef9d2015-11-19 17:09:49 -0800192 grpc_mdstr *key =
193 &grpc_static_mdstr_table[grpc_static_metadata_elem_indices[2 * i + 0]];
194 grpc_mdstr *value =
195 &grpc_static_mdstr_table[grpc_static_metadata_elem_indices[2 * i + 1]];
Craig Tiller7536af02015-12-22 13:49:30 -0800196 uint32_t hash = GRPC_MDSTR_KV_HASH(key->hash, value->hash);
Craig Tillerb774be42015-11-19 07:56:13 -0800197 *(grpc_mdstr **)&elem->key = key;
198 *(grpc_mdstr **)&elem->value = value;
Craig Tillerb2b42612015-11-20 12:02:17 -0800199 for (j = 0;; j++) {
200 size_t idx = (hash + j) % GPR_ARRAY_SIZE(g_static_mdtab);
201 if (g_static_mdtab[idx] == NULL) {
202 g_static_mdtab[idx] = elem;
203 break;
204 }
205 }
206 if (j > g_static_mdtab_maxprobe) {
207 g_static_mdtab_maxprobe = j;
208 }
209 }
210 /* initialize shards */
211 for (i = 0; i < STRTAB_SHARD_COUNT; i++) {
212 strtab_shard *shard = &g_strtab_shard[i];
213 gpr_mu_init(&shard->mu);
214 shard->count = 0;
215 shard->capacity = INITIAL_STRTAB_CAPACITY;
216 shard->strs = gpr_malloc(sizeof(*shard->strs) * shard->capacity);
217 memset(shard->strs, 0, sizeof(*shard->strs) * shard->capacity);
218 }
219 for (i = 0; i < MDTAB_SHARD_COUNT; i++) {
220 mdtab_shard *shard = &g_mdtab_shard[i];
221 gpr_mu_init(&shard->mu);
222 shard->count = 0;
Craig Tiller8d8f9a82016-06-03 08:49:11 -0700223 gpr_atm_no_barrier_store(&shard->free_estimate, 0);
Craig Tillerb2b42612015-11-20 12:02:17 -0800224 shard->capacity = INITIAL_MDTAB_CAPACITY;
225 shard->elems = gpr_malloc(sizeof(*shard->elems) * shard->capacity);
226 memset(shard->elems, 0, sizeof(*shard->elems) * shard->capacity);
Craig Tiller0e72ede2015-11-19 07:48:53 -0800227 }
228}
229
Craig Tillerb2b42612015-11-20 12:02:17 -0800230void grpc_mdctx_global_shutdown(void) {
231 size_t i;
232 for (i = 0; i < MDTAB_SHARD_COUNT; i++) {
233 mdtab_shard *shard = &g_mdtab_shard[i];
234 gpr_mu_destroy(&shard->mu);
Craig Tiller34075442015-11-23 16:16:54 -0800235 gc_mdtab(shard);
236 /* TODO(ctiller): GPR_ASSERT(shard->count == 0); */
237 if (shard->count != 0) {
yang-gd88e1d82015-12-02 13:23:33 -0800238 gpr_log(GPR_DEBUG, "WARNING: %d metadata elements were leaked",
239 shard->count);
Craig Tiller0cb803d2016-03-02 22:17:24 -0800240 if (grpc_iomgr_abort_on_leaks()) {
241 abort();
242 }
Craig Tiller34075442015-11-23 16:16:54 -0800243 }
Craig Tillerb2b42612015-11-20 12:02:17 -0800244 gpr_free(shard->elems);
245 }
246 for (i = 0; i < STRTAB_SHARD_COUNT; i++) {
247 strtab_shard *shard = &g_strtab_shard[i];
248 gpr_mu_destroy(&shard->mu);
Craig Tiller34075442015-11-23 16:16:54 -0800249 /* TODO(ctiller): GPR_ASSERT(shard->count == 0); */
250 if (shard->count != 0) {
yang-gd88e1d82015-12-02 13:23:33 -0800251 gpr_log(GPR_DEBUG, "WARNING: %d metadata strings were leaked",
252 shard->count);
Craig Tiller36930782016-04-04 20:57:53 -0700253 for (size_t j = 0; j < shard->capacity; j++) {
254 for (internal_string *s = shard->strs[j]; s; s = s->bucket_next) {
Craig Tiller7bfd8be2016-04-04 22:10:25 -0700255 gpr_log(GPR_DEBUG, "LEAKED: %s",
256 grpc_mdstr_as_c_string((grpc_mdstr *)s));
Craig Tiller36930782016-04-04 20:57:53 -0700257 }
258 }
Craig Tiller0cb803d2016-03-02 22:17:24 -0800259 if (grpc_iomgr_abort_on_leaks()) {
260 abort();
261 }
Craig Tiller34075442015-11-23 16:16:54 -0800262 }
Craig Tillerb2b42612015-11-20 12:02:17 -0800263 gpr_free(shard->strs);
264 }
265}
Craig Tiller0e72ede2015-11-19 07:48:53 -0800266
267static int is_mdstr_static(grpc_mdstr *s) {
Craig Tillerb774be42015-11-19 07:56:13 -0800268 return s >= &grpc_static_mdstr_table[0] &&
269 s < &grpc_static_mdstr_table[GRPC_STATIC_MDSTR_COUNT];
Craig Tiller0e72ede2015-11-19 07:48:53 -0800270}
271
272static int is_mdelem_static(grpc_mdelem *e) {
Craig Tillerb774be42015-11-19 07:56:13 -0800273 return e >= &grpc_static_mdelem_table[0] &&
274 e < &grpc_static_mdelem_table[GRPC_STATIC_MDELEM_COUNT];
Craig Tiller0e72ede2015-11-19 07:48:53 -0800275}
276
Craig Tillerb2b42612015-11-20 12:02:17 -0800277static void ref_md_locked(mdtab_shard *shard,
278 internal_metadata *md DEBUG_ARGS) {
Craig Tiller1a65a232015-07-06 10:22:32 -0700279#ifdef GRPC_METADATA_REFCOUNT_DEBUG
280 gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,
281 "ELM REF:%p:%d->%d: '%s' = '%s'", md,
282 gpr_atm_no_barrier_load(&md->refcnt),
283 gpr_atm_no_barrier_load(&md->refcnt) + 1,
284 grpc_mdstr_as_c_string((grpc_mdstr *)md->key),
285 grpc_mdstr_as_c_string((grpc_mdstr *)md->value));
286#endif
Craig Tiller8d8f9a82016-06-03 08:49:11 -0700287 if (0 == gpr_atm_no_barrier_fetch_add(&md->refcnt, 1)) {
288 gpr_atm_no_barrier_fetch_add(&shard->free_estimate, -1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800289 }
290}
291
Craig Tillerb2b42612015-11-20 12:02:17 -0800292static void grow_strtab(strtab_shard *shard) {
293 size_t capacity = shard->capacity * 2;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800294 size_t i;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800295 internal_string **strtab;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800296 internal_string *s, *next;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800297
298 GPR_TIMER_BEGIN("grow_strtab", 0);
299
300 strtab = gpr_malloc(sizeof(internal_string *) * capacity);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800301 memset(strtab, 0, sizeof(internal_string *) * capacity);
302
Craig Tillerb2b42612015-11-20 12:02:17 -0800303 for (i = 0; i < shard->capacity; i++) {
304 for (s = shard->strs[i]; s; s = next) {
305 size_t idx = TABLE_IDX(s->hash, LOG2_STRTAB_SHARD_COUNT, capacity);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800306 next = s->bucket_next;
Craig Tillerb2b42612015-11-20 12:02:17 -0800307 s->bucket_next = strtab[idx];
308 strtab[idx] = s;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800309 }
310 }
311
Craig Tillerb2b42612015-11-20 12:02:17 -0800312 gpr_free(shard->strs);
313 shard->strs = strtab;
314 shard->capacity = capacity;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800315
316 GPR_TIMER_END("grow_strtab", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800317}
318
Craig Tillerb2b42612015-11-20 12:02:17 -0800319static void internal_destroy_string(strtab_shard *shard, internal_string *is) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800320 internal_string **prev_next;
321 internal_string *cur;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800322 GPR_TIMER_BEGIN("internal_destroy_string", 0);
ctiller430c4992014-12-11 09:15:41 -0800323 if (is->has_base64_and_huffman_encoded) {
324 gpr_slice_unref(is->base64_and_huffman);
325 }
Craig Tillerb2b42612015-11-20 12:02:17 -0800326 for (prev_next = &shard->strs[TABLE_IDX(is->hash, LOG2_STRTAB_SHARD_COUNT,
327 shard->capacity)],
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800328 cur = *prev_next;
329 cur != is; prev_next = &cur->bucket_next, cur = cur->bucket_next)
330 ;
331 *prev_next = cur->bucket_next;
Craig Tillerb2b42612015-11-20 12:02:17 -0800332 shard->count--;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800333 gpr_free(is);
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800334 GPR_TIMER_END("internal_destroy_string", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800335}
336
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800337static void slice_ref(void *p) {
338 internal_string *is =
339 (internal_string *)((char *)p - offsetof(internal_string, refcount));
Craig Tillerb2b42612015-11-20 12:02:17 -0800340 GRPC_MDSTR_REF((grpc_mdstr *)(is));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800341}
342
343static void slice_unref(void *p) {
344 internal_string *is =
345 (internal_string *)((char *)p - offsetof(internal_string, refcount));
Craig Tillerb2b42612015-11-20 12:02:17 -0800346 GRPC_MDSTR_UNREF((grpc_mdstr *)(is));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800347}
348
Craig Tillerb2b42612015-11-20 12:02:17 -0800349grpc_mdstr *grpc_mdstr_from_string(const char *str) {
Craig Tiller7536af02015-12-22 13:49:30 -0800350 return grpc_mdstr_from_buffer((const uint8_t *)str, strlen(str));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800351}
352
Craig Tillerb2b42612015-11-20 12:02:17 -0800353grpc_mdstr *grpc_mdstr_from_slice(gpr_slice slice) {
354 grpc_mdstr *result = grpc_mdstr_from_buffer(GPR_SLICE_START_PTR(slice),
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800355 GPR_SLICE_LENGTH(slice));
356 gpr_slice_unref(slice);
357 return result;
358}
359
Craig Tiller7536af02015-12-22 13:49:30 -0800360grpc_mdstr *grpc_mdstr_from_buffer(const uint8_t *buf, size_t length) {
361 uint32_t hash = gpr_murmur_hash3(buf, length, g_hash_seed);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800362 internal_string *s;
Craig Tillerb2b42612015-11-20 12:02:17 -0800363 strtab_shard *shard =
364 &g_strtab_shard[SHARD_IDX(hash, LOG2_STRTAB_SHARD_COUNT)];
Craig Tiller0e72ede2015-11-19 07:48:53 -0800365 size_t i;
Craig Tillerb2b42612015-11-20 12:02:17 -0800366 size_t idx;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800367
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800368 GPR_TIMER_BEGIN("grpc_mdstr_from_buffer", 0);
Craig Tiller0e72ede2015-11-19 07:48:53 -0800369
370 /* search for a static string */
Craig Tillerb2b42612015-11-20 12:02:17 -0800371 for (i = 0; i <= g_static_strtab_maxprobe; i++) {
372 grpc_mdstr *ss;
373 idx = (hash + i) % GPR_ARRAY_SIZE(g_static_strtab);
374 ss = g_static_strtab[idx];
375 if (ss == NULL) break;
376 if (ss->hash == hash && GPR_SLICE_LENGTH(ss->slice) == length &&
Craig Tiller93dd0472016-05-18 15:06:37 -0700377 (length == 0 ||
378 0 == memcmp(buf, GPR_SLICE_START_PTR(ss->slice), length))) {
Craig Tiller07718b82015-11-21 14:06:45 -0800379 GPR_TIMER_END("grpc_mdstr_from_buffer", 0);
Craig Tillerb2b42612015-11-20 12:02:17 -0800380 return ss;
Craig Tiller0e72ede2015-11-19 07:48:53 -0800381 }
382 }
383
Craig Tillerb2b42612015-11-20 12:02:17 -0800384 gpr_mu_lock(&shard->mu);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800385
386 /* search for an existing string */
Craig Tillerb2b42612015-11-20 12:02:17 -0800387 idx = TABLE_IDX(hash, LOG2_STRTAB_SHARD_COUNT, shard->capacity);
388 for (s = shard->strs[idx]; s; s = s->bucket_next) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800389 if (s->hash == hash && GPR_SLICE_LENGTH(s->slice) == length &&
390 0 == memcmp(buf, GPR_SLICE_START_PTR(s->slice), length)) {
Craig Tiller773d9902016-04-26 18:27:31 -0700391 if (gpr_atm_full_fetch_add(&s->refcnt, 1) == 0) {
392 /* If we get here, we've added a ref to something that was about to
393 * die - drop it immediately.
394 * The *only* possible path here (given the shard mutex) should be to
395 * drop from one ref back to zero - assert that with a CAS */
396 GPR_ASSERT(gpr_atm_rel_cas(&s->refcnt, 1, 0));
397 /* and treat this as if we were never here... sshhh */
398 } else {
399 gpr_mu_unlock(&shard->mu);
400 GPR_TIMER_END("grpc_mdstr_from_buffer", 0);
401 return (grpc_mdstr *)s;
402 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800403 }
404 }
405
406 /* not found: create a new string */
407 if (length + 1 < GPR_SLICE_INLINED_SIZE) {
408 /* string data goes directly into the slice */
409 s = gpr_malloc(sizeof(internal_string));
Craig Tiller773d9902016-04-26 18:27:31 -0700410 gpr_atm_rel_store(&s->refcnt, 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800411 s->slice.refcount = NULL;
412 memcpy(s->slice.data.inlined.bytes, buf, length);
413 s->slice.data.inlined.bytes[length] = 0;
Craig Tiller7536af02015-12-22 13:49:30 -0800414 s->slice.data.inlined.length = (uint8_t)length;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800415 } else {
416 /* string data goes after the internal_string header, and we +1 for null
417 terminator */
418 s = gpr_malloc(sizeof(internal_string) + length + 1);
Craig Tiller773d9902016-04-26 18:27:31 -0700419 gpr_atm_rel_store(&s->refcnt, 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800420 s->refcount.ref = slice_ref;
421 s->refcount.unref = slice_unref;
422 s->slice.refcount = &s->refcount;
Craig Tiller7536af02015-12-22 13:49:30 -0800423 s->slice.data.refcounted.bytes = (uint8_t *)(s + 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800424 s->slice.data.refcounted.length = length;
425 memcpy(s->slice.data.refcounted.bytes, buf, length);
426 /* add a null terminator for cheap c string conversion when desired */
427 s->slice.data.refcounted.bytes[length] = 0;
428 }
ctiller430c4992014-12-11 09:15:41 -0800429 s->has_base64_and_huffman_encoded = 0;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800430 s->hash = hash;
yang-g4a530b02016-03-31 11:44:44 -0700431 s->size_in_decoder_table = SIZE_IN_DECODER_TABLE_NOT_SET;
Craig Tillerb2b42612015-11-20 12:02:17 -0800432 s->bucket_next = shard->strs[idx];
433 shard->strs[idx] = s;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800434
Craig Tillerb2b42612015-11-20 12:02:17 -0800435 shard->count++;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800436
Craig Tillerb2b42612015-11-20 12:02:17 -0800437 if (shard->count > shard->capacity * 2) {
438 grow_strtab(shard);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800439 }
440
Craig Tillerb2b42612015-11-20 12:02:17 -0800441 gpr_mu_unlock(&shard->mu);
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800442 GPR_TIMER_END("grpc_mdstr_from_buffer", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800443
444 return (grpc_mdstr *)s;
445}
446
Craig Tillerb2b42612015-11-20 12:02:17 -0800447static void gc_mdtab(mdtab_shard *shard) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800448 size_t i;
449 internal_metadata **prev_next;
450 internal_metadata *md, *next;
Craig Tiller8d8f9a82016-06-03 08:49:11 -0700451 gpr_atm num_freed = 0;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800452
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800453 GPR_TIMER_BEGIN("gc_mdtab", 0);
Craig Tillerb2b42612015-11-20 12:02:17 -0800454 for (i = 0; i < shard->capacity; i++) {
455 prev_next = &shard->elems[i];
456 for (md = shard->elems[i]; md; md = next) {
Craig Tiller8344daa2015-10-09 18:10:57 -0700457 void *user_data = (void *)gpr_atm_no_barrier_load(&md->user_data);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800458 next = md->bucket_next;
Craig Tiller9fa41b92015-04-10 15:08:03 -0700459 if (gpr_atm_acq_load(&md->refcnt) == 0) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800460 GRPC_MDSTR_UNREF((grpc_mdstr *)md->key);
461 GRPC_MDSTR_UNREF((grpc_mdstr *)md->value);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800462 if (md->user_data) {
Craig Tiller8344daa2015-10-09 18:10:57 -0700463 ((destroy_user_data_func)gpr_atm_no_barrier_load(
464 &md->destroy_user_data))(user_data);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800465 }
466 gpr_free(md);
467 *prev_next = next;
Craig Tiller8d8f9a82016-06-03 08:49:11 -0700468 num_freed++;
Craig Tillerb2b42612015-11-20 12:02:17 -0800469 shard->count--;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800470 } else {
471 prev_next = &md->bucket_next;
472 }
473 }
474 }
Craig Tiller8d8f9a82016-06-03 08:49:11 -0700475 gpr_atm_no_barrier_fetch_add(&shard->free_estimate, -num_freed);
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800476 GPR_TIMER_END("gc_mdtab", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800477}
478
Craig Tillerb2b42612015-11-20 12:02:17 -0800479static void grow_mdtab(mdtab_shard *shard) {
480 size_t capacity = shard->capacity * 2;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800481 size_t i;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800482 internal_metadata **mdtab;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800483 internal_metadata *md, *next;
Craig Tiller7536af02015-12-22 13:49:30 -0800484 uint32_t hash;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800485
486 GPR_TIMER_BEGIN("grow_mdtab", 0);
487
488 mdtab = gpr_malloc(sizeof(internal_metadata *) * capacity);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800489 memset(mdtab, 0, sizeof(internal_metadata *) * capacity);
490
Craig Tillerb2b42612015-11-20 12:02:17 -0800491 for (i = 0; i < shard->capacity; i++) {
492 for (md = shard->elems[i]; md; md = next) {
493 size_t idx;
ctillerfb93d192014-12-15 10:40:05 -0800494 hash = GRPC_MDSTR_KV_HASH(md->key->hash, md->value->hash);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800495 next = md->bucket_next;
Craig Tillerb2b42612015-11-20 12:02:17 -0800496 idx = TABLE_IDX(hash, LOG2_MDTAB_SHARD_COUNT, capacity);
497 md->bucket_next = mdtab[idx];
498 mdtab[idx] = md;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800499 }
500 }
501
Craig Tillerb2b42612015-11-20 12:02:17 -0800502 gpr_free(shard->elems);
503 shard->elems = mdtab;
504 shard->capacity = capacity;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800505
506 GPR_TIMER_END("grow_mdtab", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800507}
508
Craig Tillerb2b42612015-11-20 12:02:17 -0800509static void rehash_mdtab(mdtab_shard *shard) {
Craig Tiller139098c2016-06-06 08:10:08 -0700510 if (gpr_atm_no_barrier_load(&shard->free_estimate) >
511 (gpr_atm)(shard->capacity / 4)) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800512 gc_mdtab(shard);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800513 } else {
Craig Tillerb2b42612015-11-20 12:02:17 -0800514 grow_mdtab(shard);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800515 }
516}
517
Craig Tillerb2b42612015-11-20 12:02:17 -0800518grpc_mdelem *grpc_mdelem_from_metadata_strings(grpc_mdstr *mkey,
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800519 grpc_mdstr *mvalue) {
520 internal_string *key = (internal_string *)mkey;
521 internal_string *value = (internal_string *)mvalue;
Craig Tiller7536af02015-12-22 13:49:30 -0800522 uint32_t hash = GRPC_MDSTR_KV_HASH(mkey->hash, mvalue->hash);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800523 internal_metadata *md;
Craig Tillerb2b42612015-11-20 12:02:17 -0800524 mdtab_shard *shard = &g_mdtab_shard[SHARD_IDX(hash, LOG2_MDTAB_SHARD_COUNT)];
Craig Tiller0e72ede2015-11-19 07:48:53 -0800525 size_t i;
Craig Tillerb2b42612015-11-20 12:02:17 -0800526 size_t idx;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800527
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800528 GPR_TIMER_BEGIN("grpc_mdelem_from_metadata_strings", 0);
529
Craig Tiller0e72ede2015-11-19 07:48:53 -0800530 if (is_mdstr_static(mkey) && is_mdstr_static(mvalue)) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800531 for (i = 0; i <= g_static_mdtab_maxprobe; i++) {
532 grpc_mdelem *smd;
533 idx = (hash + i) % GPR_ARRAY_SIZE(g_static_mdtab);
534 smd = g_static_mdtab[idx];
535 if (smd == NULL) break;
536 if (smd->key == mkey && smd->value == mvalue) {
Craig Tiller07718b82015-11-21 14:06:45 -0800537 GPR_TIMER_END("grpc_mdelem_from_metadata_strings", 0);
Craig Tillerb2b42612015-11-20 12:02:17 -0800538 return smd;
Craig Tiller0e72ede2015-11-19 07:48:53 -0800539 }
540 }
541 }
542
Craig Tillerb2b42612015-11-20 12:02:17 -0800543 gpr_mu_lock(&shard->mu);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800544
Craig Tillerb2b42612015-11-20 12:02:17 -0800545 idx = TABLE_IDX(hash, LOG2_MDTAB_SHARD_COUNT, shard->capacity);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800546 /* search for an existing pair */
Craig Tillerb2b42612015-11-20 12:02:17 -0800547 for (md = shard->elems[idx]; md; md = md->bucket_next) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800548 if (md->key == key && md->value == value) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800549 REF_MD_LOCKED(shard, md);
550 GRPC_MDSTR_UNREF((grpc_mdstr *)key);
551 GRPC_MDSTR_UNREF((grpc_mdstr *)value);
552 gpr_mu_unlock(&shard->mu);
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800553 GPR_TIMER_END("grpc_mdelem_from_metadata_strings", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800554 return (grpc_mdelem *)md;
555 }
556 }
557
558 /* not found: create a new pair */
559 md = gpr_malloc(sizeof(internal_metadata));
Craig Tiller8d8f9a82016-06-03 08:49:11 -0700560 gpr_atm_rel_store(&md->refcnt, 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800561 md->key = key;
562 md->value = value;
Craig Tiller8344daa2015-10-09 18:10:57 -0700563 md->user_data = 0;
564 md->destroy_user_data = 0;
Craig Tillerb2b42612015-11-20 12:02:17 -0800565 md->bucket_next = shard->elems[idx];
566 shard->elems[idx] = md;
Craig Tiller83901532015-07-10 14:02:45 -0700567 gpr_mu_init(&md->mu_user_data);
Craig Tiller1a65a232015-07-06 10:22:32 -0700568#ifdef GRPC_METADATA_REFCOUNT_DEBUG
569 gpr_log(GPR_DEBUG, "ELM NEW:%p:%d: '%s' = '%s'", md,
570 gpr_atm_no_barrier_load(&md->refcnt),
571 grpc_mdstr_as_c_string((grpc_mdstr *)md->key),
572 grpc_mdstr_as_c_string((grpc_mdstr *)md->value));
573#endif
Craig Tillerb2b42612015-11-20 12:02:17 -0800574 shard->count++;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800575
Craig Tillerb2b42612015-11-20 12:02:17 -0800576 if (shard->count > shard->capacity * 2) {
577 rehash_mdtab(shard);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800578 }
579
Craig Tillerb2b42612015-11-20 12:02:17 -0800580 gpr_mu_unlock(&shard->mu);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800581
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800582 GPR_TIMER_END("grpc_mdelem_from_metadata_strings", 0);
583
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800584 return (grpc_mdelem *)md;
585}
586
Craig Tillerb2b42612015-11-20 12:02:17 -0800587grpc_mdelem *grpc_mdelem_from_strings(const char *key, const char *value) {
588 return grpc_mdelem_from_metadata_strings(grpc_mdstr_from_string(key),
589 grpc_mdstr_from_string(value));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800590}
591
Craig Tillerb2b42612015-11-20 12:02:17 -0800592grpc_mdelem *grpc_mdelem_from_slices(gpr_slice key, gpr_slice value) {
593 return grpc_mdelem_from_metadata_strings(grpc_mdstr_from_slice(key),
594 grpc_mdstr_from_slice(value));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800595}
596
Craig Tillerb2b42612015-11-20 12:02:17 -0800597grpc_mdelem *grpc_mdelem_from_string_and_buffer(const char *key,
Craig Tiller7536af02015-12-22 13:49:30 -0800598 const uint8_t *value,
Craig Tiller4dbdd6a2015-09-25 15:12:16 -0700599 size_t value_length) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800600 return grpc_mdelem_from_metadata_strings(
Craig Tillerb2b42612015-11-20 12:02:17 -0800601 grpc_mdstr_from_string(key), grpc_mdstr_from_buffer(value, value_length));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800602}
603
yang-g24ba7c12016-03-30 16:19:43 -0700604static size_t get_base64_encoded_size(size_t raw_length) {
605 static const uint8_t tail_xtra[3] = {0, 2, 3};
606 return raw_length / 3 * 4 + tail_xtra[raw_length % 3];
607}
608
yang-gcb7a8022016-03-30 14:58:53 -0700609size_t grpc_mdelem_get_size_in_hpack_table(grpc_mdelem *elem) {
610 size_t overhead_and_key = 32 + GPR_SLICE_LENGTH(elem->key->slice);
yang-g24ba7c12016-03-30 16:19:43 -0700611 size_t value_len = GPR_SLICE_LENGTH(elem->value->slice);
yang-gcb7a8022016-03-30 14:58:53 -0700612 if (is_mdstr_static(elem->value)) {
yang-g24ba7c12016-03-30 16:19:43 -0700613 if (grpc_is_binary_header(
614 (const char *)GPR_SLICE_START_PTR(elem->key->slice),
615 GPR_SLICE_LENGTH(elem->key->slice))) {
616 return overhead_and_key + get_base64_encoded_size(value_len);
617 } else {
618 return overhead_and_key + value_len;
619 }
yang-gcb7a8022016-03-30 14:58:53 -0700620 } else {
621 internal_string *is = (internal_string *)elem->value;
yang-g9cedd3f2016-04-01 00:24:59 -0700622 gpr_atm current_size = gpr_atm_acq_load(&is->size_in_decoder_table);
yang-g4a530b02016-03-31 11:44:44 -0700623 if (current_size == SIZE_IN_DECODER_TABLE_NOT_SET) {
yang-gcb7a8022016-03-30 14:58:53 -0700624 if (grpc_is_binary_header(
625 (const char *)GPR_SLICE_START_PTR(elem->key->slice),
626 GPR_SLICE_LENGTH(elem->key->slice))) {
yang-g4a530b02016-03-31 11:44:44 -0700627 current_size = (gpr_atm)get_base64_encoded_size(value_len);
yang-gcb7a8022016-03-30 14:58:53 -0700628 } else {
yang-g4a530b02016-03-31 11:44:44 -0700629 current_size = (gpr_atm)value_len;
yang-gcb7a8022016-03-30 14:58:53 -0700630 }
yang-g9cedd3f2016-04-01 00:24:59 -0700631 gpr_atm_rel_store(&is->size_in_decoder_table, current_size);
yang-gcb7a8022016-03-30 14:58:53 -0700632 }
yang-g4a530b02016-03-31 11:44:44 -0700633 return overhead_and_key + (size_t)current_size;
yang-gcb7a8022016-03-30 14:58:53 -0700634 }
635}
636
Craig Tiller1a65a232015-07-06 10:22:32 -0700637grpc_mdelem *grpc_mdelem_ref(grpc_mdelem *gmd DEBUG_ARGS) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800638 internal_metadata *md = (internal_metadata *)gmd;
Craig Tiller0e72ede2015-11-19 07:48:53 -0800639 if (is_mdelem_static(gmd)) return gmd;
Craig Tiller1a65a232015-07-06 10:22:32 -0700640#ifdef GRPC_METADATA_REFCOUNT_DEBUG
641 gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,
642 "ELM REF:%p:%d->%d: '%s' = '%s'", md,
643 gpr_atm_no_barrier_load(&md->refcnt),
644 gpr_atm_no_barrier_load(&md->refcnt) + 1,
645 grpc_mdstr_as_c_string((grpc_mdstr *)md->key),
646 grpc_mdstr_as_c_string((grpc_mdstr *)md->value));
647#endif
Craig Tiller9fa41b92015-04-10 15:08:03 -0700648 /* we can assume the ref count is >= 1 as the application is calling
649 this function - meaning that no adjustment to mdtab_free is necessary,
650 simplifying the logic here to be just an atomic increment */
651 /* use C assert to have this removed in opt builds */
Craig Tiller8d8f9a82016-06-03 08:49:11 -0700652 assert(gpr_atm_no_barrier_load(&md->refcnt) >= 1);
Craig Tiller9fa41b92015-04-10 15:08:03 -0700653 gpr_atm_no_barrier_fetch_add(&md->refcnt, 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800654 return gmd;
655}
656
Craig Tiller1a65a232015-07-06 10:22:32 -0700657void grpc_mdelem_unref(grpc_mdelem *gmd DEBUG_ARGS) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800658 internal_metadata *md = (internal_metadata *)gmd;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800659 if (!md) return;
Craig Tiller0e72ede2015-11-19 07:48:53 -0800660 if (is_mdelem_static(gmd)) return;
Craig Tiller1a65a232015-07-06 10:22:32 -0700661#ifdef GRPC_METADATA_REFCOUNT_DEBUG
662 gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,
663 "ELM UNREF:%p:%d->%d: '%s' = '%s'", md,
664 gpr_atm_no_barrier_load(&md->refcnt),
665 gpr_atm_no_barrier_load(&md->refcnt) - 1,
666 grpc_mdstr_as_c_string((grpc_mdstr *)md->key),
667 grpc_mdstr_as_c_string((grpc_mdstr *)md->value));
668#endif
Craig Tiller8d8f9a82016-06-03 08:49:11 -0700669 uint32_t hash = GRPC_MDSTR_KV_HASH(md->key->hash, md->value->hash);
670 if (1 == gpr_atm_full_fetch_add(&md->refcnt, -1)) {
671 /* once the refcount hits zero, some other thread can come along and
672 free md at any time: it's unsafe from this point on to access it */
Craig Tillerb2b42612015-11-20 12:02:17 -0800673 mdtab_shard *shard =
674 &g_mdtab_shard[SHARD_IDX(hash, LOG2_MDTAB_SHARD_COUNT)];
Craig Tiller8d8f9a82016-06-03 08:49:11 -0700675 gpr_atm_no_barrier_fetch_add(&shard->free_estimate, 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800676 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800677}
678
679const char *grpc_mdstr_as_c_string(grpc_mdstr *s) {
680 return (const char *)GPR_SLICE_START_PTR(s->slice);
681}
682
Craig Tiller1a65a232015-07-06 10:22:32 -0700683grpc_mdstr *grpc_mdstr_ref(grpc_mdstr *gs DEBUG_ARGS) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800684 internal_string *s = (internal_string *)gs;
Craig Tiller0e72ede2015-11-19 07:48:53 -0800685 if (is_mdstr_static(gs)) return gs;
Craig Tiller773d9902016-04-26 18:27:31 -0700686 GPR_ASSERT(gpr_atm_full_fetch_add(&s->refcnt, 1) > 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800687 return gs;
688}
689
Craig Tiller1a65a232015-07-06 10:22:32 -0700690void grpc_mdstr_unref(grpc_mdstr *gs DEBUG_ARGS) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800691 internal_string *s = (internal_string *)gs;
Craig Tiller0e72ede2015-11-19 07:48:53 -0800692 if (is_mdstr_static(gs)) return;
Craig Tiller773d9902016-04-26 18:27:31 -0700693 if (1 == gpr_atm_full_fetch_add(&s->refcnt, -1)) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800694 strtab_shard *shard =
695 &g_strtab_shard[SHARD_IDX(s->hash, LOG2_STRTAB_SHARD_COUNT)];
696 gpr_mu_lock(&shard->mu);
Craig Tiller773d9902016-04-26 18:27:31 -0700697 GPR_ASSERT(0 == gpr_atm_no_barrier_load(&s->refcnt));
698 internal_destroy_string(shard, s);
Craig Tillerb2b42612015-11-20 12:02:17 -0800699 gpr_mu_unlock(&shard->mu);
700 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800701}
702
Craig Tiller8344daa2015-10-09 18:10:57 -0700703void *grpc_mdelem_get_user_data(grpc_mdelem *md, void (*destroy_func)(void *)) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800704 internal_metadata *im = (internal_metadata *)md;
Craig Tiller83901532015-07-10 14:02:45 -0700705 void *result;
Craig Tillerb2b42612015-11-20 12:02:17 -0800706 if (is_mdelem_static(md)) {
707 return (void *)grpc_static_mdelem_user_data[md - grpc_static_mdelem_table];
708 }
Craig Tiller8344daa2015-10-09 18:10:57 -0700709 if (gpr_atm_acq_load(&im->destroy_user_data) == (gpr_atm)destroy_func) {
710 return (void *)gpr_atm_no_barrier_load(&im->user_data);
711 } else {
712 return NULL;
713 }
Craig Tiller83901532015-07-10 14:02:45 -0700714 return result;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800715}
716
717void grpc_mdelem_set_user_data(grpc_mdelem *md, void (*destroy_func)(void *),
718 void *user_data) {
719 internal_metadata *im = (internal_metadata *)md;
Craig Tillerebdef9d2015-11-19 17:09:49 -0800720 GPR_ASSERT(!is_mdelem_static(md));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800721 GPR_ASSERT((user_data == NULL) == (destroy_func == NULL));
Craig Tiller83901532015-07-10 14:02:45 -0700722 gpr_mu_lock(&im->mu_user_data);
Craig Tiller8344daa2015-10-09 18:10:57 -0700723 if (gpr_atm_no_barrier_load(&im->destroy_user_data)) {
Craig Tiller83901532015-07-10 14:02:45 -0700724 /* user data can only be set once */
725 gpr_mu_unlock(&im->mu_user_data);
726 if (destroy_func != NULL) {
727 destroy_func(user_data);
728 }
729 return;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800730 }
Craig Tiller8344daa2015-10-09 18:10:57 -0700731 gpr_atm_no_barrier_store(&im->user_data, (gpr_atm)user_data);
732 gpr_atm_rel_store(&im->destroy_user_data, (gpr_atm)destroy_func);
Craig Tiller83901532015-07-10 14:02:45 -0700733 gpr_mu_unlock(&im->mu_user_data);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800734}
ctiller430c4992014-12-11 09:15:41 -0800735
736gpr_slice grpc_mdstr_as_base64_encoded_and_huffman_compressed(grpc_mdstr *gs) {
737 internal_string *s = (internal_string *)gs;
738 gpr_slice slice;
Craig Tillerb2b42612015-11-20 12:02:17 -0800739 strtab_shard *shard =
740 &g_strtab_shard[SHARD_IDX(s->hash, LOG2_STRTAB_SHARD_COUNT)];
741 gpr_mu_lock(&shard->mu);
ctiller430c4992014-12-11 09:15:41 -0800742 if (!s->has_base64_and_huffman_encoded) {
743 s->base64_and_huffman =
744 grpc_chttp2_base64_encode_and_huffman_compress(s->slice);
ctiller33023c42014-12-12 16:28:33 -0800745 s->has_base64_and_huffman_encoded = 1;
ctiller430c4992014-12-11 09:15:41 -0800746 }
747 slice = s->base64_and_huffman;
Craig Tillerb2b42612015-11-20 12:02:17 -0800748 gpr_mu_unlock(&shard->mu);
ctiller430c4992014-12-11 09:15:41 -0800749 return slice;
David Garcia Quintas2bf574f2016-01-14 15:27:08 -0800750}