bpo-45953: Statically initialize the small ints. (gh-30092)

The array of small PyLong objects has been statically declared. Here I also statically initialize them. Consequently they are no longer initialized dynamically during runtime init.

I've also moved them under a new sub-struct in _PyRuntimeState, in preparation for static allocation and initialization of other global objects.

https://bugs.python.org/issue45953
This commit is contained in:
Eric Snow 2021-12-13 18:04:05 -07:00 committed by GitHub
parent cb589d1b6b
commit 121f1f893a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 360 additions and 81 deletions

View file

@ -8,10 +8,10 @@ extern "C" {
# error "this header requires Py_BUILD_CORE define"
#endif
#include "pycore_atomic.h" /* _Py_atomic_address */
#include "pycore_gil.h" // struct _gil_runtime_state
#include "pycore_long_state.h" // struct _Py_long_state
#include "pycore_unicodeobject.h" // struct _Py_unicode_runtime_ids
#include "pycore_atomic.h" /* _Py_atomic_address */
#include "pycore_gil.h" // struct _gil_runtime_state
#include "pycore_global_objects.h" // struct _Py_global_objects
#include "pycore_unicodeobject.h" // struct _Py_unicode_runtime_ids
/* ceval state */
@ -101,8 +101,6 @@ typedef struct pyruntimestate {
unsigned long main_thread;
struct _Py_long_state int_state;
#define NEXITFUNCS 32
void (*exitfuncs[NEXITFUNCS])(void);
int nexitfuncs;
@ -120,12 +118,14 @@ typedef struct pyruntimestate {
struct _Py_unicode_runtime_ids unicode_ids;
// XXX Consolidate globals found via the check-c-globals script.
struct _Py_global_objects global_objects;
// If anything gets added after global_objects then
// _PyRuntimeState_reset() needs to get updated to clear it.
} _PyRuntimeState;
#define _PyRuntimeState_INIT \
{ \
._initialized = 0, \
.global_objects = _Py_global_objects_INIT, \
}
/* Note: _PyRuntimeState_INIT sets other fields to 0/NULL */
@ -133,7 +133,8 @@ static inline void
_PyRuntimeState_reset(_PyRuntimeState *runtime)
{
/* Make it match _PyRuntimeState_INIT. */
memset(runtime, 0, sizeof(*runtime));
memset(runtime, 0, (size_t)&runtime->global_objects - (size_t)runtime);
_Py_global_objects_reset(&runtime->global_objects);
}