1 /*
2 *
3 * Copyright 2015 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include <ruby/ruby.h>
20
21 #include "rb_channel_args.h"
22
23 #include "rb_grpc.h"
24 #include "rb_grpc_imports.generated.h"
25
26 #include <grpc/grpc.h>
27 #include <grpc/support/alloc.h>
28 #include <grpc/support/log.h>
29 #include <grpc/support/string_util.h>
30
31 static rb_data_type_t grpc_rb_channel_args_data_type = {
32 "grpc_channel_args",
33 {GRPC_RB_GC_NOT_MARKED,
34 GRPC_RB_GC_DONT_FREE,
35 GRPC_RB_MEMSIZE_UNAVAILABLE,
36 {NULL, NULL}},
37 NULL,
38 NULL,
39 #ifdef RUBY_TYPED_FREE_IMMEDIATELY
40 RUBY_TYPED_FREE_IMMEDIATELY
41 #endif
42 };
43
44 /* A callback the processes the hash key values in channel_args hash */
grpc_rb_channel_create_in_process_add_args_hash_cb(VALUE key,VALUE val,VALUE args_obj)45 static int grpc_rb_channel_create_in_process_add_args_hash_cb(VALUE key,
46 VALUE val,
47 VALUE args_obj) {
48 const char* the_key;
49 grpc_channel_args* args;
50
51 switch (TYPE(key)) {
52 case T_STRING:
53 the_key = StringValuePtr(key);
54 break;
55
56 case T_SYMBOL:
57 the_key = rb_id2name(SYM2ID(key));
58 break;
59
60 default:
61 rb_raise(rb_eTypeError, "bad chan arg: got <%s>, want <String|Symbol>",
62 rb_obj_classname(key));
63 return ST_STOP;
64 }
65
66 TypedData_Get_Struct(args_obj, grpc_channel_args,
67 &grpc_rb_channel_args_data_type, args);
68 if (args->num_args <= 0) {
69 rb_raise(rb_eRuntimeError, "hash_cb bug: num_args is %lu for key:%s",
70 args->num_args, StringValueCStr(key));
71 return ST_STOP;
72 }
73
74 args->args[args->num_args - 1].key = gpr_strdup(the_key);
75 switch (TYPE(val)) {
76 case T_SYMBOL:
77 args->args[args->num_args - 1].type = GRPC_ARG_STRING;
78 args->args[args->num_args - 1].value.string =
79 gpr_strdup(rb_id2name(SYM2ID(val)));
80 --args->num_args;
81 return ST_CONTINUE;
82
83 case T_STRING:
84 args->args[args->num_args - 1].type = GRPC_ARG_STRING;
85 args->args[args->num_args - 1].value.string =
86 gpr_strdup(StringValueCStr(val));
87 --args->num_args;
88 return ST_CONTINUE;
89
90 case T_FIXNUM:
91 args->args[args->num_args - 1].type = GRPC_ARG_INTEGER;
92 args->args[args->num_args - 1].value.integer = NUM2INT(val);
93 --args->num_args;
94 return ST_CONTINUE;
95
96 default:
97 rb_raise(rb_eTypeError, "%s: bad value: got <%s>, want <String|Fixnum>",
98 StringValueCStr(key), rb_obj_classname(val));
99 return ST_STOP;
100 }
101 rb_raise(rb_eRuntimeError, "impl bug: hash_cb reached to far while on key:%s",
102 StringValueCStr(key));
103 return ST_STOP;
104 }
105
106 /* channel_convert_params allows the call to
107 grpc_rb_hash_convert_to_channel_args to be made within an rb_protect
108 exception-handler. This allows any allocated memory to be freed before
109 propagating any exception that occurs */
110 typedef struct channel_convert_params {
111 VALUE src_hash;
112 grpc_channel_args* dst;
113 } channel_convert_params;
114
grpc_rb_hash_convert_to_channel_args0(VALUE as_value)115 static VALUE grpc_rb_hash_convert_to_channel_args0(VALUE as_value) {
116 ID id_size = rb_intern("size");
117 VALUE grpc_rb_cChannelArgs = rb_define_class("TmpChannelArgs", rb_cObject);
118 rb_undef_alloc_func(grpc_rb_cChannelArgs);
119 channel_convert_params* params = (channel_convert_params*)as_value;
120 size_t num_args = 0;
121
122 if (!NIL_P(params->src_hash) && TYPE(params->src_hash) != T_HASH) {
123 rb_raise(rb_eTypeError, "bad channel args: got:<%s> want: a hash or nil",
124 rb_obj_classname(params->src_hash));
125 return Qnil;
126 }
127
128 if (TYPE(params->src_hash) == T_HASH) {
129 num_args = NUM2INT(rb_funcall(params->src_hash, id_size, 0));
130 params->dst->num_args = num_args;
131 params->dst->args = ALLOC_N(grpc_arg, num_args);
132 MEMZERO(params->dst->args, grpc_arg, num_args);
133 rb_hash_foreach(
134 params->src_hash, grpc_rb_channel_create_in_process_add_args_hash_cb,
135 TypedData_Wrap_Struct(grpc_rb_cChannelArgs,
136 &grpc_rb_channel_args_data_type, params->dst));
137 /* reset num_args as grpc_rb_channel_create_in_process_add_args_hash_cb
138 * decrements it during has processing */
139 params->dst->num_args = num_args;
140 }
141 return Qnil;
142 }
143
grpc_rb_hash_convert_to_channel_args(VALUE src_hash,grpc_channel_args * dst)144 void grpc_rb_hash_convert_to_channel_args(VALUE src_hash,
145 grpc_channel_args* dst) {
146 channel_convert_params params;
147 int status = 0;
148
149 /* Make a protected call to grpc_rb_hash_convert_channel_args */
150 params.src_hash = src_hash;
151 params.dst = dst;
152 rb_protect(grpc_rb_hash_convert_to_channel_args0, (VALUE)¶ms, &status);
153 if (status != 0) {
154 if (dst->args != NULL) {
155 /* Free any allocated memory before propagating the error */
156 xfree(dst->args);
157 }
158 rb_jump_tag(status);
159 }
160 }
161
grpc_rb_channel_args_destroy(grpc_channel_args * args)162 void grpc_rb_channel_args_destroy(grpc_channel_args* args) {
163 GPR_ASSERT(args != NULL);
164 if (args->args == NULL) return;
165 for (int i = 0; i < args->num_args; i++) {
166 // the key was created with gpr_strdup
167 gpr_free(args->args[i].key);
168 if (args->args[i].type == GRPC_ARG_STRING) {
169 // we own string pointers, which were created with gpr_strdup
170 gpr_free(args->args[i].value.string);
171 }
172 }
173 xfree(args->args);
174 }
175