int main(int argc, char **argv) { int four[4]; int five[5]; uint32_t bitset = 0; grpc_test_init(argc, argv); GPR_ASSERT(GPR_MIN(1, 2) == 1); GPR_ASSERT(GPR_MAX(1, 2) == 2); GPR_ASSERT(GPR_MIN(2, 1) == 1); GPR_ASSERT(GPR_MAX(2, 1) == 2); GPR_ASSERT(GPR_CLAMP(1, 0, 2) == 1); GPR_ASSERT(GPR_CLAMP(0, 0, 2) == 0); GPR_ASSERT(GPR_CLAMP(2, 0, 2) == 2); GPR_ASSERT(GPR_CLAMP(-1, 0, 2) == 0); GPR_ASSERT(GPR_CLAMP(3, 0, 2) == 2); GPR_ASSERT(GPR_ROTL((uint32_t)0x80000001, 1) == 3); GPR_ASSERT(GPR_ROTR((uint32_t)0x80000001, 1) == 0xc0000000); GPR_ASSERT(GPR_ARRAY_SIZE(four) == 4); GPR_ASSERT(GPR_ARRAY_SIZE(five) == 5); GPR_ASSERT(GPR_BITCOUNT((1u << 31) - 1) == 31); GPR_ASSERT(GPR_BITCOUNT(1u << 3) == 1); GPR_ASSERT(GPR_BITCOUNT(0) == 0); GPR_ASSERT(GPR_BITSET(&bitset, 3) == 8); GPR_ASSERT(GPR_BITCOUNT(bitset) == 1); GPR_ASSERT(GPR_BITGET(bitset, 3) == 1); GPR_ASSERT(GPR_BITSET(&bitset, 1) == 10); GPR_ASSERT(GPR_BITCOUNT(bitset) == 2); GPR_ASSERT(GPR_BITCLEAR(&bitset, 3) == 2); GPR_ASSERT(GPR_BITCOUNT(bitset) == 1); GPR_ASSERT(GPR_BITGET(bitset, 3) == 0); return 0; }
grpc_channel_args *grpc_channel_args_compression_algorithm_set_state( grpc_channel_args **a, grpc_compression_algorithm algorithm, int state) { int *states_arg; grpc_channel_args *result = *a; const int states_arg_found = find_compression_algorithm_states_bitset(*a, &states_arg); if (states_arg_found) { if (state != 0) { GPR_BITSET((unsigned *)states_arg, algorithm); } else { GPR_BITCLEAR((unsigned *)states_arg, algorithm); } } else { /* create a new arg */ grpc_arg tmp; tmp.type = GRPC_ARG_INTEGER; tmp.key = GRPC_COMPRESSION_ALGORITHM_STATE_ARG; /* all enabled by default */ tmp.value.integer = (1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1; if (state != 0) { GPR_BITSET((unsigned *)&tmp.value.integer, algorithm); } else { GPR_BITCLEAR((unsigned *)&tmp.value.integer, algorithm); } result = grpc_channel_args_copy_and_add(*a, &tmp, 1); grpc_channel_args_destroy(*a); *a = result; } return result; }
static void set_encodings_accepted_by_peer(grpc_call *call, grpc_mdelem *mdel) { size_t i; grpc_compression_algorithm algorithm; gpr_slice_buffer accept_encoding_parts; gpr_slice accept_encoding_slice; void *accepted_user_data; accepted_user_data = grpc_mdelem_get_user_data(mdel, destroy_encodings_accepted_by_peer); if (accepted_user_data != NULL) { call->encodings_accepted_by_peer = (gpr_uint32)(((gpr_uintptr)accepted_user_data) - 1); return; } accept_encoding_slice = mdel->value->slice; gpr_slice_buffer_init(&accept_encoding_parts); gpr_slice_split(accept_encoding_slice, ",", &accept_encoding_parts); /* No need to zero call->encodings_accepted_by_peer: grpc_call_create already * zeroes the whole grpc_call */ /* Always support no compression */ GPR_BITSET(&call->encodings_accepted_by_peer, GRPC_COMPRESS_NONE); for (i = 0; i < accept_encoding_parts.count; i++) { const gpr_slice *accept_encoding_entry_slice = &accept_encoding_parts.slices[i]; if (grpc_compression_algorithm_parse( (const char *)GPR_SLICE_START_PTR(*accept_encoding_entry_slice), GPR_SLICE_LENGTH(*accept_encoding_entry_slice), &algorithm)) { GPR_BITSET(&call->encodings_accepted_by_peer, algorithm); } else { char *accept_encoding_entry_str = gpr_dump_slice(*accept_encoding_entry_slice, GPR_DUMP_ASCII); gpr_log(GPR_ERROR, "Invalid entry in accept encoding metadata: '%s'. Ignoring.", accept_encoding_entry_str); gpr_free(accept_encoding_entry_str); } } gpr_slice_buffer_destroy(&accept_encoding_parts); grpc_mdelem_set_user_data( mdel, destroy_encodings_accepted_by_peer, (void *)(((gpr_uintptr)call->encodings_accepted_by_peer) + 1)); }
grpc_channel_args *grpc_channel_args_stream_compression_algorithm_set_state( grpc_exec_ctx *exec_ctx, grpc_channel_args **a, grpc_stream_compression_algorithm algorithm, int state) { int *states_arg = NULL; grpc_channel_args *result = *a; const int states_arg_found = find_stream_compression_algorithm_states_bitset(*a, &states_arg); if (grpc_channel_args_get_stream_compression_algorithm(*a) == algorithm && state == 0) { const char *algo_name = NULL; GPR_ASSERT(grpc_stream_compression_algorithm_name(algorithm, &algo_name) != 0); gpr_log(GPR_ERROR, "Tried to disable default stream compression algorithm '%s'. The " "operation has been ignored.", algo_name); } else if (states_arg_found) { if (state != 0) { GPR_BITSET((unsigned *)states_arg, algorithm); } else if (algorithm != GRPC_STREAM_COMPRESS_NONE) { GPR_BITCLEAR((unsigned *)states_arg, algorithm); } } else { /* create a new arg */ grpc_arg tmp; tmp.type = GRPC_ARG_INTEGER; tmp.key = (char *)GRPC_STREAM_COMPRESSION_CHANNEL_ENABLED_ALGORITHMS_BITSET; /* all enabled by default */ tmp.value.integer = (1u << GRPC_STREAM_COMPRESS_ALGORITHMS_COUNT) - 1; if (state != 0) { GPR_BITSET((unsigned *)&tmp.value.integer, algorithm); } else if (algorithm != GRPC_STREAM_COMPRESS_NONE) { GPR_BITCLEAR((unsigned *)&tmp.value.integer, algorithm); } result = grpc_channel_args_copy_and_add(*a, &tmp, 1); grpc_channel_args_destroy(exec_ctx, *a); *a = result; } return result; }
void grpc_compression_options_enable_algorithm( grpc_compression_options *opts, grpc_compression_algorithm algorithm) { GPR_BITSET(&opts->enabled_algorithms_bitset, algorithm); }
static void test_compression_algorithm_for_level(void) { gpr_log(GPR_DEBUG, "test_compression_algorithm_for_level"); { /* accept only identity (aka none) */ uint32_t accepted_encodings = 0; GPR_BITSET(&accepted_encodings, GRPC_COMPRESS_NONE); /* always */ GPR_ASSERT(GRPC_COMPRESS_NONE == grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_NONE, accepted_encodings)); GPR_ASSERT(GRPC_COMPRESS_NONE == grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_LOW, accepted_encodings)); GPR_ASSERT(GRPC_COMPRESS_NONE == grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_MED, accepted_encodings)); GPR_ASSERT(GRPC_COMPRESS_NONE == grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_HIGH, accepted_encodings)); } { /* accept only gzip */ uint32_t accepted_encodings = 0; GPR_BITSET(&accepted_encodings, GRPC_COMPRESS_NONE); /* always */ GPR_BITSET(&accepted_encodings, GRPC_COMPRESS_GZIP); GPR_ASSERT(GRPC_COMPRESS_NONE == grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_NONE, accepted_encodings)); GPR_ASSERT(GRPC_COMPRESS_GZIP == grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_LOW, accepted_encodings)); GPR_ASSERT(GRPC_COMPRESS_GZIP == grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_MED, accepted_encodings)); GPR_ASSERT(GRPC_COMPRESS_GZIP == grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_HIGH, accepted_encodings)); } { /* accept only deflate */ uint32_t accepted_encodings = 0; GPR_BITSET(&accepted_encodings, GRPC_COMPRESS_NONE); /* always */ GPR_BITSET(&accepted_encodings, GRPC_COMPRESS_DEFLATE); GPR_ASSERT(GRPC_COMPRESS_NONE == grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_NONE, accepted_encodings)); GPR_ASSERT(GRPC_COMPRESS_DEFLATE == grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_LOW, accepted_encodings)); GPR_ASSERT(GRPC_COMPRESS_DEFLATE == grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_MED, accepted_encodings)); GPR_ASSERT(GRPC_COMPRESS_DEFLATE == grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_HIGH, accepted_encodings)); } { /* accept gzip and deflate */ uint32_t accepted_encodings = 0; GPR_BITSET(&accepted_encodings, GRPC_COMPRESS_NONE); /* always */ GPR_BITSET(&accepted_encodings, GRPC_COMPRESS_GZIP); GPR_BITSET(&accepted_encodings, GRPC_COMPRESS_DEFLATE); GPR_ASSERT(GRPC_COMPRESS_NONE == grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_NONE, accepted_encodings)); GPR_ASSERT(GRPC_COMPRESS_GZIP == grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_LOW, accepted_encodings)); GPR_ASSERT(GRPC_COMPRESS_DEFLATE == grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_MED, accepted_encodings)); GPR_ASSERT(GRPC_COMPRESS_DEFLATE == grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_HIGH, accepted_encodings)); } }