Skip to content

Commit 6ddb6d7

Browse files
committed
perlapi: Reword and correct savepv entry
Shared memory must be freed by a different (undocumented) function than non-shared. This wasn't mentioned before this commit.
1 parent 8777114 commit 6ddb6d7

File tree

2 files changed

+49
-24
lines changed

2 files changed

+49
-24
lines changed

inline.h

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4698,40 +4698,64 @@ Perl_padnamelist_refcnt_inc(PADNAMELIST *pnl)
46984698
=for apidoc_item savesharedpvs
46994699
=for apidoc_item savesharedsvpv
47004700
4701-
Perl's version of C<strdup()> (or C<strndup()> would be if it existed).
4702-
47034701
These each return a pointer to a newly allocated string which is a duplicate of
4704-
the input string.
4702+
the one given by the input argument. Effectively, they implement the C library
4703+
L<C<strdup(3)>>.
4704+
4705+
The forms differ in three main ways:
4706+
4707+
=over
4708+
4709+
=item 1.
4710+
4711+
Whether or not the newly allocated memory is in an area that is sharable
4712+
between threads (the forms with C<shared> in their names) or if it is in an
4713+
area exclusive to the calling thread (the other forms).
4714+
4715+
=item 2.
4716+
4717+
How the string to be copied is specified.
4718+
4719+
In the C<savepv> and C<savesharedpv> forms, the source string is a C language
4720+
NUL-terminated string, or C<NULL>. If C<NULL>, no allocation is done, and
4721+
the functions return NULL.
47054722
4706-
The forms differ in how the string to be copied is specified, and where the new
4707-
memory is allocated from.
4723+
In the C<savepvn> and C<savesharedpvn> forms, C<pv> (if not NULL) points to the
4724+
first byte of the string to duplicate, and an additional parameter, C<len>,
4725+
specifies the number of bytes to copy. Hence, C<pv> may contain embedded-NUL
4726+
characters. It is illegal for C<pv> to be NULL when calling C<savesharedpvn>
4727+
(asserted against in DEBUGGING builds). If it is NULL in C<savepvn>, C<len>
4728+
bytes of zeroed memory are allocated.
4729+
4730+
In the C<savepvs> and C<savesharedpvs> forms, the string must be a C literal
4731+
string, enclosed in double quotes.
4732+
4733+
In the C<savesvpv> and C<savesharedsvpv> forms, the string to duplicate is
4734+
extracted from C<sv> using L</C<SvPV_const>>. C<sv> must not be NULL.
4735+
4736+
=item 3.
4737+
4738+
Memory deallocation
47084739
47094740
To prevent memory leaks, the memory allocated for the new string needs to be
4710-
freed when no longer needed. This can be done with the C<L</Safefree>>
4711-
function, or L<C<SAVEFREEPV>|perlguts/SAVEFREEPV(p)>.
4741+
freed when no longer needed.
47124742
4713-
The forms whose names contain C<shared> differ from the corresponding form
4714-
without that in its name, only in that the memory in the former comes from
4715-
memory shared between threads. This is needed, because on some platforms,
4716-
Windows for example, all allocated memory owned by a thread is deallocated when
4717-
that thread ends. So if you need that not to happen, you need to use the
4718-
shared memory forms.
4743+
=over
47194744
4720-
The string to copy in C<savepvs> is a C language string literal surrounded by
4721-
double quotes.
4745+
=item C<non-shared> forms
47224746
4723-
The string to copy in the forms whose name contains C<svpv> comes from the PV
4724-
in the SV argument C<sv>, using C<SvPV()>
4747+
Use the C<L</Safefree>> function, or L<C<SAVEFREEPV>|perlguts/SAVEFREEPV(p)>.
4748+
However, BE AWARE, this can happen automatically on some platforms, such as
4749+
Windows, when the thread that allocated it ends. So if you need that not to
4750+
happen, you need to use a C<shared> form.
47254751
4726-
The string to copy in the remaining forms comes from the C<pv> argument.
4752+
=item C<shared> forms
47274753
4728-
In the case of C<savepv>, the size of the string is determined by C<strlen()>,
4729-
which means it may not contain embedded C<NUL> characters, and must have a
4730-
trailing C<NUL>.
4754+
Use the C<PerlMemShared_free> function.
47314755
4732-
In the case of C<savepvn>, C<len> gives the length of C<pv>, hence it may
4733-
contain embedded C<NUL> characters. The copy will be guaranteed to have a
4734-
trailing NUL added if not already present.
4756+
=back
4757+
4758+
=back
47354759
47364760
=cut
47374761
*/

t/porting/known_pod_issues.dat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ splain
337337
sprintf(3)
338338
stat(2)
339339
strchr(3)
340+
strdup(3)
340341
strftime(3)
341342
strictures
342343
String::Base

0 commit comments

Comments
 (0)