mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
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()
This commit is contained in:
parent
51fa458d0a
commit
1a7425f67a
8 changed files with 76 additions and 76 deletions
|
@ -229,7 +229,7 @@ decode_ascii_surrogateescape(const char *arg, size_t *size)
|
|||
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 @@ _Py_char2wchar(const char* arg, size_t *size)
|
|||
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 @@ _Py_char2wchar(const char* arg, size_t *size)
|
|||
return res;
|
||||
}
|
||||
}
|
||||
PyMem_Free(res);
|
||||
PyMem_RawFree(res);
|
||||
}
|
||||
/* Conversion failed. Fall back to escaping with surrogateescape. */
|
||||
#ifdef HAVE_MBRTOWC
|
||||
|
@ -309,7 +309,7 @@ _Py_char2wchar(const char* arg, size_t *size)
|
|||
/* 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 @@ _Py_char2wchar(const char* arg, size_t *size)
|
|||
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 @@ _Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t bufsiz)
|
|||
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 @@ _Py_wrealpath(const wchar_t *path,
|
|||
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 @@ _Py_wgetcwd(wchar_t *buf, size_t size)
|
|||
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
|
||||
}
|
||||
|
|
|
@ -6,11 +6,11 @@
|
|||
/* --------------------------------------------------------------------------
|
||||
CAUTION
|
||||
|
||||
Always use malloc() and free() directly in this file. A number of these
|
||||
functions are advertised as safe to call when the GIL isn't held, and in
|
||||
a debug build Python redirects (e.g.) PyMem_NEW (etc) to Python's debugging
|
||||
obmalloc functions. Those aren't thread-safe (they rely on the GIL to avoid
|
||||
the expense of doing their own locking).
|
||||
Always use PyMem_RawMalloc() and PyMem_RawFree() directly in this file. A
|
||||
number of these functions are advertised as safe to call when the GIL isn't
|
||||
held, and in a debug build Python redirects (e.g.) PyMem_NEW (etc) to Python's
|
||||
debugging obmalloc functions. Those aren't thread-safe (they rely on the GIL
|
||||
to avoid the expense of doing their own locking).
|
||||
-------------------------------------------------------------------------- */
|
||||
|
||||
#ifdef HAVE_DLOPEN
|
||||
|
@ -60,7 +60,7 @@ PyInterpreterState *
|
|||
PyInterpreterState_New(void)
|
||||
{
|
||||
PyInterpreterState *interp = (PyInterpreterState *)
|
||||
malloc(sizeof(PyInterpreterState));
|
||||
PyMem_RawMalloc(sizeof(PyInterpreterState));
|
||||
|
||||
if (interp != NULL) {
|
||||
HEAD_INIT();
|
||||
|
@ -148,7 +148,7 @@ PyInterpreterState_Delete(PyInterpreterState *interp)
|
|||
Py_FatalError("PyInterpreterState_Delete: remaining threads");
|
||||
*p = interp->next;
|
||||
HEAD_UNLOCK();
|
||||
free(interp);
|
||||
PyMem_RawFree(interp);
|
||||
#ifdef WITH_THREAD
|
||||
if (interp_head == NULL && head_mutex != NULL) {
|
||||
PyThread_free_lock(head_mutex);
|
||||
|
@ -168,7 +168,7 @@ threadstate_getframe(PyThreadState *self)
|
|||
static PyThreadState *
|
||||
new_threadstate(PyInterpreterState *interp, int init)
|
||||
{
|
||||
PyThreadState *tstate = (PyThreadState *)malloc(sizeof(PyThreadState));
|
||||
PyThreadState *tstate = (PyThreadState *)PyMem_RawMalloc(sizeof(PyThreadState));
|
||||
|
||||
if (_PyThreadState_GetFrame == NULL)
|
||||
_PyThreadState_GetFrame = threadstate_getframe;
|
||||
|
@ -365,7 +365,7 @@ tstate_delete_common(PyThreadState *tstate)
|
|||
if (tstate->next)
|
||||
tstate->next->prev = tstate->prev;
|
||||
HEAD_UNLOCK();
|
||||
free(tstate);
|
||||
PyMem_RawFree(tstate);
|
||||
}
|
||||
|
||||
|
||||
|
@ -432,7 +432,7 @@ _PyThreadState_DeleteExcept(PyThreadState *tstate)
|
|||
for (p = garbage; p; p = next) {
|
||||
next = p->next;
|
||||
PyThreadState_Clear(p);
|
||||
free(p);
|
||||
PyMem_RawFree(p);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -231,7 +231,7 @@ find_key(int key, void *value)
|
|||
assert(p == NULL);
|
||||
goto Done;
|
||||
}
|
||||
p = (struct key *)malloc(sizeof(struct key));
|
||||
p = (struct key *)PyMem_RawMalloc(sizeof(struct key));
|
||||
if (p != NULL) {
|
||||
p->id = id;
|
||||
p->key = key;
|
||||
|
@ -270,7 +270,7 @@ PyThread_delete_key(int key)
|
|||
while ((p = *q) != NULL) {
|
||||
if (p->key == key) {
|
||||
*q = p->next;
|
||||
free((void *)p);
|
||||
PyMem_RawFree((void *)p);
|
||||
/* NB This does *not* free p->value! */
|
||||
}
|
||||
else
|
||||
|
@ -324,7 +324,7 @@ PyThread_delete_key_value(int key)
|
|||
while ((p = *q) != NULL) {
|
||||
if (p->key == key && p->id == id) {
|
||||
*q = p->next;
|
||||
free((void *)p);
|
||||
PyMem_RawFree((void *)p);
|
||||
/* NB This does *not* free p->value! */
|
||||
break;
|
||||
}
|
||||
|
@ -357,7 +357,7 @@ PyThread_ReInitTLS(void)
|
|||
while ((p = *q) != NULL) {
|
||||
if (p->id != id) {
|
||||
*q = p->next;
|
||||
free((void *)p);
|
||||
PyMem_RawFree((void *)p);
|
||||
/* NB This does *not* free p->value! */
|
||||
}
|
||||
else
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue