dmm_init.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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_t *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].used_blocks_head = NULL;
  15. main_allocator->heaps[i].rov_ptr = NULL;
  16. main_allocator->heaps[i].num_objects = 0;
  17. main_allocator->heaps[i].dmm_stats.num_malloc = 0;
  18. main_allocator->heaps[i].dmm_stats.num_free = 0;
  19. pthread_mutex_init(&main_allocator->heaps[i].mutex, NULL);
  20. }
  21. // Custom number of fixed lists and their initialization
  22. // 2 first ones with 32, 64, 128 and 256 (4 fixed lists per heap)
  23. // 2 last ones with 64 and 256 (2 fixed lists per heap)
  24. // 2 * 4 + 2 * 2 = 12 maptable nodes
  25. current_heap = &main_allocator->heaps[0];
  26. maptablenode = (maptable_node_t *) sbrk((int)(12*(sizeof(maptable_node_t))));
  27. maptablenode->size = 32;
  28. maptablenode->fixed_list_head = NULL;
  29. maptablenode->next = maptablenode+1;
  30. current_heap->maptable_head = maptablenode;
  31. (maptablenode+1)->size = 64;
  32. (maptablenode+1)->fixed_list_head = NULL;
  33. (maptablenode+1)->next = maptablenode+2;
  34. (maptablenode+2)->size = 128;
  35. (maptablenode+2)->fixed_list_head = NULL;
  36. (maptablenode+2)->next = maptablenode+3;
  37. (maptablenode+3)->size = 256;
  38. (maptablenode+3)->fixed_list_head = NULL;
  39. (maptablenode+3)->next = NULL;
  40. current_heap = &main_allocator->heaps[1];
  41. maptablenode += 4;
  42. maptablenode->size = 32;
  43. maptablenode->fixed_list_head = NULL;
  44. maptablenode->next = maptablenode+1;
  45. current_heap->maptable_head = maptablenode;
  46. (maptablenode+1)->size = 64;
  47. (maptablenode+1)->fixed_list_head = NULL;
  48. (maptablenode+1)->next = maptablenode+2;
  49. (maptablenode+2)->size = 128;
  50. (maptablenode+2)->fixed_list_head = NULL;
  51. (maptablenode+2)->next = maptablenode+3;
  52. (maptablenode+3)->size = 256;
  53. (maptablenode+3)->fixed_list_head = NULL;
  54. (maptablenode+3)->next = NULL;
  55. current_heap = &main_allocator->heaps[2];
  56. maptablenode += 4;
  57. maptablenode->size = 64;
  58. maptablenode->fixed_list_head = NULL;
  59. maptablenode->next = maptablenode+1;
  60. current_heap->maptable_head = maptablenode;
  61. (maptablenode+1)->size = 256;
  62. (maptablenode+1)->fixed_list_head = NULL;
  63. (maptablenode+1)->next = NULL;
  64. current_heap = &main_allocator->heaps[3];
  65. maptablenode += 2;
  66. maptablenode->size = 64;
  67. maptablenode->fixed_list_head = NULL;
  68. maptablenode->next = maptablenode+1;
  69. current_heap->maptable_head = maptablenode;
  70. (maptablenode+1)->size = 256;
  71. (maptablenode+1)->fixed_list_head = NULL;
  72. (maptablenode+1)->next = NULL;
  73. return main_allocator;
  74. }