summaryrefslogtreecommitdiffstats
path: root/Objects/obmalloc.c
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2017-11-15 23:48:08 (GMT)
committerGitHub <noreply@github.com>2017-11-15 23:48:08 (GMT)
commitf7e5b56c37eb859e225e886c79c5d742c567ee95 (patch)
tree7d722ca38595aaa68e02a1ee1ea53e17a54b0188 /Objects/obmalloc.c
parent43605e6bfa8d49612df4a38460d063d6ba781906 (diff)
downloadcpython-f7e5b56c37eb859e225e886c79c5d742c567ee95.zip
cpython-f7e5b56c37eb859e225e886c79c5d742c567ee95.tar.gz
cpython-f7e5b56c37eb859e225e886c79c5d742c567ee95.tar.bz2
bpo-32030: Split Py_Main() into subfunctions (#4399)
* Don't use "Python runtime" anymore to parse command line options or to get environment variables: pymain_init() is now a strict separation. * Use an error message rather than "crashing" directly with Py_FatalError(). Limit the number of calls to Py_FatalError(). It prepares the code to handle errors more nicely later. * Warnings options (-W, PYTHONWARNINGS) and "XOptions" (-X) are now only added to the sys module once Python core is properly initialized. * _PyMain is now the well identified owner of some important strings like: warnings options, XOptions, and the "program name". The program name string is now properly freed at exit. pymain_free() is now responsible to free the "command" string. * Rename most methods in Modules/main.c to use a "pymain_" prefix to avoid conflits and ease debug. * Replace _Py_CommandLineDetails_INIT with memset(0) * Reorder a lot of code to fix the initialization ordering. For example, initializing standard streams now comes before parsing PYTHONWARNINGS. * Py_Main() now handles errors when adding warnings options and XOptions. * Add _PyMem_GetDefaultRawAllocator() private function. * Cleanup _PyMem_Initialize(): remove useless global constants: move them into _PyMem_Initialize(). * Call _PyRuntime_Initialize() as soon as possible: _PyRuntime_Initialize() now returns an error message on failure. * Add _PyInitError structure and following macros: * _Py_INIT_OK() * _Py_INIT_ERR(msg) * _Py_INIT_USER_ERR(msg): "user" error, don't abort() in that case * _Py_INIT_FAILED(err)
Diffstat (limited to 'Objects/obmalloc.c')
-rw-r--r--Objects/obmalloc.c77
1 files changed, 41 insertions, 36 deletions
diff --git a/Objects/obmalloc.c b/Objects/obmalloc.c
index 4441c82..699cce9 100644
--- a/Objects/obmalloc.c
+++ b/Objects/obmalloc.c
@@ -182,31 +182,17 @@ static struct {
#define _PyMem_Raw _PyRuntime.mem.allocators.raw
-static const PyMemAllocatorEx _pymem_raw = {
-#ifdef Py_DEBUG
- &_PyMem_Debug.raw, PYRAWDBG_FUNCS
-#else
- NULL, PYRAW_FUNCS
-#endif
- };
#define _PyMem _PyRuntime.mem.allocators.mem
-static const PyMemAllocatorEx _pymem = {
-#ifdef Py_DEBUG
- &_PyMem_Debug.mem, PYDBG_FUNCS
-#else
- NULL, PYMEM_FUNCS
-#endif
- };
#define _PyObject _PyRuntime.mem.allocators.obj
-static const PyMemAllocatorEx _pyobject = {
-#ifdef Py_DEBUG
- &_PyMem_Debug.obj, PYDBG_FUNCS
-#else
- NULL, PYOBJ_FUNCS
-#endif
- };
+
+void
+_PyMem_GetDefaultRawAllocator(PyMemAllocatorEx *alloc_p)
+{
+ PyMemAllocatorEx alloc = {NULL, PYRAW_FUNCS};
+ *alloc_p = alloc;
+}
int
_PyMem_SetupAllocators(const char *opt)
@@ -267,34 +253,52 @@ _PyMem_SetupAllocators(const char *opt)
return 0;
}
-#undef PYRAW_FUNCS
-#undef PYMEM_FUNCS
-#undef PYOBJ_FUNCS
-#undef PYRAWDBG_FUNCS
-#undef PYDBG_FUNCS
-static const PyObjectArenaAllocator _PyObject_Arena = {NULL,
+void
+_PyObject_Initialize(struct _pyobj_runtime_state *state)
+{
+ PyObjectArenaAllocator _PyObject_Arena = {NULL,
#ifdef MS_WINDOWS
- _PyObject_ArenaVirtualAlloc, _PyObject_ArenaVirtualFree
+ _PyObject_ArenaVirtualAlloc, _PyObject_ArenaVirtualFree
#elif defined(ARENAS_USE_MMAP)
- _PyObject_ArenaMmap, _PyObject_ArenaMunmap
+ _PyObject_ArenaMmap, _PyObject_ArenaMunmap
#else
- _PyObject_ArenaMalloc, _PyObject_ArenaFree
+ _PyObject_ArenaMalloc, _PyObject_ArenaFree
#endif
};
-void
-_PyObject_Initialize(struct _pyobj_runtime_state *state)
-{
state->allocator_arenas = _PyObject_Arena;
}
+
void
_PyMem_Initialize(struct _pymem_runtime_state *state)
{
- state->allocators.raw = _pymem_raw;
- state->allocators.mem = _pymem;
- state->allocators.obj = _pyobject;
+ PyMemAllocatorEx pymem_raw = {
+#ifdef Py_DEBUG
+ &_PyMem_Debug.raw, PYRAWDBG_FUNCS
+#else
+ NULL, PYRAW_FUNCS
+#endif
+ };
+ PyMemAllocatorEx pymem = {
+#ifdef Py_DEBUG
+ &_PyMem_Debug.mem, PYDBG_FUNCS
+#else
+ NULL, PYMEM_FUNCS
+#endif
+ };
+ PyMemAllocatorEx pyobject = {
+#ifdef Py_DEBUG
+ &_PyMem_Debug.obj, PYDBG_FUNCS
+#else
+ NULL, PYOBJ_FUNCS
+#endif
+ };
+
+ state->allocators.raw = pymem_raw;
+ state->allocators.mem = pymem;
+ state->allocators.obj = pyobject;
#ifdef WITH_PYMALLOC
Py_BUILD_ASSERT(NB_SMALL_SIZE_CLASSES == 64);
@@ -311,6 +315,7 @@ _PyMem_Initialize(struct _pymem_runtime_state *state)
#endif /* WITH_PYMALLOC */
}
+
#ifdef WITH_PYMALLOC
static int
_PyMem_DebugEnabled(void)