string.c: size hints for free and realloc calls

Another part of the plan to reduce dependencies on malloc_usable_size:
https://bugs.ruby-lang.org/issues/10238

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63485 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
normal 2018-05-22 01:13:08 +00:00
parent 1c1ff52f43
commit 0a4be5beda

View File

@ -147,7 +147,8 @@ VALUE rb_cSymbol;
}\ }\
else {\ else {\
assert(!FL_TEST((str), STR_SHARED)); \ assert(!FL_TEST((str), STR_SHARED)); \
REALLOC_N(RSTRING(str)->as.heap.ptr, char, (size_t)(capacity) + (termlen));\ SIZED_REALLOC_N(RSTRING(str)->as.heap.ptr, char, \
(size_t)(capacity) + (termlen), STR_HEAP_SIZE(str)); \
RSTRING(str)->as.heap.aux.capa = (capacity);\ RSTRING(str)->as.heap.aux.capa = (capacity);\
}\ }\
} while (0) } while (0)
@ -1586,7 +1587,8 @@ rb_str_init(int argc, VALUE *argv, VALUE str)
RSTRING(str)->as.heap.ptr = ALLOC_N(char, (size_t)capa + termlen); RSTRING(str)->as.heap.ptr = ALLOC_N(char, (size_t)capa + termlen);
} }
else if (STR_HEAP_SIZE(str) != (size_t)capa + termlen) { else if (STR_HEAP_SIZE(str) != (size_t)capa + termlen) {
REALLOC_N(RSTRING(str)->as.heap.ptr, char, (size_t)capa + termlen); SIZED_REALLOC_N(RSTRING(str)->as.heap.ptr, char,
(size_t)capa + termlen, STR_HEAP_SIZE(str));
} }
RSTRING(str)->as.heap.len = len; RSTRING(str)->as.heap.len = len;
TERM_FILL(&RSTRING(str)->as.heap.ptr[len], termlen); TERM_FILL(&RSTRING(str)->as.heap.ptr[len], termlen);
@ -2710,7 +2712,8 @@ rb_str_resize(VALUE str, long len)
} }
else if ((capa = RSTRING(str)->as.heap.aux.capa) < len || else if ((capa = RSTRING(str)->as.heap.aux.capa) < len ||
(capa - len) > (len < 1024 ? len : 1024)) { (capa - len) > (len < 1024 ? len : 1024)) {
REALLOC_N(RSTRING(str)->as.heap.ptr, char, (size_t)len + termlen); SIZED_REALLOC_N(RSTRING(str)->as.heap.ptr, char,
(size_t)len + termlen, STR_HEAP_SIZE(str));
RSTRING(str)->as.heap.aux.capa = len; RSTRING(str)->as.heap.aux.capa = len;
} }
else if (len == slen) return str; else if (len == slen) return str;
@ -6482,7 +6485,7 @@ rb_str_casemap(VALUE source, OnigCaseFoldType *flags, rb_encoding *enc)
while (current_buffer) { while (current_buffer) {
previous_buffer = current_buffer; previous_buffer = current_buffer;
current_buffer = current_buffer->next; current_buffer = current_buffer->next;
xfree(previous_buffer); ruby_sized_xfree(previous_buffer, previous_buffer->capa);
} }
rb_raise(rb_eArgError, "input string invalid"); rb_raise(rb_eArgError, "input string invalid");
} }
@ -6494,7 +6497,7 @@ rb_str_casemap(VALUE source, OnigCaseFoldType *flags, rb_encoding *enc)
if (buffer_count==1) { if (buffer_count==1) {
target = rb_str_new_with_class(source, (const char*)current_buffer->space, target_length); target = rb_str_new_with_class(source, (const char*)current_buffer->space, target_length);
xfree(current_buffer); ruby_sized_xfree(current_buffer, current_buffer->capa);
} }
else { else {
char *target_current; char *target_current;
@ -6508,7 +6511,7 @@ rb_str_casemap(VALUE source, OnigCaseFoldType *flags, rb_encoding *enc)
target_current += current_buffer->used; target_current += current_buffer->used;
previous_buffer = current_buffer; previous_buffer = current_buffer;
current_buffer = current_buffer->next; current_buffer = current_buffer->next;
xfree(previous_buffer); ruby_sized_xfree(previous_buffer, previous_buffer->capa);
} }
} }
@ -7032,8 +7035,9 @@ tr_trans(VALUE str, VALUE src, VALUE repl, int sflag)
if (enc != e1) may_modify = 1; if (enc != e1) may_modify = 1;
} }
if ((offset = t - buf) + tlen > max) { if ((offset = t - buf) + tlen > max) {
size_t old = max + termlen;
max = offset + tlen + (send - s); max = offset + tlen + (send - s);
REALLOC_N(buf, char, max + termlen); SIZED_REALLOC_N(buf, char, max + termlen, old);
t = buf + offset; t = buf + offset;
} }
rb_enc_mbcput(c, t, enc); rb_enc_mbcput(c, t, enc);
@ -7104,8 +7108,9 @@ tr_trans(VALUE str, VALUE src, VALUE repl, int sflag)
if (enc != e1) may_modify = 1; if (enc != e1) may_modify = 1;
} }
if ((offset = t - buf) + tlen > max) { if ((offset = t - buf) + tlen > max) {
size_t old = max + termlen;
max = offset + tlen + (long)((send - s) * 1.2); max = offset + tlen + (long)((send - s) * 1.2);
REALLOC_N(buf, char, max + termlen); SIZED_REALLOC_N(buf, char, max + termlen, old);
t = buf + offset; t = buf + offset;
} }
if (s != t) { if (s != t) {