diff options
Diffstat (limited to 'src/ruby/ext')
-rw-r--r-- | src/ruby/ext/grpc/rb_call.c | 33 | ||||
-rw-r--r-- | src/ruby/ext/grpc/rb_call.h | 3 | ||||
-rw-r--r-- | src/ruby/ext/grpc/rb_call_credentials.c | 7 | ||||
-rw-r--r-- | src/ruby/ext/grpc/rb_channel_credentials.c | 6 | ||||
-rw-r--r-- | src/ruby/ext/grpc/rb_grpc_imports.generated.h | 12 |
5 files changed, 44 insertions, 17 deletions
diff --git a/src/ruby/ext/grpc/rb_call.c b/src/ruby/ext/grpc/rb_call.c index 67425a68e6..b99954883f 100644 --- a/src/ruby/ext/grpc/rb_call.c +++ b/src/ruby/ext/grpc/rb_call.c @@ -24,6 +24,8 @@ #include <grpc/grpc.h> #include <grpc/impl/codegen/compression_types.h> #include <grpc/support/alloc.h> +#include <grpc/support/alloc.h> +#include <grpc/support/log.h> #include "rb_byte_buffer.h" #include "rb_call_credentials.h" @@ -368,7 +370,7 @@ static VALUE grpc_rb_call_set_credentials(VALUE self, VALUE credentials) { to fill grpc_metadata_array. it's capacity should have been computed via a prior call to - grpc_rb_md_ary_fill_hash_cb + grpc_rb_md_ary_capacity_hash_cb */ static int grpc_rb_md_ary_fill_hash_cb(VALUE key, VALUE val, VALUE md_ary_obj) { grpc_metadata_array *md_ary = NULL; @@ -376,7 +378,7 @@ static int grpc_rb_md_ary_fill_hash_cb(VALUE key, VALUE val, VALUE md_ary_obj) { long i; grpc_slice key_slice; grpc_slice value_slice; - char *tmp_str; + char *tmp_str = NULL; if (TYPE(key) == T_SYMBOL) { key_slice = grpc_slice_from_static_string(rb_id2name(SYM2ID(key))); @@ -386,6 +388,7 @@ static int grpc_rb_md_ary_fill_hash_cb(VALUE key, VALUE val, VALUE md_ary_obj) { } else { rb_raise(rb_eTypeError, "grpc_rb_md_ary_fill_hash_cb: bad type for key parameter"); + return ST_STOP; } if (!grpc_header_key_is_legal(key_slice)) { @@ -413,6 +416,7 @@ static int grpc_rb_md_ary_fill_hash_cb(VALUE key, VALUE val, VALUE md_ary_obj) { tmp_str); return ST_STOP; } + GPR_ASSERT(md_ary->count < md_ary->capacity); md_ary->metadata[md_ary->count].key = key_slice; md_ary->metadata[md_ary->count].value = value_slice; md_ary->count += 1; @@ -428,6 +432,7 @@ static int grpc_rb_md_ary_fill_hash_cb(VALUE key, VALUE val, VALUE md_ary_obj) { tmp_str); return ST_STOP; } + GPR_ASSERT(md_ary->count < md_ary->capacity); md_ary->metadata[md_ary->count].key = key_slice; md_ary->metadata[md_ary->count].value = value_slice; md_ary->count += 1; @@ -435,7 +440,6 @@ static int grpc_rb_md_ary_fill_hash_cb(VALUE key, VALUE val, VALUE md_ary_obj) { rb_raise(rb_eArgError, "Header values must be of type string or array"); return ST_STOP; } - return ST_CONTINUE; } @@ -458,6 +462,7 @@ static int grpc_rb_md_ary_capacity_hash_cb(VALUE key, VALUE val, } else { md_ary->capacity += 1; } + return ST_CONTINUE; } @@ -480,7 +485,7 @@ void grpc_rb_md_ary_convert(VALUE md_ary_hash, grpc_metadata_array *md_ary) { md_ary_obj = TypedData_Wrap_Struct(grpc_rb_cMdAry, &grpc_rb_md_ary_data_type, md_ary); rb_hash_foreach(md_ary_hash, grpc_rb_md_ary_capacity_hash_cb, md_ary_obj); - md_ary->metadata = gpr_malloc(md_ary->capacity * sizeof(grpc_metadata)); + md_ary->metadata = gpr_zalloc(md_ary->capacity * sizeof(grpc_metadata)); rb_hash_foreach(md_ary_hash, grpc_rb_md_ary_fill_hash_cb, md_ary_obj); } @@ -611,13 +616,25 @@ static void grpc_run_batch_stack_init(run_batch_stack *st, st->write_flag = write_flag; } +void grpc_rb_metadata_array_destroy_including_entries( + grpc_metadata_array *array) { + size_t i; + if (array->metadata) { + for (i = 0; i < array->count; i++) { + grpc_slice_unref(array->metadata[i].key); + grpc_slice_unref(array->metadata[i].value); + } + } + grpc_metadata_array_destroy(array); +} + /* grpc_run_batch_stack_cleanup ensures the run_batch_stack is properly * cleaned up */ static void grpc_run_batch_stack_cleanup(run_batch_stack *st) { size_t i = 0; - grpc_metadata_array_destroy(&st->send_metadata); - grpc_metadata_array_destroy(&st->send_trailing_metadata); + grpc_rb_metadata_array_destroy_including_entries(&st->send_metadata); + grpc_rb_metadata_array_destroy_including_entries(&st->send_trailing_metadata); grpc_metadata_array_destroy(&st->recv_metadata); grpc_metadata_array_destroy(&st->recv_trailing_metadata); @@ -658,8 +675,6 @@ static void grpc_run_batch_stack_fill_ops(run_batch_stack *st, VALUE ops_hash) { st->ops[st->op_num].flags = 0; switch (NUM2INT(this_op)) { case GRPC_OP_SEND_INITIAL_METADATA: - /* N.B. later there is no need to explicitly delete the metadata keys - * and values, they are references to data in ruby objects. */ grpc_rb_md_ary_convert(this_value, &st->send_metadata); st->ops[st->op_num].data.send_initial_metadata.count = st->send_metadata.count; @@ -675,8 +690,6 @@ static void grpc_run_batch_stack_fill_ops(run_batch_stack *st, VALUE ops_hash) { case GRPC_OP_SEND_CLOSE_FROM_CLIENT: break; case GRPC_OP_SEND_STATUS_FROM_SERVER: - /* N.B. later there is no need to explicitly delete the metadata keys - * and values, they are references to data in ruby objects. */ grpc_rb_op_update_status_from_server( &st->ops[st->op_num], &st->send_trailing_metadata, &st->send_status_details, this_value); diff --git a/src/ruby/ext/grpc/rb_call.h b/src/ruby/ext/grpc/rb_call.h index be791cf972..bfe8035e0f 100644 --- a/src/ruby/ext/grpc/rb_call.h +++ b/src/ruby/ext/grpc/rb_call.h @@ -40,6 +40,9 @@ VALUE grpc_rb_md_ary_to_h(grpc_metadata_array *md_ary); */ void grpc_rb_md_ary_convert(VALUE md_ary_hash, grpc_metadata_array *md_ary); +void grpc_rb_metadata_array_destroy_including_entries( + grpc_metadata_array *md_ary); + /* grpc_rb_eCallError is the ruby class of the exception thrown during call operations. */ extern VALUE grpc_rb_eCallError; diff --git a/src/ruby/ext/grpc/rb_call_credentials.c b/src/ruby/ext/grpc/rb_call_credentials.c index 95e01a2c19..049a869bdc 100644 --- a/src/ruby/ext/grpc/rb_call_credentials.c +++ b/src/ruby/ext/grpc/rb_call_credentials.c @@ -108,7 +108,7 @@ static void grpc_rb_call_credentials_callback_with_gil(void *param) { error_details = StringValueCStr(details); params->callback(params->user_data, md_ary.metadata, md_ary.count, status, error_details); - grpc_metadata_array_destroy(&md_ary); + grpc_rb_metadata_array_destroy_including_entries(&md_ary); gpr_free(params); } @@ -239,6 +239,7 @@ static VALUE grpc_rb_call_credentials_compose(int argc, VALUE *argv, VALUE self) { grpc_call_credentials *creds; grpc_call_credentials *other; + grpc_call_credentials *prev = NULL; VALUE mark; if (argc == 0) { return self; @@ -249,6 +250,10 @@ static VALUE grpc_rb_call_credentials_compose(int argc, VALUE *argv, rb_ary_push(mark, argv[i]); other = grpc_rb_get_wrapped_call_credentials(argv[i]); creds = grpc_composite_call_credentials_create(creds, other, NULL); + if (prev != NULL) { + grpc_call_credentials_release(prev); + } + prev = creds; } return grpc_rb_wrap_call_credentials(creds, mark); } diff --git a/src/ruby/ext/grpc/rb_channel_credentials.c b/src/ruby/ext/grpc/rb_channel_credentials.c index 07935a13dc..83601ca694 100644 --- a/src/ruby/ext/grpc/rb_channel_credentials.c +++ b/src/ruby/ext/grpc/rb_channel_credentials.c @@ -184,6 +184,7 @@ static VALUE grpc_rb_channel_credentials_compose(int argc, VALUE *argv, VALUE self) { grpc_channel_credentials *creds; grpc_call_credentials *other; + grpc_channel_credentials *prev = NULL; VALUE mark; if (argc == 0) { return self; @@ -195,6 +196,11 @@ static VALUE grpc_rb_channel_credentials_compose(int argc, VALUE *argv, rb_ary_push(mark, argv[i]); other = grpc_rb_get_wrapped_call_credentials(argv[i]); creds = grpc_composite_channel_credentials_create(creds, other, NULL); + if (prev != NULL) { + grpc_channel_credentials_release(prev); + } + prev = creds; + if (creds == NULL) { rb_raise(rb_eRuntimeError, "Failed to compose channel and call credentials"); diff --git a/src/ruby/ext/grpc/rb_grpc_imports.generated.h b/src/ruby/ext/grpc/rb_grpc_imports.generated.h index 3b570bb7f0..b64199be8e 100644 --- a/src/ruby/ext/grpc/rb_grpc_imports.generated.h +++ b/src/ruby/ext/grpc/rb_grpc_imports.generated.h @@ -653,22 +653,22 @@ extern gpr_get_allocation_functions_type gpr_get_allocation_functions_import; typedef gpr_avl(*gpr_avl_create_type)(const gpr_avl_vtable *vtable); extern gpr_avl_create_type gpr_avl_create_import; #define gpr_avl_create gpr_avl_create_import -typedef gpr_avl(*gpr_avl_ref_type)(gpr_avl avl); +typedef gpr_avl(*gpr_avl_ref_type)(gpr_avl avl, void *user_data); extern gpr_avl_ref_type gpr_avl_ref_import; #define gpr_avl_ref gpr_avl_ref_import -typedef void(*gpr_avl_unref_type)(gpr_avl avl); +typedef void(*gpr_avl_unref_type)(gpr_avl avl, void *user_data); extern gpr_avl_unref_type gpr_avl_unref_import; #define gpr_avl_unref gpr_avl_unref_import -typedef gpr_avl(*gpr_avl_add_type)(gpr_avl avl, void *key, void *value); +typedef gpr_avl(*gpr_avl_add_type)(gpr_avl avl, void *key, void *value, void *user_data); extern gpr_avl_add_type gpr_avl_add_import; #define gpr_avl_add gpr_avl_add_import -typedef gpr_avl(*gpr_avl_remove_type)(gpr_avl avl, void *key); +typedef gpr_avl(*gpr_avl_remove_type)(gpr_avl avl, void *key, void *user_data); extern gpr_avl_remove_type gpr_avl_remove_import; #define gpr_avl_remove gpr_avl_remove_import -typedef void *(*gpr_avl_get_type)(gpr_avl avl, void *key); +typedef void *(*gpr_avl_get_type)(gpr_avl avl, void *key, void *user_data); extern gpr_avl_get_type gpr_avl_get_import; #define gpr_avl_get gpr_avl_get_import -typedef int(*gpr_avl_maybe_get_type)(gpr_avl avl, void *key, void **value); +typedef int(*gpr_avl_maybe_get_type)(gpr_avl avl, void *key, void **value, void *user_data); extern gpr_avl_maybe_get_type gpr_avl_maybe_get_import; #define gpr_avl_maybe_get gpr_avl_maybe_get_import typedef int(*gpr_avl_is_empty_type)(gpr_avl avl); |