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

ruby-changes:35133

From: nagachika <ko1@a...>
Date: Mon, 18 Aug 2014 23:34:54 +0900 (JST)
Subject: [ruby-changes:35133] nagachika:r47215 (ruby_2_1): merge revision(s) r46408, r46410, r46413, r46414, r46424, r46436, r46437: [Backport #9934]

nagachika	2014-08-18 23:34:31 +0900 (Mon, 18 Aug 2014)

  New Revision: 47215

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

  Log:
    merge revision(s) r46408,r46410,r46413,r46414,r46424,r46436,r46437: [Backport #9934]
    
    string.c: shrink too big buffer
    
    * string.c (rb_str_resize): shrink the buffer even if new length
      is same but it is enough smaller than the capacity.
    * file.c (expand_path): shrink expanded path which no longer needs
      rooms to append.  [ruby-core:63114] [Bug #9934]
    
    * string.c (rb_str_resize): should consider the capacity instead
      of the old length, as pointed out by nagachika.
    
    * string.c (rb_str_resize): update capa only when buffer get
      reallocated.
      http://d.hatena.ne.jp/nagachika/20140613/ruby_trunk_changes_46413_46420#r46413

  Modified directories:
    branches/ruby_2_1/
  Modified files:
    branches/ruby_2_1/ChangeLog
    branches/ruby_2_1/file.c
    branches/ruby_2_1/string.c
    branches/ruby_2_1/test/ruby/test_file_exhaustive.rb
    branches/ruby_2_1/version.h
Index: ruby_2_1/ChangeLog
===================================================================
--- ruby_2_1/ChangeLog	(revision 47214)
+++ ruby_2_1/ChangeLog	(revision 47215)
@@ -1,3 +1,19 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_1/ChangeLog#L1
+Mon Aug 18 23:22:03 2014  Nobuyoshi Nakada  <nobu@r...>
+
+	* string.c (rb_str_resize): update capa only when buffer get
+	  reallocated.
+	  http://d.hatena.ne.jp/nagachika/20140613/ruby_trunk_changes_46413_46420#r46413
+
+Mon Aug 18 23:22:03 2014  Nobuyoshi Nakada  <nobu@r...>
+
+	* string.c (rb_str_resize): should consider the capacity instead
+	  of the old length, as pointed out by nagachika.
+
+Mon Aug 18 23:22:03 2014  Nobuyoshi Nakada  <nobu@r...>
+
+	* file.c (expand_path): shrink expanded path which no longer needs
+	  rooms to append.  [ruby-core:63114] [Bug #9934]
+
 Mon Aug 11 23:55:32 2014  Mark Lorenz  <mlorenz@c...>
 
 	* lib/erb.rb (result): [DOC] no longer accepts a Proc, as
Index: ruby_2_1/string.c
===================================================================
--- ruby_2_1/string.c	(revision 47214)
+++ ruby_2_1/string.c	(revision 47215)
@@ -2034,9 +2034,11 @@ rb_str_resize(VALUE str, long len) https://github.com/ruby/ruby/blob/trunk/ruby_2_1/string.c#L2034
     independent = str_independent(str);
     ENC_CODERANGE_CLEAR(str);
     slen = RSTRING_LEN(str);
-    if (len != slen) {
+    {
+	long capa;
 	const int termlen = TERM_LEN(str);
 	if (STR_EMBED_P(str)) {
+	    if (len == slen) return str;
 	    if (len + termlen <= RSTRING_EMBED_LEN_MAX + 1) {
 		STR_SET_EMBED_LEN(str, len);
 		TERM_FILL(RSTRING(str)->as.ary + len, termlen);
@@ -2056,14 +2058,15 @@ rb_str_resize(VALUE str, long len) https://github.com/ruby/ruby/blob/trunk/ruby_2_1/string.c#L2058
 	    return str;
 	}
 	else if (!independent) {
+	    if (len == slen) return str;
 	    str_make_independent_expand(str, len - slen);
 	}
-	else if (slen < len || slen - len > 1024) {
+	else if ((capa = RSTRING(str)->as.heap.aux.capa) < len ||
+		 (capa - len) > (len < 1024 ? len : 1024)) {
 	    REALLOC_N(RSTRING(str)->as.heap.ptr, char, len + termlen);
-	}
-	if (!STR_NOCAPA_P(str)) {
 	    RSTRING(str)->as.heap.aux.capa = len;
 	}
+	else if (len == slen) return str;
 	RSTRING(str)->as.heap.len = len;
 	TERM_FILL(RSTRING(str)->as.heap.ptr + len, termlen); /* sentinel */
     }
Index: ruby_2_1/version.h
===================================================================
--- ruby_2_1/version.h	(revision 47214)
+++ ruby_2_1/version.h	(revision 47215)
@@ -1,10 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_1/version.h#L1
 #define RUBY_VERSION "2.1.2"
-#define RUBY_RELEASE_DATE "2014-08-11"
-#define RUBY_PATCHLEVEL 200
+#define RUBY_RELEASE_DATE "2014-08-18"
+#define RUBY_PATCHLEVEL 201
 
 #define RUBY_RELEASE_YEAR 2014
 #define RUBY_RELEASE_MONTH 8
-#define RUBY_RELEASE_DAY 11
+#define RUBY_RELEASE_DAY 18
 
 #include "ruby/version.h"
 
Index: ruby_2_1/test/ruby/test_file_exhaustive.rb
===================================================================
--- ruby_2_1/test/ruby/test_file_exhaustive.rb	(revision 47214)
+++ ruby_2_1/test/ruby/test_file_exhaustive.rb	(revision 47215)
@@ -458,6 +458,15 @@ class TestFileExhaustive < Test::Unit::T https://github.com/ruby/ruby/blob/trunk/ruby_2_1/test/ruby/test_file_exhaustive.rb#L458
     end
   end
 
+  def test_expand_path_memsize
+    bug9934 = '[ruby-core:63114] [Bug #9934]'
+    require "objspace"
+    path = File.expand_path("/foo")
+    assert_operator(ObjectSpace.memsize_of(path), :<=, path.bytesize, bug9934)
+    path = File.expand_path("/a"*25)
+    assert_equal(path.bytesize+1, ObjectSpace.memsize_of(path), bug9934)
+  end
+
   def test_expand_path_encoding
     drive = (DRIVE ? 'C:' : '')
     if Encoding.find("filesystem") == Encoding::CP1251
Index: ruby_2_1/file.c
===================================================================
--- ruby_2_1/file.c	(revision 47214)
+++ ruby_2_1/file.c	(revision 47215)
@@ -3397,6 +3397,16 @@ rb_file_expand_path_internal(VALUE fname https://github.com/ruby/ruby/blob/trunk/ruby_2_1/file.c#L3397
 
 #define EXPAND_PATH_BUFFER() rb_usascii_str_new(0, MAXPATHLEN + 2)
 
+static VALUE
+str_shrink(VALUE str)
+{
+    rb_str_resize(str, RSTRING_LEN(str));
+    return str;
+}
+
+#define expand_path(fname, dname, abs_mode, long_name, result) \
+    str_shrink(rb_file_expand_path_internal(fname, dname, abs_mode, long_name, result))
+
 #define check_expand_path_args(fname, dname) \
     (((fname) = rb_get_path(fname)), \
      (void)(NIL_P(dname) ? (dname) : ((dname) = rb_get_path(dname))))
@@ -3411,13 +3421,13 @@ VALUE https://github.com/ruby/ruby/blob/trunk/ruby_2_1/file.c#L3421
 rb_file_expand_path(VALUE fname, VALUE dname)
 {
     check_expand_path_args(fname, dname);
-    return rb_file_expand_path_internal(fname, dname, 0, 1, EXPAND_PATH_BUFFER());
+    return expand_path(fname, dname, 0, 1, EXPAND_PATH_BUFFER());
 }
 
 VALUE
 rb_file_expand_path_fast(VALUE fname, VALUE dname)
 {
-    return rb_file_expand_path_internal(fname, dname, 0, 0, EXPAND_PATH_BUFFER());
+    return expand_path(fname, dname, 0, 0, EXPAND_PATH_BUFFER());
 }
 
 /*
@@ -3465,7 +3475,7 @@ VALUE https://github.com/ruby/ruby/blob/trunk/ruby_2_1/file.c#L3475
 rb_file_absolute_path(VALUE fname, VALUE dname)
 {
     check_expand_path_args(fname, dname);
-    return rb_file_expand_path_internal(fname, dname, 1, 1, EXPAND_PATH_BUFFER());
+    return expand_path(fname, dname, 1, 1, EXPAND_PATH_BUFFER());
 }
 
 /*
@@ -5408,6 +5418,7 @@ is_explicit_relative(const char *path) https://github.com/ruby/ruby/blob/trunk/ruby_2_1/file.c#L5418
 static VALUE
 copy_path_class(VALUE path, VALUE orig)
 {
+    str_shrink(path);
     RBASIC_SET_CLASS(path, rb_obj_class(orig));
     OBJ_FREEZE(path);
     return path;

Property changes on: ruby_2_1
___________________________________________________________________
Modified: svn:mergeinfo
   Merged /trunk:r46408,46410,46413-46414,46424,46436-46437


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

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