• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef Py_INTERNAL_RUNTIME_INIT_H
2 #define Py_INTERNAL_RUNTIME_INIT_H
3 #ifdef __cplusplus
4 extern "C" {
5 #endif
6 
7 #ifndef Py_BUILD_CORE
8 #  error "this header requires Py_BUILD_CORE define"
9 #endif
10 
11 #include "pycore_ceval_state.h"   // _PyEval_RUNTIME_PERF_INIT
12 #include "pycore_faulthandler.h"  // _faulthandler_runtime_state_INIT
13 #include "pycore_floatobject.h"   // _py_float_format_unknown
14 #include "pycore_object.h"        // _PyObject_HEAD_INIT
15 #include "pycore_obmalloc_init.h" // _obmalloc_global_state_INIT
16 #include "pycore_parser.h"        // _parser_runtime_state_INIT
17 #include "pycore_pyhash.h"        // pyhash_state_INIT
18 #include "pycore_pymem_init.h"    // _pymem_allocators_standard_INIT
19 #include "pycore_pythread.h"      // _pythread_RUNTIME_INIT
20 #include "pycore_qsbr.h"          // QSBR_INITIAL
21 #include "pycore_runtime_init_generated.h"  // _Py_bytes_characters_INIT
22 #include "pycore_signal.h"        // _signals_RUNTIME_INIT
23 #include "pycore_tracemalloc.h"   // _tracemalloc_runtime_state_INIT
24 
25 
26 extern PyTypeObject _PyExc_MemoryError;
27 
28 
29 /* The static initializers defined here should only be used
30    in the runtime init code (in pystate.c and pylifecycle.c). */
31 
32 #define _PyRuntimeState_INIT(runtime, debug_cookie) \
33     { \
34         .debug_offsets = { \
35             .cookie = debug_cookie, \
36             .version = PY_VERSION_HEX, \
37             .free_threaded = _Py_Debug_Free_Threaded, \
38             .runtime_state = { \
39                 .size = sizeof(_PyRuntimeState), \
40                 .finalizing = offsetof(_PyRuntimeState, _finalizing), \
41                 .interpreters_head = offsetof(_PyRuntimeState, interpreters.head), \
42             }, \
43             .interpreter_state = { \
44                 .size = sizeof(PyInterpreterState), \
45                 .id = offsetof(PyInterpreterState, id), \
46                 .next = offsetof(PyInterpreterState, next), \
47                 .threads_head = offsetof(PyInterpreterState, threads.head), \
48                 .gc = offsetof(PyInterpreterState, gc), \
49                 .imports_modules = offsetof(PyInterpreterState, imports.modules), \
50                 .sysdict = offsetof(PyInterpreterState, sysdict), \
51                 .builtins = offsetof(PyInterpreterState, builtins), \
52                 .ceval_gil = offsetof(PyInterpreterState, ceval.gil), \
53                 .gil_runtime_state = offsetof(PyInterpreterState, _gil), \
54                 .gil_runtime_state_enabled = _Py_Debug_gilruntimestate_enabled, \
55                 .gil_runtime_state_locked = offsetof(PyInterpreterState, _gil.locked), \
56                 .gil_runtime_state_holder = offsetof(PyInterpreterState, _gil.last_holder), \
57             }, \
58             .thread_state = { \
59                 .size = sizeof(PyThreadState), \
60                 .prev = offsetof(PyThreadState, prev), \
61                 .next = offsetof(PyThreadState, next), \
62                 .interp = offsetof(PyThreadState, interp), \
63                 .current_frame = offsetof(PyThreadState, current_frame), \
64                 .thread_id = offsetof(PyThreadState, thread_id), \
65                 .native_thread_id = offsetof(PyThreadState, native_thread_id), \
66                 .datastack_chunk = offsetof(PyThreadState, datastack_chunk), \
67                 .status = offsetof(PyThreadState, _status), \
68             }, \
69             .interpreter_frame = { \
70                 .size = sizeof(_PyInterpreterFrame), \
71                 .previous = offsetof(_PyInterpreterFrame, previous), \
72                 .executable = offsetof(_PyInterpreterFrame, f_executable), \
73                 .instr_ptr = offsetof(_PyInterpreterFrame, instr_ptr), \
74                 .localsplus = offsetof(_PyInterpreterFrame, localsplus), \
75                 .owner = offsetof(_PyInterpreterFrame, owner), \
76             }, \
77             .code_object = { \
78                 .size = sizeof(PyCodeObject), \
79                 .filename = offsetof(PyCodeObject, co_filename), \
80                 .name = offsetof(PyCodeObject, co_name), \
81                 .qualname = offsetof(PyCodeObject, co_qualname), \
82                 .linetable = offsetof(PyCodeObject, co_linetable), \
83                 .firstlineno = offsetof(PyCodeObject, co_firstlineno), \
84                 .argcount = offsetof(PyCodeObject, co_argcount), \
85                 .localsplusnames = offsetof(PyCodeObject, co_localsplusnames), \
86                 .localspluskinds = offsetof(PyCodeObject, co_localspluskinds), \
87                 .co_code_adaptive = offsetof(PyCodeObject, co_code_adaptive), \
88             }, \
89             .pyobject = { \
90                 .size = sizeof(PyObject), \
91                 .ob_type = offsetof(PyObject, ob_type), \
92             }, \
93             .type_object = { \
94                 .size = sizeof(PyTypeObject), \
95                 .tp_name = offsetof(PyTypeObject, tp_name), \
96                 .tp_repr = offsetof(PyTypeObject, tp_repr), \
97                 .tp_flags = offsetof(PyTypeObject, tp_flags), \
98             }, \
99             .tuple_object = { \
100                 .size = sizeof(PyTupleObject), \
101                 .ob_item = offsetof(PyTupleObject, ob_item), \
102                 .ob_size = offsetof(PyTupleObject, ob_base.ob_size), \
103             }, \
104             .list_object = { \
105                 .size = sizeof(PyListObject), \
106                 .ob_item = offsetof(PyListObject, ob_item), \
107                 .ob_size = offsetof(PyListObject, ob_base.ob_size), \
108             }, \
109             .dict_object = { \
110                 .size = sizeof(PyDictObject), \
111                 .ma_keys = offsetof(PyDictObject, ma_keys), \
112                 .ma_values = offsetof(PyDictObject, ma_values), \
113             }, \
114             .float_object = { \
115                 .size = sizeof(PyFloatObject), \
116                 .ob_fval = offsetof(PyFloatObject, ob_fval), \
117             }, \
118             .long_object = { \
119                 .size = sizeof(PyLongObject), \
120                 .lv_tag = offsetof(PyLongObject, long_value.lv_tag), \
121                 .ob_digit = offsetof(PyLongObject, long_value.ob_digit), \
122             }, \
123             .bytes_object = { \
124                 .size = sizeof(PyBytesObject), \
125                 .ob_size = offsetof(PyBytesObject, ob_base.ob_size), \
126                 .ob_sval = offsetof(PyBytesObject, ob_sval), \
127             }, \
128             .unicode_object = { \
129                 .size = sizeof(PyUnicodeObject), \
130                 .state = offsetof(PyUnicodeObject, _base._base.state), \
131                 .length = offsetof(PyUnicodeObject, _base._base.length), \
132                 .asciiobject_size = sizeof(PyASCIIObject), \
133             }, \
134             .gc = { \
135                 .size = sizeof(struct _gc_runtime_state), \
136                 .collecting = offsetof(struct _gc_runtime_state, collecting), \
137             }, \
138         }, \
139         .allocators = { \
140             .standard = _pymem_allocators_standard_INIT(runtime), \
141             .debug = _pymem_allocators_debug_INIT, \
142             .obj_arena = _pymem_allocators_obj_arena_INIT, \
143             .is_debug_enabled = _pymem_is_debug_enabled_INIT, \
144         }, \
145         .obmalloc = _obmalloc_global_state_INIT, \
146         .pyhash_state = pyhash_state_INIT, \
147         .threads = _pythread_RUNTIME_INIT(runtime.threads), \
148         .signals = _signals_RUNTIME_INIT, \
149         .interpreters = { \
150             /* This prevents interpreters from getting created \
151               until _PyInterpreterState_Enable() is called. */ \
152             .next_id = -1, \
153         }, \
154         .xi = { \
155             .registry = { \
156                 .global = 1, \
157             }, \
158         }, \
159         /* A TSS key must be initialized with Py_tss_NEEDS_INIT \
160            in accordance with the specification. */ \
161         .autoTSSkey = Py_tss_NEEDS_INIT, \
162         .parser = _parser_runtime_state_INIT, \
163         .ceval = { \
164             .pending_mainthread = { \
165                 .max = MAXPENDINGCALLS_MAIN, \
166                 .maxloop = MAXPENDINGCALLSLOOP_MAIN, \
167             }, \
168             .perf = _PyEval_RUNTIME_PERF_INIT, \
169         }, \
170         .gilstate = { \
171             .check_enabled = 1, \
172         }, \
173         .fileutils = { \
174             .force_ascii = -1, \
175         }, \
176         .faulthandler = _faulthandler_runtime_state_INIT, \
177         .tracemalloc = _tracemalloc_runtime_state_INIT, \
178         .ref_tracer = { \
179             .tracer_func = NULL, \
180             .tracer_data = NULL, \
181         }, \
182         .stoptheworld = { \
183             .is_global = 1, \
184         }, \
185         .float_state = { \
186             .float_format = _py_float_format_unknown, \
187             .double_format = _py_float_format_unknown, \
188         }, \
189         .types = { \
190             .next_version_tag = 1, \
191         }, \
192         .static_objects = { \
193             .singletons = { \
194                 .small_ints = _Py_small_ints_INIT, \
195                 .bytes_empty = _PyBytes_SIMPLE_INIT(0, 0), \
196                 .bytes_characters = _Py_bytes_characters_INIT, \
197                 .strings = { \
198                     .literals = _Py_str_literals_INIT, \
199                     .identifiers = _Py_str_identifiers_INIT, \
200                     .ascii = _Py_str_ascii_INIT, \
201                     .latin1 = _Py_str_latin1_INIT, \
202                 }, \
203                 .tuple_empty = { \
204                     .ob_base = _PyVarObject_HEAD_INIT(&PyTuple_Type, 0), \
205                 }, \
206                 .hamt_bitmap_node_empty = { \
207                     .ob_base = _PyVarObject_HEAD_INIT(&_PyHamt_BitmapNode_Type, 0), \
208                 }, \
209                 .context_token_missing = { \
210                     .ob_base = _PyObject_HEAD_INIT(&_PyContextTokenMissing_Type), \
211                 }, \
212             }, \
213         }, \
214         ._main_interpreter = _PyInterpreterState_INIT(runtime._main_interpreter), \
215     }
216 
217 #define _PyInterpreterState_INIT(INTERP) \
218     { \
219         .id_refcount = -1, \
220         ._whence = _PyInterpreterState_WHENCE_NOTSET, \
221         .imports = IMPORTS_INIT, \
222         .ceval = { \
223             .recursion_limit = Py_DEFAULT_RECURSION_LIMIT, \
224             .pending = { \
225                 .max = MAXPENDINGCALLS, \
226                 .maxloop = MAXPENDINGCALLSLOOP, \
227             }, \
228         }, \
229         .gc = { \
230             .enabled = 1, \
231             .generations = { \
232                 /* .head is set in _PyGC_InitState(). */ \
233                 { .threshold = 2000, }, \
234                 { .threshold = 10, }, \
235                 { .threshold = 10, }, \
236             }, \
237         }, \
238         .qsbr = { \
239             .wr_seq = QSBR_INITIAL, \
240             .rd_seq = QSBR_INITIAL, \
241         }, \
242         .dtoa = _dtoa_state_INIT(&(INTERP)), \
243         .dict_state = _dict_state_INIT, \
244         .mem_free_queue = _Py_mem_free_queue_INIT(INTERP.mem_free_queue), \
245         .func_state = { \
246             .next_version = 1, \
247         }, \
248         .types = { \
249             .next_version_tag = _Py_TYPE_BASE_VERSION_TAG, \
250         }, \
251         .static_objects = { \
252             .singletons = { \
253                 ._not_used = 1, \
254                 .hamt_empty = { \
255                     .ob_base = _PyObject_HEAD_INIT(&_PyHamt_Type), \
256                     .h_root = (PyHamtNode*)&_Py_SINGLETON(hamt_bitmap_node_empty), \
257                 }, \
258                 .last_resort_memory_error = { \
259                     _PyObject_HEAD_INIT(&_PyExc_MemoryError), \
260                     .args = (PyObject*)&_Py_SINGLETON(tuple_empty) \
261                 }, \
262             }, \
263         }, \
264         ._initial_thread = _PyThreadStateImpl_INIT, \
265     }
266 
267 #define _PyThreadStateImpl_INIT \
268     { \
269         .base = _PyThreadState_INIT, \
270     }
271 
272 #define _PyThreadState_INIT \
273     { \
274         ._whence = _PyThreadState_WHENCE_NOTSET, \
275         .py_recursion_limit = Py_DEFAULT_RECURSION_LIMIT, \
276         .context_ver = 1, \
277     }
278 
279 
280 // global objects
281 
282 #define _PyBytes_SIMPLE_INIT(CH, LEN) \
283     { \
284         _PyVarObject_HEAD_INIT(&PyBytes_Type, (LEN)), \
285         .ob_shash = -1, \
286         .ob_sval = { (CH) }, \
287     }
288 #define _PyBytes_CHAR_INIT(CH) \
289     { \
290         _PyBytes_SIMPLE_INIT((CH), 1) \
291     }
292 
293 #define _PyUnicode_ASCII_BASE_INIT(LITERAL, ASCII) \
294     { \
295         .ob_base = _PyObject_HEAD_INIT(&PyUnicode_Type), \
296         .length = sizeof(LITERAL) - 1, \
297         .hash = -1, \
298         .state = { \
299             .kind = 1, \
300             .compact = 1, \
301             .ascii = (ASCII), \
302             .statically_allocated = 1, \
303         }, \
304     }
305 #define _PyASCIIObject_INIT(LITERAL) \
306     { \
307         ._ascii = _PyUnicode_ASCII_BASE_INIT((LITERAL), 1), \
308         ._data = (LITERAL) \
309     }
310 #define INIT_STR(NAME, LITERAL) \
311     ._py_ ## NAME = _PyASCIIObject_INIT(LITERAL)
312 #define INIT_ID(NAME) \
313     ._py_ ## NAME = _PyASCIIObject_INIT(#NAME)
314 #define _PyUnicode_LATIN1_INIT(LITERAL, UTF8) \
315     { \
316         ._latin1 = { \
317             ._base = _PyUnicode_ASCII_BASE_INIT((LITERAL), 0), \
318             .utf8 = (UTF8), \
319             .utf8_length = sizeof(UTF8) - 1, \
320         }, \
321         ._data = (LITERAL), \
322     }
323 
324 #include "pycore_runtime_init_generated.h"
325 
326 #ifdef __cplusplus
327 }
328 #endif
329 #endif /* !Py_INTERNAL_RUNTIME_INIT_H */
330