Skip to content
Snippets Groups Projects
Commit 9232f124 authored by Yuki Yugui Sonoda's avatar Yuki Yugui Sonoda
Browse files

Use TypedData for GRPC::Core::Server

parent d441c2e5
No related branches found
No related tags found
No related merge requests found
...@@ -88,13 +88,23 @@ static void grpc_rb_server_mark(void *p) { ...@@ -88,13 +88,23 @@ static void grpc_rb_server_mark(void *p) {
} }
} }
static const rb_data_type_t grpc_rb_server_data_type = {
"grpc_server",
{grpc_rb_server_mark, grpc_rb_server_free, GRPC_RB_MEMSIZE_UNAVAILABLE},
NULL,
NULL,
/* It is unsafe to specify RUBY_TYPED_FREE_IMMEDIATELY because the free function would block
* and we might want to unlock GVL
* TODO(yugui) Unlock GVL?
*/
0};
/* Allocates grpc_rb_server instances. */ /* Allocates grpc_rb_server instances. */
static VALUE grpc_rb_server_alloc(VALUE cls) { static VALUE grpc_rb_server_alloc(VALUE cls) {
grpc_rb_server *wrapper = ALLOC(grpc_rb_server); grpc_rb_server *wrapper = ALLOC(grpc_rb_server);
wrapper->wrapped = NULL; wrapper->wrapped = NULL;
wrapper->mark = Qnil; wrapper->mark = Qnil;
return Data_Wrap_Struct(cls, grpc_rb_server_mark, grpc_rb_server_free, return TypedData_Wrap_Struct(cls, &grpc_rb_server_data_type, wrapper);
wrapper);
} }
/* /*
...@@ -110,7 +120,8 @@ static VALUE grpc_rb_server_init(VALUE self, VALUE cqueue, VALUE channel_args) { ...@@ -110,7 +120,8 @@ static VALUE grpc_rb_server_init(VALUE self, VALUE cqueue, VALUE channel_args) {
grpc_channel_args args; grpc_channel_args args;
MEMZERO(&args, grpc_channel_args, 1); MEMZERO(&args, grpc_channel_args, 1);
cq = grpc_rb_get_wrapped_completion_queue(cqueue); cq = grpc_rb_get_wrapped_completion_queue(cqueue);
Data_Get_Struct(self, grpc_rb_server, wrapper); TypedData_Get_Struct(self, grpc_rb_server, &grpc_rb_server_data_type,
wrapper);
grpc_rb_hash_convert_to_channel_args(channel_args, &args); grpc_rb_hash_convert_to_channel_args(channel_args, &args);
srv = grpc_server_create(cq, &args); srv = grpc_server_create(cq, &args);
...@@ -146,8 +157,10 @@ static VALUE grpc_rb_server_init_copy(VALUE copy, VALUE orig) { ...@@ -146,8 +157,10 @@ static VALUE grpc_rb_server_init_copy(VALUE copy, VALUE orig) {
rb_raise(rb_eTypeError, "not a %s", rb_obj_classname(grpc_rb_cServer)); rb_raise(rb_eTypeError, "not a %s", rb_obj_classname(grpc_rb_cServer));
} }
Data_Get_Struct(orig, grpc_rb_server, orig_srv); TypedData_Get_Struct(orig, grpc_rb_server, &grpc_rb_server_data_type,
Data_Get_Struct(copy, grpc_rb_server, copy_srv); orig_srv);
TypedData_Get_Struct(copy, grpc_rb_server, &grpc_rb_server_data_type,
copy_srv);
/* use ruby's MEMCPY to make a byte-for-byte copy of the server wrapper /* use ruby's MEMCPY to make a byte-for-byte copy of the server wrapper
object. */ object. */
...@@ -194,7 +207,7 @@ static VALUE grpc_rb_server_request_call(VALUE self, VALUE cqueue, ...@@ -194,7 +207,7 @@ static VALUE grpc_rb_server_request_call(VALUE self, VALUE cqueue,
grpc_call_error err; grpc_call_error err;
request_call_stack st; request_call_stack st;
VALUE result; VALUE result;
Data_Get_Struct(self, grpc_rb_server, s); TypedData_Get_Struct(self, grpc_rb_server, &grpc_rb_server_data_type, s);
if (s->wrapped == NULL) { if (s->wrapped == NULL) {
rb_raise(rb_eRuntimeError, "closed!"); rb_raise(rb_eRuntimeError, "closed!");
return Qnil; return Qnil;
...@@ -245,7 +258,7 @@ static VALUE grpc_rb_server_request_call(VALUE self, VALUE cqueue, ...@@ -245,7 +258,7 @@ static VALUE grpc_rb_server_request_call(VALUE self, VALUE cqueue,
static VALUE grpc_rb_server_start(VALUE self) { static VALUE grpc_rb_server_start(VALUE self) {
grpc_rb_server *s = NULL; grpc_rb_server *s = NULL;
Data_Get_Struct(self, grpc_rb_server, s); TypedData_Get_Struct(self, grpc_rb_server, &grpc_rb_server_data_type, s);
if (s->wrapped == NULL) { if (s->wrapped == NULL) {
rb_raise(rb_eRuntimeError, "closed!"); rb_raise(rb_eRuntimeError, "closed!");
} else { } else {
...@@ -256,7 +269,7 @@ static VALUE grpc_rb_server_start(VALUE self) { ...@@ -256,7 +269,7 @@ static VALUE grpc_rb_server_start(VALUE self) {
static VALUE grpc_rb_server_destroy(VALUE self) { static VALUE grpc_rb_server_destroy(VALUE self) {
grpc_rb_server *s = NULL; grpc_rb_server *s = NULL;
Data_Get_Struct(self, grpc_rb_server, s); TypedData_Get_Struct(self, grpc_rb_server, &grpc_rb_server_data_type, s);
if (s->wrapped != NULL) { if (s->wrapped != NULL) {
grpc_server_shutdown(s->wrapped); grpc_server_shutdown(s->wrapped);
grpc_server_destroy(s->wrapped); grpc_server_destroy(s->wrapped);
...@@ -288,7 +301,7 @@ static VALUE grpc_rb_server_add_http2_port(int argc, VALUE *argv, VALUE self) { ...@@ -288,7 +301,7 @@ static VALUE grpc_rb_server_add_http2_port(int argc, VALUE *argv, VALUE self) {
/* "11" == 1 mandatory args, 1 (rb_creds) is optional */ /* "11" == 1 mandatory args, 1 (rb_creds) is optional */
rb_scan_args(argc, argv, "11", &port, &rb_creds); rb_scan_args(argc, argv, "11", &port, &rb_creds);
Data_Get_Struct(self, grpc_rb_server, s); TypedData_Get_Struct(self, grpc_rb_server, &grpc_rb_server_data_type, s);
if (s->wrapped == NULL) { if (s->wrapped == NULL) {
rb_raise(rb_eRuntimeError, "closed!"); rb_raise(rb_eRuntimeError, "closed!");
return Qnil; return Qnil;
...@@ -340,6 +353,6 @@ void Init_grpc_server() { ...@@ -340,6 +353,6 @@ void Init_grpc_server() {
/* Gets the wrapped server from the ruby wrapper */ /* Gets the wrapped server from the ruby wrapper */
grpc_server *grpc_rb_get_wrapped_server(VALUE v) { grpc_server *grpc_rb_get_wrapped_server(VALUE v) {
grpc_rb_server *wrapper = NULL; grpc_rb_server *wrapper = NULL;
Data_Get_Struct(v, grpc_rb_server, wrapper); TypedData_Get_Struct(v, grpc_rb_server, &grpc_rb_server_data_type, wrapper);
return wrapper->wrapped; return wrapper->wrapped;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment