Debian Codegraph @

Include/internal/pycore_runtime_structs.h

raw
1 /* This file contains the struct definitions for the runtime, interpreter
2 * and thread states, plus all smaller structs contained therein */
3
4 #ifndef Py_INTERNAL_RUNTIME_STRUCTS_H
5 #define Py_INTERNAL_RUNTIME_STRUCTS_H
6 #ifdef __cplusplus
7 extern "C" {
8 #endif
9
10 #include "pycore_interp_structs.h" // _PyGC_Head_UNUSED
11 #include "pycore_obmalloc.h" // struct _obmalloc_global_state
12
13 /************ Runtime state ************/
14
15 typedef struct {
16 /* We tag each block with an API ID in order to tag API violations */
17 char api_id;
18 PyMemAllocatorEx alloc;
19 } debug_alloc_api_t;
20
21 struct _pymem_allocators {
22 PyMutex mutex;
23 struct {
24 PyMemAllocatorEx raw;
25 PyMemAllocatorEx mem;
26 PyMemAllocatorEx obj;
27 } standard;
28 struct {
29 debug_alloc_api_t raw;
30 debug_alloc_api_t mem;
31 debug_alloc_api_t obj;
32 } debug;
33 int is_debug_enabled;
34 PyObjectArenaAllocator obj_arena;
35 };
36
37 enum _py_float_format_type {
38 _py_float_format_unknown,
39 _py_float_format_ieee_big_endian,
40 _py_float_format_ieee_little_endian,
41 };
42
43 struct _Py_float_runtime_state {
44 enum _py_float_format_type float_format;
45 enum _py_float_format_type double_format;
46 };
47
48 struct pyhash_runtime_state {
49 struct {
50 #ifndef MS_WINDOWS
51 int fd;
52 dev_t st_dev;
53 ino_t st_ino;
54 #else
55 // This is a placeholder so the struct isn't empty on Windows.
56 int _not_used;
57 #endif
58 } urandom_cache;
59 };
60
61 #include "pycore_tracemalloc.h" // struct _tracemalloc_runtime_state
62
63 struct _fileutils_state {
64 int force_ascii;
65 };
66
67 #include "pycore_interpframe_structs.h" // _PyInterpreterFrame
68 #include "pycore_debug_offsets.h" // _Py_DebugOffsets
69 #include "pycore_signal.h" // struct _signals_runtime_state
70 #include "pycore_faulthandler.h" // struct _faulthandler_runtime_state
71 #include "pycore_ast.h" // struct _expr
72
73 #ifdef Py_DEBUG
74 #define _PYPEGEN_NSTATISTICS 2000
75 #endif
76
77 struct _parser_runtime_state {
78 #ifdef Py_DEBUG
79 long memo_statistics[_PYPEGEN_NSTATISTICS];
80 #ifdef Py_GIL_DISABLED
81 PyMutex mutex;
82 #endif
83 #else
84 int _not_used;
85 #endif
86 struct _expr dummy_name;
87 };
88
89 typedef struct {
90 PyTime_t numer;
91 PyTime_t denom;
92 } _PyTimeFraction;
93
94 struct _Py_time_runtime_state {
95 #if defined(MS_WINDOWS) || defined(__APPLE__)
96 _PyTimeFraction base;
97 #else
98 char _unused;
99 #endif
100 };
101
102
103 struct _Py_cached_objects {
104 // XXX We could statically allocate the hashtable.
105 _Py_hashtable_t *interned_strings;
106 };
107
108 // These would be in pycore_long.h if it weren't for an include cycle.
109 #define _PY_NSMALLPOSINTS 257
110 #define _PY_NSMALLNEGINTS 5
111
112 #include "pycore_global_strings.h" // struct _Py_global_strings
113
114 struct _Py_static_objects {
115 struct {
116 /* Small integers are preallocated in this array so that they
117 * can be shared.
118 * The integers that are preallocated are those in the range
119 * -_PY_NSMALLNEGINTS (inclusive) to _PY_NSMALLPOSINTS (exclusive).
120 */
121 PyLongObject small_ints[_PY_NSMALLNEGINTS + _PY_NSMALLPOSINTS];
122
123 PyBytesObject bytes_empty;
124 struct {
125 PyBytesObject ob;
126 char eos;
127 } bytes_characters[256];
128
129 struct _Py_global_strings strings;
130
131 _PyGC_Head_UNUSED _tuple_empty_gc_not_used;
132 PyTupleObject tuple_empty;
133
134 _PyGC_Head_UNUSED _hamt_bitmap_node_empty_gc_not_used;
135 PyHamtNode_Bitmap hamt_bitmap_node_empty;
136 _PyContextTokenMissing context_token_missing;
137 } singletons;
138 };
139
140 /* Full Python runtime state */
141
142 /* _PyRuntimeState holds the global state for the CPython runtime.
143 That data is exported by the internal API as a global variable
144 (_PyRuntime, defined near the top of pylifecycle.c).
145 */
146 struct pyruntimestate {
147 /* This field must be first to facilitate locating it by out of process
148 * debuggers. Out of process debuggers will use the offsets contained in this
149 * field to be able to locate other fields in several interpreter structures
150 * in a way that doesn't require them to know the exact layout of those
151 * structures.
152 *
153 * IMPORTANT:
154 * This struct is **NOT** backwards compatible between minor version of the
155 * interpreter and the members, order of members and size can change between
156 * minor versions. This struct is only guaranteed to be stable between patch
157 * versions for a given minor version of the interpreter.
158 */
159 _Py_DebugOffsets debug_offsets;
160
161 /* Has been initialized to a safe state.
162
163 In order to be effective, this must be set to 0 during or right
164 after allocation. */
165 int _initialized;
166
167 /* Is running Py_PreInitialize()? */
168 int preinitializing;
169
170 /* Is Python preinitialized? Set to 1 by Py_PreInitialize() */
171 int preinitialized;
172
173 /* Is Python core initialized? Set to 1 by _Py_InitializeCore() */
174 int core_initialized;
175
176 /* Is Python fully initialized? Set to 1 by Py_Initialize() */
177 int initialized;
178
179 /* Set by Py_FinalizeEx(). Only reset to NULL if Py_Initialize()
180 is called again.
181
182 Use _PyRuntimeState_GetFinalizing() and _PyRuntimeState_SetFinalizing()
183 to access it, don't access it directly. */
184 PyThreadState *_finalizing;
185 /* The ID of the OS thread in which we are finalizing. */
186 unsigned long _finalizing_id;
187
188 struct pyinterpreters {
189 PyMutex mutex;
190 /* The linked list of interpreters, newest first. */
191 PyInterpreterState *head;
192 /* The runtime's initial interpreter, which has a special role
193 in the operation of the runtime. It is also often the only
194 interpreter. */
195 PyInterpreterState *main;
196 /* next_id is an auto-numbered sequence of small
197 integers. It gets initialized in _PyInterpreterState_Enable(),
198 which is called in Py_Initialize(), and used in
199 PyInterpreterState_New(). A negative interpreter ID
200 indicates an error occurred. The main interpreter will
201 always have an ID of 0. Overflow results in a RuntimeError.
202 If that becomes a problem later then we can adjust, e.g. by
203 using a Python int. */
204 int64_t next_id;
205 } interpreters;
206
207 /* Platform-specific identifier and PyThreadState, respectively, for the
208 main thread in the main interpreter. */
209 unsigned long main_thread;
210 PyThreadState *main_tstate;
211
212 /* ---------- IMPORTANT ---------------------------
213 The fields above this line are declared as early as
214 possible to facilitate out-of-process observability
215 tools. */
216
217 /* cross-interpreter data and utils */
218 _PyXI_global_state_t xi;
219
220 struct _pymem_allocators allocators;
221 struct _obmalloc_global_state obmalloc;
222 struct pyhash_runtime_state pyhash_state;
223 struct _pythread_runtime_state threads;
224 struct _signals_runtime_state signals;
225
226 /* Used for the thread state bound to the current thread. */
227 Py_tss_t autoTSSkey;
228
229 /* Used instead of PyThreadState.trash when there is not current tstate. */
230 Py_tss_t trashTSSkey;
231
232 PyWideStringList orig_argv;
233
234 struct _parser_runtime_state parser;
235
236 struct _atexit_runtime_state atexit;
237
238 struct _import_runtime_state imports;
239 struct _ceval_runtime_state ceval;
240 struct _gilstate_runtime_state {
241 /* bpo-26558: Flag to disable PyGILState_Check().
242 If set to non-zero, PyGILState_Check() always return 1. */
243 int check_enabled;
244 /* The single PyInterpreterState used by this process'
245 GILState implementation
246 */
247 /* TODO: Given interp_main, it may be possible to kill this ref */
248 PyInterpreterState *autoInterpreterState;
249 } gilstate;
250 struct _getargs_runtime_state {
251 struct _PyArg_Parser *static_parsers;
252 } getargs;
253 struct _fileutils_state fileutils;
254 struct _faulthandler_runtime_state faulthandler;
255 struct _tracemalloc_runtime_state tracemalloc;
256 struct _reftracer_runtime_state ref_tracer;
257
258 // The rwmutex is used to prevent overlapping global and per-interpreter
259 // stop-the-world events. Global stop-the-world events lock the mutex
260 // exclusively (as a "writer"), while per-interpreter stop-the-world events
261 // lock it non-exclusively (as "readers").
262 _PyRWMutex stoptheworld_mutex;
263 struct _stoptheworld_state stoptheworld;
264
265 PyPreConfig preconfig;
266
267 // Audit values must be preserved when Py_Initialize()/Py_Finalize()
268 // is called multiple times.
269 Py_OpenCodeHookFunction open_code_hook;
270 void *open_code_userdata;
271 struct {
272 PyMutex mutex;
273 struct _Py_AuditHookEntry *head;
274 } audit_hooks;
275
276 struct _py_object_runtime_state object_state;
277 struct _Py_float_runtime_state float_state;
278 struct _Py_unicode_runtime_state unicode_state;
279 struct _types_runtime_state types;
280 struct _Py_time_runtime_state time;
281
282 #if defined(__EMSCRIPTEN__) && defined(PY_CALL_TRAMPOLINE)
283 // Used in "Python/emscripten_trampoline.c" to choose between wasm-gc
284 // trampoline and JavaScript trampoline.
285 PyObject* (*emscripten_trampoline)(int* success,
286 PyCFunctionWithKeywords func,
287 PyObject* self,
288 PyObject* args,
289 PyObject* kw);
290 #endif
291
292 /* All the objects that are shared by the runtime's interpreters. */
293 struct _Py_cached_objects cached_objects;
294 struct _Py_static_objects static_objects;
295
296 /* The following fields are here to avoid allocation during init.
297 The data is exposed through _PyRuntimeState pointer fields.
298 These fields should not be accessed directly outside of init.
299
300 All other _PyRuntimeState pointer fields are populated when
301 needed and default to NULL.
302
303 For now there are some exceptions to that rule, which require
304 allocation during init. These will be addressed on a case-by-case
305 basis. Most notably, we don't pre-allocated the several mutex
306 (PyThread_type_lock) fields, because on Windows we only ever get
307 a pointer type.
308 */
309
310 /* _PyRuntimeState.interpreters.main */
311 PyInterpreterState _main_interpreter;
312 // _main_interpreter should be the last field of _PyRuntimeState.
313 // See https://github.com/python/cpython/issues/127117.
314 };
315
316
317 #ifdef __cplusplus
318 }
319 #endif
320 #endif /* Py_INTERNAL_RUNTIME_STRUCTS_H */