Skip to content

Commit f91ad03

Browse files
committed
Document things in EXTERN.h, INTERN.h
1 parent f589a70 commit f91ad03

File tree

3 files changed

+108
-11
lines changed

3 files changed

+108
-11
lines changed

EXTERN.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,8 @@
88
*
99
*/
1010

11-
/*
12-
* EXT: designates a global var which is defined in perl.h
13-
*
14-
* dEXT: designates a global var which is defined in another
15-
* file, so we can't count on finding it in perl.h
16-
* (this practice should be avoided).
17-
*/
11+
/* These are documented in INTERN.h */
12+
1813
#undef EXT
1914
#undef dEXT
2015
#undef EXTCONST

INTERN.h

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,37 @@
99
*/
1010

1111
/*
12-
* EXT designates a global var which is defined in perl.h
13-
* dEXT designates a global var which is defined in another
14-
* file, so we can't count on finding it in perl.h
15-
* (this practice should be avoided).
12+
13+
=for apidoc CmU||EXT
14+
=for apidoc_item EXTCONST
15+
=for apidoc_item dEXT
16+
=for apidoc_item dEXTCONST
17+
18+
These each designate a global variable. The C<CONST> forms indicate that it is
19+
constant.
20+
21+
Use them like this:
22+
23+
Include either EXTERN.h or INTERN.h, but not both
24+
...
25+
#include "perl.h"
26+
...
27+
EXT char PL_WARN_ALL INIT(0);
28+
EXTCONST U8 PL_revision INIT(PERL_REVISION);
29+
30+
This will handle everything for you regarding whether they are to actually be
31+
defined and initialized or just declared C<extern>.
32+
33+
If the initialization is complex, you may have to use C<L</DOINIT>>.
34+
35+
If some constants you wish to reference will not become defined by #including
36+
F<perl.h>, instead use C<dEXT> and C<dEXTCONST> for them and include whatever
37+
files you need to to get them. This is currently very rare, and should be
38+
avoided.
39+
40+
=cut
1641
*/
42+
1743
#undef EXT
1844
#undef dEXT
1945
#undef EXTCONST
@@ -45,7 +71,38 @@
4571
# endif
4672
# endif
4773

74+
/*
75+
=for apidoc Cm||INIT|const_expr
76+
77+
Macro to initialize something, used like so:
78+
79+
EXTCONST char PL_memory_wrap[] INIT("panic: memory wrap");
80+
81+
It expands to nothing in F<EXTERN.h>.
82+
83+
=cut
84+
*/
4885
#undef INIT
4986
#define INIT(...) = __VA_ARGS__
5087

88+
/*
89+
=for apidoc C#||DOINIT
90+
91+
This is defined in F<INTERN.h>, undefined in F<EXTERN.h>
92+
93+
Most of the time you can use C<L</INIT>> to initialize your data structures.
94+
But not always. In such cases, you can do the following:
95+
96+
#ifdef DOINIT
97+
... do the declaration and definition ...
98+
#else
99+
declaration only
100+
#endif
101+
102+
A typical reason for needing this is when the definition includes #ifdef's.
103+
You can't put that portably in a call to C<INIT>, as a macro generally can't
104+
itself contain preprocessor directives.
105+
106+
=cut
107+
*/
51108
#define DOINIT

autodoc.pl

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@
198198
my $filters_scn = 'Source Filters';
199199
my $floating_scn = 'Floating point';
200200
my $genconfig_scn = 'General Configuration';
201+
my $global_definitions_scn = 'Declaration and Initialization of Globals';
201202
my $globals_scn = 'Global Variables';
202203
my $GV_scn = 'GV Handling and Stashes';
203204
my $hook_scn = 'Hook manipulation';
@@ -373,6 +374,49 @@
373374
=back
374375
EOT
375376
},
377+
$global_definitions_scn => {
378+
header => <<~'EOT',
379+
Global variables are defined and initialized in one place, but
380+
referred to from multiple files. They need to be defined and any
381+
initialization done in that one place, but extern declarations
382+
made for them in each file that may refer to them. Note that
383+
there is no harm in declaring a global and not using it.
384+
385+
Perl has a mechanism that allows both purposes to be served while
386+
minimizing code duplication. F<EXTERN.h> and F<INTERN.h> define
387+
the same relatively few macros, but their definitions are
388+
different. In F<EXTERN.h>, the macros expand to
389+
declarations of the globals as external to the file. In
390+
F<INTERN.h> they actually cause the space to be allocated and
391+
possibly initialized.
392+
393+
Most files will follow this paradigm:
394+
395+
#include "EXTERN.h"
396+
...
397+
#include "perl.h">
398+
399+
This causes every global symbol that is referred to in F<perl.h>
400+
and every file it includes (which is nearly every top level Perl
401+
header file) to be declared as external.
402+
403+
The very few files that define globals will instead do
404+
405+
#include "INTERN.h"
406+
...
407+
#include "perl.h">
408+
include the file
409+
410+
It doesn't work for a file to both define some globals and refer
411+
to others as externs. That is, you can only include one of
412+
F<INTERN.h> and F<EXTERN.h>.
413+
414+
This section documents the macros that are defined in these two
415+
header files. F<perl.h> has many uses of them that can serve as
416+
paradigms for you.
417+
EOT
418+
may_be_empty_in_perlapi => 1,
419+
},
376420
$globals_scn => {},
377421
$GV_scn => {},
378422
$hook_scn => {},
@@ -475,6 +519,7 @@
475519
'gv.c' => $GV_scn,
476520
'gv.h' => $GV_scn,
477521
'hv.h' => $HV_scn,
522+
'INTERN.h' => $global_definitions_scn,
478523
'locale.c' => $locale_scn,
479524
'malloc.c' => $memory_scn,
480525
'numeric.c' => $numeric_scn,

0 commit comments

Comments
 (0)