Proper fix for CvOUTSIDE weak refcounting
[p5sagit/p5-mst-13.2.git] / pod / perlintern.pod
1 =head1 NAME
2
3 perlintern - autogenerated documentation of purely B<internal>
4                  Perl functions
5
6 =head1 DESCRIPTION
7
8 This file is the autogenerated documentation of functions in the
9 Perl interpreter that are documented using Perl's internal documentation
10 format but are not marked as part of the Perl API. In other words,
11 B<they are not for use in extensions>!
12
13
14 =head1 CV reference counts and CvOUTSIDE
15
16 =over 8
17
18 =item CvWEAKOUTSIDE
19
20 Each CV has a pointer, C<CvOUTSIDE()>, to its lexically enclosing
21 CV (if any). Because pointers to anonymous sub prototypes are
22 stored in C<&> pad slots, it is a possible to get a circular reference,
23 with the parent pointing to the child and vice-versa. To avoid the
24 ensuing memory leak, we do not increment the reference count of the CV
25 pointed to by C<CvOUTSIDE> in the I<one specific instance> that the parent
26 has a C<&> pad slot pointing back to us. In this case, we set the
27 C<CvWEAKOUTSIDE> flag in the child. This allows us to determine under what
28 circumstances we should decrement the refcount of the parent when freeing
29 the child.
30
31 There is a further complication with non-closure anonymous subs (ie those
32 that do not refer to any lexicals outside that sub). In this case, the
33 anonymous prototype is shared rather than being cloned. This has the
34 consequence that the parent may be freed while there are still active
35 children, eg
36
37     BEGIN { $a = sub { eval '$x' } }
38
39 In this case, the BEGIN is freed immediately after execution since there
40 are no active references to it: the anon sub prototype has
41 C<CvWEAKOUTSIDE> set since it's not a closure, and $a points to the same
42 CV, so it doesn't contribute to BEGIN's refcount either.  When $a is
43 executed, the C<eval '$x'> causes the chain of C<CvOUTSIDE>s to be followed,
44 and the freed BEGIN is accessed.
45
46 To avoid this, whenever a CV and its associated pad is freed, any
47 C<&> entries in the pad are explicitly removed from the pad, and if the
48 refcount of the pointed-to anon sub is still positive, then that
49 child's C<CvOUTSIDE> is set to point to its grandparent. This will only
50 occur in the single specific case of a non-closure anon prototype
51 having one or more active references (such as C<$a> above).
52
53 One other thing to consider is that a CV may be merely undefined
54 rather than freed, eg C<undef &foo>. In this case, its refcount may
55 not have reached zero, but we still delete its pad and its C<CvROOT> etc.
56 Since various children may still have their C<CvOUTSIDE> pointing at this
57 undefined CV, we keep its own C<CvOUTSIDE> for the time being, so that
58 the chain of lexical scopes is unbroken. For example, the following
59 should print 123:
60
61     my $x = 123;
62     sub tmp { sub { eval '$x' } }
63     my $a = tmp();
64     undef &tmp;
65     print  $a->();
66
67         bool    CvWEAKOUTSIDE(CV *cv)
68
69 =for hackers
70 Found in file cv.h
71
72
73 =back
74
75 =head1 Functions in file pad.h
76
77
78 =over 8
79
80 =item CX_CURPAD_SAVE
81
82 Save the current pad in the given context block structure.
83
84         void    CX_CURPAD_SAVE(struct context)
85
86 =for hackers
87 Found in file pad.h
88
89 =item CX_CURPAD_SV
90
91 Access the SV at offset po in the saved current pad in the given
92 context block structure (can be used as an lvalue).
93
94         SV *    CX_CURPAD_SV(struct context, PADOFFSET po)
95
96 =for hackers
97 Found in file pad.h
98
99 =item PAD_BASE_SV       
100
101 Get the value from slot C<po> in the base (DEPTH=1) pad of a padlist
102
103         SV *    PAD_BASE_SV     (PADLIST padlist, PADOFFSET po)
104
105 =for hackers
106 Found in file pad.h
107
108 =item PAD_CLONE_VARS
109
110 |CLONE_PARAMS* param
111 Clone the state variables associated with running and compiling pads.
112
113         void    PAD_CLONE_VARS(PerlInterpreter *proto_perl \)
114
115 =for hackers
116 Found in file pad.h
117
118 =item PAD_COMPNAME_FLAGS
119
120 Return the flags for the current compiling pad name
121 at offset C<po>. Assumes a valid slot entry.
122
123         U32     PAD_COMPNAME_FLAGS(PADOFFSET po)
124
125 =for hackers
126 Found in file pad.h
127
128 =item PAD_COMPNAME_GEN
129
130 The generation number of the name at offset C<po> in the current
131 compiling pad (lvalue). Note that C<SvCUR> is hijacked for this purpose.
132
133         STRLEN  PAD_COMPNAME_GEN(PADOFFSET po)
134
135 =for hackers
136 Found in file pad.h
137
138 =item PAD_COMPNAME_OURSTASH
139
140 Return the stash associated with an C<our> variable.
141 Assumes the slot entry is a valid C<our> lexical.
142
143         HV *    PAD_COMPNAME_OURSTASH(PADOFFSET po)
144
145 =for hackers
146 Found in file pad.h
147
148 =item PAD_COMPNAME_PV
149
150 Return the name of the current compiling pad name
151 at offset C<po>. Assumes a valid slot entry.
152
153         char *  PAD_COMPNAME_PV(PADOFFSET po)
154
155 =for hackers
156 Found in file pad.h
157
158 =item PAD_COMPNAME_TYPE
159
160 Return the type (stash) of the current compiling pad name at offset
161 C<po>. Must be a valid name. Returns null if not typed.
162
163         HV *    PAD_COMPNAME_TYPE(PADOFFSET po)
164
165 =for hackers
166 Found in file pad.h
167
168 =item PAD_DUP
169
170 Clone a padlist.
171
172         void    PAD_DUP(PADLIST dstpad, PADLIST srcpad, CLONE_PARAMS* param)
173
174 =for hackers
175 Found in file pad.h
176
177 =item PAD_RESTORE_LOCAL
178
179 Restore the old pad saved into the local variable opad by PAD_SAVE_LOCAL()
180
181         void    PAD_RESTORE_LOCAL(PAD *opad)
182
183 =for hackers
184 Found in file pad.h
185
186 =item PAD_SAVE_LOCAL
187
188 Save the current pad to the local variable opad, then make the
189 current pad equal to npad
190
191         void    PAD_SAVE_LOCAL(PAD *opad, PAD *npad)
192
193 =for hackers
194 Found in file pad.h
195
196 =item PAD_SAVE_SETNULLPAD
197
198 Save the current pad then set it to null.
199
200         void    PAD_SAVE_SETNULLPAD()
201
202 =for hackers
203 Found in file pad.h
204
205 =item PAD_SETSV 
206
207 Set the slot at offset C<po> in the current pad to C<sv>
208
209         SV *    PAD_SETSV       (PADOFFSET po, SV* sv)
210
211 =for hackers
212 Found in file pad.h
213
214 =item PAD_SET_CUR       
215
216 Set the current pad to be pad C<n> in the padlist, saving
217 the previous current pad.
218
219         void    PAD_SET_CUR     (PADLIST padlist, I32 n)
220
221 =for hackers
222 Found in file pad.h
223
224 =item PAD_SV    
225
226 Get the value at offset C<po> in the current pad
227
228         void    PAD_SV  (PADOFFSET po)
229
230 =for hackers
231 Found in file pad.h
232
233 =item PAD_SVl   
234
235 Lightweight and lvalue version of C<PAD_SV>.
236 Get or set the value at offset C<po> in the current pad.
237 Unlike C<PAD_SV>, does not print diagnostics with -DX.
238 For internal use only.
239
240         SV *    PAD_SVl (PADOFFSET po)
241
242 =for hackers
243 Found in file pad.h
244
245 =item SAVECLEARSV       
246
247 Clear the pointed to pad value on scope exit. (ie the runtime action of 'my')
248
249         void    SAVECLEARSV     (SV **svp)
250
251 =for hackers
252 Found in file pad.h
253
254 =item SAVECOMPPAD
255
256 save PL_comppad and PL_curpad
257
258
259
260
261
262         void    SAVECOMPPAD()
263
264 =for hackers
265 Found in file pad.h
266
267 =item SAVEPADSV 
268
269 Save a pad slot (used to restore after an iteration)
270
271 XXX DAPM it would make more sense to make the arg a PADOFFSET
272         void    SAVEPADSV       (PADOFFSET po)
273
274 =for hackers
275 Found in file pad.h
276
277
278 =back
279
280 =head1 Functions in file pp_ctl.c
281
282
283 =over 8
284
285 =item find_runcv
286
287 Locate the CV corresponding to the currently executing sub or eval.
288
289         CV*     find_runcv()
290
291 =for hackers
292 Found in file pp_ctl.c
293
294
295 =back
296
297 =head1 Global Variables
298
299 =over 8
300
301 =item PL_DBsingle
302
303 When Perl is run in debugging mode, with the B<-d> switch, this SV is a
304 boolean which indicates whether subs are being single-stepped.
305 Single-stepping is automatically turned on after every step.  This is the C
306 variable which corresponds to Perl's $DB::single variable.  See
307 C<PL_DBsub>.
308
309         SV *    PL_DBsingle
310
311 =for hackers
312 Found in file intrpvar.h
313
314 =item PL_DBsub
315
316 When Perl is run in debugging mode, with the B<-d> switch, this GV contains
317 the SV which holds the name of the sub being debugged.  This is the C
318 variable which corresponds to Perl's $DB::sub variable.  See
319 C<PL_DBsingle>.
320
321         GV *    PL_DBsub
322
323 =for hackers
324 Found in file intrpvar.h
325
326 =item PL_DBtrace
327
328 Trace variable used when Perl is run in debugging mode, with the B<-d>
329 switch.  This is the C variable which corresponds to Perl's $DB::trace
330 variable.  See C<PL_DBsingle>.
331
332         SV *    PL_DBtrace
333
334 =for hackers
335 Found in file intrpvar.h
336
337 =item PL_dowarn
338
339 The C variable which corresponds to Perl's $^W warning variable.
340
341         bool    PL_dowarn
342
343 =for hackers
344 Found in file intrpvar.h
345
346 =item PL_last_in_gv
347
348 The GV which was last used for a filehandle input operation. (C<< <FH> >>)
349
350         GV*     PL_last_in_gv
351
352 =for hackers
353 Found in file thrdvar.h
354
355 =item PL_ofs_sv
356
357 The output field separator - C<$,> in Perl space.
358
359         SV*     PL_ofs_sv
360
361 =for hackers
362 Found in file thrdvar.h
363
364 =item PL_rs
365
366 The input record separator - C<$/> in Perl space.
367
368         SV*     PL_rs
369
370 =for hackers
371 Found in file thrdvar.h
372
373
374 =back
375
376 =head1 GV Functions
377
378 =over 8
379
380 =item is_gv_magical
381
382 Returns C<TRUE> if given the name of a magical GV.
383
384 Currently only useful internally when determining if a GV should be
385 created even in rvalue contexts.
386
387 C<flags> is not used at present but available for future extension to
388 allow selecting particular classes of magical variable.
389
390         bool    is_gv_magical(char *name, STRLEN len, U32 flags)
391
392 =for hackers
393 Found in file gv.c
394
395
396 =back
397
398 =head1 IO Functions
399
400 =over 8
401
402 =item start_glob
403
404 Function called by C<do_readline> to spawn a glob (or do the glob inside
405 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
406 this glob starter is only used by miniperl during the build process.
407 Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
408
409         PerlIO* start_glob(SV* pattern, IO *io)
410
411 =for hackers
412 Found in file doio.c
413
414
415 =back
416
417 =head1 Pad Data Structures
418
419 =over 8
420
421 =item CvPADLIST
422
423 CV's can have CvPADLIST(cv) set to point to an AV.
424
425 For these purposes "forms" are a kind-of CV, eval""s are too (except they're
426 not callable at will and are always thrown away after the eval"" is done
427 executing).
428
429 XSUBs don't have CvPADLIST set - dXSTARG fetches values from PL_curpad,
430 but that is really the callers pad (a slot of which is allocated by
431 every entersub).
432
433 The CvPADLIST AV has does not have AvREAL set, so REFCNT of component items
434 is managed "manual" (mostly in pad.c) rather than normal av.c rules.
435 The items in the AV are not SVs as for a normal AV, but other AVs:
436
437 0'th Entry of the CvPADLIST is an AV which represents the "names" or rather
438 the "static type information" for lexicals.
439
440 The CvDEPTH'th entry of CvPADLIST AV is an AV which is the stack frame at that
441 depth of recursion into the CV.
442 The 0'th slot of a frame AV is an AV which is @_.
443 other entries are storage for variables and op targets.
444
445 During compilation:
446 C<PL_comppad_name> is set the the the names AV.
447 C<PL_comppad> is set the the frame AV for the frame CvDEPTH == 1.
448 C<PL_curpad> is set the body of the frame AV (i.e. AvARRAY(PL_comppad)).
449
450 During execution, C<PL_comppad> and C<PL_curpad> refer to the live
451 frame of the currently executing sub.
452
453 Iterating over the names AV iterates over all possible pad
454 items. Pad slots that are SVs_PADTMP (targets/GVs/constants) end up having
455 &PL_sv_undef "names" (see pad_alloc()).
456
457 Only my/our variable (SVs_PADMY/SVs_PADOUR) slots get valid names.
458 The rest are op targets/GVs/constants which are statically allocated
459 or resolved at compile time.  These don't have names by which they
460 can be looked up from Perl code at run time through eval"" like
461 my/our variables can be.  Since they can't be looked up by "name"
462 but only by their index allocated at compile time (which is usually
463 in PL_op->op_targ), wasting a name SV for them doesn't make sense.
464
465 The SVs in the names AV have their PV being the name of the variable.
466 NV+1..IV inclusive is a range of cop_seq numbers for which the name is
467 valid.  For typed lexicals name SV is SVt_PVMG and SvSTASH points at the
468 type.  For C<our> lexicals, the type is SVt_PVGV, and GvSTASH points at the
469 stash of the associated global (so that duplicate C<our> delarations in the
470 same package can be detected).  SvCUR is sometimes hijacked to
471 store the generation number during compilation.
472
473 If SvFAKE is set on the name SV then slot in the frame AVs are
474 a REFCNT'ed references to a lexical from "outside". In this case,
475 the name SV does not have a cop_seq range, since it is in scope
476 throughout.
477
478 If the 'name' is '&' the the corresponding entry in frame AV
479 is a CV representing a possible closure.
480 (SvFAKE and name of '&' is not a meaningful combination currently but could
481 become so if C<my sub foo {}> is implemented.)
482
483         AV *    CvPADLIST(CV *cv)
484
485 =for hackers
486 Found in file pad.c
487
488 =item cv_clone
489
490 Clone a CV: make a new CV which points to the same code etc, but which
491 has a newly-created pad built by copying the prototype pad and capturing
492 any outer lexicals.
493
494         CV*     cv_clone(CV* proto)
495
496 =for hackers
497 Found in file pad.c
498
499 =item cv_dump
500
501 dump the contents of a CV
502
503         void    cv_dump(CV *cv, char *title)
504
505 =for hackers
506 Found in file pad.c
507
508 =item do_dump_pad
509
510 Dump the contents of a padlist
511
512         void    do_dump_pad(I32 level, PerlIO *file, PADLIST *padlist, int full)
513
514 =for hackers
515 Found in file pad.c
516
517 =item intro_my
518
519 "Introduce" my variables to visible status.
520
521         U32     intro_my()
522
523 =for hackers
524 Found in file pad.c
525
526 =item pad_add_anon
527
528 Add an anon code entry to the current compiling pad
529
530         PADOFFSET       pad_add_anon(SV* sv, OPCODE op_type)
531
532 =for hackers
533 Found in file pad.c
534
535 =item pad_add_name
536
537 Create a new name in the current pad at the specified offset.
538 If C<typestash> is valid, the name is for a typed lexical; set the
539 name's stash to that value.
540 If C<ourstash> is valid, it's an our lexical, set the name's
541 GvSTASH to that value
542
543 Also, if the name is @.. or %.., create a new array or hash for that slot
544
545 If fake, it means we're cloning an existing entry
546
547         PADOFFSET       pad_add_name(char *name, HV* typestash, HV* ourstash, bool clone)
548
549 =for hackers
550 Found in file pad.c
551
552 =item pad_alloc
553
554 Allocate a new my or tmp pad entry. For a my, simply push a null SV onto
555 the end of PL_comppad, but for a tmp, scan the pad from PL_padix upwards
556 for a slot which has no name and and no active value.
557
558         PADOFFSET       pad_alloc(I32 optype, U32 tmptype)
559
560 =for hackers
561 Found in file pad.c
562
563 =item pad_block_start
564
565 Update the pad compilation state variables on entry to a new block
566
567         void    pad_block_start(int full)
568
569 =for hackers
570 Found in file pad.c
571
572 =item pad_check_dup
573
574 Check for duplicate declarations: report any of:
575      * a my in the current scope with the same name;
576      * an our (anywhere in the pad) with the same name and the same stash
577        as C<ourstash>
578 C<is_our> indicates that the name to check is an 'our' declaration
579
580         void    pad_check_dup(char* name, bool is_our, HV* ourstash)
581
582 =for hackers
583 Found in file pad.c
584
585 =item pad_findlex
586
587 Find a named lexical anywhere in a chain of nested pads. Add fake entries
588 in the inner pads if it's found in an outer one. innercv is the CV *inside*
589 the chain of outer CVs to be searched. If newoff is non-null, this is a
590 run-time cloning: don't add fake entries, just find the lexical and add a
591 ref to it at newoff in the current pad.
592
593         PADOFFSET       pad_findlex(char* name, PADOFFSET newoff, CV* innercv)
594
595 =for hackers
596 Found in file pad.c
597
598 =item pad_findmy
599
600 Given a lexical name, try to find its offset, first in the current pad,
601 or failing that, in the pads of any lexically enclosing subs (including
602 the complications introduced by eval). If the name is found in an outer pad,
603 then a fake entry is added to the current pad.
604 Returns the offset in the current pad, or NOT_IN_PAD on failure.
605
606         PADOFFSET       pad_findmy(char* name)
607
608 =for hackers
609 Found in file pad.c
610
611 =item pad_fixup_inner_anons
612
613 For any anon CVs in the pad, change CvOUTSIDE of that CV from
614 old_cv to new_cv if necessary. Needed when a newly-compiled CV has to be
615 moved to a pre-existing CV struct.
616
617         void    pad_fixup_inner_anons(PADLIST *padlist, CV *old_cv, CV *new_cv)
618
619 =for hackers
620 Found in file pad.c
621
622 =item pad_free
623
624 Free the SV at offet po in the current pad.
625
626         void    pad_free(PADOFFSET po)
627
628 =for hackers
629 Found in file pad.c
630
631 =item pad_leavemy
632
633 Cleanup at end of scope during compilation: set the max seq number for
634 lexicals in this scope and warn of any lexicals that never got introduced.
635
636         void    pad_leavemy()
637
638 =for hackers
639 Found in file pad.c
640
641 =item pad_new
642
643 Create a new compiling padlist, saving and updating the various global
644 vars at the same time as creating the pad itself. The following flags
645 can be OR'ed together:
646
647     padnew_CLONE        this pad is for a cloned CV
648     padnew_SAVE         save old globals
649     padnew_SAVESUB      also save extra stuff for start of sub
650
651         PADLIST*        pad_new(padnew_flags flags)
652
653 =for hackers
654 Found in file pad.c
655
656 =item pad_push
657
658 Push a new pad frame onto the padlist, unless there's already a pad at
659 this depth, in which case don't bother creating a new one.
660 If has_args is true, give the new pad an @_ in slot zero.
661
662         void    pad_push(PADLIST *padlist, int depth, int has_args)
663
664 =for hackers
665 Found in file pad.c
666
667 =item pad_reset
668
669 Mark all the current temporaries for reuse
670
671         void    pad_reset()
672
673 =for hackers
674 Found in file pad.c
675
676 =item pad_setsv
677
678 Set the entry at offset po in the current pad to sv.
679 Use the macro PAD_SETSV() rather than calling this function directly.
680
681         void    pad_setsv(PADOFFSET po, SV* sv)
682
683 =for hackers
684 Found in file pad.c
685
686 =item pad_swipe
687
688 Abandon the tmp in the current pad at offset po and replace with a
689 new one.
690
691         void    pad_swipe(PADOFFSET po, bool refadjust)
692
693 =for hackers
694 Found in file pad.c
695
696 =item pad_tidy
697
698 Tidy up a pad after we've finished compiling it:
699     * remove most stuff from the pads of anonsub prototypes;
700     * give it a @_;
701     * mark tmps as such.
702
703         void    pad_tidy(padtidy_type type)
704
705 =for hackers
706 Found in file pad.c
707
708 =item pad_undef
709
710 Free the padlist associated with a CV.
711 If parts of it happen to be current, we null the relevant
712 PL_*pad* global vars so that we don't have any dangling references left.
713 We also repoint the CvOUTSIDE of any about-to-be-orphaned
714 inner subs to the outer of this cv.
715
716 (This function should really be called pad_free, but the name was already
717 taken)
718
719         void    pad_undef(CV* cv)
720
721 =for hackers
722 Found in file pad.c
723
724
725 =back
726
727 =head1 Stack Manipulation Macros
728
729 =over 8
730
731 =item djSP
732
733 Declare Just C<SP>. This is actually identical to C<dSP>, and declares
734 a local copy of perl's stack pointer, available via the C<SP> macro.
735 See C<SP>.  (Available for backward source code compatibility with the
736 old (Perl 5.005) thread model.)
737
738                 djSP;
739
740 =for hackers
741 Found in file pp.h
742
743 =item LVRET
744
745 True if this op will be the return value of an lvalue subroutine
746
747 =for hackers
748 Found in file pp.h
749
750
751 =back
752
753 =head1 SV Manipulation Functions
754
755 =over 8
756
757 =item report_uninit
758
759 Print appropriate "Use of uninitialized variable" warning
760
761         void    report_uninit()
762
763 =for hackers
764 Found in file sv.c
765
766 =item sv_add_arena
767
768 Given a chunk of memory, link it to the head of the list of arenas,
769 and split it into a list of free SVs.
770
771         void    sv_add_arena(char* ptr, U32 size, U32 flags)
772
773 =for hackers
774 Found in file sv.c
775
776 =item sv_clean_all
777
778 Decrement the refcnt of each remaining SV, possibly triggering a
779 cleanup. This function may have to be called multiple times to free
780 SVs which are in complex self-referential hierarchies.
781
782         I32     sv_clean_all()
783
784 =for hackers
785 Found in file sv.c
786
787 =item sv_clean_objs
788
789 Attempt to destroy all objects not yet freed
790
791         void    sv_clean_objs()
792
793 =for hackers
794 Found in file sv.c
795
796 =item sv_free_arenas
797
798 Deallocate the memory used by all arenas. Note that all the individual SV
799 heads and bodies within the arenas must already have been freed.
800
801         void    sv_free_arenas()
802
803 =for hackers
804 Found in file sv.c
805
806
807 =back
808
809 =head1 AUTHORS
810
811 The autodocumentation system was originally added to the Perl core by
812 Benjamin Stuhl. Documentation is by whoever was kind enough to
813 document their functions.
814
815 =head1 SEE ALSO
816
817 perlguts(1), perlapi(1)
818