The find_fit function uses 3 auxillary helpers:
- GET_SIZE(q) returns the size of a block begining at address q. This address should be the address of the header, NOT the allocated/free part.
- GET_ALLOC(q) returns 1 if the block at address q is allocated and 0 if the block is free.
- NEXT_BLKP(p) returns the beginning address of the allocated/free part of the next block after the block pointed to by p. But (IMPORTANT) the address p should also point to the allocated/free part of the current block, not to its header.
1 static void *find_fit(size_t size)
2 {
3 void *bp;
4
5 /* Find the first free block big enough */
6 for(bp = heap_listp; GET_SIZE(HDRP(bp)) > 0; bp = NEXT_BLKP(bp)) {
7 if (!GET_ALLOC(HDRP(bp)) && (asize <= GET_SIZE(HDRP(bp))) ) {
8 return bp;
9 }
10 }
11 return NULL; // No free block big enough
12 }
13