Skip to content

Commit 50d24e4

Browse files
author
Wei Li
committed
remove realloc1
1 parent c5f6d89 commit 50d24e4

File tree

1 file changed

+17
-106
lines changed
  • malloclab-handout

1 file changed

+17
-106
lines changed

malloclab-handout/mm.c

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

2626

27-
#define FIFO
27+
#define SIZE_ORDER
2828
#ifdef LIFO
2929
#define insert_free_block insert_free_block_lifo
3030
#elif defined(FIFO)
@@ -457,133 +457,44 @@ void free (void *ptr)
457457
coalesce(ptr);
458458
}
459459

460+
460461
/*
461-
* realloc - you may want to look at mm-naive.c
462-
* highly optimized version of realloc. may contain bugs.
462+
* realloc - Change the size of the block by mallocing a new block,
463+
* copying its data, and freeing the old block. I'm too lazy
464+
* to do better.
463465
*/
464-
void *realloc1(void *oldptr, size_t size)
466+
void *realloc(void *oldptr, size_t size)
465467
{
466468
size_t oldsize;
467469
void *newptr;
468470

469471
/* If size == 0 then this is just free, and we return NULL. */
470472
if(size == 0) {
471473
free(oldptr);
472-
return NULL;
474+
return 0;
473475
}
474476

475477
/* If oldptr is NULL, then this is just malloc. */
476478
if(oldptr == NULL) {
477479
return malloc(size);
478480
}
479481

480-
oldsize = GET_SIZE(HDRP(oldptr));
482+
newptr = malloc(size);
481483

482-
/*
483-
* Basic idea:
484-
* 1. if the size we want is smaller, then we don't need reallocate.
485-
* But we need to split the old block if necessary
486-
* 2. check whether there is a free block next to the old block.
487-
* If the free block is large enough, we directly use it. Split
488-
* the free block if necessary.
489-
* 3. Otherwise, we allocate a new block
490-
*/
491-
492-
if (size <= oldsize) {
493-
if (size + MIN_FREE_BLOCK_SIZE <= oldsize) {
494-
// split block
495-
void *free_block =
496-
split_block(oldptr,
497-
PACK(size, 1),
498-
PACK(oldsize-size, 0));
499-
free_block = coalesce(free_block);
500-
} else {
501-
return oldptr;
502-
}
503-
} else {
504-
// check whether the block next to current block is free and the size
505-
// is large enough
506-
void *next_block = NEXT_BLKP(oldptr);
507-
size_t next_block_size = GET_SIZE(HDRP(next_block));
508-
if (IS_FREE(next_block) && next_block_size + oldsize >= size) {
509-
remove_free_block(next_block);
510-
if (next_block_size + oldsize >= size + MIN_FREE_BLOCK_SIZE) {
511-
// split the next block
512-
size_t second_block_size = next_block_size + oldsize - size;
513-
size_t first_block_size = next_block_size - second_block_size;
514-
void *free_block =
515-
split_block(next_block,
516-
PACK(first_block_size, 1),
517-
PACK(second_block_size, 0));
518-
insert_free_block(free_block);
519-
// change the size of the new block
520-
PUT(HDRP(oldptr), PACK(size, 1));
521-
PUT(FTRP(oldptr), PACK(size, 1));
522-
} else {
523-
size = oldsize + next_block_size;
524-
// directly use the next free block
525-
PUT(HDRP(oldptr), PACK(size, 1));
526-
PUT(FTRP(oldptr), PACK(size, 1));
527-
}
528-
return oldptr;
529-
} else {
530-
// allocate a new block
531-
newptr = malloc(size);
532-
/* If realloc() fails the original block is left untouched */
533-
if (!newptr) {
534-
return 0;
535-
}
536-
/* Copy the old data. */
537-
oldsize = GET_SIZE(HDRP(oldptr));
538-
if(size < oldsize) oldsize = size;
539-
memcpy(newptr, oldptr, oldsize);
540-
541-
/* Free the old block. */
542-
free(oldptr);
543-
544-
return newptr;
545-
}
484+
/* If realloc() fails the original block is left untouched */
485+
if(!newptr) {
486+
return 0;
546487
}
547-
return NULL; // make compiler happy
548-
}
549488

550-
/*
551-
* realloc - Change the size of the block by mallocing a new block,
552-
* copying its data, and freeing the old block. I'm too lazy
553-
* to do better.
554-
*/
555-
void *realloc(void *oldptr, size_t size)
556-
{
557-
size_t oldsize;
558-
void *newptr;
489+
/* Copy the old data. */
490+
oldsize = GET_SIZE(HDRP(oldptr));
491+
if(size < oldsize) oldsize = size;
492+
memcpy(newptr, oldptr, oldsize);
559493

560-
/* If size == 0 then this is just free, and we return NULL. */
561-
if(size == 0) {
494+
/* Free the old block. */
562495
free(oldptr);
563-
return 0;
564-
}
565496

566-
/* If oldptr is NULL, then this is just malloc. */
567-
if(oldptr == NULL) {
568-
return malloc(size);
569-
}
570-
571-
newptr = malloc(size);
572-
573-
/* If realloc() fails the original block is left untouched */
574-
if(!newptr) {
575-
return 0;
576-
}
577-
578-
/* Copy the old data. */
579-
oldsize = GET_SIZE(HDRP(oldptr));
580-
if(size < oldsize) oldsize = size;
581-
memcpy(newptr, oldptr, oldsize);
582-
583-
/* Free the old block. */
584-
free(oldptr);
585-
586-
return newptr;
497+
return newptr;
587498
}
588499

589500
/*

0 commit comments

Comments
 (0)