Skip to content

Commit

Permalink
perlguts.pod - add some description of real vs fake AVs
Browse files Browse the repository at this point in the history
  • Loading branch information
richardleach committed Jun 9, 2024
1 parent c854587 commit 10a186f
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions pod/perlguts.pod
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,43 @@ of normal operations. It is therefore safest, unless you are sure of the
lifecycle of an AV, to only use these new functions close to the point
of AV creation.

=head3 Real AVs - and those that are not

The standard or typical AV is sometime referred to as being a "real" AV,
a state that can be checked for with the C<AvREAL> macro. What this state
basically signifies is that:

=over

=item * Every accessible element in the array contains either a NULL
pointer, meaning that the value is uninitialized, or a pointer to a live SV.

=item * When a SV* is assigned to an array element, the reference count on
the SV is incremented. Conversely, when the element is unset, or assigned
a different SV*, the reference count of the expelled SV is decremented.

=back

One reason for an AV to be "fake" is for better performance; this typically
implies that the array elements will not be arbitrarily accessed.

Originally this was the case for the interpreter's argument stack;
however, this gives rise to "stack-not-refcounted" bugs when SVs are
freed during the execution of a statement but still needed later in
that statement. Efforts are currently underway to move to a refcounted
stack to fix this class of bugs. (The argument stack may remain "fake"
in that there's no guarantee that arbitrary elements beyond the stack
pointer will be valid.)

A "fake" AV can be converted into a "real" AV by C<av_reify>, but only if
the AV has the C<SVpav_REIFY> flag set, indicating that it is safe to
convert. The C<AvREIFY()> macro must be used to check for this flag.
Trying to store a SV* into, or delete from, an AV usually involves an
C<AvREIFY()> check and conversion if that macro returns true.

See comments in F<av.h> and the functions in F<av.c> if you really
want to know more.

=head2 Working with HVs

To create an HV, you use the following routine:
Expand Down

0 comments on commit 10a186f

Please sign in to comment.