custom_free.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include "dmmlib.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_ahfree(allocator_t *allocator, heap_t* heap, void *ptr) {
  8. size_t size;
  9. int heap_id, fixed_list_id, i;
  10. maptable_node_t *current_maptable_node;
  11. if(allocator == NULL) {
  12. allocator = &systemallocator;
  13. }
  14. if(heap == NULL) {
  15. heap_id = map_thread_heap();
  16. heap = &allocator->heaps[heap_id];
  17. }
  18. size = get_size(ptr);
  19. #ifdef HAVE_LOCKS
  20. posix_lock(heap);
  21. #endif /* HAVE_LOCKS */
  22. remove_block(ptr, heap->used_blocks_head);
  23. fixed_list_id = map_size_to_list(heap, size);
  24. if(fixed_list_id != -1) {
  25. current_maptable_node = heap->maptable_head;
  26. if(fixed_list_id == 0) {
  27. set_next(ptr, current_maptable_node->fixed_list_head);
  28. current_maptable_node->fixed_list_head = ptr;
  29. } else {
  30. for(i = 1; i < fixed_list_id; i++) {
  31. current_maptable_node = current_maptable_node->next;
  32. }
  33. set_next(ptr, current_maptable_node->fixed_list_head);
  34. current_maptable_node->fixed_list_head = ptr;
  35. }
  36. } else { // put it in the free list
  37. set_next(ptr, heap->free_list_head);
  38. heap->free_list_head = ptr;
  39. }
  40. // Begin of Stats
  41. heap->dmm_stats.live_objects -= 1;
  42. heap->dmm_stats.num_free += 1;
  43. // End of Stats
  44. #ifdef HAVE_LOCKS
  45. posix_unlock(heap);
  46. #endif /* HAVE_LOCKS */
  47. }
  48. void custom_free(void *ptr) {
  49. custom_ahfree(NULL, NULL, ptr);
  50. }