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

ruby-changes:31253

From: ko1 <ko1@a...>
Date: Thu, 17 Oct 2013 17:35:13 +0900 (JST)
Subject: [ruby-changes:31253] ko1:r43332 (trunk): * array.c, string.c: use ruby_xsizedfree() and ruby_xsizedrealloc().

ko1	2013-10-17 17:35:06 +0900 (Thu, 17 Oct 2013)

  New Revision: 43332

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

  Log:
    * array.c, string.c: use ruby_xsizedfree() and ruby_xsizedrealloc().
    * internal.h (SIZED_REALLOC_N): define a macro as REALLOC_N().

  Modified files:
    trunk/ChangeLog
    trunk/array.c
    trunk/internal.h
    trunk/string.c
Index: array.c
===================================================================
--- array.c	(revision 43331)
+++ array.c	(revision 43332)
@@ -109,6 +109,7 @@ ary_memcpy(VALUE ary, long beg, long arg https://github.com/ruby/ruby/blob/trunk/array.c#L109
     (assert(ARY_EMBED_P(a)), \
      (long)((RBASIC(a)->flags >> RARRAY_EMBED_LEN_SHIFT) & \
 	 (RARRAY_EMBED_LEN_MASK >> RARRAY_EMBED_LEN_SHIFT)))
+#define ARY_HEAP_SIZE(a) (assert(!ARY_EMBED_P(a)), assert(ARY_OWNS_HEAP_P(a)), RARRAY(a)->as.heap.aux.capa * sizeof(VALUE))
 
 #define ARY_OWNS_HEAP_P(a) (!FL_TEST((a), ELTS_SHARED|RARRAY_EMBED_FLAG))
 #define FL_SET_EMBED(a) do { \
@@ -210,7 +211,7 @@ ary_resize_capa(VALUE ary, long capacity https://github.com/ruby/ruby/blob/trunk/array.c#L211
             ARY_SET_HEAP_LEN(ary, len);
         }
         else {
-            REALLOC_N(RARRAY(ary)->as.heap.ptr, VALUE, (capacity));
+	    SIZED_REALLOC_N(RARRAY(ary)->as.heap.ptr, VALUE, capacity, RARRAY(ary)->as.heap.aux.capa);
         }
         ARY_SET_CAPA(ary, (capacity));
     }
@@ -218,11 +219,13 @@ ary_resize_capa(VALUE ary, long capacity https://github.com/ruby/ruby/blob/trunk/array.c#L219
         if (!ARY_EMBED_P(ary)) {
             long len = RARRAY_LEN(ary);
 	    const VALUE *ptr = RARRAY_CONST_PTR(ary);
-            if (len > capacity) len = capacity;
+	    size_t size = ARY_HEAP_SIZE(ary);
+
+	    if (len > capacity) len = capacity;
             MEMCPY((VALUE *)RARRAY(ary)->as.ary, ptr, VALUE, len);
             FL_SET_EMBED(ary);
             ARY_SET_LEN(ary, len);
-            xfree((VALUE *)ptr);
+	    ruby_xsizedfree((VALUE *)ptr, size);
         }
     }
 }
@@ -533,7 +536,7 @@ void https://github.com/ruby/ruby/blob/trunk/array.c#L536
 rb_ary_free(VALUE ary)
 {
     if (ARY_OWNS_HEAP_P(ary)) {
-	xfree((void *)ARY_HEAP_PTR(ary));
+	ruby_xsizedfree((void *)ARY_HEAP_PTR(ary), ARY_HEAP_SIZE(ary));
     }
 }
 
