custom_free.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. fixed_list_id = map_size_to_list(heap, size);
  25. if(fixed_list_id != -1) {
  26. current_maptable_node = heap->maptable_head;
  27. if(fixed_list_id == 0) {
  28. set_next(ptr, current_maptable_node->fixed_list_head);
  29. current_maptable_node->fixed_list_head = ptr;
  30. } else {
  31. for(i = 1; i < fixed_list_id; i++) {
  32. current_maptable_node = current_maptable_node->next;
  33. }
  34. set_next(ptr, current_maptable_node->fixed_list_head);
  35. current_maptable_node->fixed_list_head = ptr;
  36. }
  37. } else { // put it in the free list
  38. set_next(ptr, heap->free_list_head);
  39. heap->free_list_head = ptr;
  40. }
  41. // Begin of Stats
  42. heap->dmm_stats.live_objects -= 1;
  43. heap->dmm_stats.num_free += 1;
  44. // End of Stats
  45. #ifdef HAVE_LOCKS
  46. posix_unlock(heap);
  47. #endif /* HAVE_LOCKS */
  48. }
  49. void custom_free(void *ptr) {
  50. custom_ahfree(NULL, NULL, ptr);
  51. }