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

ruby-changes:24932

From: nobu <ko1@a...>
Date: Sun, 16 Sep 2012 18:24:58 +0900 (JST)
Subject: [ruby-changes:24932] nobu:r36984 (trunk): file.c: reduce xmalloc

nobu	2012-09-16 18:24:44 +0900 (Sun, 16 Sep 2012)

  New Revision: 36984

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

  Log:
    file.c: reduce xmalloc
    
    * file.c (rb_readlink): read symlink in the result string directly.

  Modified files:
    trunk/file.c

Index: file.c
===================================================================
--- file.c	(revision 36983)
+++ file.c	(revision 36984)
@@ -2443,7 +2443,6 @@
 static VALUE
 rb_readlink(VALUE path)
 {
-    char *buf;
     int size = 100;
     ssize_t rv;
     VALUE v;
@@ -2451,21 +2450,20 @@
     rb_secure(2);
     FilePathValue(path);
     path = rb_str_encode_ospath(path);
-    buf = xmalloc(size);
-    while ((rv = readlink(RSTRING_PTR(path), buf, size)) == size
+    v = rb_enc_str_new(0, size, rb_filesystem_encoding());
+    while ((rv = readlink(RSTRING_PTR(path), RSTRING_PTR(v), size)) == size
 #ifdef _AIX
 	    || (rv < 0 && errno == ERANGE) /* quirky behavior of GPFS */
 #endif
 	) {
+	rb_str_modify_expand(v, size);
 	size *= 2;
-	buf = xrealloc(buf, size);
     }
     if (rv < 0) {
-	xfree(buf);
+	rb_str_resize(v, 0);
 	rb_sys_fail_path(path);
     }
-    v = rb_filesystem_str_new(buf, rv);
-    xfree(buf);
+    rb_str_resize(v, rv);
 
     return v;
 }

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

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