Skip to content

Commit fca7428

Browse files
committed
only use memcpy realloc to shrink if an exact-sized free chunk exists
otherwise, shrink in-place. as explained in the description of commit 3e16313, the split here is valid without holding split_merge_lock because all chunks involved are in the in-use state.
1 parent cb5babd commit fca7428

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/malloc/oldmalloc/malloc.c

+12
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,18 @@ void *realloc(void *p, size_t n)
385385
/* Crash on corrupted footer (likely from buffer overflow) */
386386
if (next->psize != self->csize) a_crash();
387387

388+
if (n < n0) {
389+
int i = bin_index_up(n);
390+
int j = bin_index(n0);
391+
if (i<j && (mal.binmap & (1ULL << i)))
392+
goto copy_realloc;
393+
struct chunk *split = (void *)((char *)self + n);
394+
self->csize = split->psize = n | C_INUSE;
395+
split->csize = next->psize = n0-n | C_INUSE;
396+
__bin_chunk(split);
397+
return CHUNK_TO_MEM(self);
398+
}
399+
388400
lock(mal.split_merge_lock);
389401

390402
size_t nsize = next->csize & C_INUSE ? 0 : CHUNK_SIZE(next);

0 commit comments

Comments
 (0)