@@ -713,7 +716,7 @@ rb_ary_initialize(int argc, VALUE *argv, https://github.com/ruby/ruby/blob/trunk/array.c#L716
     rb_ary_modify(ary);
     if (argc == 0) {
 	if (ARY_OWNS_HEAP_P(ary) && RARRAY_CONST_PTR(ary) != 0) {
-	    xfree((void *)RARRAY_CONST_PTR(ary));
+	    ruby_xsizedfree((void *)RARRAY_CONST_PTR(ary), ARY_HEAP_SIZE(ary));
 	}
         rb_ary_unshare_safe(ary);
         FL_SET_EMBED(ary);
@@ -1644,7 +1647,7 @@ rb_ary_resize(VALUE ary, long len) https://github.com/ruby/ruby/blob/trunk/array.c#L1647
     }
     else {
 	if (olen > len + ARY_DEFAULT_SIZE) {
-	    REALLOC_N(RARRAY(ary)->as.heap.ptr, VALUE, len);
+	    SIZED_REALLOC_N(RARRAY(ary)->as.heap.ptr, VALUE, len, RARRAY(ary)->as.heap.aux.capa);
 	    ARY_SET_CAPA(ary, len);
 	}
 	ARY_SET_HEAP_LEN(ary, len);
@@ -2433,7 +2436,7 @@ rb_ary_sort_bang(VALUE ary) https://github.com/ruby/ruby/blob/trunk/array.c#L2436
                     rb_ary_unshare(ary);
                 }
                 else {
-		    xfree((void *)ARY_HEAP_PTR(ary));
+		    ruby_xsizedfree((void *)ARY_HEAP_PTR(ary), ARY_HEAP_SIZE(ary));
                 }
                 ARY_SET_PTR(ary, RARRAY_CONST_PTR(tmp));
                 ARY_SET_HEAP_LEN(ary, len);
@@ -3297,7 +3300,7 @@ rb_ary_replace(VALUE copy, VALUE orig) https://github.com/ruby/ruby/blob/trunk/array.c#L3300
         VALUE shared = 0;
 
         if (ARY_OWNS_HEAP_P(copy)) {
-	    RARRAY_PTR_USE(copy, ptr, xfree(ptr));
+	    RARRAY_PTR_USE(copy, ptr, ruby_xsizedfree(ptr, ARY_HEAP_SIZE(copy)));
 	}
         else if (ARY_SHARED_P(copy)) {
             shared = ARY_SHARED(copy);
@@ -3313,7 +3316,7 @@ rb_ary_replace(VALUE copy, VALUE orig) https://github.com/ruby/ruby/blob/trunk/array.c#L3316
     else {
         VALUE shared = ary_make_shared(orig);
         if (ARY_OWNS_HEAP_P(copy)) {
-	    RARRAY_PTR_USE(copy, ptr, xfree(ptr));
+	    RARRAY_PTR_USE(copy, ptr, ruby_xsizedfree(ptr, ARY_HEAP_SIZE(copy)));
         }
         else {
             rb_ary_unshare_safe(copy);
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 43331)
+++ ChangeLog	(revision 43332)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu Oct 17 17:32:51 2013  Koichi Sasada  <ko1@a...>
+
+	* array.c, string.c: use ruby_xsizedfree() and ruby_xsizedrealloc().
+
+	* internal.h (SIZED_REALLOC_N): define a macro as REALLOC_N().
+
 Thu Oct 17 17:11:17 2013  Nobuyoshi Nakada  <nobu@r...>
 
 	* win32/win32.c (console_emulator_p): check by comparison between
Index: string.c
===================================================================
--- string.c	(revision 43331)
+++ string.c	(revision 43332)
@@ -126,6 +126,9 @@ VALUE rb_cSymbol; https://github.com/ruby/ruby/blob/trunk/string.c#L126
     FL_SET((str), ELTS_SHARED); \
 } while (0)
 
+#define STR_HEAP_PTR(str)  (RSTRING(str)->as.heap.ptr)
+#define STR_HEAP_SIZE(str) (RSTRING(str)->as.heap.aux.capa)
+
 #define is_ascii_string(str) (rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT)
 #define is_broken_string(str) (rb_enc_str_coderange(str) == ENC_CODERANGE_BROKEN)
 
@@ -880,7 +883,7 @@ rb_str_free(VALUE str) https://github.com/ruby/ruby/blob/trunk/string.c#L883
 	st_delete(frozen_strings, &fstr, NULL);
     }
     if (!STR_EMBED_P(str) && !STR_SHARED_P(str)) {
-	xfree(RSTRING(str)->as.heap.ptr);
+	ruby_xsizedfree(STR_HEAP_PTR(str), STR_HEAP_SIZE(str));
     }
 }
 
