aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Python/context.c
diff options
context:
space:
mode:
Diffstat (limited to 'Python/context.c')
-rw-r--r--Python/context.c23
1 files changed, 15 insertions, 8 deletions
diff --git a/Python/context.c b/Python/context.c
index d78f7f993bb..a20ec712373 100644
--- a/Python/context.c
+++ b/Python/context.c
@@ -9,9 +9,6 @@
#include "structmember.h" // PyMemberDef
-#define CONTEXT_FREELIST_MAXLEN 255
-
-
#include "clinic/context.c.h"
/*[clinic input]
module _contextvars
@@ -66,12 +63,14 @@ static int
contextvar_del(PyContextVar *var);
+#if PyContext_MAXFREELIST > 0
static struct _Py_context_state *
get_context_state(void)
{
PyInterpreterState *interp = _PyInterpreterState_GET();
return &interp->context;
}
+#endif
PyObject *
@@ -340,8 +339,9 @@ class _contextvars.Context "PyContext *" "&PyContext_Type"
static inline PyContext *
_context_alloc(void)
{
- struct _Py_context_state *state = get_context_state();
PyContext *ctx;
+#if PyContext_MAXFREELIST > 0
+ struct _Py_context_state *state = get_context_state();
#ifdef Py_DEBUG
// _context_alloc() must not be called after _PyContext_Fini()
assert(state->numfree != -1);
@@ -353,7 +353,9 @@ _context_alloc(void)
ctx->ctx_weakreflist = NULL;
_Py_NewReference((PyObject *)ctx);
}
- else {
+ else
+#endif
+ {
ctx = PyObject_GC_New(PyContext, &PyContext_Type);
if (ctx == NULL) {
return NULL;
@@ -469,17 +471,20 @@ context_tp_dealloc(PyContext *self)
}
(void)context_tp_clear(self);
+#if PyContext_MAXFREELIST > 0
struct _Py_context_state *state = get_context_state();
#ifdef Py_DEBUG
// _context_alloc() must not be called after _PyContext_Fini()
assert(state->numfree != -1);
#endif
- if (state->numfree < CONTEXT_FREELIST_MAXLEN) {
+ if (state->numfree < PyContext_MAXFREELIST) {
state->numfree++;
self->ctx_weakreflist = (PyObject *)state->freelist;
state->freelist = self;
}
- else {
+ else
+#endif
+ {
Py_TYPE(self)->tp_free(self);
}
}
@@ -1289,6 +1294,7 @@ get_token_missing(void)
void
_PyContext_ClearFreeList(PyInterpreterState *interp)
{
+#if PyContext_MAXFREELIST > 0
struct _Py_context_state *state = &interp->context;
for (; state->numfree; state->numfree--) {
PyContext *ctx = state->freelist;
@@ -1296,6 +1302,7 @@ _PyContext_ClearFreeList(PyInterpreterState *interp)
ctx->ctx_weakreflist = NULL;
PyObject_GC_Del(ctx);
}
+#endif
}
@@ -1306,7 +1313,7 @@ _PyContext_Fini(PyInterpreterState *interp)
Py_CLEAR(_token_missing);
}
_PyContext_ClearFreeList(interp);
-#ifdef Py_DEBUG
+#if defined(Py_DEBUG) && PyContext_MAXFREELIST > 0
struct _Py_context_state *state = &interp->context;
state->numfree = -1;
#endif