24
24
#endif
25
25
26
26
27
+ #define ADDRESS_ORDER
28
+ #ifdef LIFO
29
+ #define insert_free_block insert_free_block_lifo
30
+ #elif defined(FIFO )
31
+ #define insert_free_block insert_free_block_fifo
32
+ #elif defined(ADDRESS_ORDER )
33
+ #define insert_free_block insert_free_block_address_order
34
+ #else
35
+ #define insert_free_block insert_free_block_lifo
36
+ #endif
37
+
38
+
27
39
/* do not change the following! */
28
40
#ifdef DRIVER
29
41
/* create aliases for driver tests */
@@ -142,38 +154,44 @@ inline static void *get_class_ptr(size_t size)
142
154
return FREE_LIST_REF (offset );
143
155
}
144
156
157
+
145
158
/*
146
- * append free block to the tail of free list
159
+ * insert free block bp after prev_bp
147
160
*/
148
- #if 0
149
- inline static void append_free_block (void * bp )
161
+ inline static void insert_free_block_after (void * prev_bp , void * bp )
162
+ {
163
+ void * succ_bp = SUCC_BLKP (prev_bp );
164
+ PUT (SUCC (bp ), GET (SUCC (prev_bp )));
165
+ PUT (PRED (bp ), GET_OFFSET (prev_bp ));
166
+ PUT (SUCC (prev_bp ), GET_OFFSET (bp ));
167
+ PUT (PRED (succ_bp ), GET_OFFSET (bp ));
168
+ }
169
+
170
+ /*
171
+ * first in first out
172
+ */
173
+ inline static void insert_free_block_fifo (void * bp )
150
174
{
151
175
// usable block size should exlude the header/footer size
152
176
size_t size = GET_SIZE (HDRP (bp ));
153
177
void * class_ptr = get_class_ptr (size );
154
178
void * tail_bp = PRED_BLKP (class_ptr );
155
179
156
- // insert_free_block(tail_bp, bp);
180
+ insert_free_block_after (tail_bp , bp );
181
+ /*
157
182
PUT(SUCC(bp), GET(SUCC(tail_bp)));
158
183
PUT(PRED(bp), GET_OFFSET(tail_bp));
159
184
PUT(SUCC(tail_bp), GET_OFFSET(bp));
160
185
PUT(PRED(class_ptr), GET_OFFSET(bp));
186
+ */
161
187
}
162
- #endif
188
+
189
+
163
190
164
191
165
192
/*
166
- * insert free block bp after prev_bp
193
+ * insert free block in address order
167
194
*/
168
- inline static void insert_free_block (void * prev_bp , void * bp )
169
- {
170
- void * succ_bp = SUCC_BLKP (prev_bp );
171
- PUT (SUCC (bp ), GET (SUCC (prev_bp )));
172
- PUT (PRED (bp ), GET_OFFSET (prev_bp ));
173
- PUT (SUCC (prev_bp ), GET_OFFSET (bp ));
174
- PUT (PRED (succ_bp ), GET_OFFSET (bp ));
175
- }
176
-
177
195
inline static void insert_free_block_address_order (void * bp )
178
196
{
179
197
size_t size = GET_SIZE (HDRP (bp ));
@@ -184,7 +202,17 @@ inline static void insert_free_block_address_order(void *bp)
184
202
break ;
185
203
}
186
204
}
187
- insert_free_block (PRED_BLKP (cur_bp ), bp );
205
+ insert_free_block_after (PRED_BLKP (cur_bp ), bp );
206
+ }
207
+
208
+ /*
209
+ * last in first out
210
+ */
211
+ inline static void insert_free_block_lifo (void * bp )
212
+ {
213
+ size_t size = GET_SIZE (HDRP (bp ));
214
+ void * class_ptr = get_class_ptr (size );
215
+ insert_free_block_after (class_ptr , bp );
188
216
}
189
217
190
218
/*
@@ -296,7 +324,7 @@ static void *coalesce(void *bp)
296
324
PUT (FTRP (NEXT_BLKP (bp )), PACK (size , 0 ));
297
325
bp = PREV_BLKP (bp );
298
326
}
299
- insert_free_block_address_order (bp );
327
+ insert_free_block (bp );
300
328
return bp ;
301
329
}
302
330
@@ -351,7 +379,7 @@ static void place(void *bp, size_t size)
351
379
PACK (size , 1 ), // allocated block
352
380
PACK (total_size - size , 0 ) // free block
353
381
);
354
- insert_free_block_address_order (free_bp );
382
+ insert_free_block (free_bp );
355
383
} else {
356
384
PUT (HDRP (bp ), PACK (total_size , 1 ));
357
385
PUT (FTRP (bp ), PACK (total_size , 1 ));
@@ -466,7 +494,7 @@ void *realloc1(void *oldptr, size_t size)
466
494
split_block (next_block ,
467
495
PACK (first_block_size , 1 ),
468
496
PACK (second_block_size , 0 ));
469
- insert_free_block_address_order (free_block );
497
+ insert_free_block (free_block );
470
498
// change the size of the new block
471
499
PUT (HDRP (oldptr ), PACK (size , 1 ));
472
500
PUT (FTRP (oldptr ), PACK (size , 1 ));
0 commit comments