LCOV - code coverage report
Current view: top level - src/core/channel - channel_args.c (source / functions) Hit Total Coverage
Test: tmp.zDYK9MVh93 Lines: 92 93 98.9 %
Date: 2015-10-10 Functions: 11 11 100.0 %

          Line data    Source code
       1             : /*
       2             :  *
       3             :  * Copyright 2015, 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/support/alloc.h>
      39             : #include <grpc/support/string_util.h>
      40             : #include <grpc/support/useful.h>
      41             : 
      42             : #include <string.h>
      43             : 
      44       10261 : static grpc_arg copy_arg(const grpc_arg *src) {
      45             :   grpc_arg dst;
      46       10261 :   dst.type = src->type;
      47       10261 :   dst.key = gpr_strdup(src->key);
      48       10261 :   switch (dst.type) {
      49             :     case GRPC_ARG_STRING:
      50        5289 :       dst.value.string = gpr_strdup(src->value.string);
      51        5289 :       break;
      52             :     case GRPC_ARG_INTEGER:
      53        2218 :       dst.value.integer = src->value.integer;
      54        2218 :       break;
      55             :     case GRPC_ARG_POINTER:
      56        2754 :       dst.value.pointer = src->value.pointer;
      57        5508 :       dst.value.pointer.p = src->value.pointer.copy
      58        2754 :                                 ? src->value.pointer.copy(src->value.pointer.p)
      59        5508 :                                 : src->value.pointer.p;
      60        2754 :       break;
      61             :   }
      62       10261 :   return dst;
      63             : }
      64             : 
      65        9236 : grpc_channel_args *grpc_channel_args_copy_and_add(const grpc_channel_args *src,
      66             :                                                   const grpc_arg *to_add,
      67             :                                                   size_t num_to_add) {
      68        9236 :   grpc_channel_args *dst = gpr_malloc(sizeof(grpc_channel_args));
      69             :   size_t i;
      70        9236 :   size_t src_num_args = (src == NULL) ? 0 : src->num_args;
      71        9236 :   if (!src && !to_add) {
      72        3358 :     dst->num_args = 0;
      73        3358 :     dst->args = NULL;
      74        3358 :     return dst;
      75             :   }
      76        5878 :   dst->num_args = src_num_args + num_to_add;
      77        5878 :   dst->args = gpr_malloc(sizeof(grpc_arg) * dst->num_args);
      78       11791 :   for (i = 0; i < src_num_args; i++) {
      79        5913 :     dst->args[i] = copy_arg(&src->args[i]);
      80             :   }
      81       10226 :   for (i = 0; i < num_to_add; i++) {
      82        4348 :     dst->args[i + src_num_args] = copy_arg(&to_add[i]);
      83             :   }
      84        5878 :   return dst;
      85             : }
      86             : 
      87        5492 : grpc_channel_args *grpc_channel_args_copy(const grpc_channel_args *src) {
      88        5492 :   return grpc_channel_args_copy_and_add(src, NULL, 0);
      89             : }
      90             : 
      91        1470 : grpc_channel_args *grpc_channel_args_merge(const grpc_channel_args *a,
      92             :                                            const grpc_channel_args *b) {
      93        1470 :   return grpc_channel_args_copy_and_add(a, b->args, b->num_args);
      94             : }
      95             : 
      96        9236 : void grpc_channel_args_destroy(grpc_channel_args *a) {
      97             :   size_t i;
      98       19497 :   for (i = 0; i < a->num_args; i++) {
      99       10261 :     switch (a->args[i].type) {
     100             :       case GRPC_ARG_STRING:
     101        5289 :         gpr_free(a->args[i].value.string);
     102        5289 :         break;
     103             :       case GRPC_ARG_INTEGER:
     104        2218 :         break;
     105             :       case GRPC_ARG_POINTER:
     106        2754 :         if (a->args[i].value.pointer.destroy) {
     107        2754 :           a->args[i].value.pointer.destroy(a->args[i].value.pointer.p);
     108             :         }
     109        2754 :         break;
     110             :     }
     111       10261 :     gpr_free(a->args[i].key);
     112             :   }
     113        9236 :   gpr_free(a->args);
     114        9236 :   gpr_free(a);
     115        9236 : }
     116             : 
     117        1761 : int grpc_channel_args_is_census_enabled(const grpc_channel_args *a) {
     118             :   size_t i;
     119        1761 :   if (a == NULL) return 0;
     120        1780 :   for (i = 0; i < a->num_args; i++) {
     121         983 :     if (0 == strcmp(a->args[i].key, GRPC_ARG_ENABLE_CENSUS)) {
     122          17 :       return a->args[i].value.integer != 0;
     123             :     }
     124             :   }
     125         797 :   return 0;
     126             : }
     127             : 
     128        3739 : grpc_compression_algorithm grpc_channel_args_get_compression_algorithm(
     129             :     const grpc_channel_args *a) {
     130             :   size_t i;
     131        3739 :   if (a == NULL) return 0;
     132        5247 :   for (i = 0; i < a->num_args; ++i) {
     133        4083 :     if (a->args[i].type == GRPC_ARG_INTEGER &&
     134         829 :         !strcmp(GRPC_COMPRESSION_ALGORITHM_ARG, a->args[i].key)) {
     135         478 :       return (grpc_compression_algorithm)a->args[i].value.integer;
     136             :       break;
     137             :     }
     138             :   }
     139        1993 :   return GRPC_COMPRESS_NONE;
     140             : }
     141             : 
     142         559 : grpc_channel_args *grpc_channel_args_set_compression_algorithm(
     143             :     grpc_channel_args *a, grpc_compression_algorithm algorithm) {
     144             :   grpc_arg tmp;
     145         559 :   tmp.type = GRPC_ARG_INTEGER;
     146         559 :   tmp.key = GRPC_COMPRESSION_ALGORITHM_ARG;
     147         559 :   tmp.value.integer = algorithm;
     148         559 :   return grpc_channel_args_copy_and_add(a, &tmp, 1);
     149             : }
     150             : 
     151             : /** Returns 1 if the argument for compression algorithm's enabled states bitset
     152             :  * was found in \a a, returning the arg's value in \a states. Otherwise, returns
     153             :  * 0. */
     154        3745 : static int find_compression_algorithm_states_bitset(const grpc_channel_args *a,
     155             :                                                     int **states_arg) {
     156        3745 :   if (a != NULL) {
     157             :     size_t i;
     158        5615 :     for (i = 0; i < a->num_args; ++i) {
     159        4165 :       if (a->args[i].type == GRPC_ARG_INTEGER &&
     160         857 :           !strcmp(GRPC_COMPRESSION_ALGORITHM_STATE_ARG, a->args[i].key)) {
     161         170 :         *states_arg = &a->args[i].value.integer;
     162         170 :         return 1; /* GPR_TRUE */
     163             :       }
     164             :     }
     165             :   }
     166        3575 :   return 0; /* GPR_FALSE */
     167             : }
     168             : 
     169           3 : grpc_channel_args *grpc_channel_args_compression_algorithm_set_state(
     170             :     grpc_channel_args **a, grpc_compression_algorithm algorithm, int state) {
     171             :   int *states_arg;
     172           3 :   grpc_channel_args *result = *a;
     173           3 :   const int states_arg_found =
     174           3 :       find_compression_algorithm_states_bitset(*a, &states_arg);
     175             : 
     176           3 :   if (states_arg_found) {
     177           2 :     if (state != 0) {
     178           1 :       GPR_BITSET((unsigned *)states_arg, algorithm);
     179             :     } else {
     180           1 :       GPR_BITCLEAR((unsigned *)states_arg, algorithm);
     181             :     }
     182             :   } else {
     183             :     /* create a new arg */
     184             :     grpc_arg tmp;
     185           1 :     tmp.type = GRPC_ARG_INTEGER;
     186           1 :     tmp.key = GRPC_COMPRESSION_ALGORITHM_STATE_ARG;
     187             :     /* all enabled by default */
     188           1 :     tmp.value.integer = (1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1;
     189           1 :     if (state != 0) {
     190           0 :       GPR_BITSET((unsigned *)&tmp.value.integer, algorithm);
     191             :     } else {
     192           1 :       GPR_BITCLEAR((unsigned *)&tmp.value.integer, algorithm);
     193             :     }
     194           1 :     result = grpc_channel_args_copy_and_add(*a, &tmp, 1);
     195           1 :     grpc_channel_args_destroy(*a);
     196           1 :     *a = result;
     197             :   }
     198           3 :   return result;
     199             : }
     200             : 
     201        3742 : int grpc_channel_args_compression_algorithm_get_states(
     202             :     const grpc_channel_args *a) {
     203             :   int *states_arg;
     204        3742 :   if (find_compression_algorithm_states_bitset(a, &states_arg)) {
     205         168 :     return *states_arg;
     206             :   } else {
     207        3574 :     return (1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1; /* All algs. enabled */
     208             :   }
     209             : }

Generated by: LCOV version 1.10