Extract minimum PV buffer/AV element size to common definitions

In a nutshell, for a long time the minimum PV length (hardcoded
in Perl_sv_grow) has been 10 bytes and the minimum AV array size
(hardcoded in av_extend_guts) has been 4 elements.

These numbers have been used elsewhere for consistency (e.g.
Perl_sv_grow_fresh) in the past couple of development cycles.
Having a standard definition, rather than hardcoding in multiple
places, is more maintainable. This commit therefore introduces
into perl.h:

    PERL_ARRAY_NEW_MIN_KEY
    PERL_STRLEN_NEW_MIN

(Note: Subsequent commit(s) will actually change the values.)
This commit is contained in:
Richard Leach 2022-11-08 23:40:06 +00:00 committed by Yves Orton
parent b17e77fbd8
commit dbf3614df9
4 changed files with 24 additions and 7 deletions

3
av.c
View File

@ -177,7 +177,8 @@ Perl_av_extend_guts(pTHX_ AV *av, SSize_t key, SSize_t *maxp, SV ***allocp,
PL_stack_max = PL_stack_base + newmax;
}
} else { /* there is no SV* array yet */
*maxp = key < 3 ? 3 : key;
*maxp = key < PERL_ARRAY_NEW_MIN_KEY ?
PERL_ARRAY_NEW_MIN_KEY : key;
{
/* see comment above about newmax+1*/
MEM_WRAP_CHECK_s(*maxp, SV*,

17
perl.h
View File

@ -1593,6 +1593,23 @@ Use L</UV> to declare variables of the maximum usable size on this platform.
#define MEM_SIZE Size_t
/* av_extend and analogues enforce a minimum number of array elements.
* This has been 4 elements (so a minimum key size of 3) for a long
* time, but the rationale behind this seems to have been lost to the
* mists of time. */
#ifndef PERL_ARRAY_NEW_MIN_KEY
#define PERL_ARRAY_NEW_MIN_KEY 3
#endif
/* Functions like Perl_sv_grow mandate a minimum string size.
* This was 10 bytes for a long time, the rationale for which seems lost
* to the mists of time. However, since this does not correlate to what
* modern malloc implementations will actually return, it can be revised
* to be more appropriate. */
#ifndef PERL_STRLEN_NEW_MIN
#define PERL_STRLEN_NEW_MIN 10
#endif
/* Round all values passed to malloc up, by default to a multiple of
sizeof(size_t)
*/

View File

@ -5152,7 +5152,8 @@ Perl_clear_defarray(pTHX_ AV* av, bool abandon)
else {
const SSize_t size = AvFILLp(av) + 1;
/* The ternary gives consistency with av_extend() */
AV *newav = newAV_alloc_x(size < 4 ? 4 : size);
AV *newav = newAV_alloc_x(size < PERL_ARRAY_NEW_MIN_KEY ?
PERL_ARRAY_NEW_MIN_KEY : size);
AvREIFY_only(newav);
PAD_SVl(0) = MUTABLE_SV(newav);
SvREFCNT_dec_NN(av);

8
sv.c
View File

@ -1328,7 +1328,7 @@ Perl_sv_grow(pTHX_ SV *const sv, STRLEN newlen)
if (newlen > SvLEN(sv)) { /* need more room? */
STRLEN minlen = SvCUR(sv);
minlen += (minlen >> PERL_STRLEN_EXPAND_SHIFT) + 10;
minlen += (minlen >> PERL_STRLEN_EXPAND_SHIFT) + PERL_STRLEN_NEW_MIN;
if (newlen < minlen)
newlen = minlen;
#ifndef PERL_UNWARANTED_CHUMMINESS_WITH_MALLOC
@ -1402,10 +1402,8 @@ Perl_sv_grow_fresh(pTHX_ SV *const sv, STRLEN newlen)
newlen++;
#endif
/* 10 is a longstanding, hardcoded minimum length in sv_grow. */
/* Just doing the same here for consistency. */
if (newlen < 10)
newlen = 10;
if (newlen < PERL_STRLEN_NEW_MIN)
newlen = PERL_STRLEN_NEW_MIN;
s = (char*)safemalloc(newlen);
SvPV_set(sv, s);