Add get_xxx_state() function to factorize duplicated code.
#include "clinic/dictobject.c.h"
+
+static struct _Py_dict_state *
+get_dict_state(void)
+{
+ PyInterpreterState *interp = _PyInterpreterState_GET();
+ return &interp->dict_state;
+}
+
+
void
_PyDict_ClearFreeList(PyThreadState *tstate)
{
{
_PyDict_ClearFreeList(tstate);
#ifdef Py_DEBUG
- PyInterpreterState *interp = _PyInterpreterState_GET();
- struct _Py_dict_state *state = &interp->dict_state;
+ struct _Py_dict_state *state = get_dict_state();
state->numfree = -1;
state->keys_numfree = -1;
#endif
void
_PyDict_DebugMallocStats(FILE *out)
{
- PyInterpreterState *interp = _PyInterpreterState_GET();
- struct _Py_dict_state *state = &interp->dict_state;
+ struct _Py_dict_state *state = get_dict_state();
_PyDebugAllocatorStats(out, "free PyDictObject",
state->numfree, sizeof(PyDictObject));
}
es = sizeof(Py_ssize_t);
}
- PyInterpreterState *interp = _PyInterpreterState_GET();
- struct _Py_dict_state *state = &interp->dict_state;
+ struct _Py_dict_state *state = get_dict_state();
#ifdef Py_DEBUG
// new_keys_object() must not be called after _PyDict_Fini()
assert(state->keys_numfree != -1);
Py_XDECREF(entries[i].me_key);
Py_XDECREF(entries[i].me_value);
}
- PyInterpreterState *interp = _PyInterpreterState_GET();
- struct _Py_dict_state *state = &interp->dict_state;
+ struct _Py_dict_state *state = get_dict_state();
#ifdef Py_DEBUG
// free_keys_object() must not be called after _PyDict_Fini()
assert(state->keys_numfree != -1);
{
PyDictObject *mp;
assert(keys != NULL);
- PyInterpreterState *interp = _PyInterpreterState_GET();
- struct _Py_dict_state *state = &interp->dict_state;
+ struct _Py_dict_state *state = get_dict_state();
#ifdef Py_DEBUG
// new_dict() must not be called after _PyDict_Fini()
assert(state->numfree != -1);
#ifdef Py_REF_DEBUG
_Py_RefTotal--;
#endif
- PyInterpreterState *interp = _PyInterpreterState_GET();
- struct _Py_dict_state *state = &interp->dict_state;
+ struct _Py_dict_state *state = get_dict_state();
#ifdef Py_DEBUG
// dictresize() must not be called after _PyDict_Fini()
assert(state->keys_numfree != -1);
assert(keys->dk_refcnt == 1);
dictkeys_decref(keys);
}
- PyInterpreterState *interp = _PyInterpreterState_GET();
- struct _Py_dict_state *state = &interp->dict_state;
+ struct _Py_dict_state *state = get_dict_state();
#ifdef Py_DEBUG
// new_dict() must not be called after _PyDict_Fini()
assert(state->numfree != -1);
# define PyFloat_MAXFREELIST 100
#endif
+
+static struct _Py_float_state *
+get_float_state(void)
+{
+ PyInterpreterState *interp = _PyInterpreterState_GET();
+ return &interp->float_state;
+}
+
+
double
PyFloat_GetMax(void)
{
PyObject *
PyFloat_FromDouble(double fval)
{
- PyInterpreterState *interp = _PyInterpreterState_GET();
- struct _Py_float_state *state = &interp->float_state;
+ struct _Py_float_state *state = get_float_state();
PyFloatObject *op = state->free_list;
if (op != NULL) {
#ifdef Py_DEBUG
float_dealloc(PyFloatObject *op)
{
if (PyFloat_CheckExact(op)) {
- PyInterpreterState *interp = _PyInterpreterState_GET();
- struct _Py_float_state *state = &interp->float_state;
+ struct _Py_float_state *state = get_float_state();
#ifdef Py_DEBUG
// float_dealloc() must not be called after _PyFloat_Fini()
assert(state->numfree != -1);
Py_SET_TYPE(op, (PyTypeObject *)state->free_list);
state->free_list = op;
}
- else
+ else {
Py_TYPE(op)->tp_free((PyObject *)op);
+ }
}
double
void
_PyFloat_DebugMallocStats(FILE *out)
{
- PyInterpreterState *interp = _PyInterpreterState_GET();
- struct _Py_float_state *state = &interp->float_state;
+ struct _Py_float_state *state = get_float_state();
_PyDebugAllocatorStats(out,
"free PyFloatObject",
state->numfree, sizeof(PyFloatObject));
{NULL} /* Sentinel */
};
+
+static struct _Py_frame_state *
+get_frame_state(void)
+{
+ PyInterpreterState *interp = _PyInterpreterState_GET();
+ return &interp->frame;
+}
+
+
static PyObject *
frame_getlocals(PyFrameObject *f, void *closure)
{
co->co_zombieframe = f;
}
else {
- PyInterpreterState *interp = _PyInterpreterState_GET();
- struct _Py_frame_state *state = &interp->frame;
+ struct _Py_frame_state *state = get_frame_state();
#ifdef Py_DEBUG
// frame_dealloc() must not be called after _PyFrame_Fini()
assert(state->numfree != -1);
Py_ssize_t ncells = PyTuple_GET_SIZE(code->co_cellvars);
Py_ssize_t nfrees = PyTuple_GET_SIZE(code->co_freevars);
Py_ssize_t extras = code->co_stacksize + code->co_nlocals + ncells + nfrees;
- PyInterpreterState *interp = _PyInterpreterState_GET();
- struct _Py_frame_state *state = &interp->frame;
+ struct _Py_frame_state *state = get_frame_state();
if (state->free_list == NULL)
{
f = PyObject_GC_NewVar(PyFrameObject, &PyFrame_Type, extras);
void
_PyFrame_DebugMallocStats(FILE *out)
{
- PyInterpreterState *interp = _PyInterpreterState_GET();
- struct _Py_frame_state *state = &interp->frame;
+ struct _Py_frame_state *state = get_frame_state();
_PyDebugAllocatorStats(out,
"free PyFrameObject",
state->numfree, sizeof(PyFrameObject));
};
+static struct _Py_async_gen_state *
+get_async_gen_state(void)
+{
+ PyInterpreterState *interp = _PyInterpreterState_GET();
+ return &interp->async_gen;
+}
+
+
PyObject *
PyAsyncGen_New(PyFrameObject *f, PyObject *name, PyObject *qualname)
{
_PyObject_GC_UNTRACK((PyObject *)o);
Py_CLEAR(o->ags_gen);
Py_CLEAR(o->ags_sendval);
- PyInterpreterState *interp = _PyInterpreterState_GET();
- struct _Py_async_gen_state *state = &interp->async_gen;
+ struct _Py_async_gen_state *state = get_async_gen_state();
#ifdef Py_DEBUG
// async_gen_asend_dealloc() must not be called after _PyAsyncGen_Fini()
assert(state->asend_numfree != -1);
async_gen_asend_new(PyAsyncGenObject *gen, PyObject *sendval)
{
PyAsyncGenASend *o;
- PyInterpreterState *interp = _PyInterpreterState_GET();
- struct _Py_async_gen_state *state = &interp->async_gen;
+ struct _Py_async_gen_state *state = get_async_gen_state();
#ifdef Py_DEBUG
// async_gen_asend_new() must not be called after _PyAsyncGen_Fini()
assert(state->asend_numfree != -1);
{
_PyObject_GC_UNTRACK((PyObject *)o);
Py_CLEAR(o->agw_val);
- PyInterpreterState *interp = _PyInterpreterState_GET();
- struct _Py_async_gen_state *state = &interp->async_gen;
+ struct _Py_async_gen_state *state = get_async_gen_state();
#ifdef Py_DEBUG
// async_gen_wrapped_val_dealloc() must not be called after _PyAsyncGen_Fini()
assert(state->value_numfree != -1);
_PyAsyncGenWrappedValue *o;
assert(val);
- PyInterpreterState *interp = _PyInterpreterState_GET();
- struct _Py_async_gen_state *state = &interp->async_gen;
+ struct _Py_async_gen_state *state = get_async_gen_state();
#ifdef Py_DEBUG
// _PyAsyncGenValueWrapperNew() must not be called after _PyAsyncGen_Fini()
assert(state->value_numfree != -1);
#include "clinic/listobject.c.h"
+
+static struct _Py_list_state *
+get_list_state(void)
+{
+ PyInterpreterState *interp = _PyInterpreterState_GET();
+ return &interp->list;
+}
+
+
/* Ensure ob_item has room for at least newsize elements, and set
* ob_size to newsize. If newsize > ob_size on entry, the content
* of the new slots at exit is undefined heap trash; it's the caller's
void
_PyList_DebugMallocStats(FILE *out)
{
- PyInterpreterState *interp = _PyInterpreterState_GET();
- struct _Py_list_state *state = &interp->list;
+ struct _Py_list_state *state = get_list_state();
_PyDebugAllocatorStats(out,
"free PyListObject",
state->numfree, sizeof(PyListObject));
return NULL;
}
- PyInterpreterState *interp = _PyInterpreterState_GET();
- struct _Py_list_state *state = &interp->list;
+ struct _Py_list_state *state = get_list_state();
PyListObject *op;
#ifdef Py_DEBUG
// PyList_New() must not be called after _PyList_Fini()
}
PyMem_FREE(op->ob_item);
}
- PyInterpreterState *interp = _PyInterpreterState_GET();
- struct _Py_list_state *state = &interp->list;
+ struct _Py_list_state *state = get_list_state();
#ifdef Py_DEBUG
// list_dealloc() must not be called after _PyList_Fini()
assert(state->numfree != -1);
PyObject *
PySlice_New(PyObject *start, PyObject *stop, PyObject *step)
{
+ if (step == NULL) {
+ step = Py_None;
+ }
+ if (start == NULL) {
+ start = Py_None;
+ }
+ if (stop == NULL) {
+ stop = Py_None;
+ }
+
PyInterpreterState *interp = _PyInterpreterState_GET();
PySliceObject *obj;
if (interp->slice_cache != NULL) {
obj = interp->slice_cache;
interp->slice_cache = NULL;
_Py_NewReference((PyObject *)obj);
- } else {
+ }
+ else {
obj = PyObject_GC_New(PySliceObject, &PySlice_Type);
- if (obj == NULL)
+ if (obj == NULL) {
return NULL;
+ }
}
- if (step == NULL) step = Py_None;
Py_INCREF(step);
- if (start == NULL) start = Py_None;
- Py_INCREF(start);
- if (stop == NULL) stop = Py_None;
- Py_INCREF(stop);
-
obj->step = step;
+ Py_INCREF(start);
obj->start = start;
+ Py_INCREF(stop);
obj->stop = stop;
_PyObject_GC_TRACK(obj);
#include "clinic/tupleobject.c.h"
+
+static struct _Py_tuple_state *
+get_tuple_state(void)
+{
+ PyInterpreterState *interp = _PyInterpreterState_GET();
+ return &interp->tuple;
+}
+
+
static inline void
tuple_gc_track(PyTupleObject *op)
{
_PyObject_GC_TRACK(op);
}
+
/* Print summary info about the state of the optimized allocator */
void
_PyTuple_DebugMallocStats(FILE *out)
{
#if PyTuple_MAXSAVESIZE > 0
- PyInterpreterState *interp = _PyInterpreterState_GET();
- struct _Py_tuple_state *state = &interp->tuple;
+ struct _Py_tuple_state *state = get_tuple_state();
for (int i = 1; i < PyTuple_MAXSAVESIZE; i++) {
char buf[128];
PyOS_snprintf(buf, sizeof(buf),
{
PyTupleObject *op;
#if PyTuple_MAXSAVESIZE > 0
- PyInterpreterState *interp = _PyInterpreterState_GET();
- struct _Py_tuple_state *state = &interp->tuple;
+ struct _Py_tuple_state *state = get_tuple_state();
if (size == 0 && state->free_list[0]) {
op = state->free_list[0];
Py_INCREF(op);
return PyTuple_New(0);
}
- PyInterpreterState *interp = _PyInterpreterState_GET();
- struct _Py_tuple_state *state = &interp->tuple;
+ struct _Py_tuple_state *state = get_tuple_state();
va_start(vargs, n);
PyTupleObject *result = tuple_alloc(state, n);
Py_XDECREF(op->ob_item[i]);
}
#if PyTuple_MAXSAVESIZE > 0
- PyInterpreterState *interp = _PyInterpreterState_GET();
- struct _Py_tuple_state *state = &interp->tuple;
+ struct _Py_tuple_state *state = get_tuple_state();
#ifdef Py_DEBUG
// tupledealloc() must not be called after _PyTuple_Fini()
assert(state->numfree[0] != -1);
return PyTuple_New(0);
}
- PyInterpreterState *interp = _PyInterpreterState_GET();
- struct _Py_tuple_state *state = &interp->tuple;
+ struct _Py_tuple_state *state = get_tuple_state();
PyTupleObject *tuple = tuple_alloc(state, n);
if (tuple == NULL) {
return NULL;
return PyTuple_New(0);
}
- PyInterpreterState *interp = _PyInterpreterState_GET();
- struct _Py_tuple_state *state = &interp->tuple;
+ struct _Py_tuple_state *state = get_tuple_state();
np = tuple_alloc(state, size);
if (np == NULL) {
return NULL;
if (n > PY_SSIZE_T_MAX / Py_SIZE(a))
return PyErr_NoMemory();
size = Py_SIZE(a) * n;
- PyInterpreterState *interp = _PyInterpreterState_GET();
- struct _Py_tuple_state *state = &interp->tuple;
+ struct _Py_tuple_state *state = get_tuple_state();
np = tuple_alloc(state, size);
if (np == NULL)
return NULL;
return (PyObject *)self;
}
else {
- PyInterpreterState *interp = _PyInterpreterState_GET();
- struct _Py_tuple_state *state = &interp->tuple;
+ struct _Py_tuple_state *state = get_tuple_state();
PyTupleObject* result = tuple_alloc(state, slicelength);
if (!result) return NULL;
contextvar_del(PyContextVar *var);
+static struct _Py_context_state *
+get_context_state(void)
+{
+ PyInterpreterState *interp = _PyInterpreterState_GET();
+ return &interp->context;
+}
+
+
PyObject *
_PyContext_NewHamtForTests(void)
{
static inline PyContext *
_context_alloc(void)
{
- PyInterpreterState *interp = _PyInterpreterState_GET();
- struct _Py_context_state *state = &interp->context;
+ struct _Py_context_state *state = get_context_state();
PyContext *ctx;
#ifdef Py_DEBUG
// _context_alloc() must not be called after _PyContext_Fini()
}
(void)context_tp_clear(self);
- PyInterpreterState *interp = _PyInterpreterState_GET();
- struct _Py_context_state *state = &interp->context;
+ struct _Py_context_state *state = get_context_state();
#ifdef Py_DEBUG
// _context_alloc() must not be called after _PyContext_Fini()
assert(state->numfree != -1);