diff --git a/Doc/c-api/dict.rst b/Doc/c-api/dict.rst index 89db16e..c527294 100644 --- a/Doc/c-api/dict.rst +++ b/Doc/c-api/dict.rst @@ -216,3 +216,7 @@ Dictionary Objects for key, value in seq2: if override or key not in a: a[key] = value + +.. cfunction:: int PyDict_ClearFreeList(void) + + Clear the free list. Return the total number of freed items. diff --git a/Doc/c-api/list.rst b/Doc/c-api/list.rst index 89f0f9d..c1819e5 100644 --- a/Doc/c-api/list.rst +++ b/Doc/c-api/list.rst @@ -142,3 +142,7 @@ List Objects Return a new tuple object containing the contents of *list*; equivalent to ``tuple(list)``. + +.. cfunction:: int PyList_ClearFreeList(void) + + Clear the free list. Return the total number of freed items. diff --git a/Doc/c-api/set.rst b/Doc/c-api/set.rst index 4348108..31231c5 100644 --- a/Doc/c-api/set.rst +++ b/Doc/c-api/set.rst @@ -157,3 +157,7 @@ subtypes but not for instances of :class:`frozenset` or its subtypes. .. cfunction:: int PySet_Clear(PyObject *set) Empty an existing set of all elements. + +.. cfunction:: int PySet_ClearFreeList(void) + + Clear the free list. Return the total number of freed items. diff --git a/Include/dictobject.h b/Include/dictobject.h index 2481921..f44cda6 100644 --- a/Include/dictobject.h +++ b/Include/dictobject.h @@ -153,6 +153,9 @@ PyAPI_FUNC(PyObject *) PyDict_GetItemString(PyObject *dp, const char *key); PyAPI_FUNC(int) PyDict_SetItemString(PyObject *dp, const char *key, PyObject *item); PyAPI_FUNC(int) PyDict_DelItemString(PyObject *dp, const char *key); +/* free list api */ +PyAPI_FUNC(int) PyDict_ClearFreeList(void); + #ifdef __cplusplus } #endif diff --git a/Include/listobject.h b/Include/listobject.h index 755fbbe..6bfe67b 100644 --- a/Include/listobject.h +++ b/Include/listobject.h @@ -65,6 +65,9 @@ PyAPI_FUNC(PyObject *) _PyList_Extend(PyListObject *, PyObject *); #define PyList_SET_ITEM(op, i, v) (((PyListObject *)(op))->ob_item[i] = (v)) #define PyList_GET_SIZE(op) Py_SIZE(op) +/* free list api */ +PyAPI_FUNC(int) PyList_ClearFreeList(void); + #ifdef __cplusplus } #endif diff --git a/Include/setobject.h b/Include/setobject.h index 4a9baff..9185efd 100644 --- a/Include/setobject.h +++ b/Include/setobject.h @@ -93,6 +93,9 @@ PyAPI_FUNC(int) _PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, PyAPI_FUNC(PyObject *) PySet_Pop(PyObject *set); PyAPI_FUNC(int) _PySet_Update(PyObject *set, PyObject *iterable); +/* free list api */ +PyAPI_FUNC(int) PySet_ClearFreeList(void); + #ifdef __cplusplus } #endif diff --git a/Objects/dictobject.c b/Objects/dictobject.c index 745e2dc..1b0fa3d 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -226,9 +226,10 @@ show_track(void) static PyDictObject *free_list[PyDict_MAXFREELIST]; static int numfree = 0; -void -PyDict_Fini(void) +int +PyDict_ClearFreeList(void) { + int freelist_size = numfree; PyDictObject *op; while (numfree) { @@ -236,6 +237,13 @@ PyDict_Fini(void) assert(PyDict_CheckExact(op)); PyObject_GC_Del(op); } + return freelist_size; +} + +void +PyDict_Fini(void) +{ + (void)PyDict_ClearFreeList(); } PyObject * diff --git a/Objects/listobject.c b/Objects/listobject.c index 0dab784..76ec019 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -97,9 +97,10 @@ show_alloc(void) static PyListObject *free_list[PyList_MAXFREELIST]; static int numfree = 0; -void -PyList_Fini(void) +int +PyList_ClearFreeList(void) { + int freelist_size = numfree; PyListObject *op; while (numfree) { @@ -107,6 +108,12 @@ PyList_Fini(void) assert(PyList_CheckExact(op)); PyObject_GC_Del(op); } + return freelist_size; +} + +void +PyList_Fini(void) { + (void)PyList_ClearFreeList(); } PyObject * diff --git a/Objects/setobject.c b/Objects/setobject.c index b35fef5..0cbe11d 100644 --- a/Objects/setobject.c +++ b/Objects/setobject.c @@ -1070,9 +1070,10 @@ frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds) return emptyfrozenset; } -void -PySet_Fini(void) +int +PySet_ClearFreeList(void) { + int freelist_size = numfree; PySetObject *so; while (numfree) { @@ -1080,6 +1081,12 @@ PySet_Fini(void) so = free_list[numfree]; PyObject_GC_Del(so); } + return freelist_size; +} + +void +PySet_Fini(void) { + (void)PySet_ClearFreeList(); Py_CLEAR(dummy); Py_CLEAR(emptyfrozenset); }