Issue #18203: Replace malloc() with PyMem_Malloc() in Python modules

Replace malloc() with PyMem_Malloc() when the GIL is held, or with
PyMem_RawMalloc() otherwise.
This commit is contained in:
Victor Stinner 2013-07-07 16:21:41 +02:00
parent 1a7425f67a
commit b64049183c
10 changed files with 52 additions and 55 deletions

View File

@ -117,7 +117,7 @@ insert_lop(PyCursesPanelObject *po)
{ {
list_of_panels *new; list_of_panels *new;
if ((new = (list_of_panels *)malloc(sizeof(list_of_panels))) == NULL) { if ((new = (list_of_panels *)PyMem_Malloc(sizeof(list_of_panels))) == NULL) {
PyErr_NoMemory(); PyErr_NoMemory();
return -1; return -1;
} }
@ -136,7 +136,7 @@ remove_lop(PyCursesPanelObject *po)
temp = lop; temp = lop;
if (temp->po == po) { if (temp->po == po) {
lop = temp->next; lop = temp->next;
free(temp); PyMem_Free(temp);
return; return;
} }
while (temp->next == NULL || temp->next->po != po) { while (temp->next == NULL || temp->next->po != po) {
@ -148,7 +148,7 @@ remove_lop(PyCursesPanelObject *po)
temp = temp->next; temp = temp->next;
} }
n = temp->next->next; n = temp->next->next;
free(temp->next); PyMem_Free(temp->next);
temp->next = n; temp->next = n;
return; return;
} }

View File

