Skip to content

Commit 37c7199

Browse files
committed
Add Perl_expected_size stub macro
Passed a number of bytes, this macro will return: * PERL_STRLEN_NEW_MIN if that is larger than the argument value * the argument value rounded up to PTRSIZE The intention is to later convert `Perl_expected_size` into a function that has some knowledge of the underlying malloc library's behaviour and can return values more reflective of the actual useable allocation size for the specified number of bytes. This can aid in reducing unnecessary calls to realloc().
1 parent 434c9cd commit 37c7199

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

embed.fnc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,6 +1157,7 @@ AOdp |SV * |eval_pv |NN const char *p \
11571157
|I32 croak_on_error
11581158
AOdp |SSize_t|eval_sv |NN SV *sv \
11591159
|I32 flags
1160+
EMTpx |Size_t |expected_size |UV size
11601161
ATdmp |bool |extended_utf8_to_uv \
11611162
|NN const U8 * const s \
11621163
|NN const U8 * const e \

perl.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,6 +1686,23 @@ Use L</UV> to declare variables of the maximum usable size on this platform.
16861686
# define PERL_STRLEN_EXPAND_SHIFT 2
16871687
#endif
16881688

1689+
/* "expected_size" is a functional stub for building on in the future.
1690+
* The intention is to pass it a number of bytes, prior to using that
1691+
* number in a call to something like malloc() or realloc(), and a best-
1692+
* guess at the size to be allocated will be returned. ("Best-guess"
1693+
* may vary by platform and malloc implementation.)
1694+
* This will be useful to (1) grow strings (or anything else) in a way
1695+
* that results in SvLEN more accurately reflecting the usable space
1696+
* that has been allocated, and (2) we don't try to shrink an
1697+
* allocation that won't actually shrink in practice.
1698+
* Right now, this macro just rounds up a given number to the nearest
1699+
* multiple of PTRSIZE, for a minimum of PERL_STRLEN_NEW_MIN. This is
1700+
* not entirely useless, just not terribly accurate.
1701+
*/
1702+
#define expected_size(n) ( ((n) > PERL_STRLEN_NEW_MIN) \
1703+
? (((n) + PTRSIZE - 1) & ~(PTRSIZE - 1)) \
1704+
: PERL_STRLEN_NEW_MIN )
1705+
16891706
/* This use of offsetof() requires /Zc:offsetof- for VS2017 (and presumably
16901707
* onwards) when building Socket.xs, but we can just use a different definition
16911708
* for STRUCT_OFFSET instead. */

proto.h

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)