bpo-45522: Allow to disable freelists on build time (GH-29056)

Freelists for object structs can now be disabled. A new ``configure``
option ``--without-freelists`` can be used to disable all freelists
except empty tuple singleton. Internal Py*_MAXFREELIST macros can now
be defined as 0 without causing compiler warnings and segfaults.

Signed-off-by: Christian Heimes <christian@python.org>
This commit is contained in:
Christian Heimes 2021-10-21 16:12:20 +03:00 committed by GitHub
parent 5a14f71fe8
commit 9942f42a93
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 218 additions and 35 deletions

View file

@ -28,12 +28,14 @@ class float "PyObject *" "&PyFloat_Type"
#endif
#if PyFloat_MAXFREELIST > 0
static struct _Py_float_state *
get_float_state(void)
{
PyInterpreterState *interp = _PyInterpreterState_GET();
return &interp->float_state;
}
#endif
double
@ -126,8 +128,10 @@ PyFloat_GetInfo(void)
PyObject *
PyFloat_FromDouble(double fval)
{
PyFloatObject *op;
#if PyFloat_MAXFREELIST > 0
struct _Py_float_state *state = get_float_state();
PyFloatObject *op = state->free_list;
op = state->free_list;
if (op != NULL) {
#ifdef Py_DEBUG
// PyFloat_FromDouble() must not be called after _PyFloat_Fini()
@ -136,7 +140,9 @@ PyFloat_FromDouble(double fval)
state->free_list = (PyFloatObject *) Py_TYPE(op);
state->numfree--;
}
else {
else
#endif
{
op = PyObject_Malloc(sizeof(PyFloatObject));
if (!op) {
return PyErr_NoMemory();
@ -233,6 +239,7 @@ PyFloat_FromString(PyObject *v)
static void
float_dealloc(PyFloatObject *op)
{
#if PyFloat_MAXFREELIST > 0
if (PyFloat_CheckExact(op)) {
struct _Py_float_state *state = get_float_state();
#ifdef Py_DEBUG
@ -247,7 +254,9 @@ float_dealloc(PyFloatObject *op)
Py_SET_TYPE(op, (PyTypeObject *)state->free_list);
state->free_list = op;
}
else {
else
#endif
{
Py_TYPE(op)->tp_free((PyObject *)op);
}
}
@ -2036,6 +2045,7 @@ _PyFloat_InitTypes(void)
void
_PyFloat_ClearFreeList(PyInterpreterState *interp)
{
#if PyFloat_MAXFREELIST > 0
struct _Py_float_state *state = &interp->float_state;
PyFloatObject *f = state->free_list;
while (f != NULL) {
@ -2045,13 +2055,14 @@ _PyFloat_ClearFreeList(PyInterpreterState *interp)
}
state->free_list = NULL;
state->numfree = 0;
#endif
}
void
_PyFloat_Fini(PyInterpreterState *interp)
{
_PyFloat_ClearFreeList(interp);
#ifdef Py_DEBUG
#if defined(Py_DEBUG) && PyFloat_MAXFREELIST > 0
struct _Py_float_state *state = &interp->float_state;
state->numfree = -1;
#endif
@ -2061,10 +2072,12 @@ _PyFloat_Fini(PyInterpreterState *interp)
void
_PyFloat_DebugMallocStats(FILE *out)
{
#if PyFloat_MAXFREELIST > 0
struct _Py_float_state *state = get_float_state();
_PyDebugAllocatorStats(out,
"free PyFloatObject",
state->numfree, sizeof(PyFloatObject));
#endif
}