@ -3406,7 +3406,7 @@ PyInit__curses(void)
continue; continue;
if (strncmp(key_n,"KEY_F(",6)==0) { if (strncmp(key_n,"KEY_F(",6)==0) {
char *p1, *p2; char *p1, *p2;
key_n2 = malloc(strlen(key_n)+1); key_n2 = PyMem_Malloc(strlen(key_n)+1);
if (!key_n2) { if (!key_n2) {
PyErr_NoMemory(); PyErr_NoMemory();
break; break;
@ -3425,7 +3425,7 @@ PyInit__curses(void)
key_n2 = key_n; key_n2 = key_n;
SetDictInt(key_n2,key); SetDictInt(key_n2,key);
if (key_n2 != key_n) if (key_n2 != key_n)
free(key_n2); PyMem_Free(key_n2);
} }
#endif #endif
SetDictInt("KEY_MIN", KEY_MIN); SetDictInt("KEY_MIN", KEY_MIN);

View File

@ -223,7 +223,7 @@ static ProfilerEntry*
newProfilerEntry(ProfilerObject *pObj, void *key, PyObject *userObj) newProfilerEntry(ProfilerObject *pObj, void *key, PyObject *userObj)
{ {
ProfilerEntry *self; ProfilerEntry *self;
self = (ProfilerEntry*) malloc(sizeof(ProfilerEntry)); self = (ProfilerEntry*) PyMem_Malloc(sizeof(ProfilerEntry));
if (self == NULL) { if (self == NULL) {
pObj->flags |= POF_NOMEMORY; pObj->flags |= POF_NOMEMORY;
return NULL; return NULL;
@ -231,7 +231,7 @@ newProfilerEntry(ProfilerObject *pObj, void *key, PyObject *userObj)
userObj = normalizeUserObj(userObj); userObj = normalizeUserObj(userObj);
if (userObj == NULL) { if (userObj == NULL) {
PyErr_Clear(); PyErr_Clear();
free(self); PyMem_Free(self);
pObj->flags |= POF_NOMEMORY; pObj->flags |= POF_NOMEMORY;
return NULL; return NULL;
} }
@ -264,7 +264,7 @@ static ProfilerSubEntry *
newSubEntry(ProfilerObject *pObj, ProfilerEntry *caller, ProfilerEntry* entry) newSubEntry(ProfilerObject *pObj, ProfilerEntry *caller, ProfilerEntry* entry)
{ {
ProfilerSubEntry *self; ProfilerSubEntry *self;
self = (ProfilerSubEntry*) malloc(sizeof(ProfilerSubEntry)); self = (ProfilerSubEntry*) PyMem_Malloc(sizeof(ProfilerSubEntry));
if (self == NULL) { if (self == NULL) {
pObj->flags |= POF_NOMEMORY; pObj->flags |= POF_NOMEMORY;
return NULL; return NULL;
@ -282,7 +282,7 @@ newSubEntry(ProfilerObject *pObj, ProfilerEntry *caller, ProfilerEntry* entry)
static int freeSubEntry(rotating_node_t *header, void *arg) static int freeSubEntry(rotating_node_t *header, void *arg)
{ {
ProfilerSubEntry *subentry = (ProfilerSubEntry*) header; ProfilerSubEntry *subentry = (ProfilerSubEntry*) header;
free(subentry); PyMem_Free(subentry);
return 0; return 0;
} }
@ -291,7 +291,7 @@ static int freeEntry(rotating_node_t *header, void *arg)
ProfilerEntry *entry = (ProfilerEntry*) header; ProfilerEntry *entry = (ProfilerEntry*) header;
RotatingTree_Enum(entry->calls, freeSubEntry, NULL); RotatingTree_Enum(entry->calls, freeSubEntry, NULL);
Py_DECREF(entry->userObj); Py_DECREF(entry->userObj);
free(entry); PyMem_Free(entry);
return 0; return 0;
} }
@ -301,13 +301,13 @@ static void clearEntries(ProfilerObject *pObj)
pObj->profilerEntries = EMPTY_ROTATING_TREE; pObj->profilerEntries = EMPTY_ROTATING_TREE;
/* release the memory hold by the ProfilerContexts */ /* release the memory hold by the ProfilerContexts */
if (pObj->currentProfilerContext) { if (pObj->currentProfilerContext) {
free(pObj->currentProfilerContext); PyMem_Free(pObj->currentProfilerContext);
pObj->currentProfilerContext = NULL; pObj->currentProfilerContext = NULL;
} }
while (pObj->freelistProfilerContext) { while (pObj->freelistProfilerContext) {
ProfilerContext *c = pObj->freelistProfilerContext; ProfilerContext *c = pObj->freelistProfilerContext;
pObj->freelistProfilerContext = c->previous; pObj->freelistProfilerContext = c->previous;
free(c); PyMem_Free(c);
} }
pObj->freelistProfilerContext = NULL; pObj->freelistProfilerContext = NULL;
} }
@ -393,7 +393,7 @@ ptrace_enter_call(PyObject *self, void *key, PyObject *userObj)
else { else {
/* free list exhausted, allocate a new one */ /* free list exhausted, allocate a new one */
pContext = (ProfilerContext*) pContext = (ProfilerContext*)
malloc(sizeof(ProfilerContext)); PyMem_Malloc(sizeof(ProfilerContext));
if (pContext == NULL) { if (pContext == NULL) {
pObj->flags |= POF_NOMEMORY; pObj->flags |= POF_NOMEMORY;
goto restorePyerr; goto restorePyerr;
@ -712,7 +712,7 @@ flush_unmatched(ProfilerObject *pObj)
else else
pObj->currentProfilerContext = pContext->previous; pObj->currentProfilerContext = pContext->previous;
if (pContext) if (pContext)
free(pContext); PyMem_Free(pContext);
} }
} }

View File

@ -3118,7 +3118,7 @@ static int _setup_ssl_threads(void) {
if (_ssl_locks == NULL) { if (_ssl_locks == NULL) {
_ssl_locks_count = CRYPTO_num_locks(); _ssl_locks_count = CRYPTO_num_locks();
_ssl_locks = (PyThread_type_lock *) _ssl_locks = (PyThread_type_lock *)
malloc(sizeof(PyThread_type_lock) * _ssl_locks_count); PyMem_Malloc(sizeof(PyThread_type_lock) * _ssl_locks_count);
if (_ssl_locks == NULL) if (_ssl_locks == NULL)
return 0; return 0;
memset(_ssl_locks, 0, memset(_ssl_locks, 0,
@ -3130,7 +3130,7 @@ static int _setup_ssl_threads(void) {
for (j = 0; j < i; j++) { for (j = 0; j < i; j++) {
PyThread_free_lock(_ssl_locks[j]); PyThread_free_lock(_ssl_locks[j]);
} }
free(_ssl_locks); PyMem_Free(_ssl_locks);
return 0; return 0;
} }
} }

View File

@ -1137,8 +1137,8 @@ audioop_ratecv(PyObject *self, PyObject *args)
"not enough memory for output buffer"); "not enough memory for output buffer");
return 0; return 0;
} }
prev_i = (int *) malloc(nchannels * sizeof(int)); prev_i = (int *) PyMem_Malloc(nchannels * sizeof(int));
cur_i = (int *) malloc(nchannels * sizeof(int)); cur_i = (int *) PyMem_Malloc(nchannels * sizeof(int));
if (prev_i == NULL || cur_i == NULL) { if (prev_i == NULL || cur_i == NULL) {
(void) PyErr_NoMemory(); (void) PyErr_NoMemory();
goto exit; goto exit;
@ -1257,10 +1257,8 @@ audioop_ratecv(PyObject *self, PyObject *args)
} }
} }
exit: exit:
if (prev_i != NULL) PyMem_Free(prev_i);
free(prev_i); PyMem_Free(cur_i);
if (cur_i != NULL)
free(cur_i);
return rv; return rv;
} }

