| 1 |
/* This file contains the struct definitions for interpreter state |
| 2 |
* and other necessary structs */ |
| 3 |
|
| 4 |
#ifndef Py_INTERNAL_INTERP_STRUCTS_H |
| 5 |
#define Py_INTERNAL_INTERP_STRUCTS_H |
| 6 |
#ifdef __cplusplus |
| 7 |
extern "C" { |
| 8 |
#endif |
| 9 |
|
| 10 |
#include "pycore_ast_state.h" // struct ast_state |
| 11 |
#include "pycore_llist.h" // struct llist_node |
| 12 |
#include "pycore_opcode_utils.h" // NUM_COMMON_CONSTANTS |
| 13 |
#include "pycore_pymath.h" // _PY_SHORT_FLOAT_REPR |
| 14 |
#include "pycore_structs.h" // PyHamtObject |
| 15 |
#include "pycore_tstate.h" // _PyThreadStateImpl |
| 16 |
#include "pycore_typedefs.h" // _PyRuntimeState |
| 17 |
|
| 18 |
|
| 19 |
#define CODE_MAX_WATCHERS 8 |
| 20 |
#define CONTEXT_MAX_WATCHERS 8 |
| 21 |
#define FUNC_MAX_WATCHERS 8 |
| 22 |
#define TYPE_MAX_WATCHERS 8 |
| 23 |
|
| 24 |
|
| 25 |
#ifdef Py_GIL_DISABLED |
| 26 |
// This should be prime but otherwise the choice is arbitrary. A larger value |
| 27 |
// increases concurrency at the expense of memory. |
| 28 |
# define NUM_WEAKREF_LIST_LOCKS 127 |
| 29 |
#endif |
| 30 |
|
| 31 |
typedef int (*_Py_pending_call_func)(void *); |
| 32 |
|
| 33 |
struct _pending_call { |
| 34 |
_Py_pending_call_func func; |
| 35 |
void *arg; |
| 36 |
int flags; |
| 37 |
}; |
| 38 |
|
| 39 |
#define PENDINGCALLSARRAYSIZE 300 |
| 40 |
|
| 41 |
struct _pending_calls { |
| 42 |
PyThreadState *handling_thread; |
| 43 |
PyMutex mutex; |
| 44 |
/* Request for running pending calls. */ |
| 45 |
int32_t npending; |
| 46 |
/* The maximum allowed number of pending calls. |
| 47 |
If the queue fills up to this point then _PyEval_AddPendingCall() |
| 48 |
will return _Py_ADD_PENDING_FULL. */ |
| 49 |
int32_t max; |
| 50 |
/* We don't want a flood of pending calls to interrupt any one thread |
| 51 |
for too long, so we keep a limit on the number handled per pass. |
| 52 |
A value of 0 means there is no limit (other than the maximum |
| 53 |
size of the list of pending calls). */ |
| 54 |
int32_t maxloop; |
| 55 |
struct _pending_call calls[PENDINGCALLSARRAYSIZE]; |
| 56 |
int first; |
| 57 |
int next; |
| 58 |
}; |
| 59 |
|
| 60 |
typedef enum { |
| 61 |
PERF_STATUS_FAILED = -1, // Perf trampoline is in an invalid state |
| 62 |
PERF_STATUS_NO_INIT = 0, // Perf trampoline is not initialized |
| 63 |
PERF_STATUS_OK = 1, // Perf trampoline is ready to be executed |
| 64 |
} perf_status_t; |
| 65 |
|
| 66 |
#ifdef PY_HAVE_PERF_TRAMPOLINE |
| 67 |
struct code_arena_st; |
| 68 |
|
| 69 |
struct trampoline_api_st { |
| 70 |
void* (*init_state)(void); |
| 71 |
void (*write_state)(void* state, const void *code_addr, |
| 72 |
unsigned int code_size, PyCodeObject* code); |
| 73 |
int (*free_state)(void* state); |
| 74 |
void *state; |
| 75 |
Py_ssize_t code_padding; |
| 76 |
}; |
| 77 |
#endif |
| 78 |
|
| 79 |
|
| 80 |
struct _ceval_runtime_state { |
| 81 |
struct { |
| 82 |
#ifdef PY_HAVE_PERF_TRAMPOLINE |
| 83 |
perf_status_t status; |
| 84 |
int perf_trampoline_type; |
| 85 |
Py_ssize_t extra_code_index; |
| 86 |
struct code_arena_st *code_arena; |
| 87 |
struct trampoline_api_st trampoline_api; |
| 88 |
FILE *map_file; |
| 89 |
Py_ssize_t persist_after_fork; |
| 90 |
_PyFrameEvalFunction prev_eval_frame; |
| 91 |
Py_ssize_t trampoline_refcount; |
| 92 |
int code_watcher_id; |
| 93 |
#else |
| 94 |
int _not_used; |
| 95 |
#endif |
| 96 |
} perf; |
| 97 |
/* Pending calls to be made only on the main thread. */ |
| 98 |
// The signal machinery falls back on this |
| 99 |
// so it must be especially stable and efficient. |
| 100 |
// For example, we use a preallocated array |
| 101 |
// for the list of pending calls. |
| 102 |
struct _pending_calls pending_mainthread; |
| 103 |
PyMutex unused_sys_trace_profile_mutex; // kept for ABI compatibility |
| 104 |
}; |
| 105 |
|
| 106 |
|
| 107 |
struct _ceval_state { |
| 108 |
/* This variable holds the global instrumentation version. When a thread is |
| 109 |
running, this value is overlaid onto PyThreadState.eval_breaker so that |
| 110 |
changes in the instrumentation version will trigger the eval breaker. */ |
| 111 |
uintptr_t instrumentation_version; |
| 112 |
int recursion_limit; |
| 113 |
struct _gil_runtime_state *gil; |
| 114 |
int own_gil; |
| 115 |
struct _pending_calls pending; |
| 116 |
}; |
| 117 |
|
| 118 |
|
| 119 |
//############### |
| 120 |
// runtime atexit |
| 121 |
|
| 122 |
typedef void (*atexit_callbackfunc)(void); |
| 123 |
|
| 124 |
struct _atexit_runtime_state { |
| 125 |
PyMutex mutex; |
| 126 |
#define NEXITFUNCS 32 |
| 127 |
atexit_callbackfunc callbacks[NEXITFUNCS]; |
| 128 |
int ncallbacks; |
| 129 |
}; |
| 130 |
|
| 131 |
|
| 132 |
//################### |
| 133 |
// interpreter atexit |
| 134 |
|
| 135 |
typedef void (*atexit_datacallbackfunc)(void *); |
| 136 |
|
| 137 |
typedef struct atexit_callback { |
| 138 |
atexit_datacallbackfunc func; |
| 139 |
void *data; |
| 140 |
struct atexit_callback *next; |
| 141 |
} atexit_callback; |
| 142 |
|
| 143 |
struct atexit_state { |
| 144 |
#ifdef Py_GIL_DISABLED |
| 145 |
PyMutex ll_callbacks_lock; |
| 146 |
#endif |
| 147 |
atexit_callback *ll_callbacks; |
| 148 |
|
| 149 |
// XXX The rest of the state could be moved to the atexit module state |
| 150 |
// and a low-level callback added for it during module exec. |
| 151 |
// For the moment we leave it here. |
| 152 |
|
| 153 |
// List containing tuples with callback information. |
| 154 |
// e.g. [(func, args, kwargs), ...] |
| 155 |
PyObject *callbacks; |
| 156 |
}; |
| 157 |
|
| 158 |
|
| 159 |
/****** Garbage collector **********/ |
| 160 |
|
| 161 |
/* GC information is stored BEFORE the object structure. */ |
| 162 |
typedef struct { |
| 163 |
// Tagged pointer to next object in the list. |
| 164 |
// 0 means the object is not tracked |
| 165 |
uintptr_t _gc_next; |
| 166 |
|
| 167 |
// Tagged pointer to previous object in the list. |
| 168 |
// Lowest two bits are used for flags documented later. |
| 169 |
uintptr_t _gc_prev; |
| 170 |
} PyGC_Head; |
| 171 |
|
| 172 |
#define _PyGC_Head_UNUSED PyGC_Head |
| 173 |
|
| 174 |
struct gc_generation { |
| 175 |
PyGC_Head head; |
| 176 |
int threshold; /* collection threshold */ |
| 177 |
int count; /* count of allocations or collections of younger |
| 178 |
generations */ |
| 179 |
}; |
| 180 |
|
| 181 |
struct gc_collection_stats { |
| 182 |
/* number of collected objects */ |
| 183 |
Py_ssize_t collected; |
| 184 |
/* total number of uncollectable objects (put into gc.garbage) */ |
| 185 |
Py_ssize_t uncollectable; |
| 186 |
}; |
| 187 |
|
| 188 |
/* Running stats per generation */ |
| 189 |
struct gc_generation_stats { |
| 190 |
/* total number of collections */ |
| 191 |
Py_ssize_t collections; |
| 192 |
/* total number of collected objects */ |
| 193 |
Py_ssize_t collected; |
| 194 |
/* total number of uncollectable objects (put into gc.garbage) */ |
| 195 |
Py_ssize_t uncollectable; |
| 196 |
}; |
| 197 |
|
| 198 |
/* If we change this, we need to change the default value in the |
| 199 |
signature of gc.collect. */ |
| 200 |
#define NUM_GENERATIONS 3 |
| 201 |
|
| 202 |
struct _gc_runtime_state { |
| 203 |
/* List of objects that still need to be cleaned up, singly linked |
| 204 |
* via their gc headers' gc_prev pointers. */ |
| 205 |
PyObject *trash_delete_later; |
| 206 |
/* Current call-stack depth of tp_dealloc calls. */ |
| 207 |
int trash_delete_nesting; |
| 208 |
|
| 209 |
/* Is automatic collection enabled? */ |
| 210 |
int enabled; |
| 211 |
int debug; |
| 212 |
/* linked lists of container objects */ |
| 213 |
#ifndef Py_GIL_DISABLED |
| 214 |
struct gc_generation generations[NUM_GENERATIONS]; |
| 215 |
#else |
| 216 |
struct gc_generation young; |
| 217 |
struct gc_generation old[2]; |
| 218 |
#endif |
| 219 |
|
| 220 |
/* a permanent generation which won't be collected */ |
| 221 |
struct gc_generation permanent_generation; |
| 222 |
struct gc_generation_stats generation_stats[NUM_GENERATIONS]; |
| 223 |
/* true if we are currently running the collector */ |
| 224 |
int collecting; |
| 225 |
/* list of uncollectable objects */ |
| 226 |
PyObject *garbage; |
| 227 |
/* a list of callbacks to be invoked when collection is performed */ |
| 228 |
PyObject *callbacks; |
| 229 |
|
| 230 |
/* The number of live objects. */ |
| 231 |
Py_ssize_t heap_size; |
| 232 |
|
| 233 |
/* dummy members to preserve other offsets */ |
| 234 |
Py_ssize_t dummy1; /* was work_to_do */ |
| 235 |
int dummy2; /* was visited_space */ |
| 236 |
int dummy3; /* was phase */ |
| 237 |
|
| 238 |
/* This is the number of objects that survived the last full |
| 239 |
collection. It approximates the number of long lived objects |
| 240 |
tracked by the GC. |
| 241 |
|
| 242 |
(by "full collection", we mean a collection of the oldest |
| 243 |
generation). */ |
| 244 |
Py_ssize_t long_lived_total; |
| 245 |
/* This is the number of objects that survived all "non-full" |
| 246 |
collections, and are awaiting to undergo a full collection for |
| 247 |
the first time. */ |
| 248 |
Py_ssize_t long_lived_pending; |
| 249 |
|
| 250 |
#ifdef Py_GIL_DISABLED |
| 251 |
/* True if gc.freeze() has been used. */ |
| 252 |
int freeze_active; |
| 253 |
|
| 254 |
/* Memory usage of the process (RSS + swap) after last GC. */ |
| 255 |
Py_ssize_t last_mem; |
| 256 |
|
| 257 |
/* This accumulates the new object count whenever collection is deferred |
| 258 |
due to the RSS increase condition not being meet. Reset on collection. */ |
| 259 |
Py_ssize_t deferred_count; |
| 260 |
|
| 261 |
/* Mutex held for gc_should_collect_mem_usage(). */ |
| 262 |
PyMutex mutex; |
| 263 |
#else |
| 264 |
PyGC_Head *generation0; |
| 265 |
#endif |
| 266 |
}; |
| 267 |
|
| 268 |
#ifndef Py_GIL_DISABLED |
| 269 |
#define GC_GENERATION_INIT \ |
| 270 |
.generations = { \ |
| 271 |
{ .threshold = 2000, }, \ |
| 272 |
{ .threshold = 10, }, \ |
| 273 |
{ .threshold = 10, }, \ |
| 274 |
}, \ |
| 275 |
.heap_size = 0, |
| 276 |
#else |
| 277 |
#define GC_GENERATION_INIT \ |
| 278 |
.young = { .threshold = 2000, }, \ |
| 279 |
.old = { \ |
| 280 |
{ .threshold = 10, }, \ |
| 281 |
{ .threshold = 10, }, \ |
| 282 |
}, |
| 283 |
#endif |
| 284 |
|
| 285 |
#include "pycore_gil.h" // struct _gil_runtime_state |
| 286 |
|
| 287 |
/**** Import ********/ |
| 288 |
|
| 289 |
struct _import_runtime_state { |
| 290 |
/* The builtin modules (defined in config.c). */ |
| 291 |
struct _inittab *inittab; |
| 292 |
/* The most recent value assigned to a PyModuleDef.m_base.m_index. |
| 293 |
This is incremented each time PyModuleDef_Init() is called, |
| 294 |
which is just about every time an extension module is imported. |
| 295 |
See PyInterpreterState.modules_by_index for more info. */ |
| 296 |
Py_ssize_t last_module_index; |
| 297 |
struct { |
| 298 |
/* A lock to guard the cache. */ |
| 299 |
PyMutex mutex; |
| 300 |
/* The actual cache of (filename, name, PyModuleDef) for modules. |
| 301 |
Only legacy (single-phase init) extension modules are added |
| 302 |
and only if they support multiple initialization (m_size >= 0) |
| 303 |
or are imported in the main interpreter. |
| 304 |
This is initialized lazily in fix_up_extension() in import.c. |
| 305 |
Modules are added there and looked up in _imp.find_extension(). */ |
| 306 |
struct _Py_hashtable_t *hashtable; |
| 307 |
} extensions; |
| 308 |
/* Package context -- the full module name for package imports */ |
| 309 |
const char * pkgcontext; |
| 310 |
}; |
| 311 |
|
| 312 |
struct _import_state { |
| 313 |
/* cached sys.modules dictionary */ |
| 314 |
PyObject *modules; |
| 315 |
/* This is the list of module objects for all legacy (single-phase init) |
| 316 |
extension modules ever loaded in this process (i.e. imported |
| 317 |
in this interpreter or in any other). Py_None stands in for |
| 318 |
modules that haven't actually been imported in this interpreter. |
| 319 |
|
| 320 |
A module's index (PyModuleDef.m_base.m_index) is used to look up |
| 321 |
the corresponding module object for this interpreter, if any. |
| 322 |
(See PyState_FindModule().) When any extension module |
| 323 |
is initialized during import, its moduledef gets initialized by |
| 324 |
PyModuleDef_Init(), and the first time that happens for each |
| 325 |
PyModuleDef, its index gets set to the current value of |
| 326 |
a global counter (see _PyRuntimeState.imports.last_module_index). |
| 327 |
The entry for that index in this interpreter remains unset until |
| 328 |
the module is actually imported here. (Py_None is used as |
| 329 |
a placeholder.) Note that multi-phase init modules always get |
| 330 |
an index for which there will never be a module set. |
| 331 |
|
| 332 |
This is initialized lazily in PyState_AddModule(), which is also |
| 333 |
where modules get added. */ |
| 334 |
PyObject *modules_by_index; |
| 335 |
/* importlib module._bootstrap */ |
| 336 |
PyObject *importlib; |
| 337 |
/* override for config->use_frozen_modules (for tests) |
| 338 |
(-1: "off", 1: "on", 0: no override) */ |
| 339 |
int override_frozen_modules; |
| 340 |
int override_multi_interp_extensions_check; |
| 341 |
#ifdef HAVE_DLOPEN |
| 342 |
int dlopenflags; |
| 343 |
#endif |
| 344 |
PyObject *import_func; |
| 345 |
/* The global import lock. */ |
| 346 |
_PyRecursiveMutex lock; |
| 347 |
/* diagnostic info in PyImport_ImportModuleLevelObject() */ |
| 348 |
struct { |
| 349 |
int import_level; |
| 350 |
PyTime_t accumulated; |
| 351 |
int header; |
| 352 |
} find_and_load; |
| 353 |
}; |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
/********** Interpreter state **************/ |
| 358 |
|
| 359 |
#include "pycore_object_state.h" // struct _py_object_state |
| 360 |
#include "pycore_crossinterp.h" // _PyXI_state_t |
| 361 |
|
| 362 |
|
| 363 |
struct _Py_long_state { |
| 364 |
int max_str_digits; |
| 365 |
}; |
| 366 |
|
| 367 |
struct codecs_state { |
| 368 |
// A list of callable objects used to search for codecs. |
| 369 |
PyObject *search_path; |
| 370 |
|
| 371 |
// A dict mapping codec names to codecs returned from a callable in |
| 372 |
// search_path. |
| 373 |
PyObject *search_cache; |
| 374 |
|
| 375 |
// A dict mapping error handling strategies to functions to implement them. |
| 376 |
PyObject *error_registry; |
| 377 |
|
| 378 |
#ifdef Py_GIL_DISABLED |
| 379 |
// Used to safely delete a specific item from search_path. |
| 380 |
PyMutex search_path_mutex; |
| 381 |
#endif |
| 382 |
|
| 383 |
// Whether or not the rest of the state is initialized. |
| 384 |
int initialized; |
| 385 |
}; |
| 386 |
|
| 387 |
// Support for stop-the-world events. This exists in both the PyRuntime struct |
| 388 |
// for global pauses and in each PyInterpreterState for per-interpreter pauses. |
| 389 |
struct _stoptheworld_state { |
| 390 |
PyMutex mutex; // Serializes stop-the-world attempts. |
| 391 |
|
| 392 |
// NOTE: The below fields are protected by HEAD_LOCK(runtime), not by the |
| 393 |
// above mutex. |
| 394 |
bool requested; // Set when a pause is requested. |
| 395 |
bool world_stopped; // Set when the world is stopped. |
| 396 |
bool is_global; // Set when contained in PyRuntime struct. |
| 397 |
|
| 398 |
PyEvent stop_event; // Set when thread_countdown reaches zero. |
| 399 |
Py_ssize_t thread_countdown; // Number of threads that must pause. |
| 400 |
|
| 401 |
PyThreadState *requester; // Thread that requested the pause (may be NULL). |
| 402 |
}; |
| 403 |
|
| 404 |
/* Tracks some rare events per-interpreter, used by the optimizer to turn on/off |
| 405 |
specific optimizations. */ |
| 406 |
typedef struct _rare_events { |
| 407 |
/* Setting an object's class, obj.__class__ = ... */ |
| 408 |
uint8_t set_class; |
| 409 |
/* Setting the bases of a class, cls.__bases__ = ... */ |
| 410 |
uint8_t set_bases; |
| 411 |
/* Setting the PEP 523 frame eval function, _PyInterpreterState_SetFrameEvalFunc() */ |
| 412 |
uint8_t set_eval_frame_func; |
| 413 |
/* Modifying the builtins, __builtins__.__dict__[var] = ... */ |
| 414 |
uint8_t builtin_dict; |
| 415 |
/* Modifying a function, e.g. func.__defaults__ = ..., etc. */ |
| 416 |
uint8_t func_modification; |
| 417 |
} _rare_events; |
| 418 |
|
| 419 |
struct |
| 420 |
Bigint { |
| 421 |
struct Bigint *next; |
| 422 |
int k, maxwds, sign, wds; |
| 423 |
uint32_t x[1]; |
| 424 |
}; |
| 425 |
|
| 426 |
#if defined(Py_USING_MEMORY_DEBUGGER) || _PY_SHORT_FLOAT_REPR == 0 |
| 427 |
|
| 428 |
struct _dtoa_state { |
| 429 |
int _not_used; |
| 430 |
}; |
| 431 |
|
| 432 |
#else // !Py_USING_MEMORY_DEBUGGER && _PY_SHORT_FLOAT_REPR != 0 |
| 433 |
|
| 434 |
/* The size of the Bigint freelist */ |
| 435 |
#define Bigint_Kmax 7 |
| 436 |
|
| 437 |
/* The size of the cached powers of 5 array */ |
| 438 |
#define Bigint_Pow5size 8 |
| 439 |
|
| 440 |
#ifndef PRIVATE_MEM |
| 441 |
#define PRIVATE_MEM 2304 |
| 442 |
#endif |
| 443 |
#define Bigint_PREALLOC_SIZE \ |
| 444 |
((PRIVATE_MEM+sizeof(double)-1)/sizeof(double)) |
| 445 |
|
| 446 |
struct _dtoa_state { |
| 447 |
// p5s is an array of powers of 5 of the form: |
| 448 |
// 5**(2**(i+2)) for 0 <= i < Bigint_Pow5size |
| 449 |
struct Bigint *p5s[Bigint_Pow5size]; |
| 450 |
// XXX This should be freed during runtime fini. |
| 451 |
struct Bigint *freelist[Bigint_Kmax+1]; |
| 452 |
double preallocated[Bigint_PREALLOC_SIZE]; |
| 453 |
double *preallocated_next; |
| 454 |
}; |
| 455 |
|
| 456 |
#endif // !Py_USING_MEMORY_DEBUGGER |
| 457 |
|
| 458 |
struct _py_code_state { |
| 459 |
PyMutex mutex; |
| 460 |
// Interned constants from code objects. Used by the free-threaded build. |
| 461 |
struct _Py_hashtable_t *constants; |
| 462 |
}; |
| 463 |
|
| 464 |
#define FUNC_VERSION_CACHE_SIZE (1<<12) /* Must be a power of 2 */ |
| 465 |
|
| 466 |
struct _func_version_cache_item { |
| 467 |
PyFunctionObject *func; |
| 468 |
PyObject *code; |
| 469 |
}; |
| 470 |
|
| 471 |
struct _py_func_state { |
| 472 |
#ifdef Py_GIL_DISABLED |
| 473 |
// Protects next_version |
| 474 |
PyMutex mutex; |
| 475 |
#endif |
| 476 |
|
| 477 |
uint32_t next_version; |
| 478 |
// Borrowed references to function and code objects whose |
| 479 |
// func_version % FUNC_VERSION_CACHE_SIZE |
| 480 |
// once was equal to the index in the table. |
| 481 |
// They are cleared when the function or code object is deallocated. |
| 482 |
struct _func_version_cache_item func_version_cache[FUNC_VERSION_CACHE_SIZE]; |
| 483 |
}; |
| 484 |
|
| 485 |
#include "pycore_dict_state.h" // struct _Py_dict_state |
| 486 |
#include "pycore_exceptions.h" // struct _Py_exc_state |
| 487 |
|
| 488 |
|
| 489 |
/****** type state *********/ |
| 490 |
|
| 491 |
/* For now we hard-code this to a value for which we are confident |
| 492 |
all the static builtin types will fit (for all builds). */ |
| 493 |
#define _Py_MAX_MANAGED_STATIC_BUILTIN_TYPES 200 |
| 494 |
#define _Py_MAX_MANAGED_STATIC_EXT_TYPES 10 |
| 495 |
#define _Py_MAX_MANAGED_STATIC_TYPES \ |
| 496 |
(_Py_MAX_MANAGED_STATIC_BUILTIN_TYPES + _Py_MAX_MANAGED_STATIC_EXT_TYPES) |
| 497 |
|
| 498 |
struct _types_runtime_state { |
| 499 |
/* Used to set PyTypeObject.tp_version_tag for core static types. */ |
| 500 |
// bpo-42745: next_version_tag remains shared by all interpreters |
| 501 |
// because of static types. |
| 502 |
unsigned int next_version_tag; |
| 503 |
|
| 504 |
struct { |
| 505 |
struct { |
| 506 |
PyTypeObject *type; |
| 507 |
int64_t interp_count; |
| 508 |
} types[_Py_MAX_MANAGED_STATIC_TYPES]; |
| 509 |
} managed_static; |
| 510 |
}; |
| 511 |
|
| 512 |
|
| 513 |
// Type attribute lookup cache: speed up attribute and method lookups, |
| 514 |
// see _PyType_Lookup(). |
| 515 |
struct type_cache_entry { |
| 516 |
unsigned int version; // initialized from type->tp_version_tag |
| 517 |
#ifdef Py_GIL_DISABLED |
| 518 |
_PySeqLock sequence; |
| 519 |
#endif |
| 520 |
PyObject *name; // reference to exactly a str or None |
| 521 |
PyObject *value; // borrowed reference or NULL |
| 522 |
}; |
| 523 |
|
| 524 |
#define MCACHE_SIZE_EXP 12 |
| 525 |
|
| 526 |
struct type_cache { |
| 527 |
struct type_cache_entry hashtable[1 << MCACHE_SIZE_EXP]; |
| 528 |
}; |
| 529 |
|
| 530 |
typedef struct { |
| 531 |
PyTypeObject *type; |
| 532 |
int isbuiltin; |
| 533 |
int readying; |
| 534 |
int ready; |
| 535 |
// XXX tp_dict can probably be statically allocated, |
| 536 |
// instead of dynamically and stored on the interpreter. |
| 537 |
PyObject *tp_dict; |
| 538 |
PyObject *tp_subclasses; |
| 539 |
/* We never clean up weakrefs for static builtin types since |
| 540 |
they will effectively never get triggered. However, there |
| 541 |
are also some diagnostic uses for the list of weakrefs, |
| 542 |
so we still keep it. */ |
| 543 |
PyObject *tp_weaklist; |
| 544 |
} managed_static_type_state; |
| 545 |
|
| 546 |
#define TYPE_VERSION_CACHE_SIZE (1<<12) /* Must be a power of 2 */ |
| 547 |
|
| 548 |
struct types_state { |
| 549 |
/* Used to set PyTypeObject.tp_version_tag. |
| 550 |
It starts at _Py_MAX_GLOBAL_TYPE_VERSION_TAG + 1, |
| 551 |
where all those lower numbers are used for core static types. */ |
| 552 |
unsigned int next_version_tag; |
| 553 |
|
| 554 |
struct type_cache type_cache; |
| 555 |
|
| 556 |
/* Every static builtin type is initialized for each interpreter |
| 557 |
during its own initialization, including for the main interpreter |
| 558 |
during global runtime initialization. This is done by calling |
| 559 |
_PyStaticType_InitBuiltin(). |
| 560 |
|
| 561 |
The first time a static builtin type is initialized, all the |
| 562 |
normal PyType_Ready() stuff happens. The only difference from |
| 563 |
normal is that there are three PyTypeObject fields holding |
| 564 |
objects which are stored here (on PyInterpreterState) rather |
| 565 |
than in the corresponding PyTypeObject fields. Those are: |
| 566 |
tp_dict (cls.__dict__), tp_subclasses (cls.__subclasses__), |
| 567 |
and tp_weaklist. |
| 568 |
|
| 569 |
When a subinterpreter is initialized, each static builtin type |
| 570 |
is still initialized, but only the interpreter-specific portion, |
| 571 |
namely those three objects. |
| 572 |
|
| 573 |
Those objects are stored in the PyInterpreterState.types.builtins |
| 574 |
array, at the index corresponding to each specific static builtin |
| 575 |
type. That index (a size_t value) is stored in the tp_subclasses |
| 576 |
field. For static builtin types, we re-purposed the now-unused |
| 577 |
tp_subclasses to avoid adding another field to PyTypeObject. |
| 578 |
In all other cases tp_subclasses holds a dict like before. |
| 579 |
(The field was previously defined as PyObject*, but is now void* |
| 580 |
to reflect its dual use.) |
| 581 |
|
| 582 |
The index for each static builtin type isn't statically assigned. |
| 583 |
Instead it is calculated the first time a type is initialized |
| 584 |
(by the main interpreter). The index matches the order in which |
| 585 |
the type was initialized relative to the others. The actual |
| 586 |
value comes from the current value of num_builtins_initialized, |
| 587 |
as each type is initialized for the main interpreter. |
| 588 |
|
| 589 |
num_builtins_initialized is incremented once for each static |
| 590 |
builtin type. Once initialization is over for a subinterpreter, |
| 591 |
the value will be the same as for all other interpreters. */ |
| 592 |
struct { |
| 593 |
size_t num_initialized; |
| 594 |
managed_static_type_state initialized[_Py_MAX_MANAGED_STATIC_BUILTIN_TYPES]; |
| 595 |
} builtins; |
| 596 |
/* We apply a similar strategy for managed extension modules. */ |
| 597 |
struct { |
| 598 |
size_t num_initialized; |
| 599 |
size_t next_index; |
| 600 |
managed_static_type_state initialized[_Py_MAX_MANAGED_STATIC_EXT_TYPES]; |
| 601 |
} for_extensions; |
| 602 |
PyMutex mutex; |
| 603 |
|
| 604 |
// Borrowed references to type objects whose |
| 605 |
// tp_version_tag % TYPE_VERSION_CACHE_SIZE |
| 606 |
// once was equal to the index in the table. |
| 607 |
// They are cleared when the type object is deallocated. |
| 608 |
PyTypeObject *type_version_cache[TYPE_VERSION_CACHE_SIZE]; |
| 609 |
}; |
| 610 |
|
| 611 |
struct _warnings_runtime_state { |
| 612 |
/* Both 'filters' and 'onceregistry' can be set in warnings.py; |
| 613 |
get_warnings_attr() will reset these variables accordingly. */ |
| 614 |
PyObject *filters; /* List */ |
| 615 |
PyObject *once_registry; /* Dict */ |
| 616 |
PyObject *default_action; /* String */ |
| 617 |
_PyRecursiveMutex lock; |
| 618 |
long filters_version; |
| 619 |
PyObject *context; |
| 620 |
}; |
| 621 |
|
| 622 |
struct _Py_mem_interp_free_queue { |
| 623 |
int has_work; // true if the queue is not empty |
| 624 |
PyMutex mutex; // protects the queue |
| 625 |
struct llist_node head; // queue of _mem_work_chunk items |
| 626 |
}; |
| 627 |
|
| 628 |
|
| 629 |
/****** Unicode state *********/ |
| 630 |
|
| 631 |
typedef enum { |
| 632 |
_Py_ERROR_UNKNOWN=0, |
| 633 |
_Py_ERROR_STRICT, |
| 634 |
_Py_ERROR_SURROGATEESCAPE, |
| 635 |
_Py_ERROR_REPLACE, |
| 636 |
_Py_ERROR_IGNORE, |
| 637 |
_Py_ERROR_BACKSLASHREPLACE, |
| 638 |
_Py_ERROR_SURROGATEPASS, |
| 639 |
_Py_ERROR_XMLCHARREFREPLACE, |
| 640 |
_Py_ERROR_OTHER |
| 641 |
} _Py_error_handler; |
| 642 |
|
| 643 |
struct _Py_unicode_runtime_ids { |
| 644 |
PyMutex mutex; |
| 645 |
// next_index value must be preserved when Py_Initialize()/Py_Finalize() |
| 646 |
// is called multiple times: see _PyUnicode_FromId() implementation. |
| 647 |
Py_ssize_t next_index; |
| 648 |
}; |
| 649 |
|
| 650 |
struct _Py_unicode_runtime_state { |
| 651 |
struct _Py_unicode_runtime_ids ids; |
| 652 |
}; |
| 653 |
|
| 654 |
/* fs_codec.encoding is initialized to NULL. |
| 655 |
Later, it is set to a non-NULL string by _PyUnicode_InitEncodings(). */ |
| 656 |
struct _Py_unicode_fs_codec { |
| 657 |
char *encoding; // Filesystem encoding (encoded to UTF-8) |
| 658 |
int utf8; // encoding=="utf-8"? |
| 659 |
char *errors; // Filesystem errors (encoded to UTF-8) |
| 660 |
_Py_error_handler error_handler; |
| 661 |
}; |
| 662 |
|
| 663 |
struct _Py_unicode_ids { |
| 664 |
Py_ssize_t size; |
| 665 |
PyObject **array; |
| 666 |
}; |
| 667 |
|
| 668 |
#include "pycore_ucnhash.h" // _PyUnicode_Name_CAPI |
| 669 |
|
| 670 |
struct _Py_unicode_state { |
| 671 |
struct _Py_unicode_fs_codec fs_codec; |
| 672 |
|
| 673 |
_PyUnicode_Name_CAPI *ucnhash_capi; |
| 674 |
|
| 675 |
// Unicode identifiers (_Py_Identifier): see _PyUnicode_FromId() |
| 676 |
struct _Py_unicode_ids ids; |
| 677 |
}; |
| 678 |
|
| 679 |
// Borrowed references to common callables: |
| 680 |
struct callable_cache { |
| 681 |
PyObject *isinstance; |
| 682 |
PyObject *len; |
| 683 |
PyObject *list_append; |
| 684 |
PyObject *object__getattribute__; |
| 685 |
}; |
| 686 |
|
| 687 |
/* Length of array of slotdef pointers used to store slots with the |
| 688 |
same __name__. There should be at most MAX_EQUIV-1 slotdef entries with |
| 689 |
the same __name__, for any __name__. Since that's a static property, it is |
| 690 |
appropriate to declare fixed-size arrays for this. */ |
| 691 |
#define MAX_EQUIV 10 |
| 692 |
|
| 693 |
typedef struct wrapperbase pytype_slotdef; |
| 694 |
|
| 695 |
|
| 696 |
struct _Py_interp_cached_objects { |
| 697 |
#ifdef Py_GIL_DISABLED |
| 698 |
PyMutex interned_mutex; |
| 699 |
#endif |
| 700 |
PyObject *interned_strings; |
| 701 |
|
| 702 |
/* object.__reduce__ */ |
| 703 |
PyObject *objreduce; |
| 704 |
PyObject *type_slots_pname; |
| 705 |
pytype_slotdef *type_slots_ptrs[MAX_EQUIV]; |
| 706 |
|
| 707 |
/* TypeVar and related types */ |
| 708 |
PyTypeObject *generic_type; |
| 709 |
PyTypeObject *typevar_type; |
| 710 |
PyTypeObject *typevartuple_type; |
| 711 |
PyTypeObject *paramspec_type; |
| 712 |
PyTypeObject *paramspecargs_type; |
| 713 |
PyTypeObject *paramspeckwargs_type; |
| 714 |
PyTypeObject *constevaluator_type; |
| 715 |
}; |
| 716 |
|
| 717 |
struct _Py_interp_static_objects { |
| 718 |
struct { |
| 719 |
int _not_used; |
| 720 |
// hamt_empty is here instead of global because of its weakreflist. |
| 721 |
_PyGC_Head_UNUSED _hamt_empty_gc_not_used; |
| 722 |
PyHamtObject hamt_empty; |
| 723 |
PyBaseExceptionObject last_resort_memory_error; |
| 724 |
} singletons; |
| 725 |
}; |
| 726 |
|
| 727 |
#include "pycore_instruments.h" // PY_MONITORING_TOOL_IDS |
| 728 |
|
| 729 |
|
| 730 |
#ifdef Py_GIL_DISABLED |
| 731 |
|
| 732 |
// A min-heap of indices |
| 733 |
typedef struct _PyIndexHeap { |
| 734 |
int32_t *values; |
| 735 |
|
| 736 |
// Number of items stored in values |
| 737 |
Py_ssize_t size; |
| 738 |
|
| 739 |
// Maximum number of items that can be stored in values |
| 740 |
Py_ssize_t capacity; |
| 741 |
} _PyIndexHeap; |
| 742 |
|
| 743 |
// An unbounded pool of indices. Indices are allocated starting from 0. They |
| 744 |
// may be released back to the pool once they are no longer in use. |
| 745 |
typedef struct _PyIndexPool { |
| 746 |
PyMutex mutex; |
| 747 |
|
| 748 |
// Min heap of indices available for allocation |
| 749 |
_PyIndexHeap free_indices; |
| 750 |
|
| 751 |
// Next index to allocate if no free indices are available |
| 752 |
int32_t next_index; |
| 753 |
|
| 754 |
// Generation counter incremented on thread creation/destruction |
| 755 |
// Used for TLBC cache invalidation in remote debugging |
| 756 |
uint32_t tlbc_generation; |
| 757 |
} _PyIndexPool; |
| 758 |
|
| 759 |
typedef union _Py_unique_id_entry { |
| 760 |
// Points to the next free type id, when part of the freelist |
| 761 |
union _Py_unique_id_entry *next; |
| 762 |
|
| 763 |
// Stores the object when the id is assigned |
| 764 |
PyObject *obj; |
| 765 |
} _Py_unique_id_entry; |
| 766 |
|
| 767 |
struct _Py_unique_id_pool { |
| 768 |
PyMutex mutex; |
| 769 |
|
| 770 |
// combined table of object with allocated unique ids and unallocated ids. |
| 771 |
_Py_unique_id_entry *table; |
| 772 |
|
| 773 |
// Next entry to allocate inside 'table' or NULL |
| 774 |
_Py_unique_id_entry *freelist; |
| 775 |
|
| 776 |
// size of 'table' |
| 777 |
Py_ssize_t size; |
| 778 |
}; |
| 779 |
|
| 780 |
#endif |
| 781 |
|
| 782 |
|
| 783 |
/* PyInterpreterState holds the global state for one of the runtime's |
| 784 |
interpreters. Typically the initial (main) interpreter is the only one. |
| 785 |
|
| 786 |
The PyInterpreterState typedef is in Include/pytypedefs.h. |
| 787 |
*/ |
| 788 |
struct _is { |
| 789 |
|
| 790 |
/* This struct contains the eval_breaker, |
| 791 |
* which is by far the hottest field in this struct |
| 792 |
* and should be placed at the beginning. */ |
| 793 |
struct _ceval_state ceval; |
| 794 |
|
| 795 |
// unused, kept for ABI compatibility |
| 796 |
void *_malloced; |
| 797 |
|
| 798 |
PyInterpreterState *next; |
| 799 |
|
| 800 |
int64_t id; |
| 801 |
Py_ssize_t id_refcount; |
| 802 |
int requires_idref; |
| 803 |
|
| 804 |
long _whence; |
| 805 |
|
| 806 |
/* Has been initialized to a safe state. |
| 807 |
|
| 808 |
In order to be effective, this must be set to 0 during or right |
| 809 |
after allocation. */ |
| 810 |
int _initialized; |
| 811 |
/* Has been fully initialized via pylifecycle.c. */ |
| 812 |
int _ready; |
| 813 |
int finalizing; |
| 814 |
|
| 815 |
uintptr_t last_restart_version; |
| 816 |
struct pythreads { |
| 817 |
uint64_t next_unique_id; |
| 818 |
/* The linked list of threads, newest first. */ |
| 819 |
PyThreadState *head; |
| 820 |
_PyThreadStateImpl *preallocated; |
| 821 |
/* The thread currently executing in the __main__ module, if any. */ |
| 822 |
PyThreadState *main; |
| 823 |
/* Used in Modules/_threadmodule.c. */ |
| 824 |
Py_ssize_t count; |
| 825 |
/* Support for runtime thread stack size tuning. |
| 826 |
A value of 0 means using the platform's default stack size |
| 827 |
or the size specified by the THREAD_STACK_SIZE macro. */ |
| 828 |
/* Used in Python/thread.c. */ |
| 829 |
size_t stacksize; |
| 830 |
} threads; |
| 831 |
|
| 832 |
/* Reference to the _PyRuntime global variable. This field exists |
| 833 |
to not have to pass runtime in addition to tstate to a function. |
| 834 |
Get runtime from tstate: tstate->interp->runtime. */ |
| 835 |
_PyRuntimeState *runtime; |
| 836 |
|
| 837 |
/* Set by Py_EndInterpreter(). |
| 838 |
|
| 839 |
Use _PyInterpreterState_GetFinalizing() |
| 840 |
and _PyInterpreterState_SetFinalizing() |
| 841 |
to access it, don't access it directly. */ |
| 842 |
PyThreadState* _finalizing; |
| 843 |
/* The ID of the OS thread in which we are finalizing. */ |
| 844 |
unsigned long _finalizing_id; |
| 845 |
|
| 846 |
struct _gc_runtime_state gc; |
| 847 |
|
| 848 |
/* The following fields are here to avoid allocation during init. |
| 849 |
The data is exposed through PyInterpreterState pointer fields. |
| 850 |
These fields should not be accessed directly outside of init. |
| 851 |
|
| 852 |
All other PyInterpreterState pointer fields are populated when |
| 853 |
needed and default to NULL. |
| 854 |
|
| 855 |
For now there are some exceptions to that rule, which require |
| 856 |
allocation during init. These will be addressed on a case-by-case |
| 857 |
basis. Also see _PyRuntimeState regarding the various mutex fields. |
| 858 |
*/ |
| 859 |
|
| 860 |
// Dictionary of the sys module |
| 861 |
PyObject *sysdict; |
| 862 |
|
| 863 |
// Dictionary of the builtins module |
| 864 |
PyObject *builtins; |
| 865 |
|
| 866 |
struct _import_state imports; |
| 867 |
|
| 868 |
/* The per-interpreter GIL, which might not be used. */ |
| 869 |
struct _gil_runtime_state _gil; |
| 870 |
|
| 871 |
uint64_t _code_object_generation; |
| 872 |
|
| 873 |
/* ---------- IMPORTANT --------------------------- |
| 874 |
The fields above this line are declared as early as |
| 875 |
possible to facilitate out-of-process observability |
| 876 |
tools. */ |
| 877 |
|
| 878 |
struct codecs_state codecs; |
| 879 |
|
| 880 |
PyConfig config; |
| 881 |
unsigned long feature_flags; |
| 882 |
|
| 883 |
PyObject *dict; /* Stores per-interpreter state */ |
| 884 |
|
| 885 |
PyObject *sysdict_copy; |
| 886 |
PyObject *builtins_copy; |
| 887 |
// Initialized to _PyEval_EvalFrameDefault(). |
| 888 |
_PyFrameEvalFunction eval_frame; |
| 889 |
|
| 890 |
PyFunction_WatchCallback func_watchers[FUNC_MAX_WATCHERS]; |
| 891 |
// One bit is set for each non-NULL entry in func_watchers |
| 892 |
uint8_t active_func_watchers; |
| 893 |
|
| 894 |
Py_ssize_t co_extra_user_count; |
| 895 |
freefunc co_extra_freefuncs[MAX_CO_EXTRA_USERS]; |
| 896 |
|
| 897 |
/* cross-interpreter data and utils */ |
| 898 |
_PyXI_state_t xi; |
| 899 |
|
| 900 |
#ifdef HAVE_FORK |
| 901 |
PyObject *before_forkers; |
| 902 |
PyObject *after_forkers_parent; |
| 903 |
PyObject *after_forkers_child; |
| 904 |
#endif |
| 905 |
|
| 906 |
struct _warnings_runtime_state warnings; |
| 907 |
struct atexit_state atexit; |
| 908 |
struct _stoptheworld_state stoptheworld; |
| 909 |
struct _qsbr_shared qsbr; |
| 910 |
|
| 911 |
#if defined(Py_GIL_DISABLED) |
| 912 |
struct _mimalloc_interp_state mimalloc; |
| 913 |
struct _brc_state brc; // biased reference counting state |
| 914 |
struct _Py_unique_id_pool unique_ids; // object ids for per-thread refcounts |
| 915 |
PyMutex weakref_locks[NUM_WEAKREF_LIST_LOCKS]; |
| 916 |
_PyIndexPool tlbc_indices; |
| 917 |
#endif |
| 918 |
// Per-interpreter list of tasks, any lingering tasks from thread |
| 919 |
// states gets added here and removed from the corresponding |
| 920 |
// thread state's list. |
| 921 |
struct llist_node asyncio_tasks_head; |
| 922 |
// `asyncio_tasks_lock` is used when tasks are moved |
| 923 |
// from thread's list to interpreter's list. |
| 924 |
PyMutex asyncio_tasks_lock; |
| 925 |
|
| 926 |
// Per-interpreter state for the obmalloc allocator. For the main |
| 927 |
// interpreter and for all interpreters that don't have their |
| 928 |
// own obmalloc state, this points to the static structure in |
| 929 |
// obmalloc.c obmalloc_state_main. For other interpreters, it is |
| 930 |
// heap allocated by _PyMem_init_obmalloc() and freed when the |
| 931 |
// interpreter structure is freed. In the case of a heap allocated |
| 932 |
// obmalloc state, it is not safe to hold on to or use memory after |
| 933 |
// the interpreter is freed. The obmalloc state corresponding to |
| 934 |
// that allocated memory is gone. See free_obmalloc_arenas() for |
| 935 |
// more comments. |
| 936 |
struct _obmalloc_state *obmalloc; |
| 937 |
|
| 938 |
PyObject *audit_hooks; |
| 939 |
PyType_WatchCallback type_watchers[TYPE_MAX_WATCHERS]; |
| 940 |
PyCode_WatchCallback code_watchers[CODE_MAX_WATCHERS]; |
| 941 |
PyContext_WatchCallback context_watchers[CONTEXT_MAX_WATCHERS]; |
| 942 |
// One bit is set for each non-NULL entry in code_watchers |
| 943 |
uint8_t active_code_watchers; |
| 944 |
uint8_t active_context_watchers; |
| 945 |
|
| 946 |
struct _py_object_state object_state; |
| 947 |
struct _Py_unicode_state unicode; |
| 948 |
struct _Py_long_state long_state; |
| 949 |
struct _dtoa_state dtoa; |
| 950 |
struct _py_func_state func_state; |
| 951 |
struct _py_code_state code_state; |
| 952 |
|
| 953 |
struct _Py_dict_state dict_state; |
| 954 |
struct _Py_exc_state exc_state; |
| 955 |
struct _Py_mem_interp_free_queue mem_free_queue; |
| 956 |
|
| 957 |
struct ast_state ast; |
| 958 |
struct types_state types; |
| 959 |
struct callable_cache callable_cache; |
| 960 |
PyObject *common_consts[NUM_COMMON_CONSTANTS]; |
| 961 |
bool jit; |
| 962 |
struct _PyExecutorObject *executor_list_head; |
| 963 |
struct _PyExecutorObject *executor_deletion_list_head; |
| 964 |
int executor_deletion_list_remaining_capacity; |
| 965 |
size_t trace_run_counter; |
| 966 |
_rare_events rare_events; |
| 967 |
PyDict_WatchCallback builtins_dict_watcher; |
| 968 |
|
| 969 |
_Py_GlobalMonitors monitors; |
| 970 |
_PyOnceFlag sys_profile_once_flag; |
| 971 |
_PyOnceFlag sys_trace_once_flag; |
| 972 |
Py_ssize_t sys_profiling_threads; /* Count of threads with c_profilefunc set */ |
| 973 |
Py_ssize_t sys_tracing_threads; /* Count of threads with c_tracefunc set */ |
| 974 |
PyObject *monitoring_callables[PY_MONITORING_TOOL_IDS][_PY_MONITORING_EVENTS]; |
| 975 |
PyObject *monitoring_tool_names[PY_MONITORING_TOOL_IDS]; |
| 976 |
uintptr_t monitoring_tool_versions[PY_MONITORING_TOOL_IDS]; |
| 977 |
|
| 978 |
struct _Py_interp_cached_objects cached_objects; |
| 979 |
struct _Py_interp_static_objects static_objects; |
| 980 |
|
| 981 |
Py_ssize_t _interactive_src_count; |
| 982 |
|
| 983 |
#if !defined(Py_GIL_DISABLED) && defined(Py_STACKREF_DEBUG) |
| 984 |
uint64_t next_stackref; |
| 985 |
_Py_hashtable_t *open_stackrefs_table; |
| 986 |
# ifdef Py_STACKREF_CLOSE_DEBUG |
| 987 |
_Py_hashtable_t *closed_stackrefs_table; |
| 988 |
# endif |
| 989 |
#endif |
| 990 |
|
| 991 |
/* the initial PyInterpreterState.threads.head */ |
| 992 |
_PyThreadStateImpl _initial_thread; |
| 993 |
// _initial_thread should be the last field of PyInterpreterState. |
| 994 |
// See https://github.com/python/cpython/issues/127117. |
| 995 |
}; |
| 996 |
|
| 997 |
|
| 998 |
#ifdef __cplusplus |
| 999 |
} |
| 1000 |
#endif |
| 1001 |
#endif /* Py_INTERNAL_INTERP_STRUCTS_H */ |