Mercurial > cpython-lru_cache-weakref
diff Python/fileutils.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 | dfc020b4b123 |
| children | fc01f9497da7 |
line wrap: on
line diff
--- a/Python/fileutils.c Sun Jul 07 15:50:49 2013 +0200 +++ b/Python/fileutils.c Sun Jul 07 16:25:15 2013 +0200 @@ -229,7 +229,7 @@ Use _Py_wchar2char() to encode the character string back to a byte string. Return a pointer to a newly allocated wide character string (use - PyMem_Free() to free the memory) and write the number of written wide + PyMem_RawFree() to free the memory) and write the number of written wide characters excluding the null character into *size if size is not NULL, or NULL on error (decoding or memory allocation error). If size is not NULL, *size is set to (size_t)-1 on memory error and (size_t)-2 on decoding @@ -283,7 +283,7 @@ argsize = mbstowcs(NULL, arg, 0); #endif if (argsize != (size_t)-1) { - res = (wchar_t *)PyMem_Malloc((argsize+1)*sizeof(wchar_t)); + res = (wchar_t *)PyMem_RawMalloc((argsize+1)*sizeof(wchar_t)); if (!res) goto oom; count = mbstowcs(res, arg, argsize+1); @@ -300,7 +300,7 @@ return res; } } - PyMem_Free(res); + PyMem_RawFree(res); } /* Conversion failed. Fall back to escaping with surrogateescape. */ #ifdef HAVE_MBRTOWC @@ -309,7 +309,7 @@ /* Overallocate; as multi-byte characters are in the argument, the actual output could use less memory. */ argsize = strlen(arg) + 1; - res = (wchar_t*)PyMem_Malloc(argsize*sizeof(wchar_t)); + res = (wchar_t*)PyMem_RawMalloc(argsize*sizeof(wchar_t)); if (!res) goto oom; in = (unsigned char*)arg; @@ -325,7 +325,7 @@ since we provide everything that we have - unless there is a bug in the C library, or I misunderstood how mbrtowc works. */ - PyMem_Free(res); + PyMem_RawFree(res); if (size != NULL) *size = (size_t)-2; return NULL; @@ -648,12 +648,12 @@ return -1; } if (bufsiz <= r1) { - PyMem_Free(wbuf); + PyMem_RawFree(wbuf); errno = EINVAL; return -1; } wcsncpy(buf, wbuf, bufsiz); - PyMem_Free(wbuf); + PyMem_RawFree(wbuf); return (int)r1; } #endif @@ -689,12 +689,12 @@ return NULL; } if (resolved_path_size <= r) { - PyMem_Free(wresolved_path); + PyMem_RawFree(wresolved_path); errno = EINVAL; return NULL; } wcsncpy(resolved_path, wresolved_path, resolved_path_size); - PyMem_Free(wresolved_path); + PyMem_RawFree(wresolved_path); return resolved_path; } #endif @@ -720,11 +720,11 @@ if (wname == NULL) return NULL; if (size <= len) { - PyMem_Free(wname); + PyMem_RawFree(wname); return NULL; } wcsncpy(buf, wname, size); - PyMem_Free(wname); + PyMem_RawFree(wname); return buf; #endif }
