Skip to content

Commit e256897

Browse files
committed
Document things in EXTERN.h, INTERN.h
1 parent 9d2a16b commit e256897

File tree

3 files changed

+107
-11
lines changed

3 files changed

+107
-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: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,36 @@
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 "perl.h"
24+
...
25+
EXT char PL_WARN_ALL INIT(0);
26+
EXTCONST U8 PL_revision INIT(PERL_REVISION);
27+
28+
This will handle everything for you regarding whether they are to actually be
29+
defined and initialized or just declared C<extern>.
30+
31+
If the initialization is complex, you may have to use C<L</DOINIT>>.
32+
33+
Use C<EXT> and C<EXTCONST> for variables which will become defined by
34+
#including F<perl.h>.
35+
36+
Use C<dEXT> and C<dEXTCONST> for variables that we can't count on finding in
37+
F<perl.h>. (This practice should be avoided).
38+
39+
=cut
1640
*/
41+
1742
#undef EXT
1843
#undef dEXT
1944
#undef EXTCONST
@@ -45,7 +70,38 @@
4570
# endif
4671
# endif
4772

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

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