[前][次][番号順一覧][スレッド一覧]

ruby-changes:35715

From: nobu <ko1@a...>
Date: Sun, 5 Oct 2014 08:33:32 +0900 (JST)
Subject: [ruby-changes:35715] nobu:r47799 (trunk): openssl: typed data

nobu	2014-10-05 08:33:15 +0900 (Sun, 05 Oct 2014)

  New Revision: 47799

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=47799

  Log:
    openssl: typed data

  Modified files:
    trunk/ext/openssl/ossl_bn.c
    trunk/ext/openssl/ossl_cipher.c
Index: ext/openssl/ossl_cipher.c
===================================================================
--- ext/openssl/ossl_cipher.c	(revision 47798)
+++ ext/openssl/ossl_cipher.c	(revision 47799)
@@ -11,13 +11,13 @@ https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl_cipher.c#L11
 #include "ossl.h"
 
 #define WrapCipher(obj, klass, ctx) \
-    (obj) = Data_Wrap_Struct((klass), 0, ossl_cipher_free, (ctx))
+    (obj) = TypedData_Wrap_Struct((klass), &ossl_cipher_type, (ctx))
 #define MakeCipher(obj, klass, ctx) \
-    (obj) = Data_Make_Struct((klass), EVP_CIPHER_CTX, 0, ossl_cipher_free, (ctx))
+    (obj) = TypedData_Make_Struct((klass), EVP_CIPHER_CTX, &ossl_cipher_type, (ctx))
 #define AllocCipher(obj, ctx) \
-    memset(DATA_PTR(obj) = (ctx) = ALLOC(EVP_CIPHER_CTX), 0, sizeof(EVP_CIPHER_CTX))
+    (DATA_PTR(obj) = (ctx) = ZALLOC(EVP_CIPHER_CTX))
 #define GetCipherInit(obj, ctx) do { \
-    Data_Get_Struct((obj), EVP_CIPHER_CTX, (ctx)); \
+    TypedData_Get_Struct((obj), EVP_CIPHER_CTX, &ossl_cipher_type, (ctx)); \
 } while (0)
 #define GetCipher(obj, ctx) do { \
     GetCipherInit((obj), (ctx)); \
@@ -37,6 +37,15 @@ VALUE cCipher; https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl_cipher.c#L37
 VALUE eCipherError;
 
 static VALUE ossl_cipher_alloc(VALUE klass);
+static void ossl_cipher_free(void *ptr);
+static size_t ossl_cipher_memsize(const void *ptr);
+
+static const rb_data_type_t ossl_cipher_type = {
+    "OpenSSL/Cipher",
+    {0, ossl_cipher_free, ossl_cipher_memsize,},
+    NULL, NULL,
+    RUBY_TYPED_FREE_IMMEDIATELY,
+};
 
 /*
  * PUBLIC
@@ -70,14 +79,22 @@ ossl_cipher_new(const EVP_CIPHER *cipher https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl_cipher.c#L79
  * PRIVATE
  */
 static void
-ossl_cipher_free(EVP_CIPHER_CTX *ctx)
+ossl_cipher_free(void *ptr)
 {
+    EVP_CIPHER_CTX *ctx = ptr;
     if (ctx) {
 	EVP_CIPHER_CTX_cleanup(ctx);
 	ruby_xfree(ctx);
     }
 }
 
+static size_t
+ossl_cipher_memsize(const void *ptr)
+{
+    const EVP_CIPHER_CTX *ctx = ptr;
+    return ctx ? sizeof(*ctx) : 0;
+}
+
 static VALUE
 ossl_cipher_alloc(VALUE klass)
 {
Index: ext/openssl/ossl_bn.c
===================================================================
--- ext/openssl/ossl_bn.c	(revision 47798)
+++ ext/openssl/ossl_bn.c	(revision 47799)
@@ -15,11 +15,11 @@ https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl_bn.c#L15
   if (!(bn)) { \
     ossl_raise(rb_eRuntimeError, "BN wasn't initialized!"); \
   } \
-  (obj) = Data_Wrap_Struct((klass), 0, BN_clear_free, (bn)); \
+  (obj) = TypedData_Wrap_Struct((klass), &ossl_bn_type, (bn)); \
 } while (0)
 
 #define GetBN(obj, bn) do { \
-  Data_Get_Struct((obj), BIGNUM, (bn)); \
+  TypedData_Get_Struct((obj), BIGNUM, &ossl_bn_type, (bn)); \
   if (!(bn)) { \
     ossl_raise(rb_eRuntimeError, "BN wasn't initialized!"); \
   } \
@@ -30,6 +30,25 @@ https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl_bn.c#L30
   GetBN((obj), (bn)); \
 } while (0)
 
+static void
+ossl_bn_free(void *ptr)
+{
+    BN_clear_free(ptr);
+}
+
+static size_t
+ossl_bn_size(const void *ptr)
+{
+    return sizeof(BIGNUM);
+}
+
+static const rb_data_type_t ossl_bn_type = {
+    "OpenSSL/BN",
+    {0, ossl_bn_free, ossl_bn_size,},
+    NULL, NULL,
+    RUBY_TYPED_FREE_IMMEDIATELY,
+};
+
 /*
  * Classes
  */

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]