custom_free.c 1.3 KB

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