blob: 370d92d858054724688e3df453d5f14fe058669c [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"
Craig Tillera59c16c2016-10-31 07:25:01 -070050#include "src/core/lib/slice/slice_internal.h"
Craig Tiller9533d042016-03-25 17:11:06 -070051#include "src/core/lib/support/murmur_hash.h"
52#include "src/core/lib/support/string.h"
Craig Tiller9533d042016-03-25 17:11:06 -070053#include "src/core/lib/transport/static_metadata.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080054
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
Craig Tiller1a65a232015-07-06 10:22:32 -070064#ifdef GRPC_METADATA_REFCOUNT_DEBUG
65#define DEBUG_ARGS , const char *file, int line
66#define FWD_DEBUG_ARGS , file, line
Craig Tillerb2b42612015-11-20 12:02:17 -080067#define REF_MD_LOCKED(shard, s) ref_md_locked((shard), (s), __FILE__, __LINE__)
Craig Tiller1a65a232015-07-06 10:22:32 -070068#else
69#define DEBUG_ARGS
70#define FWD_DEBUG_ARGS
Craig Tillerb2b42612015-11-20 12:02:17 -080071#define REF_MD_LOCKED(shard, s) ref_md_locked((shard), (s))
Craig Tiller1a65a232015-07-06 10:22:32 -070072#endif
73
Craig Tillere52bbb12016-11-10 15:34:06 -080074#define INITIAL_SHARD_CAPACITY 8
75#define LOG2_SHARD_COUNT 4
76#define SHARD_COUNT ((size_t)(1 << LOG2_SHARD_COUNT))
77
78#define TABLE_IDX(hash, capacity) (((hash) >> (LOG2_SHARD_COUNT)) % (capacity))
79#define SHARD_IDX(hash) ((hash) & ((1 << (LOG2_SHARD_COUNT)) - 1))
Craig Tillerb2b42612015-11-20 12:02:17 -080080
Craig Tiller8344daa2015-10-09 18:10:57 -070081typedef void (*destroy_user_data_func)(void *user_data);
82
Craig Tiller0160de92016-11-18 08:46:46 -080083/* Shadow structure for grpc_mdelem_data for non-static elements */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080084typedef struct internal_metadata {
85 /* must be byte compatible with grpc_mdelem */
Craig Tillere52bbb12016-11-10 15:34:06 -080086 grpc_slice key;
87 grpc_slice value;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080088
Craig Tillerb2b42612015-11-20 12:02:17 -080089 /* private only data */
Craig Tiller9fa41b92015-04-10 15:08:03 -070090 gpr_atm refcnt;
91
Craig Tiller83901532015-07-10 14:02:45 -070092 gpr_mu mu_user_data;
Craig Tiller8344daa2015-10-09 18:10:57 -070093 gpr_atm destroy_user_data;
94 gpr_atm user_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080095
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080096 struct internal_metadata *bucket_next;
97} internal_metadata;
98
Craig Tillerb2b42612015-11-20 12:02:17 -080099typedef struct mdtab_shard {
100 gpr_mu mu;
101 internal_metadata **elems;
102 size_t count;
103 size_t capacity;
Craig Tiller8d8f9a82016-06-03 08:49:11 -0700104 /** Estimate of the number of unreferenced mdelems in the hash table.
105 This will eventually converge to the exact number, but it's instantaneous
106 accuracy is not guaranteed */
107 gpr_atm free_estimate;
Craig Tillerb2b42612015-11-20 12:02:17 -0800108} mdtab_shard;
Craig Tiller0e72ede2015-11-19 07:48:53 -0800109
Craig Tillere52bbb12016-11-10 15:34:06 -0800110static mdtab_shard g_shards[SHARD_COUNT];
Craig Tillerb2b42612015-11-20 12:02:17 -0800111
Craig Tillera59c16c2016-10-31 07:25:01 -0700112static void gc_mdtab(grpc_exec_ctx *exec_ctx, mdtab_shard *shard);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800113
Craig Tiller0e72ede2015-11-19 07:48:53 -0800114void grpc_mdctx_global_init(void) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800115 /* initialize shards */
Craig Tiller1ad51e02016-11-17 16:42:21 -0800116 for (size_t i = 0; i < SHARD_COUNT; i++) {
Craig Tillere52bbb12016-11-10 15:34:06 -0800117 mdtab_shard *shard = &g_shards[i];
Craig Tillerb2b42612015-11-20 12:02:17 -0800118 gpr_mu_init(&shard->mu);
119 shard->count = 0;
Craig Tiller8d8f9a82016-06-03 08:49:11 -0700120 gpr_atm_no_barrier_store(&shard->free_estimate, 0);
Craig Tillere52bbb12016-11-10 15:34:06 -0800121 shard->capacity = INITIAL_SHARD_CAPACITY;
Craig Tillerb2b42612015-11-20 12:02:17 -0800122 shard->elems = gpr_malloc(sizeof(*shard->elems) * shard->capacity);
123 memset(shard->elems, 0, sizeof(*shard->elems) * shard->capacity);
Craig Tiller0e72ede2015-11-19 07:48:53 -0800124 }
125}
126
Craig Tillera59c16c2016-10-31 07:25:01 -0700127void grpc_mdctx_global_shutdown(grpc_exec_ctx *exec_ctx) {
Craig Tiller1ad51e02016-11-17 16:42:21 -0800128 for (size_t i = 0; i < SHARD_COUNT; i++) {
Craig Tillere52bbb12016-11-10 15:34:06 -0800129 mdtab_shard *shard = &g_shards[i];
Craig Tillerb2b42612015-11-20 12:02:17 -0800130 gpr_mu_destroy(&shard->mu);
Craig Tillera59c16c2016-10-31 07:25:01 -0700131 gc_mdtab(exec_ctx, shard);
Craig Tiller34075442015-11-23 16:16:54 -0800132 /* TODO(ctiller): GPR_ASSERT(shard->count == 0); */
133 if (shard->count != 0) {
Yuchen Zeng64c0e8d2016-06-10 11:19:51 -0700134 gpr_log(GPR_DEBUG, "WARNING: %" PRIuPTR " metadata elements were leaked",
yang-gd88e1d82015-12-02 13:23:33 -0800135 shard->count);
Craig Tiller0cb803d2016-03-02 22:17:24 -0800136 if (grpc_iomgr_abort_on_leaks()) {
137 abort();
138 }
Craig Tiller34075442015-11-23 16:16:54 -0800139 }
Craig Tillerb2b42612015-11-20 12:02:17 -0800140 gpr_free(shard->elems);
141 }
Craig Tiller0e72ede2015-11-19 07:48:53 -0800142}
143
Craig Tiller0160de92016-11-18 08:46:46 -0800144static int is_mdelem_static(grpc_mdelem e) {
145 return e.payload >= &grpc_static_mdelem_table[0] &&
146 e.payload < &grpc_static_mdelem_table[GRPC_STATIC_MDELEM_COUNT];
Craig Tiller0e72ede2015-11-19 07:48:53 -0800147}
148
Craig Tillerb2b42612015-11-20 12:02:17 -0800149static void ref_md_locked(mdtab_shard *shard,
150 internal_metadata *md DEBUG_ARGS) {
Craig Tiller1a65a232015-07-06 10:22:32 -0700151#ifdef GRPC_METADATA_REFCOUNT_DEBUG
152 gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,
David Garcia Quintas331b9c02016-09-12 18:37:05 -0700153 "ELM REF:%p:%zu->%zu: '%s' = '%s'", (void *)md,
Craig Tiller1a65a232015-07-06 10:22:32 -0700154 gpr_atm_no_barrier_load(&md->refcnt),
155 gpr_atm_no_barrier_load(&md->refcnt) + 1,
Craig Tiller5ef31ab2016-11-10 16:27:48 -0800156 grpc_mdstr_as_c_string((grpc_slice)md->key),
157 grpc_mdstr_as_c_string((grpc_slice)md->value));
Craig Tiller1a65a232015-07-06 10:22:32 -0700158#endif
Craig Tiller8d8f9a82016-06-03 08:49:11 -0700159 if (0 == gpr_atm_no_barrier_fetch_add(&md->refcnt, 1)) {
160 gpr_atm_no_barrier_fetch_add(&shard->free_estimate, -1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800161 }
162}
163
Craig Tillera59c16c2016-10-31 07:25:01 -0700164static void gc_mdtab(grpc_exec_ctx *exec_ctx, mdtab_shard *shard) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800165 size_t i;
166 internal_metadata **prev_next;
167 internal_metadata *md, *next;
Craig Tiller8d8f9a82016-06-03 08:49:11 -0700168 gpr_atm num_freed = 0;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800169
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800170 GPR_TIMER_BEGIN("gc_mdtab", 0);
Craig Tillerb2b42612015-11-20 12:02:17 -0800171 for (i = 0; i < shard->capacity; i++) {
172 prev_next = &shard->elems[i];
173 for (md = shard->elems[i]; md; md = next) {
Craig Tiller8344daa2015-10-09 18:10:57 -0700174 void *user_data = (void *)gpr_atm_no_barrier_load(&md->user_data);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800175 next = md->bucket_next;
Craig Tiller9fa41b92015-04-10 15:08:03 -0700176 if (gpr_atm_acq_load(&md->refcnt) == 0) {
Craig Tillere52bbb12016-11-10 15:34:06 -0800177 grpc_slice_unref_internal(exec_ctx, md->key);
178 grpc_slice_unref_internal(exec_ctx, md->value);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800179 if (md->user_data) {
Craig Tiller8344daa2015-10-09 18:10:57 -0700180 ((destroy_user_data_func)gpr_atm_no_barrier_load(
181 &md->destroy_user_data))(user_data);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800182 }
183 gpr_free(md);
184 *prev_next = next;
Craig Tiller8d8f9a82016-06-03 08:49:11 -0700185 num_freed++;
Craig Tillerb2b42612015-11-20 12:02:17 -0800186 shard->count--;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800187 } else {
188 prev_next = &md->bucket_next;
189 }
190 }
191 }
Craig Tiller8d8f9a82016-06-03 08:49:11 -0700192 gpr_atm_no_barrier_fetch_add(&shard->free_estimate, -num_freed);
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800193 GPR_TIMER_END("gc_mdtab", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800194}
195
Craig Tillerb2b42612015-11-20 12:02:17 -0800196static void grow_mdtab(mdtab_shard *shard) {
197 size_t capacity = shard->capacity * 2;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800198 size_t i;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800199 internal_metadata **mdtab;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800200 internal_metadata *md, *next;
Craig Tiller7536af02015-12-22 13:49:30 -0800201 uint32_t hash;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800202
203 GPR_TIMER_BEGIN("grow_mdtab", 0);
204
205 mdtab = gpr_malloc(sizeof(internal_metadata *) * capacity);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800206 memset(mdtab, 0, sizeof(internal_metadata *) * capacity);
207
Craig Tillerb2b42612015-11-20 12:02:17 -0800208 for (i = 0; i < shard->capacity; i++) {
209 for (md = shard->elems[i]; md; md = next) {
210 size_t idx;
Craig Tillere52bbb12016-11-10 15:34:06 -0800211 hash = GRPC_MDSTR_KV_HASH(grpc_slice_hash(md->key),
212 grpc_slice_hash(md->value));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800213 next = md->bucket_next;
Craig Tillere52bbb12016-11-10 15:34:06 -0800214 idx = TABLE_IDX(hash, capacity);
Craig Tillerb2b42612015-11-20 12:02:17 -0800215 md->bucket_next = mdtab[idx];
216 mdtab[idx] = md;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800217 }
218 }
219
Craig Tillerb2b42612015-11-20 12:02:17 -0800220 gpr_free(shard->elems);
221 shard->elems = mdtab;
222 shard->capacity = capacity;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800223
224 GPR_TIMER_END("grow_mdtab", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800225}
226
Craig Tillera59c16c2016-10-31 07:25:01 -0700227static void rehash_mdtab(grpc_exec_ctx *exec_ctx, mdtab_shard *shard) {
Craig Tiller139098c2016-06-06 08:10:08 -0700228 if (gpr_atm_no_barrier_load(&shard->free_estimate) >
229 (gpr_atm)(shard->capacity / 4)) {
Craig Tillera59c16c2016-10-31 07:25:01 -0700230 gc_mdtab(exec_ctx, shard);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800231 } else {
Craig Tillerb2b42612015-11-20 12:02:17 -0800232 grow_mdtab(shard);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800233 }
234}
235
Craig Tiller0160de92016-11-18 08:46:46 -0800236grpc_mdelem grpc_mdelem_from_slices(grpc_exec_ctx *exec_ctx, grpc_slice key,
237 grpc_slice value) {
Craig Tiller1ad51e02016-11-17 16:42:21 -0800238 grpc_slice_static_intern(&key);
239 grpc_slice_static_intern(&value);
240
Craig Tiller0160de92016-11-18 08:46:46 -0800241 grpc_mdelem static_elem = grpc_static_mdelem_for_static_strings(
Craig Tiller1ad51e02016-11-17 16:42:21 -0800242 grpc_static_metadata_index(key), grpc_static_metadata_index(value));
Craig Tiller0160de92016-11-18 08:46:46 -0800243 if (!GRPC_MDISNULL(static_elem)) {
Craig Tiller1ad51e02016-11-17 16:42:21 -0800244 return static_elem;
245 }
246
Craig Tillere52bbb12016-11-10 15:34:06 -0800247 uint32_t hash =
248 GRPC_MDSTR_KV_HASH(grpc_slice_hash(key), grpc_slice_hash(value));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800249 internal_metadata *md;
Craig Tillere52bbb12016-11-10 15:34:06 -0800250 mdtab_shard *shard = &g_shards[SHARD_IDX(hash)];
Craig Tillerb2b42612015-11-20 12:02:17 -0800251 size_t idx;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800252
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800253 GPR_TIMER_BEGIN("grpc_mdelem_from_metadata_strings", 0);
254
Craig Tillerb2b42612015-11-20 12:02:17 -0800255 gpr_mu_lock(&shard->mu);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800256
Craig Tillere52bbb12016-11-10 15:34:06 -0800257 idx = TABLE_IDX(hash, shard->capacity);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800258 /* search for an existing pair */
Craig Tillerb2b42612015-11-20 12:02:17 -0800259 for (md = shard->elems[idx]; md; md = md->bucket_next) {
Craig Tillere52bbb12016-11-10 15:34:06 -0800260 if (grpc_slice_cmp(key, md->key) == 0 &&
261 grpc_slice_cmp(value, md->value) == 0) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800262 REF_MD_LOCKED(shard, md);
Craig Tillerb2b42612015-11-20 12:02:17 -0800263 gpr_mu_unlock(&shard->mu);
Craig Tillere52bbb12016-11-10 15:34:06 -0800264 grpc_slice_unref_internal(exec_ctx, key);
265 grpc_slice_unref_internal(exec_ctx, value);
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800266 GPR_TIMER_END("grpc_mdelem_from_metadata_strings", 0);
Craig Tiller0160de92016-11-18 08:46:46 -0800267 return (grpc_mdelem){(grpc_mdelem_data *)md};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800268 }
269 }
270
271 /* not found: create a new pair */
272 md = gpr_malloc(sizeof(internal_metadata));
Craig Tiller8d8f9a82016-06-03 08:49:11 -0700273 gpr_atm_rel_store(&md->refcnt, 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800274 md->key = key;
275 md->value = value;
Craig Tiller8344daa2015-10-09 18:10:57 -0700276 md->user_data = 0;
277 md->destroy_user_data = 0;
Craig Tillerb2b42612015-11-20 12:02:17 -0800278 md->bucket_next = shard->elems[idx];
279 shard->elems[idx] = md;
Craig Tiller83901532015-07-10 14:02:45 -0700280 gpr_mu_init(&md->mu_user_data);
Craig Tiller1a65a232015-07-06 10:22:32 -0700281#ifdef GRPC_METADATA_REFCOUNT_DEBUG
David Garcia Quintas331b9c02016-09-12 18:37:05 -0700282 gpr_log(GPR_DEBUG, "ELM NEW:%p:%zu: '%s' = '%s'", (void *)md,
Craig Tiller1a65a232015-07-06 10:22:32 -0700283 gpr_atm_no_barrier_load(&md->refcnt),
Craig Tiller5ef31ab2016-11-10 16:27:48 -0800284 grpc_mdstr_as_c_string((grpc_slice)md->key),
285 grpc_mdstr_as_c_string((grpc_slice)md->value));
Craig Tiller1a65a232015-07-06 10:22:32 -0700286#endif
Craig Tillerb2b42612015-11-20 12:02:17 -0800287 shard->count++;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800288
Craig Tillerb2b42612015-11-20 12:02:17 -0800289 if (shard->count > shard->capacity * 2) {
Craig Tillera59c16c2016-10-31 07:25:01 -0700290 rehash_mdtab(exec_ctx, shard);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800291 }
292
Craig Tillerb2b42612015-11-20 12:02:17 -0800293 gpr_mu_unlock(&shard->mu);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800294
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800295 GPR_TIMER_END("grpc_mdelem_from_metadata_strings", 0);
296
Craig Tiller0160de92016-11-18 08:46:46 -0800297 return (grpc_mdelem){(grpc_mdelem_data *)md};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800298}
299
yang-g24ba7c12016-03-30 16:19:43 -0700300static size_t get_base64_encoded_size(size_t raw_length) {
301 static const uint8_t tail_xtra[3] = {0, 2, 3};
302 return raw_length / 3 * 4 + tail_xtra[raw_length % 3];
303}
304
Craig Tiller0160de92016-11-18 08:46:46 -0800305size_t grpc_mdelem_get_size_in_hpack_table(grpc_mdelem elem) {
306 size_t overhead_and_key = 32 + GRPC_SLICE_LENGTH(GRPC_MDKEY(elem));
307 size_t value_len = GRPC_SLICE_LENGTH(GRPC_MDVALUE(elem));
308 if (grpc_is_binary_header(GRPC_MDKEY(elem))) {
Craig Tillere52bbb12016-11-10 15:34:06 -0800309 return overhead_and_key + get_base64_encoded_size(value_len);
yang-gcb7a8022016-03-30 14:58:53 -0700310 } else {
Craig Tillere52bbb12016-11-10 15:34:06 -0800311 return overhead_and_key + value_len;
yang-gcb7a8022016-03-30 14:58:53 -0700312 }
313}
314
Craig Tiller0160de92016-11-18 08:46:46 -0800315grpc_mdelem grpc_mdelem_ref(grpc_mdelem gmd DEBUG_ARGS) {
316 internal_metadata *md = (internal_metadata *)gmd.payload;
Craig Tiller0e72ede2015-11-19 07:48:53 -0800317 if (is_mdelem_static(gmd)) return gmd;
Craig Tiller1a65a232015-07-06 10:22:32 -0700318#ifdef GRPC_METADATA_REFCOUNT_DEBUG
319 gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,
David Garcia Quintas331b9c02016-09-12 18:37:05 -0700320 "ELM REF:%p:%zu->%zu: '%s' = '%s'", (void *)md,
Craig Tiller1a65a232015-07-06 10:22:32 -0700321 gpr_atm_no_barrier_load(&md->refcnt),
322 gpr_atm_no_barrier_load(&md->refcnt) + 1,
Craig Tiller5ef31ab2016-11-10 16:27:48 -0800323 grpc_mdstr_as_c_string((grpc_slice)md->key),
324 grpc_mdstr_as_c_string((grpc_slice)md->value));
Craig Tiller1a65a232015-07-06 10:22:32 -0700325#endif
Craig Tiller9fa41b92015-04-10 15:08:03 -0700326 /* we can assume the ref count is >= 1 as the application is calling
327 this function - meaning that no adjustment to mdtab_free is necessary,
328 simplifying the logic here to be just an atomic increment */
329 /* use C assert to have this removed in opt builds */
David Garcia Quintas331b9c02016-09-12 18:37:05 -0700330 GPR_ASSERT(gpr_atm_no_barrier_load(&md->refcnt) >= 1);
Craig Tiller9fa41b92015-04-10 15:08:03 -0700331 gpr_atm_no_barrier_fetch_add(&md->refcnt, 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800332 return gmd;
333}
334
Craig Tiller0160de92016-11-18 08:46:46 -0800335void grpc_mdelem_unref(grpc_exec_ctx *exec_ctx, grpc_mdelem gmd DEBUG_ARGS) {
336 internal_metadata *md = (internal_metadata *)gmd.payload;
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800337 if (!md) return;
Craig Tiller0e72ede2015-11-19 07:48:53 -0800338 if (is_mdelem_static(gmd)) return;
Craig Tiller1a65a232015-07-06 10:22:32 -0700339#ifdef GRPC_METADATA_REFCOUNT_DEBUG
340 gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,
David Garcia Quintas331b9c02016-09-12 18:37:05 -0700341 "ELM UNREF:%p:%zu->%zu: '%s' = '%s'", (void *)md,
Craig Tiller1a65a232015-07-06 10:22:32 -0700342 gpr_atm_no_barrier_load(&md->refcnt),
343 gpr_atm_no_barrier_load(&md->refcnt) - 1,
Craig Tiller5ef31ab2016-11-10 16:27:48 -0800344 grpc_mdstr_as_c_string((grpc_slice)md->key),
345 grpc_mdstr_as_c_string((grpc_slice)md->value));
Craig Tiller1a65a232015-07-06 10:22:32 -0700346#endif
Craig Tillere52bbb12016-11-10 15:34:06 -0800347 uint32_t hash =
348 GRPC_MDSTR_KV_HASH(grpc_slice_hash(md->key), grpc_slice_hash(md->value));
David Garcia Quintas331b9c02016-09-12 18:37:05 -0700349 const gpr_atm prev_refcount = gpr_atm_full_fetch_add(&md->refcnt, -1);
350 GPR_ASSERT(prev_refcount >= 1);
351 if (1 == prev_refcount) {
Craig Tiller8d8f9a82016-06-03 08:49:11 -0700352 /* once the refcount hits zero, some other thread can come along and
353 free md at any time: it's unsafe from this point on to access it */
Craig Tillere52bbb12016-11-10 15:34:06 -0800354 mdtab_shard *shard = &g_shards[SHARD_IDX(hash)];
Craig Tiller8d8f9a82016-06-03 08:49:11 -0700355 gpr_atm_no_barrier_fetch_add(&shard->free_estimate, 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800356 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800357}
358
Craig Tiller0160de92016-11-18 08:46:46 -0800359void *grpc_mdelem_get_user_data(grpc_mdelem md, void (*destroy_func)(void *)) {
360 internal_metadata *im = (internal_metadata *)md.payload;
Craig Tiller83901532015-07-10 14:02:45 -0700361 void *result;
Craig Tillerb2b42612015-11-20 12:02:17 -0800362 if (is_mdelem_static(md)) {
Craig Tiller0160de92016-11-18 08:46:46 -0800363 return (void *)
364 grpc_static_mdelem_user_data[md.payload - grpc_static_mdelem_table];
Craig Tillerb2b42612015-11-20 12:02:17 -0800365 }
Craig Tiller8344daa2015-10-09 18:10:57 -0700366 if (gpr_atm_acq_load(&im->destroy_user_data) == (gpr_atm)destroy_func) {
367 return (void *)gpr_atm_no_barrier_load(&im->user_data);
368 } else {
369 return NULL;
370 }
Craig Tiller83901532015-07-10 14:02:45 -0700371 return result;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800372}
373
Craig Tiller0160de92016-11-18 08:46:46 -0800374void *grpc_mdelem_set_user_data(grpc_mdelem md, void (*destroy_func)(void *),
Craig Tiller738e6db2016-11-15 09:29:44 -0800375 void *user_data) {
Craig Tiller0160de92016-11-18 08:46:46 -0800376 internal_metadata *im = (internal_metadata *)md.payload;
Craig Tillerebdef9d2015-11-19 17:09:49 -0800377 GPR_ASSERT(!is_mdelem_static(md));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800378 GPR_ASSERT((user_data == NULL) == (destroy_func == NULL));
Craig Tiller83901532015-07-10 14:02:45 -0700379 gpr_mu_lock(&im->mu_user_data);
Craig Tiller8344daa2015-10-09 18:10:57 -0700380 if (gpr_atm_no_barrier_load(&im->destroy_user_data)) {
Craig Tiller83901532015-07-10 14:02:45 -0700381 /* user data can only be set once */
382 gpr_mu_unlock(&im->mu_user_data);
383 if (destroy_func != NULL) {
384 destroy_func(user_data);
385 }
Craig Tiller738e6db2016-11-15 09:29:44 -0800386 return (void *)gpr_atm_no_barrier_load(&im->user_data);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800387 }
Craig Tiller8344daa2015-10-09 18:10:57 -0700388 gpr_atm_no_barrier_store(&im->user_data, (gpr_atm)user_data);
389 gpr_atm_rel_store(&im->destroy_user_data, (gpr_atm)destroy_func);
Craig Tiller83901532015-07-10 14:02:45 -0700390 gpr_mu_unlock(&im->mu_user_data);
Craig Tiller738e6db2016-11-15 09:29:44 -0800391 return user_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800392}
Craig Tiller0160de92016-11-18 08:46:46 -0800393
394bool grpc_mdelem_eq(grpc_mdelem a, grpc_mdelem b) {
395 if (a.payload == b.payload) return true;
396 return 0 == grpc_slice_cmp(GRPC_MDKEY(a), GRPC_MDKEY(b)) &&
397 0 == grpc_slice_cmp(GRPC_MDVALUE(a), GRPC_MDVALUE(b));
398}