View File

@ -1298,14 +1298,14 @@ win32_wchdir(LPCWSTR path)
if (!result) if (!result)
return FALSE; return FALSE;
if (result > MAX_PATH+1) { if (result > MAX_PATH+1) {
new_path = malloc(result * sizeof(wchar_t)); new_path = PyMem_RawMalloc(result * sizeof(wchar_t));
if (!new_path) { if (!new_path) {
SetLastError(ERROR_OUTOFMEMORY); SetLastError(ERROR_OUTOFMEMORY);
return FALSE; return FALSE;
} }
result = GetCurrentDirectoryW(result, new_path); result = GetCurrentDirectoryW(result, new_path);
if (!result) { if (!result) {
free(new_path); PyMem_RawFree(new_path);
return FALSE; return FALSE;
} }
} }
@ -1316,7 +1316,7 @@ win32_wchdir(LPCWSTR path)
env[1] = new_path[0]; env[1] = new_path[0];
result = SetEnvironmentVariableW(env, new_path); result = SetEnvironmentVariableW(env, new_path);
if (new_path != _new_path) if (new_path != _new_path)
free(new_path); PyMem_RawFree(new_path);
return result; return result;
} }
#endif #endif
@ -1497,7 +1497,7 @@ get_target_path(HANDLE hdl, wchar_t **target_path)
if(!buf_size) if(!buf_size)
return FALSE; return FALSE;
buf = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t)); buf = (wchar_t *)PyMem_Malloc((buf_size+1)*sizeof(wchar_t));
if (!buf) { if (!buf) {
SetLastError(ERROR_OUTOFMEMORY); SetLastError(ERROR_OUTOFMEMORY);
return FALSE; return FALSE;
@ -1507,12 +1507,12 @@ get_target_path(HANDLE hdl, wchar_t **target_path)
buf, buf_size, VOLUME_NAME_DOS); buf, buf_size, VOLUME_NAME_DOS);
if(!result_length) { if(!result_length) {
free(buf); PyMem_Free(buf);
return FALSE; return FALSE;
} }
if(!CloseHandle(hdl)) { if(!CloseHandle(hdl)) {
free(buf); PyMem_Free(buf);
return FALSE; return FALSE;
} }
@ -1603,7 +1603,7 @@ win32_xstat_impl(const char *path, struct win32_stat *result,
return -1; return -1;
code = win32_xstat_impl_w(target_path, result, FALSE); code = win32_xstat_impl_w(target_path, result, FALSE);
free(target_path); PyMem_Free(target_path);
return code; return code;
} }
} else } else
@ -1699,7 +1699,7 @@ win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
return -1; return -1;
code = win32_xstat_impl_w(target_path, result, FALSE); code = win32_xstat_impl_w(target_path, result, FALSE);
free(target_path); PyMem_Free(target_path);
return code; return code;
} }
} else } else
@ -3089,7 +3089,7 @@ posix_getcwd(int use_bytes)
terminating \0. If the buffer is too small, len includes terminating \0. If the buffer is too small, len includes
the space needed for the terminator. */ the space needed for the terminator. */
if (len >= sizeof wbuf/ sizeof wbuf[0]) { if (len >= sizeof wbuf/ sizeof wbuf[0]) {
wbuf2 = malloc(len * sizeof(wchar_t)); wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t));
if (wbuf2) if (wbuf2)
len = GetCurrentDirectoryW(len, wbuf2); len = GetCurrentDirectoryW(len, wbuf2);
} }
@ -3100,12 +3100,12 @@ posix_getcwd(int use_bytes)
} }
if (!len) { if (!len) {
if (wbuf2 != wbuf) if (wbuf2 != wbuf)
free(wbuf2); PyMem_RawFree(wbuf2);
return PyErr_SetFromWindowsErr(0); return PyErr_SetFromWindowsErr(0);
} }
resobj = PyUnicode_FromWideChar(wbuf2, len); resobj = PyUnicode_FromWideChar(wbuf2, len);
if (wbuf2 != wbuf) if (wbuf2 != wbuf)
free(wbuf2); PyMem_RawFree(wbuf2);
return resobj; return resobj;
} }
@ -3289,7 +3289,7 @@ _listdir_windows_no_opendir(path_t *path, PyObject *list)
len = wcslen(path->wide); len = wcslen(path->wide);
} }
/* The +5 is so we can append "\\*.*\0" */ /* The +5 is so we can append "\\*.*\0" */
wnamebuf = malloc((len + 5) * sizeof(wchar_t)); wnamebuf = PyMem_Malloc((len + 5) * sizeof(wchar_t));
if (!wnamebuf) { if (!wnamebuf) {
PyErr_NoMemory(); PyErr_NoMemory();
goto exit; goto exit;
@ -3410,8 +3410,7 @@ exit:
} }
} }
} }
if (wnamebuf) PyMem_Free(wnamebuf);
free(wnamebuf);
return list; return list;
} /* end of _listdir_windows_no_opendir */ } /* end of _listdir_windows_no_opendir */
@ -3575,7 +3574,7 @@ posix__getfullpathname(PyObject *self, PyObject *args)
Py_ARRAY_LENGTH(woutbuf), Py_ARRAY_LENGTH(woutbuf),
woutbuf, &wtemp); woutbuf, &wtemp);
if (result > Py_ARRAY_LENGTH(woutbuf)) { if (result > Py_ARRAY_LENGTH(woutbuf)) {
woutbufp = malloc(result * sizeof(wchar_t)); woutbufp = PyMem_Malloc(result * sizeof(wchar_t));
if (!woutbufp) if (!woutbufp)
return PyErr_NoMemory(); return PyErr_NoMemory();
result = GetFullPathNameW(wpath, result, woutbufp, &wtemp); result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
@ -3585,7 +3584,7 @@ posix__getfullpathname(PyObject *self, PyObject *args)
else else
v = win32_error_object("GetFullPathNameW", po); v = win32_error_object("GetFullPathNameW", po);
if (woutbufp != woutbuf) if (woutbufp != woutbuf)
free(woutbufp); PyMem_Free(woutbufp);
return v; return v;
} }
/* Drop the argument parsing error as narrow strings /* Drop the argument parsing error as narrow strings
@ -3655,7 +3654,7 @@ posix__getfinalpathname(PyObject *self, PyObject *args)
if(!buf_size) if(!buf_size)
return win32_error_object("GetFinalPathNameByHandle", po); return win32_error_object("GetFinalPathNameByHandle", po);
target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t)); target_path = (wchar_t *)PyMem_Malloc((buf_size+1)*sizeof(wchar_t));
if(!target_path) if(!target_path)
return PyErr_NoMemory(); return PyErr_NoMemory();
@ -3669,7 +3668,7 @@ posix__getfinalpathname(PyObject *self, PyObject *args)
target_path[result_length] = 0; target_path[result_length] = 0;
result = PyUnicode_FromWideChar(target_path, result_length); result = PyUnicode_FromWideChar(target_path, result_length);
free(target_path); PyMem_Free(target_path);
return result; return result;
} /* end of posix__getfinalpathname */ } /* end of posix__getfinalpathname */

