test.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. if(current_maptable_node->fixed_list_head != NULL) {
  42. ptr = current_maptable_node->fixed_list_head;
  43. current_maptable_node->fixed_list_head = ptr->next;
  44. set_requested_size(ptr, size);
  45. markInUse(ptr);
  46. }
  47. }
  48. if(ptr == NULL) {
  49. found = 0;
  50. // first fit from free list
  51. current_node = heap->free_list_head;
  52. if(current_node != NULL && getSize(current_node) >= size) {
  53. ptr = current_node;
  54. heap->free_list_head = ptr->next;
  55. set_requested_size(ptr, size);
  56. markInUse(ptr);
  57. }
  58. while(current_node && !found) {
  59. if(getSize(current_node->next) >= size) {
  60. ptr = current_node->next;
  61. current_node->next = ptr->next;
  62. set_requested_size(ptr, size);
  63. markInUse(ptr);
  64. found = 1;
  65. }
  66. current_node = current_node->next;
  67. }
  68. if(!found) {
  69. posix_unlock(heap);
  70. return sys_alloc(size);
  71. }
  72. }
  73. posix_unlock(heap);
  74. return ptr;
  75. }
  76. void custom_free(heap_t* heap, void *ptr);
  77. void custom_free(heap_t* heap, void *ptr) {
  78. size_t size;
  79. int fixed_list_id, i;
  80. MAPTABLE_NODE *current_maptable_node;
  81. leaHdr *test;
  82. size = getSize((char *) ptr);
  83. fixed_list_id = map_size_to_list(heap, size);
  84. posix_lock(heap);
  85. if(fixed_list_id != -1) {
  86. current_maptable_node = heap->maptable_head;
  87. if(fixed_list_id == 0) {
  88. test = getHeader(ptr);
  89. test->next = current_maptable_node->fixed_list_head;
  90. //getHeader((char *) ptr)->next = current_maptable_node->fixed_list_head;
  91. current_maptable_node->fixed_list_head = ptr;
  92. } else {
  93. for(i = 1; i < fixed_list_id; i++) {
  94. current_maptable_node = current_maptable_node->next;
  95. }
  96. getHeader(ptr)->next = current_maptable_node->fixed_list_head;
  97. current_maptable_node->fixed_list_head = ptr;
  98. }
  99. } else { // put it in the free list
  100. getHeader(ptr)->next = heap->free_list_head;
  101. heap->free_list_head = getHeader(ptr);
  102. }
  103. markFree((char *) ptr);
  104. posix_unlock(heap);
  105. }
  106. int main(void) {
  107. allocator_t *myallocator;
  108. heap_t *myheap;
  109. int heap_id;
  110. void *p;
  111. myallocator = dmm_init();
  112. heap_id = map_thread_heap();
  113. printf("This thread accesses heap %d\n", heap_id);
  114. myheap = &myallocator->heaps[heap_id];
  115. p = custom_malloc(myheap, 32);
  116. custom_free(myheap, p);
  117. }