Mercurial > cpython-lru_cache-weakref
comparison Modules/python.c @ 84490:18bb92b0c458
Issue #18203: Replace malloc() with PyMem_RawMalloc() at Python initialization
* Replace malloc() with PyMem_RawMalloc()
* Replace PyMem_Malloc() with PyMem_RawMalloc() where the GIL is not held.
* _Py_char2wchar() now returns a buffer allocated by PyMem_RawMalloc(), instead
of PyMem_Malloc()
| author | Victor Stinner <victor.stinner@gmail.com> |
|---|---|
| date | Sun, 07 Jul 2013 16:25:15 +0200 |
| parents | 3b344eac6b0f |
| children | 65f2c92ed079 |
comparison
equal
deleted
inserted
replaced
| 84489:213d6d7f5979 | 84490:18bb92b0c458 |
|---|---|
| 16 #else | 16 #else |
| 17 | 17 |
| 18 int | 18 int |
| 19 main(int argc, char **argv) | 19 main(int argc, char **argv) |
| 20 { | 20 { |
| 21 wchar_t **argv_copy = (wchar_t **)PyMem_Malloc(sizeof(wchar_t*)*(argc+1)); | 21 wchar_t **argv_copy; |
| 22 /* We need a second copies, as Python might modify the first one. */ | 22 /* We need a second copies, as Python might modify the first one. */ |
| 23 wchar_t **argv_copy2 = (wchar_t **)PyMem_Malloc(sizeof(wchar_t*)*(argc+1)); | 23 wchar_t **argv_copy2; |
| 24 int i, res; | 24 int i, res; |
| 25 char *oldloc; | 25 char *oldloc; |
| 26 | |
| 27 argv_copy = (wchar_t **)PyMem_RawMalloc(sizeof(wchar_t*) * (argc+1)); | |
| 28 argv_copy2 = (wchar_t **)PyMem_RawMalloc(sizeof(wchar_t*) * (argc+1)); | |
| 29 if (!argv_copy || !argv_copy2) { | |
| 30 fprintf(stderr, "out of memory\n"); | |
| 31 return 1; | |
| 32 } | |
| 33 | |
| 26 /* 754 requires that FP exceptions run in "no stop" mode by default, | 34 /* 754 requires that FP exceptions run in "no stop" mode by default, |
| 27 * and until C vendors implement C99's ways to control FP exceptions, | 35 * and until C vendors implement C99's ways to control FP exceptions, |
| 28 * Python requires non-stop mode. Alas, some platforms enable FP | 36 * Python requires non-stop mode. Alas, some platforms enable FP |
| 29 * exceptions by default. Here we disable them. | 37 * exceptions by default. Here we disable them. |
| 30 */ | 38 */ |
| 32 fp_except_t m; | 40 fp_except_t m; |
| 33 | 41 |
| 34 m = fpgetmask(); | 42 m = fpgetmask(); |
| 35 fpsetmask(m & ~FP_X_OFL); | 43 fpsetmask(m & ~FP_X_OFL); |
| 36 #endif | 44 #endif |
| 37 if (!argv_copy || !argv_copy2) { | 45 |
| 38 fprintf(stderr, "out of memory\n"); | |
| 39 return 1; | |
| 40 } | |
| 41 oldloc = strdup(setlocale(LC_ALL, NULL)); | 46 oldloc = strdup(setlocale(LC_ALL, NULL)); |
| 42 setlocale(LC_ALL, ""); | 47 setlocale(LC_ALL, ""); |
| 43 for (i = 0; i < argc; i++) { | 48 for (i = 0; i < argc; i++) { |
| 44 argv_copy[i] = _Py_char2wchar(argv[i], NULL); | 49 argv_copy[i] = _Py_char2wchar(argv[i], NULL); |
| 45 if (!argv_copy[i]) { | 50 if (!argv_copy[i]) { |
| 55 | 60 |
| 56 setlocale(LC_ALL, oldloc); | 61 setlocale(LC_ALL, oldloc); |
| 57 free(oldloc); | 62 free(oldloc); |
| 58 res = Py_Main(argc, argv_copy); | 63 res = Py_Main(argc, argv_copy); |
| 59 for (i = 0; i < argc; i++) { | 64 for (i = 0; i < argc; i++) { |
| 60 PyMem_Free(argv_copy2[i]); | 65 PyMem_RawFree(argv_copy2[i]); |
| 61 } | 66 } |
| 62 PyMem_Free(argv_copy); | 67 PyMem_RawFree(argv_copy); |
| 63 PyMem_Free(argv_copy2); | 68 PyMem_RawFree(argv_copy2); |
| 64 return res; | 69 return res; |
| 65 } | 70 } |
| 66 #endif | 71 #endif |
