bpo-37732: Fix GCC warning in _PyObject_Malloc() (GH-15333) (GH-15342)

pymalloc_alloc() now returns directly the pointer, return NULL on
memory allocation error.

allocate_from_new_pool() already uses NULL as marker for "allocation
failed".

(cherry picked from commit 18f8dcfa10)
This commit is contained in:
Victor Stinner 2019-08-20 13:44:32 +01:00 committed by GitHub
parent 1271ee8187
commit 30e5aff5fb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1415,13 +1415,13 @@ address_in_range(void *p, poolp pool)
block allocations typically result in a couple of instructions). block allocations typically result in a couple of instructions).
Unless the optimizer reorders everything, being too smart... Unless the optimizer reorders everything, being too smart...
Return 1 if pymalloc allocated memory and wrote the pointer into *ptr_p. Return a pointer to newly allocated memory if pymalloc allocated memory.
Return 0 if pymalloc failed to allocate the memory block: on bigger Return NULL if pymalloc failed to allocate the memory block: on bigger
requests, on error in the code below (as a last chance to serve the request) requests, on error in the code below (as a last chance to serve the request)
or when the max memory limit has been reached. */ or when the max memory limit has been reached. */
static int static void*
pymalloc_alloc(void *ctx, void **ptr_p, size_t nbytes) pymalloc_alloc(void *ctx, size_t nbytes)
{ {
block *bp; block *bp;
poolp pool; poolp pool;
@ -1433,15 +1433,15 @@ pymalloc_alloc(void *ctx, void **ptr_p, size_t nbytes)
running_on_valgrind = RUNNING_ON_VALGRIND; running_on_valgrind = RUNNING_ON_VALGRIND;
} }
if (UNLIKELY(running_on_valgrind)) { if (UNLIKELY(running_on_valgrind)) {
return 0; return NULL;
} }
#endif #endif
if (nbytes == 0) { if (nbytes == 0) {
return 0; return NULL;
} }
if (nbytes > SMALL_REQUEST_THRESHOLD) { if (nbytes > SMALL_REQUEST_THRESHOLD) {
return 0; return NULL;
} }
/* /*
@ -1609,19 +1609,18 @@ pymalloc_alloc(void *ctx, void **ptr_p, size_t nbytes)
success: success:
assert(bp != NULL); assert(bp != NULL);
*ptr_p = (void *)bp; return (void *)bp;
return 1;
failed: failed:
return 0; return NULL;
} }
static void * static void *
_PyObject_Malloc(void *ctx, size_t nbytes) _PyObject_Malloc(void *ctx, size_t nbytes)
{ {
void* ptr; void* ptr = pymalloc_alloc(ctx, nbytes);
if (pymalloc_alloc(ctx, &ptr, nbytes)) { if (ptr != NULL) {
_Py_AllocatedBlocks++; _Py_AllocatedBlocks++;
return ptr; return ptr;
} }
@ -1637,12 +1636,11 @@ _PyObject_Malloc(void *ctx, size_t nbytes)
static void * static void *
_PyObject_Calloc(void *ctx, size_t nelem, size_t elsize) _PyObject_Calloc(void *ctx, size_t nelem, size_t elsize)
{ {
void* ptr;
assert(elsize == 0 || nelem <= (size_t)PY_SSIZE_T_MAX / elsize); assert(elsize == 0 || nelem <= (size_t)PY_SSIZE_T_MAX / elsize);
size_t nbytes = nelem * elsize; size_t nbytes = nelem * elsize;
if (pymalloc_alloc(ctx, &ptr, nbytes)) { void *ptr = pymalloc_alloc(ctx, nbytes);
if (ptr != NULL) {
memset(ptr, 0, nbytes); memset(ptr, 0, nbytes);
_Py_AllocatedBlocks++; _Py_AllocatedBlocks++;
return ptr; return ptr;
@ -1743,8 +1741,8 @@ pymalloc_free(void *ctx, void *p)
* are no arenas in usable_arenas with that value. * are no arenas in usable_arenas with that value.
*/ */
struct arena_object* lastnf = nfp2lasta[nf]; struct arena_object* lastnf = nfp2lasta[nf];
assert((nf == 0 && lastnf == NULL) || assert((nf == 0 && lastnf == NULL) ||
(nf > 0 && (nf > 0 &&
lastnf != NULL && lastnf != NULL &&
lastnf->nfreepools == nf && lastnf->nfreepools == nf &&
(lastnf->nextarena == NULL || (lastnf->nextarena == NULL ||