dmm_init.c 2.6 KB

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