| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 80ed353 commit b16b4e4
12 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -120,7 +120,9 @@ typedef struct { | |||
| 120 | 120 | int utf8_mode; | |
| 121 | 121 | ||
| 122 | 122 | int dev_mode; /* Development mode. PYTHONDEVMODE, -X dev */ | |
| 123 | - char *allocator; /* Memory allocator: PYTHONMALLOC */ | ||
| 123 | + | ||
| 124 | + /* Memory allocator: PYTHONMALLOC env var */ | ||
| 125 | + PyMemAllocatorName allocator; | ||
| 124 | 126 | } _PyPreConfig; | |
| 125 | 127 | ||
| 126 | 128 | #ifdef MS_WINDOWS | |
@@ -137,7 +139,7 @@ typedef struct { | |||
| 137 | 139 | .isolated = -1, \ | |
| 138 | 140 | .use_environment = -1, \ | |
| 139 | 141 | .dev_mode = -1, \ | |
| 140 | - .allocator = NULL} | ||
| 142 | + .allocator = PYMEM_ALLOCATOR_NOT_SET} | ||
| 141 | 143 | ||
| 142 | 144 | ||
| 143 | 145 | /* --- _PyCoreConfig ---------------------------------------------- */ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -11,12 +11,8 @@ PyAPI_FUNC(void *) PyMem_RawCalloc(size_t nelem, size_t elsize); | |||
| 11 | 11 | PyAPI_FUNC(void *) PyMem_RawRealloc(void *ptr, size_t new_size); | |
| 12 | 12 | PyAPI_FUNC(void) PyMem_RawFree(void *ptr); | |
| 13 | 13 | ||
| 14 | - /* Configure the Python memory allocators. Pass NULL to use default | ||
| 15 | - allocators. */ | ||
| 16 | - PyAPI_FUNC(int) _PyMem_SetupAllocators(const char *opt); | ||
| 17 | - | ||
| 18 | 14 | /* Try to get the allocators name set by _PyMem_SetupAllocators(). */ | |
| 19 | - PyAPI_FUNC(const char*) _PyMem_GetAllocatorsName(void); | ||
| 15 | + PyAPI_FUNC(const char*) _PyMem_GetCurrentAllocatorName(void); | ||
| 20 | 16 | ||
| 21 | 17 | PyAPI_FUNC(void *) PyMem_Calloc(size_t nelem, size_t elsize); | |
| 22 | 18 | ||
@@ -41,6 +37,19 @@ typedef enum { | |||
| 41 | 37 | PYMEM_DOMAIN_OBJ | |
| 42 | 38 | } PyMemAllocatorDomain; | |
| 43 | 39 | ||
| 40 | + typedef enum { | ||
| 41 | + PYMEM_ALLOCATOR_NOT_SET = 0, | ||
| 42 | + PYMEM_ALLOCATOR_DEFAULT = 1, | ||
| 43 | + PYMEM_ALLOCATOR_DEBUG = 2, | ||
| 44 | + PYMEM_ALLOCATOR_MALLOC = 3, | ||
| 45 | + PYMEM_ALLOCATOR_MALLOC_DEBUG = 4, | ||
| 46 | + #ifdef WITH_PYMALLOC | ||
| 47 | + PYMEM_ALLOCATOR_PYMALLOC = 5, | ||
| 48 | + PYMEM_ALLOCATOR_PYMALLOC_DEBUG = 6, | ||
| 49 | + #endif | ||
| 50 | + } PyMemAllocatorName; | ||
| 51 | + | ||
| 52 | + | ||
| 44 | 53 | typedef struct { | |
| 45 | 54 | /* user context passed as the first argument to the 4 functions */ | |
| 46 | 55 | void *ctx; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -88,15 +88,14 @@ PyAPI_FUNC(_PyInitError) _PyPreCmdline_Read(_PyPreCmdline *cmdline, | |||
| 88 | 88 | ||
| 89 | 89 | /* --- _PyPreConfig ----------------------------------------------- */ | |
| 90 | 90 | ||
| 91 | - PyAPI_FUNC(void) _PyPreConfig_Clear(_PyPreConfig *config); | ||
| 92 | 91 | PyAPI_FUNC(int) _PyPreConfig_Copy(_PyPreConfig *config, | |
| 93 | 92 | const _PyPreConfig *config2); | |
| 94 | 93 | PyAPI_FUNC(PyObject*) _PyPreConfig_AsDict(const _PyPreConfig *config); | |
| 95 | 94 | PyAPI_FUNC(void) _PyCoreConfig_GetCoreConfig(_PyPreConfig *config, | |
| 96 | 95 | const _PyCoreConfig *core_config); | |
| 97 | 96 | PyAPI_FUNC(_PyInitError) _PyPreConfig_Read(_PyPreConfig *config, | |
| 98 | 97 | const _PyArgv *args); | |
| 99 | - PyAPI_FUNC(_PyInitError) _PyPreConfig_Write(_PyPreConfig *config); | ||
| 98 | + PyAPI_FUNC(_PyInitError) _PyPreConfig_Write(const _PyPreConfig *config); | ||
| 100 | 99 | ||
| 101 | 100 | ||
| 102 | 101 | /* --- _PyCoreConfig ---------------------------------------------- */ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -179,6 +179,15 @@ static inline int _PyMem_IsPtrFreed(void *ptr) | |||
| 179 | 179 | #endif | |
| 180 | 180 | } | |
| 181 | 181 | ||
| 182 | + PyAPI_FUNC(int) _PyMem_GetAllocatorName( | ||
| 183 | + const char *name, | ||
| 184 | + PyMemAllocatorName *allocator); | ||
| 185 | + | ||
| 186 | + /* Configure the Python memory allocators. | ||
| 187 | + Pass PYMEM_ALLOCATOR_DEFAULT to use default allocators. | ||
| 188 | + PYMEM_ALLOCATOR_NOT_SET does nothing. */ | ||
| 189 | + PyAPI_FUNC(int) _PyMem_SetupAllocators(PyMemAllocatorName allocator); | ||
| 190 | + | ||
| 182 | 191 | #ifdef __cplusplus | |
| 183 | 192 | } | |
| 184 | 193 | #endif | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -13,6 +13,9 @@ | |||
| 13 | 13 | ||
| 14 | 14 | ||
| 15 | 15 | MS_WINDOWS = (os.name == 'nt') | |
| 16 | + PYMEM_ALLOCATOR_NOT_SET = 0 | ||
| 17 | + PYMEM_ALLOCATOR_DEBUG = 2 | ||
| 18 | + PYMEM_ALLOCATOR_MALLOC = 3 | ||
| 16 | 19 | ||
| 17 | 20 | ||
| 18 | 21 | class EmbeddingTestsMixin: | |
@@ -272,7 +275,7 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase): | |||
| 272 | 275 | # Mark config which should be get by get_default_config() | |
| 273 | 276 | GET_DEFAULT_CONFIG = object() | |
| 274 | 277 | DEFAULT_PRE_CONFIG = { | |
| 275 | - 'allocator': None, | ||
| 278 | + 'allocator': PYMEM_ALLOCATOR_NOT_SET, | ||
| 276 | 279 | 'coerce_c_locale': 0, | |
| 277 | 280 | 'coerce_c_locale_warn': 0, | |
| 278 | 281 | 'utf8_mode': 0, | |
@@ -564,7 +567,7 @@ def test_init_global_config(self): | |||
| 564 | 567 | ||
| 565 | 568 | def test_init_from_config(self): | |
| 566 | 569 | preconfig = { | |
| 567 | - 'allocator': 'malloc', | ||
| 570 | + 'allocator': PYMEM_ALLOCATOR_MALLOC, | ||
| 568 | 571 | 'utf8_mode': 1, | |
| 569 | 572 | } | |
| 570 | 573 | config = { | |
@@ -608,7 +611,7 @@ def test_init_from_config(self): | |||
| 608 | 611 | self.check_config("init_from_config", config, preconfig) | |
| 609 | 612 | ||
| 610 | 613 | INIT_ENV_PRECONFIG = { | |
| 611 | - 'allocator': 'malloc', | ||
| 614 | + 'allocator': PYMEM_ALLOCATOR_MALLOC, | ||
| 612 | 615 | } | |
| 613 | 616 | INIT_ENV_CONFIG = { | |
| 614 | 617 | 'use_hash_seed': 1, | |
@@ -633,23 +636,23 @@ def test_init_env(self): | |||
| 633 | 636 | ||
| 634 | 637 | def test_init_env_dev_mode(self): | |
| 635 | 638 | preconfig = dict(self.INIT_ENV_PRECONFIG, | |
| 636 | - allocator='debug') | ||
| 639 | + allocator=PYMEM_ALLOCATOR_DEBUG) | ||
| 637 | 640 | config = dict(self.INIT_ENV_CONFIG, | |
| 638 | 641 | dev_mode=1, | |
| 639 | 642 | warnoptions=['default']) | |
| 640 | 643 | self.check_config("init_env_dev_mode", config, preconfig) | |
| 641 | 644 | ||
| 642 | 645 | def test_init_env_dev_mode_alloc(self): | |
| 643 | 646 | preconfig = dict(self.INIT_ENV_PRECONFIG, | |
| 644 | - allocator='malloc') | ||
| 647 | + allocator=PYMEM_ALLOCATOR_MALLOC) | ||
| 645 | 648 | config = dict(self.INIT_ENV_CONFIG, | |
| 646 | 649 | dev_mode=1, | |
| 647 | 650 | warnoptions=['default']) | |
| 648 | 651 | self.check_config("init_env_dev_mode_alloc", config, preconfig) | |
| 649 | 652 | ||
| 650 | 653 | def test_init_dev_mode(self): | |
| 651 | 654 | preconfig = { | |
| 652 | - 'allocator': 'debug', | ||
| 655 | + 'allocator': PYMEM_ALLOCATOR_DEBUG, | ||
| 653 | 656 | } | |
| 654 | 657 | config = { | |
| 655 | 658 | 'faulthandler': 1, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4297,7 +4297,7 @@ pymem_malloc_without_gil(PyObject *self, PyObject *args) | |||
| 4297 | 4297 | static PyObject* | |
| 4298 | 4298 | test_pymem_getallocatorsname(PyObject *self, PyObject *args) | |
| 4299 | 4299 | { | |
| 4300 | - const char *name = _PyMem_GetAllocatorsName(); | ||
| 4300 | + const char *name = _PyMem_GetCurrentAllocatorName(); | ||
| 4301 | 4301 | if (name == NULL) { | |
| 4302 | 4302 | PyErr_SetString(PyExc_RuntimeError, "cannot get allocators name"); | |
| 4303 | 4303 | return NULL; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -268,49 +268,94 @@ _PyMem_SetDefaultAllocator(PyMemAllocatorDomain domain, | |||
| 268 | 268 | ||
| 269 | 269 | ||
| 270 | 270 | int | |
| 271 | - _PyMem_SetupAllocators(const char *opt) | ||
| 271 | + _PyMem_GetAllocatorName(const char *name, PyMemAllocatorName *allocator) | ||
| 272 | 272 | { | |
| 273 | - if (opt == NULL || *opt == '\0') { | ||
| 273 | + if (name == NULL || *name == '\0') { | ||
| 274 | 274 | /* PYTHONMALLOC is empty or is not set or ignored (-E/-I command line | |
| 275 | - options): use default memory allocators */ | ||
| 276 | - opt = "default"; | ||
| 275 | + nameions): use default memory allocators */ | ||
| 276 | + *allocator = PYMEM_ALLOCATOR_DEFAULT; | ||
| 277 | 277 | } | |
| 278 | + else if (strcmp(name, "default") == 0) { | ||
| 279 | + *allocator = PYMEM_ALLOCATOR_DEFAULT; | ||
| 280 | + } | ||
| 281 | + else if (strcmp(name, "debug") == 0) { | ||
| 282 | + *allocator = PYMEM_ALLOCATOR_DEBUG; | ||
| 283 | + } | ||
| 284 | + #ifdef WITH_PYMALLOC | ||
| 285 | + else if (strcmp(name, "pymalloc") == 0) { | ||
| 286 | + *allocator = PYMEM_ALLOCATOR_PYMALLOC; | ||
| 287 | + } | ||
| 288 | + else if (strcmp(name, "pymalloc_debug") == 0) { | ||
| 289 | + *allocator = PYMEM_ALLOCATOR_PYMALLOC_DEBUG; | ||
| 290 | + } | ||
| 291 | + #endif | ||
| 292 | + else if (strcmp(name, "malloc") == 0) { | ||
| 293 | + *allocator = PYMEM_ALLOCATOR_MALLOC; | ||
| 294 | + } | ||
| 295 | + else if (strcmp(name, "malloc_debug") == 0) { | ||
| 296 | + *allocator = PYMEM_ALLOCATOR_MALLOC_DEBUG; | ||
| 297 | + } | ||
| 298 | + else { | ||
| 299 | + /* unknown allocator */ | ||
| 300 | + return -1; | ||
| 301 | + } | ||
| 302 | + return 0; | ||
| 303 | + } | ||
| 304 | + | ||
| 278 | 305 | ||
| 279 | - if (strcmp(opt, "default") == 0) { | ||
| 306 | + int | ||
| 307 | + _PyMem_SetupAllocators(PyMemAllocatorName allocator) | ||
| 308 | + { | ||
| 309 | + switch (allocator) { | ||
| 310 | + case PYMEM_ALLOCATOR_NOT_SET: | ||
| 311 | + /* do nothing */ | ||
| 312 | + break; | ||
| 313 | + | ||
| 314 | + case PYMEM_ALLOCATOR_DEFAULT: | ||
| 280 | 315 | (void)_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, NULL); | |
| 281 | 316 | (void)_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_MEM, NULL); | |
| 282 | 317 | (void)_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_OBJ, NULL); | |
| 283 | - } | ||
| 284 | - else if (strcmp(opt, "debug") == 0) { | ||
| 318 | + break; | ||
| 319 | + | ||
| 320 | + case PYMEM_ALLOCATOR_DEBUG: | ||
| 285 | 321 | (void)pymem_set_default_allocator(PYMEM_DOMAIN_RAW, 1, NULL); | |
| 286 | 322 | (void)pymem_set_default_allocator(PYMEM_DOMAIN_MEM, 1, NULL); | |
| 287 | 323 | (void)pymem_set_default_allocator(PYMEM_DOMAIN_OBJ, 1, NULL); | |
| 288 | - } | ||
| 324 | + break; | ||
| 325 | + | ||
| 289 | 326 | #ifdef WITH_PYMALLOC | |
| 290 | - else if (strcmp(opt, "pymalloc") == 0 || strcmp(opt, "pymalloc_debug") == 0) { | ||
| 327 | + case PYMEM_ALLOCATOR_PYMALLOC: | ||
| 328 | + case PYMEM_ALLOCATOR_PYMALLOC_DEBUG: | ||
| 329 | + { | ||
| 291 | 330 | PyMemAllocatorEx malloc_alloc = MALLOC_ALLOC; | |
| 292 | 331 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &malloc_alloc); | |
| 293 | 332 | ||
| 294 | 333 | PyMemAllocatorEx pymalloc = PYMALLOC_ALLOC; | |
| 295 | 334 | PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &pymalloc); | |
| 296 | 335 | PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &pymalloc); | |
| 297 | 336 | ||
| 298 | - if (strcmp(opt, "pymalloc_debug") == 0) { | ||
| 337 | + if (allocator == PYMEM_ALLOCATOR_PYMALLOC_DEBUG) { | ||
| 299 | 338 | PyMem_SetupDebugHooks(); | |
| 300 | 339 | } | |
| 340 | + break; | ||
| 301 | 341 | } | |
| 302 | 342 | #endif | |
| 303 | - else if (strcmp(opt, "malloc") == 0 || strcmp(opt, "malloc_debug") == 0) { | ||
| 343 | + | ||
| 344 | + case PYMEM_ALLOCATOR_MALLOC: | ||
| 345 | + case PYMEM_ALLOCATOR_MALLOC_DEBUG: | ||
| 346 | + { | ||
| 304 | 347 | PyMemAllocatorEx malloc_alloc = MALLOC_ALLOC; | |
| 305 | 348 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &malloc_alloc); | |
| 306 | 349 | PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &malloc_alloc); | |
| 307 | 350 | PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &malloc_alloc); | |
| 308 | 351 | ||
| 309 | - if (strcmp(opt, "malloc_debug") == 0) { | ||
| 352 | + if (allocator == PYMEM_ALLOCATOR_MALLOC_DEBUG) { | ||
| 310 | 353 | PyMem_SetupDebugHooks(); | |
| 311 | 354 | } | |
| 355 | + break; | ||
| 312 | 356 | } | |
| 313 | - else { | ||
| 357 | + | ||
| 358 | + default: | ||
| 314 | 359 | /* unknown allocator */ | |
| 315 | 360 | return -1; | |
| 316 | 361 | } | |
@@ -326,7 +371,7 @@ pymemallocator_eq(PyMemAllocatorEx *a, PyMemAllocatorEx *b) | |||
| 326 | 371 | ||
| 327 | 372 | ||
| 328 | 373 | const char* | |
| 329 | - _PyMem_GetAllocatorsName(void) | ||
| 374 | + _PyMem_GetCurrentAllocatorName(void) | ||
| 330 | 375 | { | |
| 331 | 376 | PyMemAllocatorEx malloc_alloc = MALLOC_ALLOC; | |
| 332 | 377 | #ifdef WITH_PYMALLOC | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -379,7 +379,7 @@ static int test_init_from_config(void) | |||
| 379 | 379 | _PyPreConfig preconfig = _PyPreConfig_INIT; | |
| 380 | 380 | ||
| 381 | 381 | putenv("PYTHONMALLOC=malloc_debug"); | |
| 382 | - preconfig.allocator = "malloc"; | ||
| 382 | + preconfig.allocator = PYMEM_ALLOCATOR_MALLOC; | ||
| 383 | 383 | ||
| 384 | 384 | putenv("PYTHONUTF8=0"); | |
| 385 | 385 | Py_UTF8Mode = 0; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2021,26 +2021,22 @@ core_read_precmdline(_PyCoreConfig *config, _PyPreCmdline *precmdline) | |||
| 2021 | 2021 | _PyPreConfig preconfig = _PyPreConfig_INIT; | |
| 2022 | 2022 | if (_PyPreConfig_Copy(&preconfig, &_PyRuntime.preconfig) < 0) { | |
| 2023 | 2023 | err = _Py_INIT_NO_MEMORY(); | |
| 2024 | - goto done; | ||
| 2024 | + return err; | ||
| 2025 | 2025 | } | |
| 2026 | 2026 | ||
| 2027 | 2027 | _PyCoreConfig_GetCoreConfig(&preconfig, config); | |
| 2028 | 2028 | ||
| 2029 | 2029 | err = _PyPreCmdline_Read(precmdline, &preconfig); | |
| 2030 | 2030 | if (_Py_INIT_FAILED(err)) { | |
| 2031 | - goto done; | ||
| 2031 | + return err; | ||
| 2032 | 2032 | } | |
| 2033 | 2033 | ||
| 2034 | 2034 | if (_PyPreCmdline_SetCoreConfig(precmdline, config) < 0) { | |
| 2035 | 2035 | err = _Py_INIT_NO_MEMORY(); | |
| 2036 | - goto done; | ||
| 2036 | + return err; | ||
| 2037 | 2037 | } | |
| 2038 | 2038 | ||
| 2039 | - err = _Py_INIT_OK(); | ||
| 2040 | - | ||
| 2041 | - done: | ||
| 2042 | - _PyPreConfig_Clear(&preconfig); | ||
| 2043 | - return err; | ||
| 2039 | + return _Py_INIT_OK(); | ||
| 2044 | 2040 | } | |
| 2045 | 2041 | ||
| 2046 | 2042 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments