realloc(): try to protect a block in the path of a growing object syslinux-3.62-pre13
authorH. Peter Anvin <hpa@zytor.com>
Fri, 22 Feb 2008 02:17:49 +0000 (18:17 -0800)
committerH. Peter Anvin <hpa@zytor.com>
Fri, 22 Feb 2008 02:17:49 +0000 (18:17 -0800)
When we realloc() a block larger, try to protect the free block
following it from being immediately allocated by something else by
placing it at the end of the freelist instead of the beginning.

com32/lib/realloc.c

index 802d973..89b63c8 100644 (file)
@@ -65,10 +65,21 @@ void *realloc(void *ptr, size_t size)
        nah->a.prev = ah;
 
        /* Insert into free list */
-       nah->next_free = __malloc_head.next_free;
-       nah->prev_free = &__malloc_head;
-       __malloc_head.next_free = nah;
-       nah->next_free->prev_free = nah;
+       if (newsize > oldsize) {
+         /* Hack: this free block is in the path of a memory object
+            which has already been grown at least once.  As such, put
+            it at the *end* of the freelist instead of the beginning;
+            trying to save it for future realloc()s of the same block. */
+         nah->prev_free = __malloc_head.prev_free;
+         nah->next_free = &__malloc_head;
+         __malloc_head.prev_free = nah;
+         nah->prev_free->next_free = nah;
+       } else {
+         nah->next_free = __malloc_head.next_free;
+         nah->prev_free = &__malloc_head;
+         __malloc_head.next_free = nah;
+         nah->next_free->prev_free = nah;
+       }
       }
       /* otherwise, use up the whole block */
       return ptr;