Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * 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 Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 34 | #include "src/core/lib/transport/metadata.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 35 | |
Craig Tiller | 9fa41b9 | 2015-04-10 15:08:03 -0700 | [diff] [blame] | 36 | #include <assert.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 37 | #include <stddef.h> |
| 38 | #include <string.h> |
| 39 | |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 40 | #include <grpc/compression.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 41 | #include <grpc/support/alloc.h> |
Craig Tiller | 9fa41b9 | 2015-04-10 15:08:03 -0700 | [diff] [blame] | 42 | #include <grpc/support/atm.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 43 | #include <grpc/support/log.h> |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 44 | #include <grpc/support/string_util.h> |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 45 | #include <grpc/support/time.h> |
Craig Tiller | 0cb803d | 2016-03-02 22:17:24 -0800 | [diff] [blame] | 46 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 47 | #include "src/core/lib/iomgr/iomgr_internal.h" |
| 48 | #include "src/core/lib/profiling/timers.h" |
| 49 | #include "src/core/lib/support/murmur_hash.h" |
| 50 | #include "src/core/lib/support/string.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 51 | #include "src/core/lib/transport/static_metadata.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 52 | |
Craig Tiller | f82ddc4 | 2016-04-05 17:15:07 -0700 | [diff] [blame] | 53 | gpr_slice (*grpc_chttp2_base64_encode_and_huffman_compress)(gpr_slice input); |
| 54 | |
Craig Tiller | 70b080d | 2015-11-19 08:04:48 -0800 | [diff] [blame] | 55 | /* There are two kinds of mdelem and mdstr instances. |
| 56 | * Static instances are declared in static_metadata.{h,c} and |
| 57 | * are initialized by grpc_mdctx_global_init(). |
| 58 | * Dynamic instances are stored in hash tables on grpc_mdctx, and are backed |
| 59 | * by internal_string and internal_element structures. |
| 60 | * Internal helper functions here-in (is_mdstr_static, is_mdelem_static) are |
| 61 | * used to determine which kind of element a pointer refers to. |
| 62 | */ |
| 63 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 64 | #define INITIAL_STRTAB_CAPACITY 4 |
| 65 | #define INITIAL_MDTAB_CAPACITY 4 |
| 66 | |
Craig Tiller | 1a65a23 | 2015-07-06 10:22:32 -0700 | [diff] [blame] | 67 | #ifdef GRPC_METADATA_REFCOUNT_DEBUG |
| 68 | #define DEBUG_ARGS , const char *file, int line |
| 69 | #define FWD_DEBUG_ARGS , file, line |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 70 | #define REF_MD_LOCKED(shard, s) ref_md_locked((shard), (s), __FILE__, __LINE__) |
Craig Tiller | 1a65a23 | 2015-07-06 10:22:32 -0700 | [diff] [blame] | 71 | #else |
| 72 | #define DEBUG_ARGS |
| 73 | #define FWD_DEBUG_ARGS |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 74 | #define REF_MD_LOCKED(shard, s) ref_md_locked((shard), (s)) |
Craig Tiller | 1a65a23 | 2015-07-06 10:22:32 -0700 | [diff] [blame] | 75 | #endif |
| 76 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 77 | #define TABLE_IDX(hash, log2_shards, capacity) \ |
| 78 | (((hash) >> (log2_shards)) % (capacity)) |
| 79 | #define SHARD_IDX(hash, log2_shards) ((hash) & ((1 << (log2_shards)) - 1)) |
| 80 | |
Craig Tiller | 8344daa | 2015-10-09 18:10:57 -0700 | [diff] [blame] | 81 | typedef void (*destroy_user_data_func)(void *user_data); |
| 82 | |
Craig Tiller | 70b080d | 2015-11-19 08:04:48 -0800 | [diff] [blame] | 83 | /* Shadow structure for grpc_mdstr for non-static values */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 84 | typedef struct internal_string { |
| 85 | /* must be byte compatible with grpc_mdstr */ |
| 86 | gpr_slice slice; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 87 | uint32_t hash; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 88 | |
| 89 | /* private only data */ |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 90 | gpr_atm refcnt; |
| 91 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 92 | uint8_t has_base64_and_huffman_encoded; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 93 | gpr_slice_refcount refcount; |
| 94 | |
ctiller | 430c499 | 2014-12-11 09:15:41 -0800 | [diff] [blame] | 95 | gpr_slice base64_and_huffman; |
| 96 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 97 | struct internal_string *bucket_next; |
| 98 | } internal_string; |
| 99 | |
Craig Tiller | 70b080d | 2015-11-19 08:04:48 -0800 | [diff] [blame] | 100 | /* Shadow structure for grpc_mdelem for non-static elements */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 101 | typedef struct internal_metadata { |
| 102 | /* must be byte compatible with grpc_mdelem */ |
| 103 | internal_string *key; |
| 104 | internal_string *value; |
| 105 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 106 | /* private only data */ |
Craig Tiller | 9fa41b9 | 2015-04-10 15:08:03 -0700 | [diff] [blame] | 107 | gpr_atm refcnt; |
| 108 | |
Craig Tiller | 8390153 | 2015-07-10 14:02:45 -0700 | [diff] [blame] | 109 | gpr_mu mu_user_data; |
Craig Tiller | 8344daa | 2015-10-09 18:10:57 -0700 | [diff] [blame] | 110 | gpr_atm destroy_user_data; |
| 111 | gpr_atm user_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 112 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 113 | struct internal_metadata *bucket_next; |
| 114 | } internal_metadata; |
| 115 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 116 | typedef struct strtab_shard { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 117 | gpr_mu mu; |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 118 | internal_string **strs; |
| 119 | size_t count; |
| 120 | size_t capacity; |
| 121 | } strtab_shard; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 122 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 123 | typedef struct mdtab_shard { |
| 124 | gpr_mu mu; |
| 125 | internal_metadata **elems; |
| 126 | size_t count; |
| 127 | size_t capacity; |
| 128 | size_t free; |
| 129 | } mdtab_shard; |
Craig Tiller | 0e72ede | 2015-11-19 07:48:53 -0800 | [diff] [blame] | 130 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 131 | #define LOG2_STRTAB_SHARD_COUNT 5 |
| 132 | #define LOG2_MDTAB_SHARD_COUNT 4 |
| 133 | #define STRTAB_SHARD_COUNT ((size_t)(1 << LOG2_STRTAB_SHARD_COUNT)) |
| 134 | #define MDTAB_SHARD_COUNT ((size_t)(1 << LOG2_MDTAB_SHARD_COUNT)) |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 135 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 136 | /* hash seed: decided at initialization time */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 137 | static uint32_t g_hash_seed; |
Craig Tiller | e62bf98 | 2015-12-02 17:11:49 -0800 | [diff] [blame] | 138 | static int g_forced_hash_seed = 0; |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 139 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 140 | /* linearly probed hash tables for static element lookup */ |
| 141 | static grpc_mdstr *g_static_strtab[GRPC_STATIC_MDSTR_COUNT * 2]; |
| 142 | static grpc_mdelem *g_static_mdtab[GRPC_STATIC_MDELEM_COUNT * 2]; |
| 143 | static size_t g_static_strtab_maxprobe; |
| 144 | static size_t g_static_mdtab_maxprobe; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 145 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 146 | static strtab_shard g_strtab_shard[STRTAB_SHARD_COUNT]; |
| 147 | static mdtab_shard g_mdtab_shard[MDTAB_SHARD_COUNT]; |
| 148 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 149 | static void gc_mdtab(mdtab_shard *shard); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 150 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 151 | void grpc_test_only_set_metadata_hash_seed(uint32_t seed) { |
Craig Tiller | e62bf98 | 2015-12-02 17:11:49 -0800 | [diff] [blame] | 152 | g_hash_seed = seed; |
| 153 | g_forced_hash_seed = 1; |
| 154 | } |
| 155 | |
Craig Tiller | 0e72ede | 2015-11-19 07:48:53 -0800 | [diff] [blame] | 156 | void grpc_mdctx_global_init(void) { |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 157 | size_t i, j; |
Craig Tiller | e62bf98 | 2015-12-02 17:11:49 -0800 | [diff] [blame] | 158 | if (!g_forced_hash_seed) { |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 159 | g_hash_seed = (uint32_t)gpr_now(GPR_CLOCK_REALTIME).tv_nsec; |
Craig Tiller | e62bf98 | 2015-12-02 17:11:49 -0800 | [diff] [blame] | 160 | } |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 161 | g_static_strtab_maxprobe = 0; |
| 162 | g_static_mdtab_maxprobe = 0; |
| 163 | /* build static tables */ |
| 164 | memset(g_static_mdtab, 0, sizeof(g_static_mdtab)); |
| 165 | memset(g_static_strtab, 0, sizeof(g_static_strtab)); |
Craig Tiller | 0e72ede | 2015-11-19 07:48:53 -0800 | [diff] [blame] | 166 | for (i = 0; i < GRPC_STATIC_MDSTR_COUNT; i++) { |
| 167 | grpc_mdstr *elem = &grpc_static_mdstr_table[i]; |
| 168 | const char *str = grpc_static_metadata_strings[i]; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 169 | uint32_t hash = gpr_murmur_hash3(str, strlen(str), g_hash_seed); |
Craig Tiller | b774be4 | 2015-11-19 07:56:13 -0800 | [diff] [blame] | 170 | *(gpr_slice *)&elem->slice = gpr_slice_from_static_string(str); |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 171 | *(uint32_t *)&elem->hash = hash; |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 172 | for (j = 0;; j++) { |
| 173 | size_t idx = (hash + j) % GPR_ARRAY_SIZE(g_static_strtab); |
| 174 | if (g_static_strtab[idx] == NULL) { |
| 175 | g_static_strtab[idx] = &grpc_static_mdstr_table[i]; |
| 176 | break; |
| 177 | } |
| 178 | } |
| 179 | if (j > g_static_strtab_maxprobe) { |
| 180 | g_static_strtab_maxprobe = j; |
| 181 | } |
Craig Tiller | 0e72ede | 2015-11-19 07:48:53 -0800 | [diff] [blame] | 182 | } |
| 183 | for (i = 0; i < GRPC_STATIC_MDELEM_COUNT; i++) { |
| 184 | grpc_mdelem *elem = &grpc_static_mdelem_table[i]; |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 185 | grpc_mdstr *key = |
| 186 | &grpc_static_mdstr_table[grpc_static_metadata_elem_indices[2 * i + 0]]; |
| 187 | grpc_mdstr *value = |
| 188 | &grpc_static_mdstr_table[grpc_static_metadata_elem_indices[2 * i + 1]]; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 189 | uint32_t hash = GRPC_MDSTR_KV_HASH(key->hash, value->hash); |
Craig Tiller | b774be4 | 2015-11-19 07:56:13 -0800 | [diff] [blame] | 190 | *(grpc_mdstr **)&elem->key = key; |
| 191 | *(grpc_mdstr **)&elem->value = value; |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 192 | for (j = 0;; j++) { |
| 193 | size_t idx = (hash + j) % GPR_ARRAY_SIZE(g_static_mdtab); |
| 194 | if (g_static_mdtab[idx] == NULL) { |
| 195 | g_static_mdtab[idx] = elem; |
| 196 | break; |
| 197 | } |
| 198 | } |
| 199 | if (j > g_static_mdtab_maxprobe) { |
| 200 | g_static_mdtab_maxprobe = j; |
| 201 | } |
| 202 | } |
| 203 | /* initialize shards */ |
| 204 | for (i = 0; i < STRTAB_SHARD_COUNT; i++) { |
| 205 | strtab_shard *shard = &g_strtab_shard[i]; |
| 206 | gpr_mu_init(&shard->mu); |
| 207 | shard->count = 0; |
| 208 | shard->capacity = INITIAL_STRTAB_CAPACITY; |
| 209 | shard->strs = gpr_malloc(sizeof(*shard->strs) * shard->capacity); |
| 210 | memset(shard->strs, 0, sizeof(*shard->strs) * shard->capacity); |
| 211 | } |
| 212 | for (i = 0; i < MDTAB_SHARD_COUNT; i++) { |
| 213 | mdtab_shard *shard = &g_mdtab_shard[i]; |
| 214 | gpr_mu_init(&shard->mu); |
| 215 | shard->count = 0; |
| 216 | shard->free = 0; |
| 217 | shard->capacity = INITIAL_MDTAB_CAPACITY; |
| 218 | shard->elems = gpr_malloc(sizeof(*shard->elems) * shard->capacity); |
| 219 | memset(shard->elems, 0, sizeof(*shard->elems) * shard->capacity); |
Craig Tiller | 0e72ede | 2015-11-19 07:48:53 -0800 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 223 | void grpc_mdctx_global_shutdown(void) { |
| 224 | size_t i; |
| 225 | for (i = 0; i < MDTAB_SHARD_COUNT; i++) { |
| 226 | mdtab_shard *shard = &g_mdtab_shard[i]; |
| 227 | gpr_mu_destroy(&shard->mu); |
Craig Tiller | 3407544 | 2015-11-23 16:16:54 -0800 | [diff] [blame] | 228 | gc_mdtab(shard); |
| 229 | /* TODO(ctiller): GPR_ASSERT(shard->count == 0); */ |
| 230 | if (shard->count != 0) { |
yang-g | d88e1d8 | 2015-12-02 13:23:33 -0800 | [diff] [blame] | 231 | gpr_log(GPR_DEBUG, "WARNING: %d metadata elements were leaked", |
| 232 | shard->count); |
Craig Tiller | 0cb803d | 2016-03-02 22:17:24 -0800 | [diff] [blame] | 233 | if (grpc_iomgr_abort_on_leaks()) { |
| 234 | abort(); |
| 235 | } |
Craig Tiller | 3407544 | 2015-11-23 16:16:54 -0800 | [diff] [blame] | 236 | } |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 237 | gpr_free(shard->elems); |
| 238 | } |
| 239 | for (i = 0; i < STRTAB_SHARD_COUNT; i++) { |
| 240 | strtab_shard *shard = &g_strtab_shard[i]; |
| 241 | gpr_mu_destroy(&shard->mu); |
Craig Tiller | 3407544 | 2015-11-23 16:16:54 -0800 | [diff] [blame] | 242 | /* TODO(ctiller): GPR_ASSERT(shard->count == 0); */ |
| 243 | if (shard->count != 0) { |
yang-g | d88e1d8 | 2015-12-02 13:23:33 -0800 | [diff] [blame] | 244 | gpr_log(GPR_DEBUG, "WARNING: %d metadata strings were leaked", |
| 245 | shard->count); |
Craig Tiller | 0cb803d | 2016-03-02 22:17:24 -0800 | [diff] [blame] | 246 | if (grpc_iomgr_abort_on_leaks()) { |
| 247 | abort(); |
| 248 | } |
Craig Tiller | 3407544 | 2015-11-23 16:16:54 -0800 | [diff] [blame] | 249 | } |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 250 | gpr_free(shard->strs); |
| 251 | } |
| 252 | } |
Craig Tiller | 0e72ede | 2015-11-19 07:48:53 -0800 | [diff] [blame] | 253 | |
| 254 | static int is_mdstr_static(grpc_mdstr *s) { |
Craig Tiller | b774be4 | 2015-11-19 07:56:13 -0800 | [diff] [blame] | 255 | return s >= &grpc_static_mdstr_table[0] && |
| 256 | s < &grpc_static_mdstr_table[GRPC_STATIC_MDSTR_COUNT]; |
Craig Tiller | 0e72ede | 2015-11-19 07:48:53 -0800 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | static int is_mdelem_static(grpc_mdelem *e) { |
Craig Tiller | b774be4 | 2015-11-19 07:56:13 -0800 | [diff] [blame] | 260 | return e >= &grpc_static_mdelem_table[0] && |
| 261 | e < &grpc_static_mdelem_table[GRPC_STATIC_MDELEM_COUNT]; |
Craig Tiller | 0e72ede | 2015-11-19 07:48:53 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 264 | static void ref_md_locked(mdtab_shard *shard, |
| 265 | internal_metadata *md DEBUG_ARGS) { |
Craig Tiller | 1a65a23 | 2015-07-06 10:22:32 -0700 | [diff] [blame] | 266 | #ifdef GRPC_METADATA_REFCOUNT_DEBUG |
| 267 | gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, |
| 268 | "ELM REF:%p:%d->%d: '%s' = '%s'", md, |
| 269 | gpr_atm_no_barrier_load(&md->refcnt), |
| 270 | gpr_atm_no_barrier_load(&md->refcnt) + 1, |
| 271 | grpc_mdstr_as_c_string((grpc_mdstr *)md->key), |
| 272 | grpc_mdstr_as_c_string((grpc_mdstr *)md->value)); |
| 273 | #endif |
Craig Tiller | b8e8267 | 2015-10-10 13:52:47 -0700 | [diff] [blame] | 274 | if (0 == gpr_atm_no_barrier_fetch_add(&md->refcnt, 2)) { |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 275 | shard->free--; |
Craig Tiller | b8e8267 | 2015-10-10 13:52:47 -0700 | [diff] [blame] | 276 | } else { |
| 277 | GPR_ASSERT(1 != gpr_atm_no_barrier_fetch_add(&md->refcnt, -1)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 281 | static void grow_strtab(strtab_shard *shard) { |
| 282 | size_t capacity = shard->capacity * 2; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 283 | size_t i; |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 284 | internal_string **strtab; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 285 | internal_string *s, *next; |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 286 | |
| 287 | GPR_TIMER_BEGIN("grow_strtab", 0); |
| 288 | |
| 289 | strtab = gpr_malloc(sizeof(internal_string *) * capacity); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 290 | memset(strtab, 0, sizeof(internal_string *) * capacity); |
| 291 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 292 | for (i = 0; i < shard->capacity; i++) { |
| 293 | for (s = shard->strs[i]; s; s = next) { |
| 294 | size_t idx = TABLE_IDX(s->hash, LOG2_STRTAB_SHARD_COUNT, capacity); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 295 | next = s->bucket_next; |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 296 | s->bucket_next = strtab[idx]; |
| 297 | strtab[idx] = s; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 301 | gpr_free(shard->strs); |
| 302 | shard->strs = strtab; |
| 303 | shard->capacity = capacity; |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 304 | |
| 305 | GPR_TIMER_END("grow_strtab", 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 306 | } |
| 307 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 308 | static void internal_destroy_string(strtab_shard *shard, internal_string *is) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 309 | internal_string **prev_next; |
| 310 | internal_string *cur; |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 311 | GPR_TIMER_BEGIN("internal_destroy_string", 0); |
ctiller | 430c499 | 2014-12-11 09:15:41 -0800 | [diff] [blame] | 312 | if (is->has_base64_and_huffman_encoded) { |
| 313 | gpr_slice_unref(is->base64_and_huffman); |
| 314 | } |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 315 | for (prev_next = &shard->strs[TABLE_IDX(is->hash, LOG2_STRTAB_SHARD_COUNT, |
| 316 | shard->capacity)], |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 317 | cur = *prev_next; |
| 318 | cur != is; prev_next = &cur->bucket_next, cur = cur->bucket_next) |
| 319 | ; |
| 320 | *prev_next = cur->bucket_next; |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 321 | shard->count--; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 322 | gpr_free(is); |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 323 | GPR_TIMER_END("internal_destroy_string", 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 324 | } |
| 325 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 326 | static void slice_ref(void *p) { |
| 327 | internal_string *is = |
| 328 | (internal_string *)((char *)p - offsetof(internal_string, refcount)); |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 329 | GRPC_MDSTR_REF((grpc_mdstr *)(is)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | static void slice_unref(void *p) { |
| 333 | internal_string *is = |
| 334 | (internal_string *)((char *)p - offsetof(internal_string, refcount)); |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 335 | GRPC_MDSTR_UNREF((grpc_mdstr *)(is)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 336 | } |
| 337 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 338 | grpc_mdstr *grpc_mdstr_from_string(const char *str) { |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 339 | return grpc_mdstr_from_buffer((const uint8_t *)str, strlen(str)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 340 | } |
| 341 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 342 | grpc_mdstr *grpc_mdstr_from_slice(gpr_slice slice) { |
| 343 | grpc_mdstr *result = grpc_mdstr_from_buffer(GPR_SLICE_START_PTR(slice), |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 344 | GPR_SLICE_LENGTH(slice)); |
| 345 | gpr_slice_unref(slice); |
| 346 | return result; |
| 347 | } |
| 348 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 349 | grpc_mdstr *grpc_mdstr_from_buffer(const uint8_t *buf, size_t length) { |
| 350 | uint32_t hash = gpr_murmur_hash3(buf, length, g_hash_seed); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 351 | internal_string *s; |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 352 | strtab_shard *shard = |
| 353 | &g_strtab_shard[SHARD_IDX(hash, LOG2_STRTAB_SHARD_COUNT)]; |
Craig Tiller | 0e72ede | 2015-11-19 07:48:53 -0800 | [diff] [blame] | 354 | size_t i; |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 355 | size_t idx; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 356 | |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 357 | GPR_TIMER_BEGIN("grpc_mdstr_from_buffer", 0); |
Craig Tiller | 0e72ede | 2015-11-19 07:48:53 -0800 | [diff] [blame] | 358 | |
| 359 | /* search for a static string */ |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 360 | for (i = 0; i <= g_static_strtab_maxprobe; i++) { |
| 361 | grpc_mdstr *ss; |
| 362 | idx = (hash + i) % GPR_ARRAY_SIZE(g_static_strtab); |
| 363 | ss = g_static_strtab[idx]; |
| 364 | if (ss == NULL) break; |
| 365 | if (ss->hash == hash && GPR_SLICE_LENGTH(ss->slice) == length && |
| 366 | 0 == memcmp(buf, GPR_SLICE_START_PTR(ss->slice), length)) { |
Craig Tiller | 07718b8 | 2015-11-21 14:06:45 -0800 | [diff] [blame] | 367 | GPR_TIMER_END("grpc_mdstr_from_buffer", 0); |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 368 | return ss; |
Craig Tiller | 0e72ede | 2015-11-19 07:48:53 -0800 | [diff] [blame] | 369 | } |
| 370 | } |
| 371 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 372 | gpr_mu_lock(&shard->mu); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 373 | |
| 374 | /* search for an existing string */ |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 375 | idx = TABLE_IDX(hash, LOG2_STRTAB_SHARD_COUNT, shard->capacity); |
| 376 | for (s = shard->strs[idx]; s; s = s->bucket_next) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 377 | if (s->hash == hash && GPR_SLICE_LENGTH(s->slice) == length && |
| 378 | 0 == memcmp(buf, GPR_SLICE_START_PTR(s->slice), length)) { |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 379 | GRPC_MDSTR_REF((grpc_mdstr *)s); |
| 380 | gpr_mu_unlock(&shard->mu); |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 381 | GPR_TIMER_END("grpc_mdstr_from_buffer", 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 382 | return (grpc_mdstr *)s; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | /* not found: create a new string */ |
| 387 | if (length + 1 < GPR_SLICE_INLINED_SIZE) { |
| 388 | /* string data goes directly into the slice */ |
| 389 | s = gpr_malloc(sizeof(internal_string)); |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 390 | gpr_atm_rel_store(&s->refcnt, 2); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 391 | s->slice.refcount = NULL; |
| 392 | memcpy(s->slice.data.inlined.bytes, buf, length); |
| 393 | s->slice.data.inlined.bytes[length] = 0; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 394 | s->slice.data.inlined.length = (uint8_t)length; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 395 | } else { |
| 396 | /* string data goes after the internal_string header, and we +1 for null |
| 397 | terminator */ |
| 398 | s = gpr_malloc(sizeof(internal_string) + length + 1); |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 399 | gpr_atm_rel_store(&s->refcnt, 2); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 400 | s->refcount.ref = slice_ref; |
| 401 | s->refcount.unref = slice_unref; |
| 402 | s->slice.refcount = &s->refcount; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 403 | s->slice.data.refcounted.bytes = (uint8_t *)(s + 1); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 404 | s->slice.data.refcounted.length = length; |
| 405 | memcpy(s->slice.data.refcounted.bytes, buf, length); |
| 406 | /* add a null terminator for cheap c string conversion when desired */ |
| 407 | s->slice.data.refcounted.bytes[length] = 0; |
| 408 | } |
ctiller | 430c499 | 2014-12-11 09:15:41 -0800 | [diff] [blame] | 409 | s->has_base64_and_huffman_encoded = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 410 | s->hash = hash; |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 411 | s->bucket_next = shard->strs[idx]; |
| 412 | shard->strs[idx] = s; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 413 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 414 | shard->count++; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 415 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 416 | if (shard->count > shard->capacity * 2) { |
| 417 | grow_strtab(shard); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 418 | } |
| 419 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 420 | gpr_mu_unlock(&shard->mu); |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 421 | GPR_TIMER_END("grpc_mdstr_from_buffer", 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 422 | |
| 423 | return (grpc_mdstr *)s; |
| 424 | } |
| 425 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 426 | static void gc_mdtab(mdtab_shard *shard) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 427 | size_t i; |
| 428 | internal_metadata **prev_next; |
| 429 | internal_metadata *md, *next; |
| 430 | |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 431 | GPR_TIMER_BEGIN("gc_mdtab", 0); |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 432 | for (i = 0; i < shard->capacity; i++) { |
| 433 | prev_next = &shard->elems[i]; |
| 434 | for (md = shard->elems[i]; md; md = next) { |
Craig Tiller | 8344daa | 2015-10-09 18:10:57 -0700 | [diff] [blame] | 435 | void *user_data = (void *)gpr_atm_no_barrier_load(&md->user_data); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 436 | next = md->bucket_next; |
Craig Tiller | 9fa41b9 | 2015-04-10 15:08:03 -0700 | [diff] [blame] | 437 | if (gpr_atm_acq_load(&md->refcnt) == 0) { |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 438 | GRPC_MDSTR_UNREF((grpc_mdstr *)md->key); |
| 439 | GRPC_MDSTR_UNREF((grpc_mdstr *)md->value); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 440 | if (md->user_data) { |
Craig Tiller | 8344daa | 2015-10-09 18:10:57 -0700 | [diff] [blame] | 441 | ((destroy_user_data_func)gpr_atm_no_barrier_load( |
| 442 | &md->destroy_user_data))(user_data); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 443 | } |
| 444 | gpr_free(md); |
| 445 | *prev_next = next; |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 446 | shard->free--; |
| 447 | shard->count--; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 448 | } else { |
| 449 | prev_next = &md->bucket_next; |
| 450 | } |
| 451 | } |
| 452 | } |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 453 | GPR_TIMER_END("gc_mdtab", 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 454 | } |
| 455 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 456 | static void grow_mdtab(mdtab_shard *shard) { |
| 457 | size_t capacity = shard->capacity * 2; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 458 | size_t i; |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 459 | internal_metadata **mdtab; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 460 | internal_metadata *md, *next; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 461 | uint32_t hash; |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 462 | |
| 463 | GPR_TIMER_BEGIN("grow_mdtab", 0); |
| 464 | |
| 465 | mdtab = gpr_malloc(sizeof(internal_metadata *) * capacity); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 466 | memset(mdtab, 0, sizeof(internal_metadata *) * capacity); |
| 467 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 468 | for (i = 0; i < shard->capacity; i++) { |
| 469 | for (md = shard->elems[i]; md; md = next) { |
| 470 | size_t idx; |
ctiller | fb93d19 | 2014-12-15 10:40:05 -0800 | [diff] [blame] | 471 | hash = GRPC_MDSTR_KV_HASH(md->key->hash, md->value->hash); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 472 | next = md->bucket_next; |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 473 | idx = TABLE_IDX(hash, LOG2_MDTAB_SHARD_COUNT, capacity); |
| 474 | md->bucket_next = mdtab[idx]; |
| 475 | mdtab[idx] = md; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 476 | } |
| 477 | } |
| 478 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 479 | gpr_free(shard->elems); |
| 480 | shard->elems = mdtab; |
| 481 | shard->capacity = capacity; |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 482 | |
| 483 | GPR_TIMER_END("grow_mdtab", 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 484 | } |
| 485 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 486 | static void rehash_mdtab(mdtab_shard *shard) { |
| 487 | if (shard->free > shard->capacity / 4) { |
| 488 | gc_mdtab(shard); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 489 | } else { |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 490 | grow_mdtab(shard); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 494 | grpc_mdelem *grpc_mdelem_from_metadata_strings(grpc_mdstr *mkey, |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 495 | grpc_mdstr *mvalue) { |
| 496 | internal_string *key = (internal_string *)mkey; |
| 497 | internal_string *value = (internal_string *)mvalue; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 498 | uint32_t hash = GRPC_MDSTR_KV_HASH(mkey->hash, mvalue->hash); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 499 | internal_metadata *md; |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 500 | mdtab_shard *shard = &g_mdtab_shard[SHARD_IDX(hash, LOG2_MDTAB_SHARD_COUNT)]; |
Craig Tiller | 0e72ede | 2015-11-19 07:48:53 -0800 | [diff] [blame] | 501 | size_t i; |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 502 | size_t idx; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 503 | |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 504 | GPR_TIMER_BEGIN("grpc_mdelem_from_metadata_strings", 0); |
| 505 | |
Craig Tiller | 0e72ede | 2015-11-19 07:48:53 -0800 | [diff] [blame] | 506 | if (is_mdstr_static(mkey) && is_mdstr_static(mvalue)) { |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 507 | for (i = 0; i <= g_static_mdtab_maxprobe; i++) { |
| 508 | grpc_mdelem *smd; |
| 509 | idx = (hash + i) % GPR_ARRAY_SIZE(g_static_mdtab); |
| 510 | smd = g_static_mdtab[idx]; |
| 511 | if (smd == NULL) break; |
| 512 | if (smd->key == mkey && smd->value == mvalue) { |
Craig Tiller | 07718b8 | 2015-11-21 14:06:45 -0800 | [diff] [blame] | 513 | GPR_TIMER_END("grpc_mdelem_from_metadata_strings", 0); |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 514 | return smd; |
Craig Tiller | 0e72ede | 2015-11-19 07:48:53 -0800 | [diff] [blame] | 515 | } |
| 516 | } |
| 517 | } |
| 518 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 519 | gpr_mu_lock(&shard->mu); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 520 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 521 | idx = TABLE_IDX(hash, LOG2_MDTAB_SHARD_COUNT, shard->capacity); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 522 | /* search for an existing pair */ |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 523 | for (md = shard->elems[idx]; md; md = md->bucket_next) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 524 | if (md->key == key && md->value == value) { |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 525 | REF_MD_LOCKED(shard, md); |
| 526 | GRPC_MDSTR_UNREF((grpc_mdstr *)key); |
| 527 | GRPC_MDSTR_UNREF((grpc_mdstr *)value); |
| 528 | gpr_mu_unlock(&shard->mu); |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 529 | GPR_TIMER_END("grpc_mdelem_from_metadata_strings", 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 530 | return (grpc_mdelem *)md; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | /* not found: create a new pair */ |
| 535 | md = gpr_malloc(sizeof(internal_metadata)); |
Craig Tiller | 63bda56 | 2015-10-09 17:40:19 -0700 | [diff] [blame] | 536 | gpr_atm_rel_store(&md->refcnt, 2); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 537 | md->key = key; |
| 538 | md->value = value; |
Craig Tiller | 8344daa | 2015-10-09 18:10:57 -0700 | [diff] [blame] | 539 | md->user_data = 0; |
| 540 | md->destroy_user_data = 0; |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 541 | md->bucket_next = shard->elems[idx]; |
| 542 | shard->elems[idx] = md; |
Craig Tiller | 8390153 | 2015-07-10 14:02:45 -0700 | [diff] [blame] | 543 | gpr_mu_init(&md->mu_user_data); |
Craig Tiller | 1a65a23 | 2015-07-06 10:22:32 -0700 | [diff] [blame] | 544 | #ifdef GRPC_METADATA_REFCOUNT_DEBUG |
| 545 | gpr_log(GPR_DEBUG, "ELM NEW:%p:%d: '%s' = '%s'", md, |
| 546 | gpr_atm_no_barrier_load(&md->refcnt), |
| 547 | grpc_mdstr_as_c_string((grpc_mdstr *)md->key), |
| 548 | grpc_mdstr_as_c_string((grpc_mdstr *)md->value)); |
| 549 | #endif |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 550 | shard->count++; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 551 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 552 | if (shard->count > shard->capacity * 2) { |
| 553 | rehash_mdtab(shard); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 554 | } |
| 555 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 556 | gpr_mu_unlock(&shard->mu); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 557 | |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 558 | GPR_TIMER_END("grpc_mdelem_from_metadata_strings", 0); |
| 559 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 560 | return (grpc_mdelem *)md; |
| 561 | } |
| 562 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 563 | grpc_mdelem *grpc_mdelem_from_strings(const char *key, const char *value) { |
| 564 | return grpc_mdelem_from_metadata_strings(grpc_mdstr_from_string(key), |
| 565 | grpc_mdstr_from_string(value)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 566 | } |
| 567 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 568 | grpc_mdelem *grpc_mdelem_from_slices(gpr_slice key, gpr_slice value) { |
| 569 | return grpc_mdelem_from_metadata_strings(grpc_mdstr_from_slice(key), |
| 570 | grpc_mdstr_from_slice(value)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 571 | } |
| 572 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 573 | grpc_mdelem *grpc_mdelem_from_string_and_buffer(const char *key, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 574 | const uint8_t *value, |
Craig Tiller | 4dbdd6a | 2015-09-25 15:12:16 -0700 | [diff] [blame] | 575 | size_t value_length) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 576 | return grpc_mdelem_from_metadata_strings( |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 577 | grpc_mdstr_from_string(key), grpc_mdstr_from_buffer(value, value_length)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 578 | } |
| 579 | |
Craig Tiller | 1a65a23 | 2015-07-06 10:22:32 -0700 | [diff] [blame] | 580 | grpc_mdelem *grpc_mdelem_ref(grpc_mdelem *gmd DEBUG_ARGS) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 581 | internal_metadata *md = (internal_metadata *)gmd; |
Craig Tiller | 0e72ede | 2015-11-19 07:48:53 -0800 | [diff] [blame] | 582 | if (is_mdelem_static(gmd)) return gmd; |
Craig Tiller | 1a65a23 | 2015-07-06 10:22:32 -0700 | [diff] [blame] | 583 | #ifdef GRPC_METADATA_REFCOUNT_DEBUG |
| 584 | gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, |
| 585 | "ELM REF:%p:%d->%d: '%s' = '%s'", md, |
| 586 | gpr_atm_no_barrier_load(&md->refcnt), |
| 587 | gpr_atm_no_barrier_load(&md->refcnt) + 1, |
| 588 | grpc_mdstr_as_c_string((grpc_mdstr *)md->key), |
| 589 | grpc_mdstr_as_c_string((grpc_mdstr *)md->value)); |
| 590 | #endif |
Craig Tiller | 9fa41b9 | 2015-04-10 15:08:03 -0700 | [diff] [blame] | 591 | /* we can assume the ref count is >= 1 as the application is calling |
| 592 | this function - meaning that no adjustment to mdtab_free is necessary, |
| 593 | simplifying the logic here to be just an atomic increment */ |
| 594 | /* use C assert to have this removed in opt builds */ |
Craig Tiller | b8e8267 | 2015-10-10 13:52:47 -0700 | [diff] [blame] | 595 | assert(gpr_atm_no_barrier_load(&md->refcnt) >= 2); |
Craig Tiller | 9fa41b9 | 2015-04-10 15:08:03 -0700 | [diff] [blame] | 596 | gpr_atm_no_barrier_fetch_add(&md->refcnt, 1); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 597 | return gmd; |
| 598 | } |
| 599 | |
Craig Tiller | 1a65a23 | 2015-07-06 10:22:32 -0700 | [diff] [blame] | 600 | void grpc_mdelem_unref(grpc_mdelem *gmd DEBUG_ARGS) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 601 | internal_metadata *md = (internal_metadata *)gmd; |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 602 | if (!md) return; |
Craig Tiller | 0e72ede | 2015-11-19 07:48:53 -0800 | [diff] [blame] | 603 | if (is_mdelem_static(gmd)) return; |
Craig Tiller | 1a65a23 | 2015-07-06 10:22:32 -0700 | [diff] [blame] | 604 | #ifdef GRPC_METADATA_REFCOUNT_DEBUG |
| 605 | gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, |
| 606 | "ELM UNREF:%p:%d->%d: '%s' = '%s'", md, |
| 607 | gpr_atm_no_barrier_load(&md->refcnt), |
| 608 | gpr_atm_no_barrier_load(&md->refcnt) - 1, |
| 609 | grpc_mdstr_as_c_string((grpc_mdstr *)md->key), |
| 610 | grpc_mdstr_as_c_string((grpc_mdstr *)md->value)); |
| 611 | #endif |
Craig Tiller | 63bda56 | 2015-10-09 17:40:19 -0700 | [diff] [blame] | 612 | if (2 == gpr_atm_full_fetch_add(&md->refcnt, -1)) { |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 613 | uint32_t hash = GRPC_MDSTR_KV_HASH(md->key->hash, md->value->hash); |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 614 | mdtab_shard *shard = |
| 615 | &g_mdtab_shard[SHARD_IDX(hash, LOG2_MDTAB_SHARD_COUNT)]; |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 616 | GPR_TIMER_BEGIN("grpc_mdelem_unref.to_zero", 0); |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 617 | gpr_mu_lock(&shard->mu); |
Craig Tiller | b8e8267 | 2015-10-10 13:52:47 -0700 | [diff] [blame] | 618 | if (1 == gpr_atm_no_barrier_load(&md->refcnt)) { |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 619 | shard->free++; |
Craig Tiller | b8e8267 | 2015-10-10 13:52:47 -0700 | [diff] [blame] | 620 | gpr_atm_no_barrier_store(&md->refcnt, 0); |
| 621 | } |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 622 | gpr_mu_unlock(&shard->mu); |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 623 | GPR_TIMER_END("grpc_mdelem_unref.to_zero", 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 624 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | const char *grpc_mdstr_as_c_string(grpc_mdstr *s) { |
| 628 | return (const char *)GPR_SLICE_START_PTR(s->slice); |
| 629 | } |
| 630 | |
Craig Tiller | 1a65a23 | 2015-07-06 10:22:32 -0700 | [diff] [blame] | 631 | grpc_mdstr *grpc_mdstr_ref(grpc_mdstr *gs DEBUG_ARGS) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 632 | internal_string *s = (internal_string *)gs; |
Craig Tiller | 0e72ede | 2015-11-19 07:48:53 -0800 | [diff] [blame] | 633 | if (is_mdstr_static(gs)) return gs; |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 634 | GPR_ASSERT(gpr_atm_full_fetch_add(&s->refcnt, 1) != 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 635 | return gs; |
| 636 | } |
| 637 | |
Craig Tiller | 1a65a23 | 2015-07-06 10:22:32 -0700 | [diff] [blame] | 638 | void grpc_mdstr_unref(grpc_mdstr *gs DEBUG_ARGS) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 639 | internal_string *s = (internal_string *)gs; |
Craig Tiller | 0e72ede | 2015-11-19 07:48:53 -0800 | [diff] [blame] | 640 | if (is_mdstr_static(gs)) return; |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 641 | if (2 == gpr_atm_full_fetch_add(&s->refcnt, -1)) { |
| 642 | strtab_shard *shard = |
| 643 | &g_strtab_shard[SHARD_IDX(s->hash, LOG2_STRTAB_SHARD_COUNT)]; |
| 644 | gpr_mu_lock(&shard->mu); |
| 645 | if (1 == gpr_atm_no_barrier_load(&s->refcnt)) { |
| 646 | internal_destroy_string(shard, s); |
| 647 | } |
| 648 | gpr_mu_unlock(&shard->mu); |
| 649 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 650 | } |
| 651 | |
Craig Tiller | 8344daa | 2015-10-09 18:10:57 -0700 | [diff] [blame] | 652 | void *grpc_mdelem_get_user_data(grpc_mdelem *md, void (*destroy_func)(void *)) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 653 | internal_metadata *im = (internal_metadata *)md; |
Craig Tiller | 8390153 | 2015-07-10 14:02:45 -0700 | [diff] [blame] | 654 | void *result; |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 655 | if (is_mdelem_static(md)) { |
| 656 | return (void *)grpc_static_mdelem_user_data[md - grpc_static_mdelem_table]; |
| 657 | } |
Craig Tiller | 8344daa | 2015-10-09 18:10:57 -0700 | [diff] [blame] | 658 | if (gpr_atm_acq_load(&im->destroy_user_data) == (gpr_atm)destroy_func) { |
| 659 | return (void *)gpr_atm_no_barrier_load(&im->user_data); |
| 660 | } else { |
| 661 | return NULL; |
| 662 | } |
Craig Tiller | 8390153 | 2015-07-10 14:02:45 -0700 | [diff] [blame] | 663 | return result; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | void grpc_mdelem_set_user_data(grpc_mdelem *md, void (*destroy_func)(void *), |
| 667 | void *user_data) { |
| 668 | internal_metadata *im = (internal_metadata *)md; |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 669 | GPR_ASSERT(!is_mdelem_static(md)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 670 | GPR_ASSERT((user_data == NULL) == (destroy_func == NULL)); |
Craig Tiller | 8390153 | 2015-07-10 14:02:45 -0700 | [diff] [blame] | 671 | gpr_mu_lock(&im->mu_user_data); |
Craig Tiller | 8344daa | 2015-10-09 18:10:57 -0700 | [diff] [blame] | 672 | if (gpr_atm_no_barrier_load(&im->destroy_user_data)) { |
Craig Tiller | 8390153 | 2015-07-10 14:02:45 -0700 | [diff] [blame] | 673 | /* user data can only be set once */ |
| 674 | gpr_mu_unlock(&im->mu_user_data); |
| 675 | if (destroy_func != NULL) { |
| 676 | destroy_func(user_data); |
| 677 | } |
| 678 | return; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 679 | } |
Craig Tiller | 8344daa | 2015-10-09 18:10:57 -0700 | [diff] [blame] | 680 | gpr_atm_no_barrier_store(&im->user_data, (gpr_atm)user_data); |
| 681 | gpr_atm_rel_store(&im->destroy_user_data, (gpr_atm)destroy_func); |
Craig Tiller | 8390153 | 2015-07-10 14:02:45 -0700 | [diff] [blame] | 682 | gpr_mu_unlock(&im->mu_user_data); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 683 | } |
ctiller | 430c499 | 2014-12-11 09:15:41 -0800 | [diff] [blame] | 684 | |
| 685 | gpr_slice grpc_mdstr_as_base64_encoded_and_huffman_compressed(grpc_mdstr *gs) { |
| 686 | internal_string *s = (internal_string *)gs; |
| 687 | gpr_slice slice; |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 688 | strtab_shard *shard = |
| 689 | &g_strtab_shard[SHARD_IDX(s->hash, LOG2_STRTAB_SHARD_COUNT)]; |
| 690 | gpr_mu_lock(&shard->mu); |
ctiller | 430c499 | 2014-12-11 09:15:41 -0800 | [diff] [blame] | 691 | if (!s->has_base64_and_huffman_encoded) { |
| 692 | s->base64_and_huffman = |
| 693 | grpc_chttp2_base64_encode_and_huffman_compress(s->slice); |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 694 | s->has_base64_and_huffman_encoded = 1; |
ctiller | 430c499 | 2014-12-11 09:15:41 -0800 | [diff] [blame] | 695 | } |
| 696 | slice = s->base64_and_huffman; |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 697 | gpr_mu_unlock(&shard->mu); |
ctiller | 430c499 | 2014-12-11 09:15:41 -0800 | [diff] [blame] | 698 | return slice; |
David Garcia Quintas | 2bf574f | 2016-01-14 15:27:08 -0800 | [diff] [blame] | 699 | } |