custom_free.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include <stdio.h>
  2. #include "custom_free.h"
  3. #include "other.h"
  4. #include "posix_lock.h"
  5. #include "block_header.h"
  6. void custom_free(heap_t* heap, void *ptr) {
  7. size_t size;
  8. int fixed_list_id, i;
  9. maptable_node_t *current_maptable_node;
  10. size = get_size(ptr);
  11. fixed_list_id = map_size_to_list(heap, size);
  12. posix_lock(heap);
  13. if(fixed_list_id != -1) {
  14. current_maptable_node = heap->maptable_head;
  15. if(fixed_list_id == 0) {
  16. set_next(ptr, current_maptable_node->fixed_list_head);
  17. current_maptable_node->fixed_list_head = ptr;
  18. } else {
  19. for(i = 1; i < fixed_list_id; i++) {
  20. current_maptable_node = current_maptable_node->next;
  21. }
  22. set_next(ptr, current_maptable_node->fixed_list_head);
  23. current_maptable_node->fixed_list_head = ptr;
  24. }
  25. printf("Block of size %d was free'd to a fixed list.\n", size);
  26. } else { // put it in the free list
  27. set_next(ptr, heap->free_list_head);
  28. heap->free_list_head = ptr;
  29. printf("Block of size %d was free'd to free list.\n", size);
  30. }
  31. posix_unlock(heap);
  32. }