custom_free.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include <dmmlib/dmmlib.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_ahfree(allocator_t *allocator, heap_t* heap, void *ptr) {
  9. size_t size;
  10. int heap_id, fixed_list_id, i;
  11. maptable_node_t *current_maptable_node;
  12. if(allocator == NULL) {
  13. allocator = &systemallocator;
  14. }
  15. if(heap == NULL) {
  16. heap_id = map_thread_heap();
  17. heap = &allocator->heaps[heap_id];
  18. }
  19. size = get_size(ptr);
  20. #ifdef HAVE_LOCKS
  21. posix_lock(heap);
  22. #endif /* HAVE_LOCKS */
  23. remove_block(ptr, heap->used_blocks_head);
  24. // Check if the block could be put in a fixed list
  25. fixed_list_id = map_size_to_list(heap, size);
  26. if(fixed_list_id != -1) {
  27. current_maptable_node = heap->maptable_head;
  28. if(fixed_list_id == 0) {
  29. set_next(ptr, current_maptable_node->fixed_list_head);
  30. current_maptable_node->fixed_list_head = ptr;
  31. } else {
  32. for(i = 1; i < fixed_list_id; i++) {
  33. current_maptable_node = current_maptable_node->next;
  34. }
  35. set_next(ptr, current_maptable_node->fixed_list_head);
  36. current_maptable_node->fixed_list_head = ptr;
  37. }
  38. } else { // put it in the free list
  39. set_next(ptr, heap->free_list_head);
  40. heap->free_list_head = ptr;
  41. }
  42. // Begin of Stats
  43. heap->dmm_stats.live_objects -= 1;
  44. heap->dmm_stats.num_free += 1;
  45. // End of Stats
  46. // Refresh the state of the heap allocator if a certain number of
  47. // free's has been served already
  48. // TODO Define 100 as a constant
  49. if(heap->dmm_stats.num_free % 100) {
  50. free_state_refresh(heap);
  51. }
  52. #ifdef HAVE_LOCKS
  53. posix_unlock(heap);
  54. #endif /* HAVE_LOCKS */
  55. }
  56. void custom_free(void *ptr) {
  57. custom_ahfree(NULL, NULL, ptr);
  58. }