View File

@ -997,7 +997,7 @@ xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args)
PyObject_GC_Track(new_parser); PyObject_GC_Track(new_parser);
if (self->buffer != NULL) { if (self->buffer != NULL) {
new_parser->buffer = malloc(new_parser->buffer_size); new_parser->buffer = PyMem_Malloc(new_parser->buffer_size);
if (new_parser->buffer == NULL) { if (new_parser->buffer == NULL) {
Py_DECREF(new_parser); Py_DECREF(new_parser);
return PyErr_NoMemory(); return PyErr_NoMemory();
@ -1014,7 +1014,7 @@ xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args)
for (i = 0; handler_info[i].name != NULL; i++) for (i = 0; handler_info[i].name != NULL; i++)
/* do nothing */; /* do nothing */;
new_parser->handlers = malloc(sizeof(PyObject *) * i); new_parser->handlers = PyMem_Malloc(sizeof(PyObject *) * i);
if (!new_parser->handlers) { if (!new_parser->handlers) {
Py_DECREF(new_parser); Py_DECREF(new_parser);
return PyErr_NoMemory(); return PyErr_NoMemory();
@ -1206,7 +1206,7 @@ newxmlparseobject(char *encoding, char *namespace_separator, PyObject *intern)
for (i = 0; handler_info[i].name != NULL; i++) for (i = 0; handler_info[i].name != NULL; i++)
/* do nothing */; /* do nothing */;
self->handlers = malloc(sizeof(PyObject *) * i); self->handlers = PyMem_Malloc(sizeof(PyObject *) * i);
if (!self->handlers) { if (!self->handlers) {
Py_DECREF(self); Py_DECREF(self);
return PyErr_NoMemory(); return PyErr_NoMemory();
@ -1233,11 +1233,11 @@ xmlparse_dealloc(xmlparseobject *self)
self->handlers[i] = NULL; self->handlers[i] = NULL;
Py_XDECREF(temp); Py_XDECREF(temp);
} }
free(self->handlers); PyMem_Free(self->handlers);
self->handlers = NULL; self->handlers = NULL;
} }
if (self->buffer != NULL) { if (self->buffer != NULL) {
free(self->buffer); PyMem_Free(self->buffer);
self->buffer = NULL; self->buffer = NULL;
} }
Py_XDECREF(self->intern); Py_XDECREF(self->intern);
@ -1437,7 +1437,7 @@ xmlparse_setattro(xmlparseobject *self, PyObject *name, PyObject *v)
return -1; return -1;
if (b) { if (b) {
if (self->buffer == NULL) { if (self->buffer == NULL) {
self->buffer = malloc(self->buffer_size); self->buffer = PyMem_Malloc(self->buffer_size);
if (self->buffer == NULL) { if (self->buffer == NULL) {
PyErr_NoMemory(); PyErr_NoMemory();
return -1; return -1;
@ -1448,7 +1448,7 @@ xmlparse_setattro(xmlparseobject *self, PyObject *name, PyObject *v)
else if (self->buffer != NULL) { else if (self->buffer != NULL) {
if (flush_character_buffer(self) < 0) if (flush_character_buffer(self) < 0)
return -1; return -1;
free(self->buffer); PyMem_Free(self->buffer);
self->buffer = NULL; self->buffer = NULL;
} }
return 0; return 0;
@ -1508,9 +1508,9 @@ xmlparse_setattro(xmlparseobject *self, PyObject *name, PyObject *v)
flush_character_buffer(self); flush_character_buffer(self);
} }
/* free existing buffer */ /* free existing buffer */
free(self->buffer); PyMem_Free(self->buffer);
} }
self->buffer = malloc(new_buffer_size); self->buffer = PyMem_Malloc(new_buffer_size);
if (self->buffer == NULL) { if (self->buffer == NULL) {
PyErr_NoMemory(); PyErr_NoMemory();
return -1; return -1;

View File

@ -84,12 +84,12 @@ parse_and_bind(PyObject *self, PyObject *args)
return NULL; return NULL;
/* Make a copy -- rl_parse_and_bind() modifies its argument */ /* Make a copy -- rl_parse_and_bind() modifies its argument */
/* Bernard Herzog */ /* Bernard Herzog */
copy = malloc(1 + strlen(s)); copy = PyMem_Malloc(1 + strlen(s));
if (copy == NULL) if (copy == NULL)
return PyErr_NoMemory(); return PyErr_NoMemory();
strcpy(copy, s); strcpy(copy, s);
rl_parse_and_bind(copy); rl_parse_and_bind(copy);
free(copy); /* Free the copy */ PyMem_Free(copy); /* Free the copy */
Py_RETURN_NONE; Py_RETURN_NONE;
} }

View File

@ -165,7 +165,7 @@ PyZlib_compress(PyObject *self, PyObject *args)
zst.avail_out = length + length/1000 + 12 + 1; zst.avail_out = length + length/1000 + 12 + 1;
output = (Byte*)malloc(zst.avail_out); output = (Byte*)PyMem_Malloc(zst.avail_out);
if (output == NULL) { if (output == NULL) {
PyErr_SetString(PyExc_MemoryError, PyErr_SetString(PyExc_MemoryError,
"Can't allocate memory to compress data"); "Can't allocate memory to compress data");
@ -218,7 +218,7 @@ PyZlib_compress(PyObject *self, PyObject *args)
error: error:
PyBuffer_Release(&pinput); PyBuffer_Release(&pinput);
free(output); PyMem_Free(output);
return ReturnVal; return ReturnVal;
} }

View File

@ -938,7 +938,7 @@ Reg2Py(BYTE *retDataBuf, DWORD retDataSize, DWORD typ)
wchar_t *data = (wchar_t *)retDataBuf; wchar_t *data = (wchar_t *)retDataBuf;
int len = retDataSize / 2; int len = retDataSize / 2;
int s = countStrings(data, len); int s = countStrings(data, len);
wchar_t **str = (wchar_t **)malloc(sizeof(wchar_t *)*s); wchar_t **str = (wchar_t **)PyMem_Malloc(sizeof(wchar_t *)*s);
if (str == NULL) if (str == NULL)
return PyErr_NoMemory(); return PyErr_NoMemory();
@ -959,7 +959,7 @@ Reg2Py(BYTE *retDataBuf, DWORD retDataSize, DWORD typ)
index, index,
PyUnicode_FromWideChar(str[index], len)); PyUnicode_FromWideChar(str[index], len));
} }
free(str); PyMem_Free(str);
break; break;
} }