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

ruby-changes:28466

From: nobu <ko1@a...>
Date: Mon, 29 Apr 2013 06:58:58 +0900 (JST)
Subject: [ruby-changes:28466] nobu:r40518 (trunk): ossl_bn.c: reduce alloca/malloc

nobu	2013-04-29 06:58:36 +0900 (Mon, 29 Apr 2013)

  New Revision: 40518

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

  Log:
    ossl_bn.c: reduce alloca/malloc
    
    * ext/openssl/ossl_bn.c (ossl_bn_initialize): no need of alloca for
      small fixed size array.
    * ext/openssl/ossl_bn.c (ossl_bn_initialize): check overflow first,
      and use alloca for small size input.

  Modified files:
    trunk/ChangeLog
    trunk/ext/openssl/ossl_bn.c

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 40517)
+++ ChangeLog	(revision 40518)
@@ -1,3 +1,11 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Mon Apr 29 06:58:30 2013  Nobuyoshi Nakada  <nobu@r...>
+
+	* ext/openssl/ossl_bn.c (ossl_bn_initialize): no need of alloca for
+	  small fixed size array.
+
+	* ext/openssl/ossl_bn.c (ossl_bn_initialize): check overflow first,
+	  and use alloca for small size input.
+
 Mon Apr 29 00:40:13 2013  Benoit Daloze  <eregontp@g...>
 
 	* lib/yaml.rb: Clarify documentation about YAML being always Psych.
Index: ext/openssl/ossl_bn.c
===================================================================
--- ext/openssl/ossl_bn.c	(revision 40517)
+++ ext/openssl/ossl_bn.c	(revision 40518)
@@ -123,7 +123,7 @@ ossl_bn_initialize(int argc, VALUE *argv https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl_bn.c#L123
 
     if (RB_TYPE_P(str, T_FIXNUM)) {
 	long i;
-	unsigned char *bin = (unsigned char*)ALLOCA_N(long, 1);
+	unsigned char bin[sizeof(long)];
 	long n = FIX2LONG(str);
 	unsigned long un = labs(n);
 
@@ -133,31 +133,32 @@ ossl_bn_initialize(int argc, VALUE *argv https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl_bn.c#L133
 	}
 
 	GetBN(self, bn);
-	if (!BN_bin2bn(bin, sizeof(long), bn)) {
+	if (!BN_bin2bn(bin, sizeof(bin), bn)) {
 	    ossl_raise(eBNError, NULL);
 	}
 	if (n < 0) BN_set_negative(bn, 1);
 	return self;
     }
     else if (RB_TYPE_P(str, T_BIGNUM)) {
-	long i, j;
+	int i, j, len = RBIGNUM_LENINT(str);
 	BDIGIT *ds = RBIGNUM_DIGITS(str);
-	unsigned char *bin = (unsigned char*)ALLOC_N(BDIGIT, RBIGNUM_LEN(str));
+	VALUE buf;
+	unsigned char *bin = (unsigned char*)ALLOCV_N(BDIGIT, buf, len);
 
-	for (i = 0; RBIGNUM_LEN(str) > i; i++) {
+	for (i = 0; len > i; i++) {
 	    BDIGIT v = ds[i];
 	    for (j = sizeof(BDIGIT) - 1; 0 <= j; j--) {
-		bin[(RBIGNUM_LEN(str)-1-i)*sizeof(BDIGIT)+j] = v&0xff;
+		bin[(len-1-i)*sizeof(BDIGIT)+j] = v&0xff;
 		v >>= 8;
 	    }
 	}
 
 	GetBN(self, bn);
-	if (!BN_bin2bn(bin, (int)sizeof(BDIGIT)*RBIGNUM_LENINT(str), bn)) {
-	    xfree(bin);
+	if (!BN_bin2bn(bin, (int)sizeof(BDIGIT)*len, bn)) {
+	    ALLOCV_END(buf);
 	    ossl_raise(eBNError, NULL);
 	}
-	xfree(bin);
+	ALLOCV_END(buf);
 	if (!RBIGNUM_SIGN(str)) BN_set_negative(bn, 1);
 	return self;
     }

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

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