Skip to content

Commit 08facd2

Browse files
committed
Issue python#19741: cleanup tracemalloc_realloc()
Explain that unhandled error case is very unlikely
1 parent fc91285 commit 08facd2

1 file changed

Lines changed: 15 additions & 8 deletions

File tree

Modules/_tracemalloc.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -563,20 +563,27 @@ tracemalloc_realloc(void *ctx, void *ptr, size_t new_size, int gil_held)
563563
ptr2 = alloc->realloc(alloc->ctx, ptr, new_size);
564564

565565
if (ptr2 != NULL) {
566-
if (ptr != NULL)
566+
if (ptr != NULL) {
567+
/* resize */
567568
tracemalloc_log_free(ptr);
568569

569-
if (tracemalloc_log_alloc(ptr2, new_size) < 0) {
570-
if (ptr == NULL) {
570+
if (tracemalloc_log_alloc(ptr2, new_size) < 0) {
571+
/* Memory allocation failed. The error cannot be reported to
572+
the caller, because realloc() may already have shrinked the
573+
memory block and so removed bytes.
574+
575+
This case is very unlikely since we just released an hash
576+
entry, so we have enough free bytes to allocate the new
577+
entry. */
578+
}
579+
}
580+
else {
581+
/* new allocation */
582+
if (tracemalloc_log_alloc(ptr2, new_size) < 0) {
571583
/* Memory allocation failed */
572584
alloc->free(alloc->ctx, ptr2);
573585
ptr2 = NULL;
574586
}
575-
else {
576-
/* Memory allocation failed. The error cannot be reported to
577-
the caller, because realloc() may already have shrinked the
578-
memory block and so removed bytes. */
579-
}
580587
}
581588
}
582589
set_reentrant(0);

0 commit comments

Comments
 (0)