custom_free.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. fixed_list_id = map_size_to_list(heap, size);
  13. #ifdef HAVE_LOCKS
  14. posix_lock(heap);
  15. #endif /* HAVE_LOCKS */
  16. if(fixed_list_id != -1) {
  17. current_maptable_node = heap->maptable_head;
  18. if(fixed_list_id == 0) {
  19. set_next(ptr, current_maptable_node->fixed_list_head);
  20. current_maptable_node->fixed_list_head = ptr;
  21. } else {
  22. for(i = 1; i < fixed_list_id; i++) {
  23. current_maptable_node = current_maptable_node->next;
  24. }
  25. set_next(ptr, current_maptable_node->fixed_list_head);
  26. current_maptable_node->fixed_list_head = ptr;
  27. }
  28. } else { // put it in the free list
  29. set_next(ptr, heap->free_list_head);
  30. heap->free_list_head = ptr;
  31. }
  32. // Begin of Stats
  33. heap->dmm_stats.live_objects -= 1;
  34. heap->dmm_stats.num_free += 1;
  35. // End of Stats
  36. #ifdef HAVE_LOCKS
  37. posix_unlock(heap);
  38. #endif /* HAVE_LOCKS */
  39. }