Commit 18f8dcfa authored by Victor Stinner's avatar Victor Stinner Committed by GitHub

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

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".
parent b0f4dab8
...@@ -1580,29 +1580,29 @@ allocate_from_new_pool(uint size) ...@@ -1580,29 +1580,29 @@ allocate_from_new_pool(uint size)
/* pymalloc allocator /* pymalloc allocator
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 inline int static inline void*
pymalloc_alloc(void *ctx, void **ptr_p, size_t nbytes) pymalloc_alloc(void *ctx, size_t nbytes)
{ {
#ifdef WITH_VALGRIND #ifdef WITH_VALGRIND
if (UNLIKELY(running_on_valgrind == -1)) { if (UNLIKELY(running_on_valgrind == -1)) {
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 (UNLIKELY(nbytes == 0)) { if (UNLIKELY(nbytes == 0)) {
return 0; return NULL;
} }
if (UNLIKELY(nbytes > SMALL_REQUEST_THRESHOLD)) { if (UNLIKELY(nbytes > SMALL_REQUEST_THRESHOLD)) {
return 0; return NULL;
} }
uint size = (uint)(nbytes - 1) >> ALIGNMENT_SHIFT; uint size = (uint)(nbytes - 1) >> ALIGNMENT_SHIFT;
...@@ -1616,6 +1616,7 @@ pymalloc_alloc(void *ctx, void **ptr_p, size_t nbytes) ...@@ -1616,6 +1616,7 @@ pymalloc_alloc(void *ctx, void **ptr_p, size_t nbytes)
*/ */
++pool->ref.count; ++pool->ref.count;
bp = pool->freeblock; bp = pool->freeblock;
assert(bp != NULL);
if (UNLIKELY((pool->freeblock = *(block **)bp) == NULL)) { if (UNLIKELY((pool->freeblock = *(block **)bp) == NULL)) {
// Reached the end of the free list, try to extend it. // Reached the end of the free list, try to extend it.
...@@ -1627,22 +1628,17 @@ pymalloc_alloc(void *ctx, void **ptr_p, size_t nbytes) ...@@ -1627,22 +1628,17 @@ pymalloc_alloc(void *ctx, void **ptr_p, size_t nbytes)
* available: use a free pool. * available: use a free pool.
*/ */
bp = allocate_from_new_pool(size); bp = allocate_from_new_pool(size);
if (UNLIKELY(bp == NULL)) {
return 0;
}
} }
assert(bp != NULL); return (void *)bp;
*ptr_p = (void *)bp;
return 1;
} }
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 (LIKELY(pymalloc_alloc(ctx, &ptr, nbytes))) { if (LIKELY(ptr != NULL)) {
return ptr; return ptr;
} }
...@@ -1657,12 +1653,11 @@ _PyObject_Malloc(void *ctx, size_t nbytes) ...@@ -1657,12 +1653,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 (LIKELY(pymalloc_alloc(ctx, &ptr, nbytes))) { void* ptr = pymalloc_alloc(ctx, nbytes);
if (LIKELY(ptr != NULL)) {
memset(ptr, 0, nbytes); memset(ptr, 0, nbytes);
return ptr; return ptr;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment