Skip to content

Commit 28db151

Browse files
committed
perlguts.pod - mention SV_THINKFIRST* macros, move blocks around
This commit: * Adds some initial description of the SV_THINKFIRST* macros to perlguts * Places that new content, along with Read-only and Copy-on-write sections closer to the sections which describe how to modify string contents. This commit is far from perfect. Reworking the the "Working with SVs" part of perlguts, such that there is a more obviously dedicated section for discussing string handling, seems like it would be definite future improvement.
1 parent c2b9cbd commit 28db151

File tree

1 file changed

+90
-48
lines changed

1 file changed

+90
-48
lines changed

Diff for: pod/perlguts.pod

+90-48
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,96 @@ IV with loss, SvIOKp, SvNOKp and SvNOK will be set, while SvIOK wont be.
521521

522522
In general, though, it's best to use the C<Sv*V> macros.
523523

524+
=head2 Read-Only Values
525+
526+
In Perl 5.16 and earlier, copy-on-write (see the next section) shared a
527+
flag bit with read-only scalars. So the only way to test whether
528+
C<sv_setsv>, etc., will raise a "Modification of a read-only value" error
529+
in those versions is:
530+
531+
SvREADONLY(sv) && !SvIsCOW(sv)
532+
533+
Under Perl 5.18 and later, SvREADONLY only applies to read-only variables,
534+
and, under 5.20, copy-on-write scalars can also be read-only, so the above
535+
check is incorrect. You just want:
536+
537+
SvREADONLY(sv)
538+
539+
If you need to do this check often, define your own macro like this:
540+
541+
#if PERL_VERSION >= 18
542+
# define SvTRULYREADONLY(sv) SvREADONLY(sv)
543+
#else
544+
# define SvTRULYREADONLY(sv) (SvREADONLY(sv) && !SvIsCOW(sv))
545+
#endif
546+
547+
Or better yet, read about THINKFIRST macros below.
548+
549+
=head2 Copy on Write
550+
551+
Perl implements a copy-on-write (COW) mechanism for scalars, in which
552+
string copies are not immediately made when requested, but are deferred
553+
until made necessary by one or the other scalar changing. This is mostly
554+
transparent, but one must take care not to modify string buffers that are
555+
shared by multiple SVs.
556+
557+
You can test whether an SV is using copy-on-write with C<SvIsCOW(sv)>.
558+
559+
You can force an SV to make its own copy of its string buffer by calling
560+
C<sv_force_normal(sv)> or SvPV_force_nolen(sv).
561+
562+
If you want to make the SV drop its string buffer, use
563+
C<sv_force_normal_flags(sv, SV_COW_DROP_PV)> or simply
564+
C<sv_setsv(sv, NULL)>.
565+
566+
All of these functions will croak on read-only scalars (see the previous
567+
section for more on those).
568+
569+
To test that your code is behaving correctly and not modifying COW buffers,
570+
on systems that support L<mmap(2)> (e.g. Unix, Linux, BSDs, macOS) you can
571+
configure perl with C<-Accflags=-DPERL_DEBUG_READONLY_COW> and it will turn
572+
buffer violations into crashes. You will find it to be marvellously slow,
573+
so you may want to skip perl's own tests.
574+
575+
=head2 THINKFIRST before writing to a string buffer
576+
577+
You are more likely to (and usually I<should>) see the C<SvTHINKFIRST(sv)>
578+
macro used to check whether an SV is ready to be written to, as this is a
579+
combined single check to see if the SV:
580+
581+
=over 4
582+
583+
=item *
584+
Points to a COW buffer
585+
586+
=item *
587+
Is READONLY
588+
589+
=item *
590+
Contains a reference
591+
592+
=item *
593+
Has some relevant magic assigned
594+
595+
=back
596+
597+
Two related macros can be used to perform those checks and, if required,
598+
also perform some action:
599+
600+
=over 4
601+
602+
=item *
603+
C<SV_CHECK_THINKFIRST(sv)> calls C<sv_force_normal(sv)> to copy the
604+
string buffer, turning it into a mutable string.
605+
606+
=item *
607+
C<SV_CHECK_THINKFIRST_COW_DROP(sv)> calls C<sv_force_normal_flags(sv, SV_COW_DROP_PV)>
608+
which drops any string buffer pointed to by C<sv>. This is usually
609+
used to avoid copying any COW string to a new buffer, prior to
610+
writing some completely new string to C<sv>.
611+
612+
=back
613+
524614
=head2 Working with AVs
525615

526616
There are two main, longstanding ways to create and load an AV. The first
@@ -1326,54 +1416,6 @@ following code:
13261416
If the order of C<sv_setiv> and C<sv_setpv> had been reversed, then the
13271417
macro C<SvPOK_on> would need to be called instead of C<SvIOK_on>.
13281418

1329-
=head2 Read-Only Values
1330-
1331-
In Perl 5.16 and earlier, copy-on-write (see the next section) shared a
1332-
flag bit with read-only scalars. So the only way to test whether
1333-
C<sv_setsv>, etc., will raise a "Modification of a read-only value" error
1334-
in those versions is:
1335-
1336-
SvREADONLY(sv) && !SvIsCOW(sv)
1337-
1338-
Under Perl 5.18 and later, SvREADONLY only applies to read-only variables,
1339-
and, under 5.20, copy-on-write scalars can also be read-only, so the above
1340-
check is incorrect. You just want:
1341-
1342-
SvREADONLY(sv)
1343-
1344-
If you need to do this check often, define your own macro like this:
1345-
1346-
#if PERL_VERSION >= 18
1347-
# define SvTRULYREADONLY(sv) SvREADONLY(sv)
1348-
#else
1349-
# define SvTRULYREADONLY(sv) (SvREADONLY(sv) && !SvIsCOW(sv))
1350-
#endif
1351-
1352-
=head2 Copy on Write
1353-
1354-
Perl implements a copy-on-write (COW) mechanism for scalars, in which
1355-
string copies are not immediately made when requested, but are deferred
1356-
until made necessary by one or the other scalar changing. This is mostly
1357-
transparent, but one must take care not to modify string buffers that are
1358-
shared by multiple SVs.
1359-
1360-
You can test whether an SV is using copy-on-write with C<SvIsCOW(sv)>.
1361-
1362-
You can force an SV to make its own copy of its string buffer by calling C<sv_force_normal(sv)> or SvPV_force_nolen(sv).
1363-
1364-
If you want to make the SV drop its string buffer, use
1365-
C<sv_force_normal_flags(sv, SV_COW_DROP_PV)> or simply
1366-
C<sv_setsv(sv, NULL)>.
1367-
1368-
All of these functions will croak on read-only scalars (see the previous
1369-
section for more on those).
1370-
1371-
To test that your code is behaving correctly and not modifying COW buffers,
1372-
on systems that support L<mmap(2)> (i.e., Unix) you can configure perl with
1373-
C<-Accflags=-DPERL_DEBUG_READONLY_COW> and it will turn buffer violations
1374-
into crashes. You will find it to be marvellously slow, so you may want to
1375-
skip perl's own tests.
1376-
13771419
=head2 Magic Variables
13781420

13791421
[This section still under construction. Ignore everything here. Post no

0 commit comments

Comments
 (0)