Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 64d80c9

Browse files
committed
PyObject_Malloc: make a tiny bit faster for platforms where malloc(0)
doesn't return NULL. PyObject_Realloc: better comment for why we don't call PyObject_Malloc(0).
1 parent 3e12071 commit 64d80c9

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

Objects/obmalloc.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,11 @@ PyObject_Malloc(size_t nbytes)
685685
* last chance to serve the request) or when the max memory limit
686686
* has been reached.
687687
*/
688-
return (void *)malloc(nbytes ? nbytes : 1);
688+
#ifdef MALLOC_ZERO_RETURNS_NULL
689+
if (nbytes == 0)
690+
nbytes = 1;
691+
#endif
692+
return (void *)malloc(nbytes);
689693
}
690694

691695
/* free */
@@ -803,7 +807,10 @@ PyObject_Realloc(void *p, size_t nbytes)
803807
}
804808
/* We're not managing this block. */
805809
if (nbytes <= SMALL_REQUEST_THRESHOLD) {
806-
/* Take over this block. */
810+
/* Take over this block -- ask for at least one byte so
811+
* we really do take it over (PyObject_Malloc(0) goes to
812+
* the system malloc).
813+
*/
807814
bp = PyObject_Malloc(nbytes ? nbytes : 1);
808815
if (bp != NULL) {
809816
memcpy(bp, p, nbytes);

0 commit comments

Comments
 (0)