From 0a23d1655f2bf14f55b8e7bc785e7fb2e0d5d5a4 Mon Sep 17 00:00:00 2001 From: Leon Timmermans Date: Thu, 28 Dec 2017 15:09:11 +0100 Subject: [PATCH] WIP --- sv.c | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/sv.c b/sv.c index 1fc1dda96d5a..b45421c73646 100644 --- a/sv.c +++ b/sv.c @@ -8432,6 +8432,8 @@ in the SV (typically, C is a suitable choice). =cut */ +#define DEFAULT_BUFFER_SIZE 8192 + char * Perl_sv_gets(pTHX_ SV *const sv, PerlIO *const fp, I32 append) { @@ -8444,8 +8446,10 @@ Perl_sv_gets(pTHX_ SV *const sv, PerlIO *const fp, I32 append) #ifdef USE_HEAP_INSTEAD_OF_STACK /* Slower way. */ STDCHAR *buf = NULL; #else - STDCHAR buf[8192]; + STDCHAR buf[DEFAULT_BUFFER_SIZE]; #endif + char *bufptr = NULL; + size_t bufsize = 0; PERL_ARGS_ASSERT_SV_GETS; @@ -8499,10 +8503,13 @@ Perl_sv_gets(pTHX_ SV *const sv, PerlIO *const fp, I32 append) #ifdef PERL_COPY_ON_WRITE /* Add an extra byte for the sake of copy-on-write's * buffer reference count. */ - (void) SvGROW(sv, (STRLEN)((st.st_size - offset) + append + 2)); + STRLEN length = (st.st_size - offset) + append + 2; #else - (void) SvGROW(sv, (STRLEN)((st.st_size - offset) + append + 1)); + STRLEN length = (st.st_size - offset) + append + 1; #endif + (void) SvGROW(sv, length); + bufptr = SvPVX(sv); + bufsize = length; } } rsptr = NULL; @@ -8553,30 +8560,34 @@ Perl_sv_gets(pTHX_ SV *const sv, PerlIO *const fp, I32 append) } #ifdef USE_HEAP_INSTEAD_OF_STACK /* Slower way. */ - Newx(buf, 8192, STDCHAR); + Newx(buf, DEFAULT_BUFFER_SIZE, STDCHAR); assert(buf); #endif + if (bufptr == NULL) { + bufptr = (char*)buf; + bufsize = DEFAULT_BUFFER_SIZE; + } screamer: if (rslen) - cnt = PerlIO_readdelim(fp, buf, sizeof(buf), rslast); + cnt = PerlIO_readdelim(fp, bufptr, bufsize, rslast); else - cnt = PerlIO_read(fp,(char*)buf, sizeof(buf)); + cnt = PerlIO_read(fp, bufptr, bufsize); /* Accommodate broken VAXC compiler, which applies U8 cast to * both args of ?: operator, causing EOF to change into 255 */ if (cnt > 0) - i = (U8)buf[cnt - 1]; + i = (U8)bufptr[cnt - 1]; else i = EOF; if (cnt < 0) cnt = 0; /* we do need to re-set the sv even when cnt <= 0 */ if (append) - sv_catpvn_nomg(sv, (char *) buf, cnt); + sv_catpvn_nomg(sv, (char *) bufptr, cnt); else - sv_setpvn(sv, (char *) buf, cnt); /* "nomg" is implied */ + sv_setpvn(sv, (char *) bufptr, cnt); /* "nomg" is implied */ if (cnt > 0 && /* joy */ (!rslen || @@ -8595,7 +8606,7 @@ Perl_sv_gets(pTHX_ SV *const sv, PerlIO *const fp, I32 append) * * - jik 9/25/96 */ - if (!(cnt < (I32)sizeof(buf) && PerlIO_eof(fp))) + if (!(cnt < (I32)bufsize && PerlIO_eof(fp))) goto screamer; }