8000 bpo-37732: Fix GCC warning in _PyObject_Malloc() (GH-15333) · python/cpython@18f8dcf · GitHub
[go: up one dir, main page]

Skip to content

Commit 18f8dcf

Browse files
authored
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".
1 parent b0f4dab commit 18f8dcf

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

Objects/obmalloc.c

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,42 +1580,43 @@ allocate_from_new_pool(uint size)
15801580

15811581
/* pymalloc allocator
15821582
1583-
Return 1 if pymalloc allocated memory and wrote the pointer into *ptr_p.
1583+
Return a pointer to newly allocated memory if pymalloc allocated memory.
15841584
1585-
Return 0 if pymalloc failed to allocate the memory block: on bigger
1585+
Return NULL if pymalloc failed to allocate the memory block: on bigger
15861586
requests, on error in the code below (as a last chance to serve the request)
15871587
or when the max memory limit has been reached.
15881588
*/
1589-
static inline int
1590-
pymalloc_alloc(void *ctx, void **ptr_p, size_t nbytes)
1589+
static inline void*
1590+
pymalloc_alloc(void *ctx, size_t nbytes)
15911591
{
15921592
#ifdef WITH_VALGRIND
15931593
if (UNLIKELY(running_on_valgrind == -1)) {
15941594
running_on_valgrind = RUNNING_ON_VALGRIND;
15951595
}
15961596
if (UNLIKELY(running_on_valgrind)) {
1597-
return 0;
1597+
return NULL;
15981598
}
15991599
#endif
16001600

16011601
if (UNLIKELY(nbytes == 0)) {
1602-
return 0;
1602+
return NULL;
16031603
}
16041604
if (UNLIKELY(nbytes > SMALL_REQUEST_THRESHOLD)) {
1605-
return 0;
1605+
return NULL;
16061606
}
16071607

16081608
uint size = (uint)(nbytes - 1) >> ALIGNMENT_SHIFT;
16091609
poolp pool = usedpools[size + size];
16101610
block *bp;
1611-
1611+
16121612
if (LIKELY(pool != pool->nextpool)) {
16131613
/*
16141614
* There is a used pool for this size class.
16151615
* Pick up the head block of its free list.
16161616
*/
16171617
++pool->ref.count;
16181618
bp = pool->freeblock;
1619+
assert(bp != NULL);
16191620

16201621
if (UNLIKELY((pool->freeblock = *(block **)bp) == NULL)) {
16211622
// 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)
16271628
* available: use a free pool.
16281629
*/
16291630
bp = allocate_from_new_pool(size);
1630-
if (UNLIKELY(bp == NULL)) {
1631-
return 0;
1632-
}
16331631
}
16341632

1635-
assert(bp != NULL);
1636-
*ptr_p = (void *)bp;
1637-
return 1;
1633+
return (void *)bp;
16381634
}
16391635

16401636

16411637
static void *
16421638
_PyObject_Malloc(void *ctx, size_t nbytes)
16431639
{
1644-
void* ptr;
1645-
if (LIKELY(pymalloc_alloc(ctx, &ptr, nbytes))) {
1640+
void* ptr = pymalloc_alloc(ctx, nbytes);
1641+
if (LIKELY(ptr != NULL)) {
16461642
return ptr;
16471643
}
16481644

@@ -1657,12 +1653,11 @@ _PyObject_Malloc(void *ctx, size_t nbytes)
16571653
static void *
16581654
_PyObject_Calloc(void *ctx, size_t nelem, size_t elsize)
16591655
{
1660-
void* ptr;
1661-
16621656
assert(elsize == 0 || nelem <= (size_t)PY_SSIZE_T_MAX / elsize);
16631657
size_t nbytes = nelem * elsize;
16641658

1665-
if (LIKELY(pymalloc_alloc(ctx, &ptr, nbytes))) {
1659+
void* ptr = pymalloc_alloc(ctx, nbytes);
1660+
if (LIKELY(ptr != NULL)) {
16661661
memset(ptr, 0, nbytes);
16671662
return ptr;
16681663
}
@@ -1711,8 +1706,8 @@ insert_to_freepool(poolp pool)
17111706
* are no arenas in usable_arenas with that value.
17121707
*/
17131708
struct arena_object* lastnf = nfp2lasta[nf];
1714-
assert((nf == 0 && lastnf == NULL) ||
1715-
(nf > 0 &&
1709+
assert((nf == 0 && lastnf == NULL) ||
1710+
(nf > 0 &&
17161711
lastnf != NULL &&
17171712
lastnf->nfreepools == nf &&
17181713
(lastnf->nextarena == NULL ||

0 commit comments

Comments
 (0)
0