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

Use TypedData for GRPC::Core::Credentials

parent bf256ae0
No related branches found
No related tags found
No related merge requests found
......@@ -83,14 +83,21 @@ static void grpc_rb_credentials_mark(void *p) {
}
}
static rb_data_type_t grpc_rb_credentials_data_type = {
"grpc_credentials",
{grpc_rb_credentials_mark, grpc_rb_credentials_free,
GRPC_RB_MEMSIZE_UNAVAILABLE},
NULL,
NULL,
RUBY_TYPED_FREE_IMMEDIATELY};
/* Allocates Credential instances.
Provides safe initial defaults for the instance fields. */
static VALUE grpc_rb_credentials_alloc(VALUE cls) {
grpc_rb_credentials *wrapper = ALLOC(grpc_rb_credentials);
wrapper->wrapped = NULL;
wrapper->mark = Qnil;
return Data_Wrap_Struct(cls, grpc_rb_credentials_mark,
grpc_rb_credentials_free, wrapper);
return TypedData_Wrap_Struct(cls, &grpc_rb_credentials_data_type, wrapper);
}
/* Clones Credentials instances.
......@@ -110,8 +117,10 @@ static VALUE grpc_rb_credentials_init_copy(VALUE copy, VALUE orig) {
rb_raise(rb_eTypeError, "not a %s", rb_obj_classname(grpc_rb_cCredentials));
}
Data_Get_Struct(orig, grpc_rb_credentials, orig_cred);
Data_Get_Struct(copy, grpc_rb_credentials, copy_cred);
TypedData_Get_Struct(orig, grpc_rb_credentials,
&grpc_rb_credentials_data_type, orig_cred);
TypedData_Get_Struct(copy, grpc_rb_credentials,
&grpc_rb_credentials_data_type, copy_cred);
/* use ruby's MEMCPY to make a byte-for-byte copy of the credentials
* wrapper object. */
......@@ -133,8 +142,7 @@ static VALUE grpc_rb_default_credentials_create(VALUE cls) {
}
wrapper->mark = Qnil;
return Data_Wrap_Struct(cls, grpc_rb_credentials_mark,
grpc_rb_credentials_free, wrapper);
return TypedData_Wrap_Struct(cls, &grpc_rb_credentials_data_type, wrapper);
}
/*
......@@ -151,8 +159,7 @@ static VALUE grpc_rb_compute_engine_credentials_create(VALUE cls) {
}
wrapper->mark = Qnil;
return Data_Wrap_Struct(cls, grpc_rb_credentials_mark,
grpc_rb_credentials_free, wrapper);
return TypedData_Wrap_Struct(cls, &grpc_rb_credentials_data_type, wrapper);
}
/*
......@@ -166,8 +173,10 @@ static VALUE grpc_rb_composite_credentials_create(VALUE self, VALUE other) {
grpc_rb_credentials *other_wrapper = NULL;
grpc_rb_credentials *wrapper = NULL;
Data_Get_Struct(self, grpc_rb_credentials, self_wrapper);
Data_Get_Struct(other, grpc_rb_credentials, other_wrapper);
TypedData_Get_Struct(self, grpc_rb_credentials,
&grpc_rb_credentials_data_type, self_wrapper);
TypedData_Get_Struct(other, grpc_rb_credentials,
&grpc_rb_credentials_data_type, other_wrapper);
wrapper = ALLOC(grpc_rb_credentials);
wrapper->wrapped = grpc_composite_credentials_create(self_wrapper->wrapped,
other_wrapper->wrapped);
......@@ -178,8 +187,8 @@ static VALUE grpc_rb_composite_credentials_create(VALUE self, VALUE other) {
}
wrapper->mark = Qnil;
return Data_Wrap_Struct(grpc_rb_cCredentials, grpc_rb_credentials_mark,
grpc_rb_credentials_free, wrapper);
return TypedData_Wrap_Struct(grpc_rb_cCredentials,
&grpc_rb_credentials_data_type, wrapper);
}
/* The attribute used on the mark object to hold the pem_root_certs. */
......@@ -214,7 +223,8 @@ static VALUE grpc_rb_credentials_init(int argc, VALUE *argv, VALUE self) {
rb_scan_args(argc, argv, "12", &pem_root_certs, &pem_private_key,
&pem_cert_chain);
Data_Get_Struct(self, grpc_rb_credentials, wrapper);
TypedData_Get_Struct(self, grpc_rb_credentials,
&grpc_rb_credentials_data_type, wrapper);
if (pem_root_certs == Qnil) {
rb_raise(rb_eRuntimeError,
"could not create a credential: nil pem_root_certs");
......@@ -225,8 +235,8 @@ static VALUE grpc_rb_credentials_init(int argc, VALUE *argv, VALUE self) {
} else {
key_cert_pair.private_key = RSTRING_PTR(pem_private_key);
key_cert_pair.cert_chain = RSTRING_PTR(pem_cert_chain);
creds = grpc_ssl_credentials_create(
RSTRING_PTR(pem_root_certs), &key_cert_pair);
creds = grpc_ssl_credentials_create(RSTRING_PTR(pem_root_certs),
&key_cert_pair);
}
if (creds == NULL) {
rb_raise(rb_eRuntimeError, "could not create a credentials, not sure why");
......@@ -253,8 +263,8 @@ void Init_grpc_credentials() {
rb_define_alloc_func(grpc_rb_cCredentials, grpc_rb_credentials_alloc);
/* Provides a ruby constructor and support for dup/clone. */
rb_define_method(grpc_rb_cCredentials, "initialize",
grpc_rb_credentials_init, -1);
rb_define_method(grpc_rb_cCredentials, "initialize", grpc_rb_credentials_init,
-1);
rb_define_method(grpc_rb_cCredentials, "initialize_copy",
grpc_rb_credentials_init_copy, 1);
......@@ -277,6 +287,7 @@ void Init_grpc_credentials() {
/* Gets the wrapped grpc_credentials from the ruby wrapper */
grpc_credentials *grpc_rb_get_wrapped_credentials(VALUE v) {
grpc_rb_credentials *wrapper = NULL;
Data_Get_Struct(v, grpc_rb_credentials, wrapper);
TypedData_Get_Struct(v, grpc_rb_credentials, &grpc_rb_credentials_data_type,
wrapper);
return wrapper->wrapped;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment