24
24
#endif
25
25
26
26
27
- #define SIZE_ORDER
27
+ #define FIFO
28
28
#ifdef LIFO
29
29
#define insert_free_block insert_free_block_lifo
30
30
#elif defined(FIFO )
@@ -349,6 +349,18 @@ static void *coalesce(void *bp)
349
349
return bp ;
350
350
}
351
351
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
+
352
364
/*
353
365
* malloc
354
366
*/
@@ -359,11 +371,8 @@ void *malloc (size_t size)
359
371
char * bp ;
360
372
size_t extendsize ;
361
373
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 );
367
376
368
377
if ((bp = find_fit (asize )) != NULL ) {
369
378
place (bp , asize );
@@ -467,6 +476,7 @@ void *realloc(void *oldptr, size_t size)
467
476
{
468
477
size_t oldsize ;
469
478
void * newptr ;
479
+ size_t asize ;
470
480
471
481
/* If size == 0 then this is just free, and we return NULL. */
472
482
if (size == 0 ) {
@@ -479,23 +489,69 @@ void *realloc(void *oldptr, size_t size)
479
489
return malloc (size );
480
490
}
481
491
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 );
483
525
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 );
486
541
return 0 ;
487
542
}
488
543
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
+ }
493
548
494
- /* Free the old block. */
495
- free (oldptr );
496
549
497
- return newptr ;
550
+ oldsize = GET_SIZE (HDRP (oldptr ));
551
+
552
+ return 0 ;
498
553
}
554
+ #endif
499
555
500
556
/*
501
557
* calloc - you may want to look at mm-naive.c
0 commit comments