Skip to content

Commit 137c463

Browse files
author
Wei Li
committed
optimize realloc
1 parent 50d24e4 commit 137c463

File tree

1 file changed

+72
-16
lines changed
  • malloclab-handout

1 file changed

+72
-16
lines changed

malloclab-handout/mm.c

Lines changed: 72 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#endif
2525

2626

27-
#define SIZE_ORDER
27+
#define FIFO
2828
#ifdef LIFO
2929
#define insert_free_block insert_free_block_lifo
3030
#elif defined(FIFO)
@@ -349,6 +349,18 @@ static void *coalesce(void *bp)
349349
return bp;
350350
}
351351

352+
inline size_t get_real_malloc_size(size_t size)
353+
{
354+
size_t asize;
355+
if (size <= DSIZE) {
356+
// asize = MIN_BLOCK_SIZE;
357+
asize = 2*DSIZE;
358+
} else {
359+
asize = DSIZE * ((size + DSIZE + DSIZE - 1) / DSIZE);
360+
}
361+
return asize;
362+
}
363+
352364
/*
353365
* malloc
354366
*/
@@ -359,11 +371,8 @@ void *malloc (size_t size)
359371
char *bp;
360372
size_t extendsize;
361373
if (size <= 0) return NULL;
362-
if (size <= DSIZE) {
363-
asize = MIN_BLOCK_SIZE;
364-
} else {
365-
asize = DSIZE * ((size + DSIZE + DSIZE - 1) / DSIZE);
366-
}
374+
375+
asize = get_real_malloc_size(size);
367376

368377
if ((bp = find_fit(asize)) != NULL) {
369378
place(bp, asize);
@@ -467,6 +476,7 @@ void *realloc(void *oldptr, size_t size)
467476
{
468477
size_t oldsize;
469478
void *newptr;
479+
size_t asize;
470480

471481
/* If size == 0 then this is just free, and we return NULL. */
472482
if(size == 0) {
@@ -479,23 +489,69 @@ void *realloc(void *oldptr, size_t size)
479489
return malloc(size);
480490
}
481491

482-
newptr = malloc(size);
492+
oldsize = GET_SIZE(HDRP(oldptr));
493+
void *next_bp = NEXT_BLKP(oldptr);
494+
if (IS_FREE(next_bp)) {
495+
// append next free block to the old block
496+
remove_free_block(next_bp);
497+
size_t next_bp_size = GET_SIZE(HDRP(next_bp));
498+
PUT(HDRP(oldptr), PACK(oldsize+next_bp_size, 1));
499+
PUT(FTRP(oldptr), PACK(oldsize+next_bp_size, 1));
500+
oldsize += next_bp_size;
501+
}
502+
503+
asize = get_real_malloc_size(size);
504+
505+
if (oldsize >= asize) {
506+
if (oldsize >= asize + MIN_FREE_BLOCK_SIZE) {
507+
void *free_bp = split_block(
508+
oldptr,
509+
PACK(asize, 1),
510+
PACK(oldsize - asize, 0));
511+
insert_free_block(free_bp);
512+
}
513+
newptr = oldptr;
514+
} else {
515+
newptr = malloc(size);
516+
517+
/* If realloc() fails the original block is left untouched */
518+
if(!newptr) {
519+
return 0;
520+
}
521+
522+
/* Copy the old data. */
523+
if(size < oldsize) oldsize = size;
524+
memcpy(newptr, oldptr, oldsize);
483525

484-
/* If realloc() fails the original block is left untouched */
485-
if(!newptr) {
526+
/* Free the old block. */
527+
free(oldptr);
528+
}
529+
return newptr;
530+
}
531+
532+
#if 0
533+
void *realloc1(void *oldptr, size_t size)
534+
{
535+
size_t oldsize;
536+
void *newptr;
537+
538+
/* If size == 0 then this is just free, and we return NULL. */
539+
if(size == 0) {
540+
free(oldptr);
486541
return 0;
487542
}
488543

489-
/* Copy the old data. */
490-
oldsize = GET_SIZE(HDRP(oldptr));
491-
if(size < oldsize) oldsize = size;
492-
memcpy(newptr, oldptr, oldsize);
544+
/* If oldptr is NULL, then this is just malloc. */
545+
if(oldptr == NULL) {
546+
return malloc(size);
547+
}
493548

494-
/* Free the old block. */
495-
free(oldptr);
496549

497-
return newptr;
550+
oldsize = GET_SIZE(HDRP(oldptr));
551+
552+
return 0;
498553
}
554+
#endif
499555

500556
/*
501557
* calloc - you may want to look at mm-naive.c

0 commit comments

Comments
 (0)