custom_free.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include "custom_free.h"
  2. #include "other.h"
  3. #ifdef HAVE_LOCKS
  4. #include "posix_lock.h"
  5. #endif /* HAVE_LOCKS */
  6. #include "block_header.h"
  7. void custom_free(heap_t* heap, void *ptr) {
  8. size_t size;
  9. int fixed_list_id, i;
  10. maptable_node_t *current_maptable_node;
  11. size = get_size(ptr);
  12. #ifdef HAVE_LOCKS
  13. posix_lock(heap);
  14. #endif /* HAVE_LOCKS */
  15. remove_block(ptr, heap->used_blocks_head);
  16. fixed_list_id = map_size_to_list(heap, size);
  17. if(fixed_list_id != -1) {
  18. current_maptable_node = heap->maptable_head;
  19. if(fixed_list_id == 0) {
  20. set_next(ptr, current_maptable_node->fixed_list_head);
  21. current_maptable_node->fixed_list_head = ptr;
  22. } else {
  23. for(i = 1; i < fixed_list_id; i++) {
  24. current_maptable_node = current_maptable_node->next;
  25. }
  26. set_next(ptr, current_maptable_node->fixed_list_head);
  27. current_maptable_node->fixed_list_head = ptr;
  28. }
  29. } else { // put it in the free list
  30. set_next(ptr, heap->free_list_head);
  31. heap->free_list_head = ptr;
  32. }
  33. // Begin of Stats
  34. heap->dmm_stats.live_objects -= 1;
  35. heap->dmm_stats.num_free += 1;
  36. // End of Stats
  37. #ifdef HAVE_LOCKS
  38. posix_unlock(heap);
  39. #endif /* HAVE_LOCKS */
  40. }