@@ -145,6 +145,7 @@ inline static void *get_class_ptr(size_t size)
145
145
/*
146
146
* append free block to the tail of free list
147
147
*/
148
+ #if 0
148
149
inline static void append_free_block (void * bp )
149
150
{
150
151
// usable block size should exlude the header/footer size
@@ -158,6 +159,8 @@ inline static void append_free_block(void *bp)
158
159
PUT (SUCC (tail_bp ), GET_OFFSET (bp ));
159
160
PUT (PRED (class_ptr ), GET_OFFSET (bp ));
160
161
}
162
+ #endif
163
+
161
164
162
165
/*
163
166
* insert free block bp after prev_bp
@@ -171,6 +174,19 @@ inline static void insert_free_block(void *prev_bp, void *bp)
171
174
PUT (PRED (succ_bp ), GET_OFFSET (bp ));
172
175
}
173
176
177
+ inline static void insert_free_block_address_order (void * bp )
178
+ {
179
+ size_t size = GET_SIZE (HDRP (bp ));
180
+ void * class_ptr = get_class_ptr (size );
181
+ void * cur_bp ;
182
+ for_each_free_block (class_ptr , cur_bp ) {
183
+ if (cur_bp > bp ) {
184
+ break ;
185
+ }
186
+ }
187
+ insert_free_block (PRED_BLKP (cur_bp ), bp );
188
+ }
189
+
174
190
/*
175
191
* remove free block from free list
176
192
* assert bp is not a class_ptr
@@ -280,7 +296,7 @@ static void *coalesce(void *bp)
280
296
PUT (FTRP (NEXT_BLKP (bp )), PACK (size , 0 ));
281
297
bp = PREV_BLKP (bp );
282
298
}
283
- append_free_block (bp );
299
+ insert_free_block_address_order (bp );
284
300
return bp ;
285
301
}
286
302
@@ -335,7 +351,7 @@ static void place(void *bp, size_t size)
335
351
PACK (size , 1 ), // allocated block
336
352
PACK (total_size - size , 0 ) // free block
337
353
);
338
- append_free_block (free_bp );
354
+ insert_free_block_address_order (free_bp );
339
355
} else {
340
356
PUT (HDRP (bp ), PACK (total_size , 1 ));
341
357
PUT (FTRP (bp ), PACK (total_size , 1 ));
@@ -390,7 +406,6 @@ void free (void *ptr)
390
406
PUT (HDRP (ptr ), PACK (size , 0 ));
391
407
PUT (FTRP (ptr ), PACK (size , 0 ));
392
408
coalesce (ptr );
393
- // append_free_block(ptr);
394
409
}
395
410
396
411
/*
@@ -433,7 +448,6 @@ void *realloc1(void *oldptr, size_t size)
433
448
PACK (size , 1 ),
434
449
PACK (oldsize - size , 0 ));
435
450
free_block = coalesce (free_block );
436
- // append_free_block(free_block);
437
451
} else {
438
452
return oldptr ;
439
453
}
@@ -452,7 +466,7 @@ void *realloc1(void *oldptr, size_t size)
452
466
split_block (next_block ,
453
467
PACK (first_block_size , 1 ),
454
468
PACK (second_block_size , 0 ));
455
- append_free_block (free_block );
469
+ insert_free_block_address_order (free_block );
456
470
// change the size of the new block
457
471
PUT (HDRP (oldptr ), PACK (size , 1 ));
458
472
PUT (FTRP (oldptr ), PACK (size , 1 ));
@@ -576,11 +590,15 @@ static int aligned(const void *p) {
576
590
}
577
591
578
592
#define CHECK_GREATER_EQUAL (v , cmp_v , lineno , msg ) if (!((v) >= (cmp_v))) {\
579
- err_report(lineno, msg); \
593
+ err_report(lineno, msg); \
580
594
}
581
595
582
596
#define CHECK_LESS_EQUAL (v , cmp_v , lineno , msg ) if (!((v) <= (cmp_v))) {\
583
- err_report(lineno, msg); \
597
+ err_report(lineno, msg); \
598
+ }
599
+
600
+ #define CHECK_LESS (v , cmp_v , lineno , msg ) if (!((v) < cmp_v)) {\
601
+ err_report(lineno, msg); \
584
602
}
585
603
586
604
@@ -708,6 +726,9 @@ void mm_checkheap(int lineno)
708
726
CHECK_GREATER_EQUAL (GET_SIZE (HDRP (bp )), min_class_size , lineno ,
709
727
"free block size in class range(min size)" );
710
728
729
+ // if blocks are inserted in address order, check address order
730
+ CHECK_LESS (PRED_BLKP (bp ), bp , lineno ,
731
+ "address order for each free list" );
711
732
}
712
733
}
713
734
0 commit comments