|
24 | 24 | #endif
|
25 | 25 |
|
26 | 26 |
|
27 |
| -#define FIFO |
| 27 | +#define SIZE_ORDER |
28 | 28 | #ifdef LIFO
|
29 | 29 | #define insert_free_block insert_free_block_lifo
|
30 | 30 | #elif defined(FIFO)
|
@@ -457,133 +457,44 @@ void free (void *ptr)
|
457 | 457 | coalesce(ptr);
|
458 | 458 | }
|
459 | 459 |
|
| 460 | + |
460 | 461 | /*
|
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. |
463 | 465 | */
|
464 |
| -void *realloc1(void *oldptr, size_t size) |
| 466 | +void *realloc(void *oldptr, size_t size) |
465 | 467 | {
|
466 | 468 | size_t oldsize;
|
467 | 469 | void *newptr;
|
468 | 470 |
|
469 | 471 | /* If size == 0 then this is just free, and we return NULL. */
|
470 | 472 | if(size == 0) {
|
471 | 473 | free(oldptr);
|
472 |
| - return NULL; |
| 474 | + return 0; |
473 | 475 | }
|
474 | 476 |
|
475 | 477 | /* If oldptr is NULL, then this is just malloc. */
|
476 | 478 | if(oldptr == NULL) {
|
477 | 479 | return malloc(size);
|
478 | 480 | }
|
479 | 481 |
|
480 |
| - oldsize = GET_SIZE(HDRP(oldptr)); |
| 482 | + newptr = malloc(size); |
481 | 483 |
|
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; |
546 | 487 | }
|
547 |
| - return NULL; // make compiler happy |
548 |
| -} |
549 | 488 |
|
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); |
559 | 493 |
|
560 |
| - /* If size == 0 then this is just free, and we return NULL. */ |
561 |
| - if(size == 0) { |
| 494 | + /* Free the old block. */ |
562 | 495 | free(oldptr);
|
563 |
| - return 0; |
564 |
| - } |
565 | 496 |
|
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; |
587 | 498 | }
|
588 | 499 |
|
589 | 500 | /*
|
|
0 commit comments