Skip to content

Commit c6b821f

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 89b92b6 commit c6b821f

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
@@ -1193,6 +1193,7 @@ AOdp |SV * |eval_pv |NN const char *p \
11931193
|I32 croak_on_error
11941194
AOdp |SSize_t|eval_sv |NN SV *sv \
11951195
|I32 flags
1196+
EMTpx |Size_t |expected_size |UV size
11961197
ATdmp |bool |extended_utf8_to_uv \
11971198
|NN const U8 * const s \
11981199
|NN const U8 * const e \

perl.h

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

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