summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorko1 <ko1@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-10-17 08:35:06 +0000
committerko1 <ko1@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-10-17 08:35:06 +0000
commit3de7ec0a3f74932d1b65089e606f194cb816e165 (patch)
tree03ea8903abe36a126c348dd945bee64e00c1eba4
parentd66c5768caaee16a0c2c2c6411858d23fb9f21a9 (diff)
* array.c, string.c: use ruby_xsizedfree() and ruby_xsizedrealloc().
* internal.h (SIZED_REALLOC_N): define a macro as REALLOC_N(). git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43332 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog6
-rw-r--r--array.c21
-rw-r--r--internal.h1
-rw-r--r--string.c16
4 files changed, 29 insertions, 15 deletions
diff --git a/ChangeLog b/ChangeLog
index 705ea75..2561e84 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Thu Oct 17 17:32:51 2013 Koichi Sasada <ko1@atdot.net>
+
+ * 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@ruby-lang.org>
* win32/win32.c (console_emulator_p): check by comparison between
diff --git a/array.c b/array.c
index df09891..2224a5c 100644
--- a/array.c
+++ b/array.c
@@ -109,6 +109,7 @@ ary_memcpy(VALUE ary, long beg, long argc, const VALUE *argv)
(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)
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)
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
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, VALUE ary)
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)
}
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)
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)
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)
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);
diff --git a/internal.h b/internal.h
index b8c0df6..efa5c1e 100644
--- a/internal.h
+++ b/internal.h
@@ -434,6 +434,7 @@ void rb_gc_writebarrier_remember_promoted(VALUE obj);
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);
diff --git a/string.c b/string.c
index 5cdf7a3..fce8a05 100644
--- a/string.c
+++ b/string.c
@@ -126,6 +126,9 @@ VALUE rb_cSymbol;
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)
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)
{
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)
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 repl, int sflag)
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 repl, int sflag)
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;