test.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include <stdio.h>
  2. #include "heap.h"
  3. #include "other.h"
  4. #include "posix_lock.h"
  5. #include "LeaHeader.h"
  6. #include "dmm_init.h"
  7. /*
  8. void *first_fit_search(size_t size, NODE *starting_node);
  9. void *first_fit_search(size_t size, NODE *starting_node) {
  10. NODE *node;
  11. int found;
  12. void *ptr;
  13. node = starting_node;
  14. found = 0;
  15. if(node != NULL &&
  16. while(node && !found) {
  17. if(getSize(node->data) >= size) {
  18. ptr = node
  19. }
  20. }
  21. }
  22. */
  23. void *custom_malloc(heap_t* heap, size_t size);
  24. void *custom_malloc(heap_t* heap, size_t size) {
  25. leaHdr *ptr;
  26. int fixed_list_id, i, found;
  27. MAPTABLE_NODE *current_maptable_node;
  28. leaHdr *current_node;
  29. ptr = NULL;
  30. posix_lock(heap);
  31. fixed_list_id = map_size_to_list(heap, size);
  32. // If a fixed list is found, do a first fit
  33. if(fixed_list_id != -1) {
  34. current_maptable_node = heap->maptable_head;
  35. // traverse through the maptable node list
  36. if(fixed_list_id != 0) {
  37. for(i = 1; i < fixed_list_id; i++) {
  38. current_maptable_node = current_maptable_node->next;
  39. }
  40. }
  41. ptr = current_maptable_node->fixed_list_head;
  42. current_maptable_node->fixed_list_head = ptr->next;
  43. set_requested_size(ptr, size);
  44. markInUse(ptr);
  45. }
  46. if(ptr == NULL) {
  47. found = 0;
  48. // first fit from free list
  49. current_node = heap->free_list_head;
  50. if(current_node != NULL && getSize(current_node) >= size) {
  51. ptr = current_node;
  52. heap->free_list_head = ptr->next;
  53. set_requested_size(ptr, size);
  54. markInUse(ptr);
  55. }
  56. while(current_node && !found) {
  57. if(getSize(current_node->next) >= size) {
  58. ptr = current_node->next;
  59. current_node->next = ptr->next;
  60. set_requested_size(ptr, size);
  61. markInUse(ptr);
  62. found = 1;
  63. }
  64. current_node = current_node->next;
  65. }
  66. if(!found) {
  67. return sys_alloc(size);
  68. }
  69. }
  70. posix_unlock(heap);
  71. return ptr;
  72. }
  73. void custom_free(heap_t* heap, void *ptr);
  74. void custom_free(heap_t* heap, void *ptr) {
  75. size_t size;
  76. int fixed_list_id, i;
  77. MAPTABLE_NODE *current_maptable_node;
  78. leaHdr *test;
  79. size = getSize((char *) ptr);
  80. fixed_list_id = map_size_to_list(heap, size);
  81. posix_lock(heap);
  82. if(fixed_list_id != -1) {
  83. current_maptable_node = heap->maptable_head;
  84. if(fixed_list_id == 0) {
  85. test = getHeader(ptr);
  86. test->next = current_maptable_node->fixed_list_head;
  87. //getHeader((char *) ptr)->next = current_maptable_node->fixed_list_head;
  88. current_maptable_node->fixed_list_head = ptr;
  89. } else {
  90. for(i = 1; i < fixed_list_id; i++) {
  91. current_maptable_node = current_maptable_node->next;
  92. }
  93. getHeader(ptr)->next = current_maptable_node->fixed_list_head;
  94. current_maptable_node->fixed_list_head = ptr;
  95. }
  96. } else { // put it in the free list
  97. getHeader(ptr)->next = heap->free_list_head;
  98. heap->free_list_head = getHeader(ptr);
  99. }
  100. markFree((char *) ptr);
  101. posix_unlock(heap);
  102. }
  103. int main(void) {
  104. allocator_t *myallocator;
  105. heap_t *myheap;
  106. int heap_id;
  107. void *p;
  108. myallocator = dmm_init();
  109. heap_id = map_thread_heap();
  110. printf("This thread accesses heap %d\n", heap_id);
  111. myheap = &myallocator->heaps[heap_id];
  112. p = custom_malloc(myheap, 32);
  113. custom_free(myheap, p);
  114. }