summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorNeil Schemenauer <nas-github@arctrix.com>2024-01-27 03:38:14 (GMT)
committerGitHub <noreply@github.com>2024-01-27 03:38:14 (GMT)
commit7a7bce5a0ab249407e866a1e955d21fa2b0c8506 (patch)
tree9bd0e6f23d545a1cae7b88ca32e087689fe697ed /Python
parent2d08af34b873d5e6b4df5082dfc30a37ef59c346 (diff)
downloadcpython-7a7bce5a0ab249407e866a1e955d21fa2b0c8506.zip
cpython-7a7bce5a0ab249407e866a1e955d21fa2b0c8506.tar.gz
cpython-7a7bce5a0ab249407e866a1e955d21fa2b0c8506.tar.bz2
gh-113055: Use pointer for interp->obmalloc state (gh-113412)
For interpreters that share state with the main interpreter, this points to the same static memory structure. For interpreters with their own obmalloc state, it is heap allocated. Add free_obmalloc_arenas() which will free the obmalloc arenas and radix tree structures for interpreters with their own obmalloc state. Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
Diffstat (limited to 'Python')
-rw-r--r--Python/pylifecycle.c16
-rw-r--r--Python/pystate.c14
2 files changed, 22 insertions, 8 deletions
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
index 261622a..fff64dd 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -32,6 +32,7 @@
#include "pycore_typevarobject.h" // _Py_clear_generic_types()
#include "pycore_unicodeobject.h" // _PyUnicode_InitTypes()
#include "pycore_weakref.h" // _PyWeakref_GET_REF()
+#include "pycore_obmalloc.h" // _PyMem_init_obmalloc()
#include "opcode.h"
@@ -645,6 +646,13 @@ pycore_create_interpreter(_PyRuntimeState *runtime,
return status;
}
+ // initialize the interp->obmalloc state. This must be done after
+ // the settings are loaded (so that feature_flags are set) but before
+ // any calls are made to obmalloc functions.
+ if (_PyMem_init_obmalloc(interp) < 0) {
+ return _PyStatus_NO_MEMORY();
+ }
+
PyThreadState *tstate = _PyThreadState_New(interp,
_PyThreadState_WHENCE_INTERP);
if (tstate == NULL) {
@@ -2144,6 +2152,14 @@ new_interpreter(PyThreadState **tstate_p, const PyInterpreterConfig *config)
goto error;
}
+ // initialize the interp->obmalloc state. This must be done after
+ // the settings are loaded (so that feature_flags are set) but before
+ // any calls are made to obmalloc functions.
+ if (_PyMem_init_obmalloc(interp) < 0) {
+ status = _PyStatus_NO_MEMORY();
+ goto error;
+ }
+
tstate = _PyThreadState_New(interp, _PyThreadState_WHENCE_INTERP);
if (tstate == NULL) {
status = _PyStatus_NO_MEMORY();
diff --git a/Python/pystate.c b/Python/pystate.c
index 5ad5b6f..8e097c8 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -18,6 +18,7 @@
#include "pycore_pystate.h"
#include "pycore_runtime_init.h" // _PyRuntimeState_INIT
#include "pycore_sysmodule.h" // _PySys_Audit()
+#include "pycore_obmalloc.h" // _PyMem_obmalloc_state_on_heap()
/* --------------------------------------------------------------------------
CAUTION
@@ -553,6 +554,11 @@ free_interpreter(PyInterpreterState *interp)
// The main interpreter is statically allocated so
// should not be freed.
if (interp != &_PyRuntime._main_interpreter) {
+ if (_PyMem_obmalloc_state_on_heap(interp)) {
+ // interpreter has its own obmalloc state, free it
+ PyMem_RawFree(interp->obmalloc);
+ interp->obmalloc = NULL;
+ }
PyMem_RawFree(interp);
}
}
@@ -595,14 +601,6 @@ init_interpreter(PyInterpreterState *interp,
assert(next != NULL || (interp == runtime->interpreters.main));
interp->next = next;
- /* Initialize obmalloc, but only for subinterpreters,
- since the main interpreter is initialized statically. */
- if (interp != &runtime->_main_interpreter) {
- poolp temp[OBMALLOC_USED_POOLS_SIZE] = \
- _obmalloc_pools_INIT(interp->obmalloc.pools);
- memcpy(&interp->obmalloc.pools.used, temp, sizeof(temp));
- }
-
PyStatus status = _PyObject_InitState(interp);
if (_PyStatus_EXCEPTION(status)) {
return status;