dmm_init.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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(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. current_heap = &main_allocator->heaps[0];
  24. maptablenode = (MAPTABLE_NODE *) sbrk(12*(sizeof(MAPTABLE_NODE)));
  25. maptablenode->size = 32;
  26. maptablenode->fixed_list_head = NULL;
  27. maptablenode->next = maptablenode+1;
  28. current_heap->maptable_head = maptablenode;
  29. (maptablenode+1)->size = 64;
  30. (maptablenode+1)->fixed_list_head = NULL;
  31. (maptablenode+1)->next = maptablenode+2;
  32. (maptablenode+2)->size = 128;
  33. (maptablenode+2)->fixed_list_head = NULL;
  34. (maptablenode+2)->next = maptablenode+3;
  35. (maptablenode+3)->size = 256;
  36. (maptablenode+3)->fixed_list_head = NULL;
  37. (maptablenode+3)->next = NULL;
  38. current_heap = &main_allocator->heaps[1];
  39. maptablenode += 4;
  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[2];
  54. maptablenode += 4;
  55. maptablenode->size = 64;
  56. maptablenode->fixed_list_head = NULL;
  57. maptablenode->next = maptablenode+1;
  58. current_heap->maptable_head = maptablenode;
  59. (maptablenode+1)->size = 256;
  60. (maptablenode+1)->fixed_list_head = NULL;
  61. (maptablenode+1)->next = NULL;
  62. current_heap = &main_allocator->heaps[3];
  63. maptablenode += 2;
  64. maptablenode->size = 64;
  65. maptablenode->fixed_list_head = NULL;
  66. maptablenode->next = maptablenode+1;
  67. current_heap->maptable_head = maptablenode;
  68. (maptablenode+1)->size = 256;
  69. (maptablenode+1)->fixed_list_head = NULL;
  70. (maptablenode+1)->next = NULL;
  71. return main_allocator;
  72. }