Skip to content

SvPV_shrink_to_cur: include space for COW #23290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: blead
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions sv.h
Original file line number Diff line number Diff line change
Expand Up @@ -1590,9 +1590,19 @@ L</C<SV_CHECK_THINKFIRST_COW_DROP>> before calling this.
=cut
*/

#define SvPV_shrink_to_cur(sv) STMT_START { \
const STRLEN _lEnGtH = SvCUR(sv) + 1; \
SvPV_renew(sv, _lEnGtH); \
/* Notes: Ensure the buffer is big enough to be COWed in the future, so
+ 1 for the trailing null byte + 1 for the COW count.
* Don't try to shrink if the saving is too small to be realistic (given
* the realities of allocators) or noticeable.
* Don't try to shrink smaller than PERL_STRLEN_NEW_MIN, as that is
* unlikely to result in a smaller allocation.
*/

#define SvPV_shrink_to_cur(sv) STMT_START { \
const STRLEN _lEnGtH = SvCUR(sv) + 2; \
if ((SvLEN(sv) > _lEnGtH + PTRSIZE) && \
( _lEnGtH >= PERL_STRLEN_NEW_MIN) ) \
SvPV_renew(sv, _lEnGtH); \
} STMT_END

/*
Expand Down
13 changes: 3 additions & 10 deletions toke.c
Original file line number Diff line number Diff line change
Expand Up @@ -4422,9 +4422,7 @@ S_scan_const(pTHX_ char *start)
}

/* shrink the sv if we allocated more than we used */
if (SvCUR(sv) + 5 < SvLEN(sv)) {
SvPV_shrink_to_cur(sv);
}
SvPV_shrink_to_cur(sv);

/* return the substring (via pl_yylval) only if we parsed anything */
if (s > start) {
Expand Down Expand Up @@ -11348,9 +11346,7 @@ S_scan_heredoc(pTHX_ char *s)
SvREFCNT_dec_NN(newstr);
}

if (SvCUR(tmpstr) + 5 < SvLEN(tmpstr)) {
SvPV_shrink_to_cur(tmpstr);
}
SvPV_shrink_to_cur(tmpstr);

if (!IN_BYTES) {
if (UTF && is_utf8_string((U8*)SvPVX_const(tmpstr), SvCUR(tmpstr)))
Expand Down Expand Up @@ -11877,10 +11873,7 @@ Perl_scan_str(pTHX_ char *start, int keep_bracketed_quoted, int keep_delims, int
PL_parser->herelines = herelines;

/* if we allocated too much space, give some back */
if (SvCUR(sv) + 5 < SvLEN(sv)) {
SvLEN_set(sv, SvCUR(sv) + 1);
SvPV_shrink_to_cur(sv);
}
SvPV_shrink_to_cur(sv);

/* decide whether this is the first or second quoted string we've read
for this op
Expand Down
Loading