Skip to content

Improve documentation of sv_dup and sv_dup_inc #23454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions embed.fnc
Original file line number Diff line number Diff line change
Expand Up @@ -6364,9 +6364,9 @@ CRdp |PERL_SI *|si_dup |NULLOK PERL_SI *si \
|NN CLONE_PARAMS *param
CRdp |ANY * |ss_dup |NN PerlInterpreter *proto_perl \
|NN CLONE_PARAMS *param
CRdp |SV * |sv_dup |NULLOK const SV * const ssv \
ARdp |SV * |sv_dup |NULLOK const SV * const ssv \
|NN CLONE_PARAMS * const param
CRdp |SV * |sv_dup_inc |NULLOK const SV * const ssv \
ARdp |SV * |sv_dup_inc |NULLOK const SV * const ssv \
|NN CLONE_PARAMS * const param
# if defined(PERL_IN_OP_C) || defined(PERL_IN_PEEP_C)
p |void |op_relocate_sv |NN SV **svp \
Expand Down
20 changes: 10 additions & 10 deletions sv.c
Original file line number Diff line number Diff line change
Expand Up @@ -14974,18 +14974,18 @@ S_sv_dup_common(pTHX_ const SV *const ssv, CLONE_PARAMS *const param)

In spite of their generic names, these are very specialized functions mainly
for use when cloning an interpreter instance. You are probably looking for
L<perlapi/newSVsv>.
L<perlapi/newSVsv>. Generally speaking you will only want to use these in
either a C<svt_dup> magic handler, or a C<CLONE> method.

They duplicate an SV of any type (not just a plain SV, but including AV, HV
I<etc>.), returning a pointer to the cloned object. The difference is that the
new SV under C<sv_dup> has a reference count of 0, but 1 under C<sv_dup_inc>.
Only specialized cases will want a zero reference count, almost certainly only
when you aren't already holding a reference. Thus, you almost always want to
use the C<sv_dup_inc> form.

The cloning process uses a cache, so that if a particular SV address has
already been duped, that duped SV is returned again rather than creating a
second duplicate.
I<etc>.), returning a pointer to the cloned object. The cloning process uses a
lookup table, so that if a particular source SV address has already been duped,
that duped SV is returned rather than creating a second duplicate.

The difference is that the new SV under C<sv_dup> will not have its reference count incremented
(potentially causing it to be zero), unlike under C<sv_dup_inc>. This is only
desirable when cloning a non-owning pointer. Thus, you almost always want to use
the C<sv_dup_inc> form.

C<param> has type S<C<CLONE_PARAMS *>>. This is mostly for internal core use
when duplicating something more complicated than an SV (code in common is
Expand Down
Loading