@@ -1463,7 +1466,7 @@ str_discard(VALUE str) https://github.com/ruby/ruby/blob/trunk/string.c#L1466
 {
     str_modifiable(str);
     if (!STR_SHARED_P(str) && !STR_EMBED_P(str)) {
-	xfree(RSTRING_PTR(str));
+	ruby_xsizedfree(STR_HEAP_PTR(str), STR_HEAP_SIZE(str));
 	RSTRING(str)->as.heap.ptr = 0;
 	RSTRING(str)->as.heap.len = 0;
     }
@@ -1980,13 +1983,14 @@ rb_str_resize(VALUE str, long len) https://github.com/ruby/ruby/blob/trunk/string.c#L1983
 	    STR_SET_NOEMBED(str);
 	}
 	else if (len + termlen <= RSTRING_EMBED_LEN_MAX + 1) {
-	    char *ptr = RSTRING(str)->as.heap.ptr;
+	    char *ptr = STR_HEAP_PTR(str);
+	    size_t size = STR_HEAP_SIZE(str);
 	    STR_SET_EMBED(str);
 	    if (slen > len) slen = len;
 	    if (slen > 0) MEMCPY(RSTRING(str)->as.ary, ptr, char, slen);
 	    TERM_FILL(RSTRING(str)->as.ary + len, termlen);
 	    STR_SET_EMBED_LEN(str, len);
-	    if (independent) xfree(ptr);
+	    if (independent) ruby_xsizedfree(ptr, size);
 	    return str;
 	}
 	else if (!independent) {
@@ -5497,7 +5501,7 @@ tr_trans(VALUE str, VALUE src, VALUE rep https://github.com/ruby/ruby/blob/trunk/string.c#L5501
 	    t += tlen;
 	}
 	if (!STR_EMBED_P(str)) {
-	    xfree(RSTRING(str)->as.heap.ptr);
+	    ruby_xsizedfree(STR_HEAP_PTR(str), STR_HEAP_SIZE(str));
 	}
 	*t = '\0';
 	RSTRING(str)->as.heap.ptr = buf;
@@ -5573,7 +5577,7 @@ tr_trans(VALUE str, VALUE src, VALUE rep https://github.com/ruby/ruby/blob/trunk/string.c#L5577
 	    t += tlen;
 	}
 	if (!STR_EMBED_P(str)) {
-	    xfree(RSTRING(str)->as.heap.ptr);
+	    ruby_xsizedfree(STR_HEAP_PTR(str), STR_HEAP_SIZE(str));
 	}
 	*t = '\0';
 	RSTRING(str)->as.heap.ptr = buf;
Index: internal.h
===================================================================
--- internal.h	(revision 43331)
+++ internal.h	(revision 43332)
@@ -434,6 +434,7 @@ void rb_gc_writebarrier_remember_promote https://github.com/ruby/ruby/blob/trunk/internal.h#L434
 
 void *ruby_xsizedrealloc(void *ptr, size_t new_size, size_t old_size) RUBY_ATTR_ALLOC_SIZE((2));;
 void ruby_xsizedfree(void *x, size_t size);
+#define SIZED_REALLOC_N(var,type,n,old_n) ((var)=(type*)ruby_xsizedrealloc((char*)(var), (n) * sizeof(type), (old_n) * sizeof(type)))
 
 /* hash.c */
 struct st_table *rb_hash_tbl_raw(VALUE hash);

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

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