12345678910111213141516171819202122232425262728293031 |
- #include "sll/first_fit_on_freelist.h"
- #include "block_header.h"
- /**
- * \details In order to remove a block from a singly linked list, we need to
- * keep track of the previous block as well: The previous block must point to
- * the current block's next block once the current one is removed.
- */
- void * first_fit_on_freelist(heap_t *heap, size_t requested_size) {
- void *current_block, *previous_block, *ptr;
- ptr = NULL;
- for(current_block = heap->free_list_head; current_block != NULL;
- current_block = get_next(current_block)) {
- if(get_size(current_block) >= requested_size) {
- if(current_block == heap->free_list_head) {
- heap->free_list_head = get_next(current_block);
- } else {
- set_next(previous_block, get_next(current_block));
- }
- ptr = current_block;
- break;
- }
- previous_block = current_block;
- }
- return ptr;
- }
|