dmm_init.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include <unistd.h>
  2. #include <pthread.h>
  3. #include "dmm_init.h"
  4. allocator_t *dmm_init(void) {
  5. int i;
  6. allocator_t *main_allocator;
  7. heap_t *current_heap;
  8. MAPTABLE_NODE *maptablenode;
  9. main_allocator = (allocator_t *) sbrk(sizeof(allocator_t));
  10. for(i = 0; i < NUM_HEAPS; i++) {
  11. main_allocator->heaps[i].maptable_head = NULL;
  12. main_allocator->heaps[i].free_list_head = NULL;
  13. main_allocator->heaps[i].rov_ptr = NULL;
  14. main_allocator->heaps[i].num_objects = 0;
  15. main_allocator->heaps[i].dmm_stats.num_malloc = 0;
  16. main_allocator->heaps[i].dmm_stats.num_free = 0;
  17. pthread_mutex_init(&main_allocator->heaps[i].mutex, NULL);
  18. }
  19. // Custom number of fixed lists and their initialization
  20. // 2 first ones with 32, 64, 128 and 256 (4 fixed lists per heap)
  21. // 2 last ones with 64 and 256 (2 fixed lists per heap)
  22. current_heap = &main_allocator->heaps[0];
  23. maptablenode = (MAPTABLE_NODE *) sbrk(12*(sizeof(MAPTABLE_NODE)));
  24. maptablenode->size = 32;
  25. maptablenode->fixed_list_head = NULL;
  26. maptablenode->next = maptablenode+1;
  27. current_heap->maptable_head = maptablenode;
  28. (maptablenode+1)->size = 64;
  29. (maptablenode+1)->fixed_list_head = NULL;
  30. (maptablenode+1)->next = maptablenode+2;
  31. (maptablenode+2)->size = 128;
  32. (maptablenode+2)->fixed_list_head = NULL;
  33. (maptablenode+2)->next = maptablenode+3;
  34. (maptablenode+3)->size = 256;
  35. (maptablenode+3)->fixed_list_head = NULL;
  36. (maptablenode+3)->next = NULL;
  37. current_heap = &main_allocator->heaps[1];
  38. maptablenode += 4;
  39. maptablenode->size = 32;
  40. maptablenode->fixed_list_head = NULL;
  41. maptablenode->next = maptablenode+1;
  42. current_heap->maptable_head = maptablenode;
  43. (maptablenode+1)->size = 64;
  44. (maptablenode+1)->fixed_list_head = NULL;
  45. (maptablenode+1)->next = maptablenode+2;
  46. (maptablenode+2)->size = 128;
  47. (maptablenode+2)->fixed_list_head = NULL;
  48. (maptablenode+2)->next = maptablenode+3;
  49. (maptablenode+3)->size = 256;
  50. (maptablenode+3)->fixed_list_head = NULL;
  51. (maptablenode+3)->next = NULL;
  52. current_heap = &main_allocator->heaps[2];
  53. maptablenode += 4;
  54. maptablenode->size = 64;
  55. maptablenode->fixed_list_head = NULL;
  56. maptablenode->next = maptablenode+1;
  57. current_heap->maptable_head = maptablenode;
  58. (maptablenode+1)->size = 256;
  59. (maptablenode+1)->fixed_list_head = NULL;
  60. (maptablenode+1)->next = NULL;
  61. current_heap = &main_allocator->heaps[3];
  62. maptablenode += 2;
  63. maptablenode->size = 64;
  64. maptablenode->fixed_list_head = NULL;
  65. maptablenode->next = maptablenode+1;
  66. current_heap->maptable_head = maptablenode;
  67. (maptablenode+1)->size = 256;
  68. (maptablenode+1)->fixed_list_head = NULL;
  69. (maptablenode+1)->next = NULL;
  70. return main_allocator;
  71. }