1 | #ifndef Py_INTERNAL_INTERP_H |
2 | #define Py_INTERNAL_INTERP_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_atomic.h" // _Py_atomic_address |
12 | #include "pycore_ast_state.h" // struct ast_state |
13 | #include "pycore_gil.h" // struct _gil_runtime_state |
14 | #include "pycore_gc.h" // struct _gc_runtime_state |
15 | #include "pycore_warnings.h" // struct _warnings_runtime_state |
16 | |
17 | struct _pending_calls { |
18 | PyThread_type_lock lock; |
19 | /* Request for running pending calls. */ |
20 | _Py_atomic_int calls_to_do; |
21 | /* Request for looking at the `async_exc` field of the current |
22 | thread state. |
23 | Guarded by the GIL. */ |
24 | int async_exc; |
25 | #define NPENDINGCALLS 32 |
26 | struct { |
27 | int (*func)(void *); |
28 | void *arg; |
29 | } calls[NPENDINGCALLS]; |
30 | int first; |
31 | int last; |
32 | }; |
33 | |
34 | struct _ceval_state { |
35 | int recursion_limit; |
36 | /* This single variable consolidates all requests to break out of |
37 | the fast path in the eval loop. */ |
38 | _Py_atomic_int eval_breaker; |
39 | /* Request for dropping the GIL */ |
40 | _Py_atomic_int gil_drop_request; |
41 | struct _pending_calls pending; |
42 | #ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS |
43 | struct _gil_runtime_state gil; |
44 | #endif |
45 | }; |
46 | |
47 | /* fs_codec.encoding is initialized to NULL. |
48 | Later, it is set to a non-NULL string by _PyUnicode_InitEncodings(). */ |
49 | struct _Py_unicode_fs_codec { |
50 | char *encoding; // Filesystem encoding (encoded to UTF-8) |
51 | int utf8; // encoding=="utf-8"? |
52 | char *errors; // Filesystem errors (encoded to UTF-8) |
53 | _Py_error_handler error_handler; |
54 | }; |
55 | |
56 | struct _Py_bytes_state { |
57 | PyObject *empty_string; |
58 | PyBytesObject *characters[256]; |
59 | }; |
60 | |
61 | struct _Py_unicode_ids { |
62 | Py_ssize_t size; |
63 | PyObject **array; |
64 | }; |
65 | |
66 | struct _Py_unicode_state { |
67 | // The empty Unicode object is a singleton to improve performance. |
68 | PyObject *empty_string; |
69 | /* Single character Unicode strings in the Latin-1 range are being |
70 | shared as well. */ |
71 | PyObject *latin1[256]; |
72 | struct _Py_unicode_fs_codec fs_codec; |
73 | |
74 | // Unused member kept for ABI backward compatibility with Python 3.10.0: |
75 | // see bpo-46006. |
76 | PyObject *unused_interned; |
77 | |
78 | // Unicode identifiers (_Py_Identifier): see _PyUnicode_FromId() |
79 | struct _Py_unicode_ids ids; |
80 | }; |
81 | |
82 | struct _Py_float_state { |
83 | /* Special free list |
84 | free_list is a singly-linked list of available PyFloatObjects, |
85 | linked via abuse of their ob_type members. */ |
86 | int numfree; |
87 | PyFloatObject *free_list; |
88 | }; |
89 | |
90 | /* Speed optimization to avoid frequent malloc/free of small tuples */ |
91 | #ifndef PyTuple_MAXSAVESIZE |
92 | // Largest tuple to save on free list |
93 | # define PyTuple_MAXSAVESIZE 20 |
94 | #endif |
95 | #ifndef PyTuple_MAXFREELIST |
96 | // Maximum number of tuples of each size to save |
97 | # define PyTuple_MAXFREELIST 2000 |
98 | #endif |
99 | |
100 | struct _Py_tuple_state { |
101 | #if PyTuple_MAXSAVESIZE > 0 |
102 | /* Entries 1 up to PyTuple_MAXSAVESIZE are free lists, |
103 | entry 0 is the empty tuple () of which at most one instance |
104 | will be allocated. */ |
105 | PyTupleObject *free_list[PyTuple_MAXSAVESIZE]; |
106 | int numfree[PyTuple_MAXSAVESIZE]; |
107 | #endif |
108 | }; |
109 | |
110 | /* Empty list reuse scheme to save calls to malloc and free */ |
111 | #ifndef PyList_MAXFREELIST |
112 | # define PyList_MAXFREELIST 80 |
113 | #endif |
114 | |
115 | struct _Py_list_state { |
116 | PyListObject *free_list[PyList_MAXFREELIST]; |
117 | int numfree; |
118 | }; |
119 | |
120 | #ifndef PyDict_MAXFREELIST |
121 | # define PyDict_MAXFREELIST 80 |
122 | #endif |
123 | |
124 | struct _Py_dict_state { |
125 | /* Dictionary reuse scheme to save calls to malloc and free */ |
126 | PyDictObject *free_list[PyDict_MAXFREELIST]; |
127 | int numfree; |
128 | PyDictKeysObject *keys_free_list[PyDict_MAXFREELIST]; |
129 | int keys_numfree; |
130 | }; |
131 | |
132 | struct _Py_frame_state { |
133 | PyFrameObject *free_list; |
134 | /* number of frames currently in free_list */ |
135 | int numfree; |
136 | }; |
137 | |
138 | #ifndef _PyAsyncGen_MAXFREELIST |
139 | # define _PyAsyncGen_MAXFREELIST 80 |
140 | #endif |
141 | |
142 | struct _Py_async_gen_state { |
143 | /* Freelists boost performance 6-10%; they also reduce memory |
144 | fragmentation, as _PyAsyncGenWrappedValue and PyAsyncGenASend |
145 | are short-living objects that are instantiated for every |
146 | __anext__() call. */ |
147 | struct _PyAsyncGenWrappedValue* value_freelist[_PyAsyncGen_MAXFREELIST]; |
148 | int value_numfree; |
149 | |
150 | struct PyAsyncGenASend* asend_freelist[_PyAsyncGen_MAXFREELIST]; |
151 | int asend_numfree; |
152 | }; |
153 | |
154 | struct _Py_context_state { |
155 | // List of free PyContext objects |
156 | PyContext *freelist; |
157 | int numfree; |
158 | }; |
159 | |
160 | struct _Py_exc_state { |
161 | // The dict mapping from errno codes to OSError subclasses |
162 | PyObject *errnomap; |
163 | PyBaseExceptionObject *memerrors_freelist; |
164 | int memerrors_numfree; |
165 | }; |
166 | |
167 | |
168 | // atexit state |
169 | typedef struct { |
170 | PyObject *func; |
171 | PyObject *args; |
172 | PyObject *kwargs; |
173 | } atexit_callback; |
174 | |
175 | struct atexit_state { |
176 | atexit_callback **callbacks; |
177 | int ncallbacks; |
178 | int callback_len; |
179 | }; |
180 | |
181 | |
182 | // Type attribute lookup cache: speed up attribute and method lookups, |
183 | // see _PyType_Lookup(). |
184 | struct type_cache_entry { |
185 | unsigned int version; // initialized from type->tp_version_tag |
186 | PyObject *name; // reference to exactly a str or None |
187 | PyObject *value; // borrowed reference or NULL |
188 | }; |
189 | |
190 | #define MCACHE_SIZE_EXP 12 |
191 | #define MCACHE_STATS 0 |
192 | |
193 | struct type_cache { |
194 | struct type_cache_entry hashtable[1 << MCACHE_SIZE_EXP]; |
195 | #if MCACHE_STATS |
196 | size_t hits; |
197 | size_t misses; |
198 | size_t collisions; |
199 | #endif |
200 | }; |
201 | |
202 | |
203 | /* interpreter state */ |
204 | |
205 | #define _PY_NSMALLPOSINTS 257 |
206 | #define _PY_NSMALLNEGINTS 5 |
207 | |
208 | // _PyLong_GetZero() and _PyLong_GetOne() must always be available |
209 | #if _PY_NSMALLPOSINTS < 2 |
210 | # error "_PY_NSMALLPOSINTS must be greater than 1" |
211 | #endif |
212 | |
213 | // The PyInterpreterState typedef is in Include/pystate.h. |
214 | struct _is { |
215 | |
216 | struct _is *next; |
217 | struct _ts *tstate_head; |
218 | |
219 | /* Reference to the _PyRuntime global variable. This field exists |
220 | to not have to pass runtime in addition to tstate to a function. |
221 | Get runtime from tstate: tstate->interp->runtime. */ |
222 | struct pyruntimestate *runtime; |
223 | |
224 | int64_t id; |
225 | int64_t id_refcount; |
226 | int requires_idref; |
227 | PyThread_type_lock id_mutex; |
228 | |
229 | int finalizing; |
230 | |
231 | struct _ceval_state ceval; |
232 | struct _gc_runtime_state gc; |
233 | |
234 | // sys.modules dictionary |
235 | PyObject *modules; |
236 | PyObject *modules_by_index; |
237 | // Dictionary of the sys module |
238 | PyObject *sysdict; |
239 | // Dictionary of the builtins module |
240 | PyObject *builtins; |
241 | // importlib module |
242 | PyObject *importlib; |
243 | |
244 | /* Used in Modules/_threadmodule.c. */ |
245 | long num_threads; |
246 | /* Support for runtime thread stack size tuning. |
247 | A value of 0 means using the platform's default stack size |
248 | or the size specified by the THREAD_STACK_SIZE macro. */ |
249 | /* Used in Python/thread.c. */ |
250 | size_t pythread_stacksize; |
251 | |
252 | PyObject *codec_search_path; |
253 | PyObject *codec_search_cache; |
254 | PyObject *codec_error_registry; |
255 | int codecs_initialized; |
256 | |
257 | PyConfig config; |
258 | #ifdef HAVE_DLOPEN |
259 | int dlopenflags; |
260 | #endif |
261 | |
262 | PyObject *dict; /* Stores per-interpreter state */ |
263 | |
264 | PyObject *builtins_copy; |
265 | PyObject *import_func; |
266 | // Initialized to _PyEval_EvalFrameDefault(). |
267 | _PyFrameEvalFunction eval_frame; |
268 | |
269 | Py_ssize_t ; |
270 | freefunc [MAX_CO_EXTRA_USERS]; |
271 | |
272 | #ifdef HAVE_FORK |
273 | PyObject *before_forkers; |
274 | PyObject *after_forkers_parent; |
275 | PyObject *after_forkers_child; |
276 | #endif |
277 | |
278 | uint64_t tstate_next_unique_id; |
279 | |
280 | struct _warnings_runtime_state warnings; |
281 | struct atexit_state atexit; |
282 | |
283 | PyObject *audit_hooks; |
284 | |
285 | /* Small integers are preallocated in this array so that they |
286 | can be shared. |
287 | The integers that are preallocated are those in the range |
288 | -_PY_NSMALLNEGINTS (inclusive) to _PY_NSMALLPOSINTS (not inclusive). |
289 | */ |
290 | PyLongObject* small_ints[_PY_NSMALLNEGINTS + _PY_NSMALLPOSINTS]; |
291 | struct _Py_bytes_state bytes; |
292 | struct _Py_unicode_state unicode; |
293 | struct _Py_float_state float_state; |
294 | /* Using a cache is very effective since typically only a single slice is |
295 | created and then deleted again. */ |
296 | PySliceObject *slice_cache; |
297 | |
298 | struct _Py_tuple_state tuple; |
299 | struct _Py_list_state list; |
300 | struct _Py_dict_state dict_state; |
301 | struct _Py_frame_state frame; |
302 | struct _Py_async_gen_state async_gen; |
303 | struct _Py_context_state context; |
304 | struct _Py_exc_state exc_state; |
305 | |
306 | struct ast_state ast; |
307 | struct type_cache type_cache; |
308 | }; |
309 | |
310 | extern void _PyInterpreterState_ClearModules(PyInterpreterState *interp); |
311 | extern void _PyInterpreterState_Clear(PyThreadState *tstate); |
312 | |
313 | |
314 | /* cross-interpreter data registry */ |
315 | |
316 | /* For now we use a global registry of shareable classes. An |
317 | alternative would be to add a tp_* slot for a class's |
318 | crossinterpdatafunc. It would be simpler and more efficient. */ |
319 | |
320 | struct _xidregitem; |
321 | |
322 | struct _xidregitem { |
323 | PyTypeObject *cls; |
324 | crossinterpdatafunc getdata; |
325 | struct _xidregitem *next; |
326 | }; |
327 | |
328 | PyAPI_FUNC(struct _is*) _PyInterpreterState_LookUpID(int64_t); |
329 | |
330 | PyAPI_FUNC(int) _PyInterpreterState_IDInitref(struct _is *); |
331 | PyAPI_FUNC(int) _PyInterpreterState_IDIncref(struct _is *); |
332 | PyAPI_FUNC(void) _PyInterpreterState_IDDecref(struct _is *); |
333 | |
334 | #ifdef __cplusplus |
335 | } |
336 | #endif |
337 | #endif /* !Py_INTERNAL_INTERP_H */ |
338 | |