dmm_init.c 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #define _BSD_SOURCE
  2. #include <unistd.h>
  3. #ifdef HAVE_LOCKS
  4. #include <pthread.h>
  5. #endif /* HAVE_LOCKS */
  6. #include "dmm_init.h"
  7. allocator_t * dmm_init(void) {
  8. int i;
  9. allocator_t *main_allocator;
  10. heap_t *current_heap;
  11. maptable_node_t *maptablenode;
  12. main_allocator = (allocator_t *) sbrk((int) sizeof(allocator_t));
  13. for(i = 0; i < NUM_HEAPS; i++) {
  14. main_allocator->heaps[i].maptable_head = NULL;
  15. main_allocator->heaps[i].free_list_head = NULL;
  16. main_allocator->heaps[i].used_blocks_head = NULL;
  17. main_allocator->heaps[i].rov_ptr = NULL;
  18. main_allocator->heaps[i].num_objects = 0;
  19. main_allocator->heaps[i].dmm_stats.mem_allocated = 0;
  20. main_allocator->heaps[i].dmm_stats.mem_requested = 0;
  21. main_allocator->heaps[i].dmm_stats.live_objects = 0;
  22. main_allocator->heaps[i].dmm_stats.num_malloc = 0;
  23. main_allocator->heaps[i].dmm_stats.num_free = 0;
  24. // Knobs initialization
  25. main_allocator->heaps[i].dmm_knobs.max_coalesce_size = -1;
  26. // FIXME Create a constant for the initial value of the next
  27. // variables
  28. main_allocator->heaps[i].dmm_knobs.frag_threshold = 1.0;
  29. main_allocator->heaps[i].dmm_knobs.mem_threshold = 17000;
  30. #ifdef HAVE_LOCKS
  31. pthread_mutex_init(&main_allocator->heaps[i].mutex, NULL);
  32. #endif /* HAVE_LOCKS */
  33. }
  34. // Custom number of fixed lists and their initialization
  35. // 2 first ones with 32, 64, 128 and 256 (4 fixed lists per heap)
  36. // 2 last ones with 64 and 256 (2 fixed lists per heap)
  37. // 2 * 4 + 2 * 2 = 12 maptable nodes
  38. current_heap = &main_allocator->heaps[0];
  39. maptablenode = (maptable_node_t *) sbrk((int)(12*(sizeof(maptable_node_t))));
  40. maptablenode->size = 32;
  41. maptablenode->fixed_list_head = NULL;
  42. maptablenode->next = maptablenode+1;
  43. current_heap->maptable_head = maptablenode;
  44. (maptablenode+1)->size = 64;
  45. (maptablenode+1)->fixed_list_head = NULL;
  46. (maptablenode+1)->next = maptablenode+2;
  47. (maptablenode+2)->size = 128;
  48. (maptablenode+2)->fixed_list_head = NULL;
  49. (maptablenode+2)->next = maptablenode+3;
  50. (maptablenode+3)->size = 256;
  51. (maptablenode+3)->fixed_list_head = NULL;
  52. (maptablenode+3)->next = NULL;
  53. current_heap = &main_allocator->heaps[1];
  54. maptablenode += 4;
  55. maptablenode->size = 32;
  56. maptablenode->fixed_list_head = NULL;
  57. maptablenode->next = maptablenode+1;
  58. current_heap->maptable_head = maptablenode;
  59. (maptablenode+1)->size = 64;
  60. (maptablenode+1)->fixed_list_head = NULL;
  61. (maptablenode+1)->next = maptablenode+2;
  62. (maptablenode+2)->size = 128;
  63. (maptablenode+2)->fixed_list_head = NULL;
  64. (maptablenode+2)->next = maptablenode+3;
  65. (maptablenode+3)->size = 256;
  66. (maptablenode+3)->fixed_list_head = NULL;
  67. (maptablenode+3)->next = NULL;
  68. current_heap = &main_allocator->heaps[2];
  69. maptablenode += 4;
  70. maptablenode->size = 64;
  71. maptablenode->fixed_list_head = NULL;
  72. maptablenode->next = maptablenode+1;
  73. current_heap->maptable_head = maptablenode;
  74. (maptablenode+1)->size = 256;
  75. (maptablenode+1)->fixed_list_head = NULL;
  76. (maptablenode+1)->next = NULL;
  77. current_heap = &main_allocator->heaps[3];
  78. maptablenode += 2;
  79. maptablenode->size = 64;
  80. maptablenode->fixed_list_head = NULL;
  81. maptablenode->next = maptablenode+1;
  82. current_heap->maptable_head = maptablenode;
  83. (maptablenode+1)->size = 256;
  84. (maptablenode+1)->fixed_list_head = NULL;
  85. (maptablenode+1)->next = NULL;
  86. return main_allocator;
  87. }