OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015-2016, Google Inc. |
| 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 |
| 34 #include <grpc/grpc.h> |
| 35 #include "src/core/channel/channel_args.h" |
| 36 #include "src/core/support/string.h" |
| 37 |
| 38 #include <grpc/census.h> |
| 39 #include <grpc/support/alloc.h> |
| 40 #include <grpc/support/log.h> |
| 41 #include <grpc/support/string_util.h> |
| 42 #include <grpc/support/useful.h> |
| 43 |
| 44 #include <string.h> |
| 45 |
| 46 static grpc_arg copy_arg(const grpc_arg *src) { |
| 47 grpc_arg dst; |
| 48 dst.type = src->type; |
| 49 dst.key = gpr_strdup(src->key); |
| 50 switch (dst.type) { |
| 51 case GRPC_ARG_STRING: |
| 52 dst.value.string = gpr_strdup(src->value.string); |
| 53 break; |
| 54 case GRPC_ARG_INTEGER: |
| 55 dst.value.integer = src->value.integer; |
| 56 break; |
| 57 case GRPC_ARG_POINTER: |
| 58 dst.value.pointer = src->value.pointer; |
| 59 dst.value.pointer.p = |
| 60 src->value.pointer.vtable->copy(src->value.pointer.p); |
| 61 break; |
| 62 } |
| 63 return dst; |
| 64 } |
| 65 |
| 66 grpc_channel_args *grpc_channel_args_copy_and_add(const grpc_channel_args *src, |
| 67 const grpc_arg *to_add, |
| 68 size_t num_to_add) { |
| 69 grpc_channel_args *dst = gpr_malloc(sizeof(grpc_channel_args)); |
| 70 size_t i; |
| 71 size_t src_num_args = (src == NULL) ? 0 : src->num_args; |
| 72 if (!src && !to_add) { |
| 73 dst->num_args = 0; |
| 74 dst->args = NULL; |
| 75 return dst; |
| 76 } |
| 77 dst->num_args = src_num_args + num_to_add; |
| 78 dst->args = gpr_malloc(sizeof(grpc_arg) * dst->num_args); |
| 79 for (i = 0; i < src_num_args; i++) { |
| 80 dst->args[i] = copy_arg(&src->args[i]); |
| 81 } |
| 82 for (i = 0; i < num_to_add; i++) { |
| 83 dst->args[i + src_num_args] = copy_arg(&to_add[i]); |
| 84 } |
| 85 return dst; |
| 86 } |
| 87 |
| 88 grpc_channel_args *grpc_channel_args_copy(const grpc_channel_args *src) { |
| 89 return grpc_channel_args_copy_and_add(src, NULL, 0); |
| 90 } |
| 91 |
| 92 grpc_channel_args *grpc_channel_args_merge(const grpc_channel_args *a, |
| 93 const grpc_channel_args *b) { |
| 94 return grpc_channel_args_copy_and_add(a, b->args, b->num_args); |
| 95 } |
| 96 |
| 97 static int cmp_arg(const grpc_arg *a, const grpc_arg *b) { |
| 98 int c = GPR_ICMP(a->type, b->type); |
| 99 if (c != 0) return c; |
| 100 c = strcmp(a->key, b->key); |
| 101 if (c != 0) return c; |
| 102 switch (a->type) { |
| 103 case GRPC_ARG_STRING: |
| 104 return strcmp(a->value.string, b->value.string); |
| 105 case GRPC_ARG_INTEGER: |
| 106 return GPR_ICMP(a->value.integer, b->value.integer); |
| 107 case GRPC_ARG_POINTER: |
| 108 c = GPR_ICMP(a->value.pointer.p, b->value.pointer.p); |
| 109 if (c != 0) { |
| 110 c = GPR_ICMP(a->value.pointer.vtable, b->value.pointer.vtable); |
| 111 if (c == 0) { |
| 112 c = a->value.pointer.vtable->cmp(a->value.pointer.p, |
| 113 b->value.pointer.p); |
| 114 } |
| 115 } |
| 116 return c; |
| 117 } |
| 118 GPR_UNREACHABLE_CODE(return 0); |
| 119 } |
| 120 |
| 121 /* stabilizing comparison function: since channel_args ordering matters for |
| 122 * keys with the same name, we need to preserve that ordering */ |
| 123 static int cmp_key_stable(const void *ap, const void *bp) { |
| 124 const grpc_arg *const *a = ap; |
| 125 const grpc_arg *const *b = bp; |
| 126 int c = strcmp((*a)->key, (*b)->key); |
| 127 if (c == 0) c = GPR_ICMP(*a, *b); |
| 128 return c; |
| 129 } |
| 130 |
| 131 grpc_channel_args *grpc_channel_args_normalize(const grpc_channel_args *a) { |
| 132 grpc_arg **args = gpr_malloc(sizeof(grpc_arg *) * a->num_args); |
| 133 for (size_t i = 0; i < a->num_args; i++) { |
| 134 args[i] = &a->args[i]; |
| 135 } |
| 136 qsort(args, a->num_args, sizeof(grpc_arg *), cmp_key_stable); |
| 137 |
| 138 grpc_channel_args *b = gpr_malloc(sizeof(grpc_channel_args)); |
| 139 b->num_args = a->num_args; |
| 140 b->args = gpr_malloc(sizeof(grpc_arg) * b->num_args); |
| 141 for (size_t i = 0; i < a->num_args; i++) { |
| 142 b->args[i] = copy_arg(args[i]); |
| 143 } |
| 144 |
| 145 gpr_free(args); |
| 146 return b; |
| 147 } |
| 148 |
| 149 void grpc_channel_args_destroy(grpc_channel_args *a) { |
| 150 size_t i; |
| 151 for (i = 0; i < a->num_args; i++) { |
| 152 switch (a->args[i].type) { |
| 153 case GRPC_ARG_STRING: |
| 154 gpr_free(a->args[i].value.string); |
| 155 break; |
| 156 case GRPC_ARG_INTEGER: |
| 157 break; |
| 158 case GRPC_ARG_POINTER: |
| 159 a->args[i].value.pointer.vtable->destroy(a->args[i].value.pointer.p); |
| 160 break; |
| 161 } |
| 162 gpr_free(a->args[i].key); |
| 163 } |
| 164 gpr_free(a->args); |
| 165 gpr_free(a); |
| 166 } |
| 167 |
| 168 int grpc_channel_args_is_census_enabled(const grpc_channel_args *a) { |
| 169 size_t i; |
| 170 if (a == NULL) return 0; |
| 171 for (i = 0; i < a->num_args; i++) { |
| 172 if (0 == strcmp(a->args[i].key, GRPC_ARG_ENABLE_CENSUS)) { |
| 173 return a->args[i].value.integer != 0 && census_enabled(); |
| 174 } |
| 175 } |
| 176 return census_enabled(); |
| 177 } |
| 178 |
| 179 grpc_compression_algorithm grpc_channel_args_get_compression_algorithm( |
| 180 const grpc_channel_args *a) { |
| 181 size_t i; |
| 182 if (a == NULL) return 0; |
| 183 for (i = 0; i < a->num_args; ++i) { |
| 184 if (a->args[i].type == GRPC_ARG_INTEGER && |
| 185 !strcmp(GRPC_COMPRESSION_ALGORITHM_ARG, a->args[i].key)) { |
| 186 return (grpc_compression_algorithm)a->args[i].value.integer; |
| 187 break; |
| 188 } |
| 189 } |
| 190 return GRPC_COMPRESS_NONE; |
| 191 } |
| 192 |
| 193 grpc_channel_args *grpc_channel_args_set_compression_algorithm( |
| 194 grpc_channel_args *a, grpc_compression_algorithm algorithm) { |
| 195 grpc_arg tmp; |
| 196 tmp.type = GRPC_ARG_INTEGER; |
| 197 tmp.key = GRPC_COMPRESSION_ALGORITHM_ARG; |
| 198 tmp.value.integer = algorithm; |
| 199 return grpc_channel_args_copy_and_add(a, &tmp, 1); |
| 200 } |
| 201 |
| 202 /** Returns 1 if the argument for compression algorithm's enabled states bitset |
| 203 * was found in \a a, returning the arg's value in \a states. Otherwise, returns |
| 204 * 0. */ |
| 205 static int find_compression_algorithm_states_bitset(const grpc_channel_args *a, |
| 206 int **states_arg) { |
| 207 if (a != NULL) { |
| 208 size_t i; |
| 209 for (i = 0; i < a->num_args; ++i) { |
| 210 if (a->args[i].type == GRPC_ARG_INTEGER && |
| 211 !strcmp(GRPC_COMPRESSION_ALGORITHM_STATE_ARG, a->args[i].key)) { |
| 212 *states_arg = &a->args[i].value.integer; |
| 213 return 1; /* GPR_TRUE */ |
| 214 } |
| 215 } |
| 216 } |
| 217 return 0; /* GPR_FALSE */ |
| 218 } |
| 219 |
| 220 grpc_channel_args *grpc_channel_args_compression_algorithm_set_state( |
| 221 grpc_channel_args **a, grpc_compression_algorithm algorithm, int state) { |
| 222 int *states_arg; |
| 223 grpc_channel_args *result = *a; |
| 224 const int states_arg_found = |
| 225 find_compression_algorithm_states_bitset(*a, &states_arg); |
| 226 |
| 227 if (states_arg_found) { |
| 228 if (state != 0) { |
| 229 GPR_BITSET((unsigned *)states_arg, algorithm); |
| 230 } else { |
| 231 GPR_BITCLEAR((unsigned *)states_arg, algorithm); |
| 232 } |
| 233 } else { |
| 234 /* create a new arg */ |
| 235 grpc_arg tmp; |
| 236 tmp.type = GRPC_ARG_INTEGER; |
| 237 tmp.key = GRPC_COMPRESSION_ALGORITHM_STATE_ARG; |
| 238 /* all enabled by default */ |
| 239 tmp.value.integer = (1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1; |
| 240 if (state != 0) { |
| 241 GPR_BITSET((unsigned *)&tmp.value.integer, algorithm); |
| 242 } else { |
| 243 GPR_BITCLEAR((unsigned *)&tmp.value.integer, algorithm); |
| 244 } |
| 245 result = grpc_channel_args_copy_and_add(*a, &tmp, 1); |
| 246 grpc_channel_args_destroy(*a); |
| 247 *a = result; |
| 248 } |
| 249 return result; |
| 250 } |
| 251 |
| 252 int grpc_channel_args_compression_algorithm_get_states( |
| 253 const grpc_channel_args *a) { |
| 254 int *states_arg; |
| 255 if (find_compression_algorithm_states_bitset(a, &states_arg)) { |
| 256 return *states_arg; |
| 257 } else { |
| 258 return (1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1; /* All algs. enabled */ |
| 259 } |
| 260 } |
| 261 |
| 262 int grpc_channel_args_compare(const grpc_channel_args *a, |
| 263 const grpc_channel_args *b) { |
| 264 int c = GPR_ICMP(a->num_args, b->num_args); |
| 265 if (c != 0) return c; |
| 266 for (size_t i = 0; i < a->num_args; i++) { |
| 267 c = cmp_arg(&a->args[i], &b->args[i]); |
| 268 if (c != 0) return c; |
| 269 } |
| 270 return 0; |
| 271 } |
OLD | NEW |