blob: 35d237fddc98d2a2e8f6c94b4bbad012ef03eaa7 [file] [log] [blame]
Willy Tarreaud13a9282018-11-25 18:36:15 +01001/*
2 * include/common/initcall.h
3 *
4 * Initcall management.
5 *
6 * Copyright (C) 2018 Willy Tarreau - w@1wt.eu
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining
9 * a copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sublicense, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be
17 * included in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
21 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
23 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26 * OTHER DEALINGS IN THE SOFTWARE.
27 */
28
29#ifndef _COMMON_INIT_H
30#define _COMMON_INIT_H
31
32/* List of known init stages. If others are added, please declare their
33 * section at the end of the file below.
34 */
35enum init_stage {
36 STG_PREPARE = 0, // preset variables, tables, list heads
37 STG_LOCK, // pre-initialize locks
38 STG_ALLOC, // allocate required structures
39 STG_POOL, // create pools
40 STG_REGISTER, // register static lists (keywords etc)
41 STG_INIT, // subsystems normal initialization
42 STG_SIZE // size of the stages array, must be last
43};
44
45/* This is the descriptor for an initcall */
46struct initcall {
47 void (*const fct)(void *arg1, void *arg2, void *arg3);
48 void *arg1;
49 void *arg2;
50 void *arg3;
51};
52
53/* Declare a static variable in the init section dedicated to stage <stg>,
54 * with an element referencing function <function> and arguments <a1..a3>.
55 * <linenum> is needed to deduplicate entries created from a same file. The
56 * trick with (stg<STG_SIZE) consists in verifying that stg if a valid enum
57 * value from the initcall set, and to emit a warning or error if it is not.
58 * The function's type is cast so that it is technically possible to call a
59 * function taking other argument types, provided they are all the same size
60 * as a pointer (args are cast to (void*)). Do not use this macro directly,
61 * use INITCALL{0..3}() instead.
62 */
63#define __DECLARE_INITCALL(stg, linenum, function, a1, a2, a3) \
64 static const struct initcall *__initcb_##linenum \
65 __attribute__((__used__,__section__("init_"#stg))) = \
66 (stg < STG_SIZE) ? &(const struct initcall) { \
67 .fct = (void (*)(void *,void *,void *))function, \
68 .arg1 = (void *)(a1), \
69 .arg2 = (void *)(a2), \
70 .arg3 = (void *)(a3), \
71 } : NULL
72
73/* This is used to resolve <linenum> to an integer before calling
74 * __DECLARE_INITCALL(). Do not use this macro directly, use INITCALL{0..3}()
75 * instead.
76 */
77#define _DECLARE_INITCALL(...) \
78 __DECLARE_INITCALL(__VA_ARGS__)
79
80/* This requires that function <function> is called with pointer argument
81 * <argument> during init stage <stage> which must be one of init_stage.
82 */
83#define INITCALL0(stage, function) \
84 _DECLARE_INITCALL(stage, __LINE__, function, 0, 0, 0)
85
86/* This requires that function <function> is called with pointer argument
87 * <argument> during init stage <stage> which must be one of init_stage.
88 */
89#define INITCALL1(stage, function, arg1) \
90 _DECLARE_INITCALL(stage, __LINE__, function, arg1, 0, 0)
91
92/* This requires that function <function> is called with pointer arguments
93 * <arg1..2> during init stage <stage> which must be one of init_stage.
94 */
95#define INITCALL2(stage, function, arg1, arg2) \
96 _DECLARE_INITCALL(stage, __LINE__, function, arg1, arg2, 0)
97
98/* This requires that function <function> is called with pointer arguments
99 * <arg1..3> during init stage <stage> which must be one of init_stage.
100 */
101#define INITCALL3(stage, function, arg1, arg2, arg3) \
102 _DECLARE_INITCALL(stage, __LINE__, function, arg1, arg2, arg3)
103
104/* Iterate pointer p (of type initcall**) over all registered calls at
105 * stage <stg>.
106 */
107#define FOREACH_INITCALL(p,stg) \
108 for ((p) = &(__start_init_##stg); (p) < &(__stop_init_##stg); (p)++)
109
110/* Declare a section for stage <stg>. The start and stop pointers are set by
111 * the linker itself, which is why they're declared extern here. The weak
112 * attribute is used so that we declare them ourselves if the section is
113 * empty. The corresponding sections must contain exclusively pointers to
114 * make sure each location may safely be visited by incrementing a pointer.
115 */
116#define DECLARE_INIT_SECTION(stg) \
117 extern __attribute__((__weak__)) const struct initcall *__start_init_##stg; \
118 extern __attribute__((__weak__)) const struct initcall *__stop_init_##stg
119
120/* Declare all initcall sections here */
121DECLARE_INIT_SECTION(STG_PREPARE);
122DECLARE_INIT_SECTION(STG_LOCK);
123DECLARE_INIT_SECTION(STG_ALLOC);
124DECLARE_INIT_SECTION(STG_POOL);
125DECLARE_INIT_SECTION(STG_REGISTER);
126DECLARE_INIT_SECTION(STG_INIT);
127
128/* not needed anymore */
129#undef DECLARE_INIT_SECTION
130
131/* Run the initcalls for stage <stg>. The test on <stg> is only there to
132 * ensure it is a valid initcall stage.
133 */
134#define RUN_INITCALLS(stg) \
135 do { \
136 const struct initcall **ptr; \
137 if (stg >= STG_SIZE) \
138 break; \
139 FOREACH_INITCALL(ptr, stg) \
140 (*ptr)->fct((*ptr)->arg1, (*ptr)->arg2, (*ptr)->arg3); \
141 } while (0)
142
143#endif /* _COMMON_INIT_H */
144
145/*
146 * Local variables:
147 * c-indent-level: 8
148 * c-basic-offset: 8
149 * End:
150 */