Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. void *alloc(size_t chunk_size)
  2. {
  3. if (!isInitalized)
  4. {
  5. pthread_mutex_init(&lock, NULL);
  6. pthread_cond_init(&read_cond, NULL);
  7. pthread_cond_init(&write_cond, NULL);
  8. isInitalized = true;
  9. }
  10. void *old_address = sbrk(0); //Gets the current address of the edge
  11. void *return_address = nullptr; //Initializes an address to return
  12. int sizeDif = 0; //Variable that holds the size remainder in the chunk
  13. Chunk selectedChunk = {0, 0}; //Creates a placeholder chunk that is assigned to when a valid chunk is found
  14.  
  15. //iterates though the list of freed chunks to see if it finds a valid chunk if the list has some elements
  16. while (selectedChunk.chunk_size == 0 && freedChunks.size() > 0)
  17. {
  18. read_lock();
  19. bool foundFittingChunk = false;
  20. int dif = -1; //the difference between the requested size and the size of the chunk
  21. std::list<Chunk>::iterator it;
  22.  
  23. for (it = freedChunks.begin(); it != freedChunks.end(); it++)
  24. {
  25. //If first fit, take the first available chunk
  26. if (allocType == FIRST_FIT)
  27. {
  28. if (it->chunk_size >= (size_t)chunk_size)
  29. {
  30. foundFittingChunk = true;
  31. sizeDif = it->chunk_size - chunk_size;
  32. return_address = (void *)it->memory_address;
  33. selectedChunk = *it;
  34. break;
  35. }
  36. }
  37. //If worst fit. find the chunk with the most size difference
  38. else if (allocType == WORST_FIT)
  39. {
  40. int chunkDif = it->chunk_size - chunk_size;
  41. if (chunkDif >= 0 && (chunkDif >= dif || dif == -1))
  42. {
  43. foundFittingChunk = true;
  44. dif = chunkDif;
  45. return_address = (void *)it->memory_address;
  46. sizeDif = dif;
  47. selectedChunk = *it;
  48. }
  49. }
  50. //If best fit, find the chunk with the least chunk difference
  51. else if (allocType == BEST_FIT)
  52. {
  53. int chunkDif = it->chunk_size - chunk_size;
  54.  
  55. if (chunkDif >= 0 && (chunkDif <= dif || dif == -1))
  56. {
  57. foundFittingChunk = true;
  58. dif = chunkDif;
  59. return_address = (void *)it->memory_address;
  60. sizeDif = dif;
  61. selectedChunk = *it;
  62. }
  63. }
  64. }
  65.  
  66. read_unlock();
  67. //If found a chunk
  68. if (selectedChunk.chunk_size != 0)
  69. {
  70. if (pthread_mutex_trylock(&selectedChunk.instance_lock) == 0)
  71. {
  72. //Move the chunk from the freed list to the allocated list and store the memory address
  73. write_lock();
  74. allocate_chunk(selectedChunk);
  75. write_unlock();
  76. return_address = (void *)selectedChunk.memory_address;
  77. break;
  78. }
  79. else
  80. {
  81. selectedChunk = {0, 0};
  82. }
  83. }
  84. else
  85. {
  86. if (!foundFittingChunk)
  87. {
  88. break;
  89. }
  90. }
  91. }
  92.  
  93. //If the remaining size of the chunk is usable. split it into its own chunk.
  94. if (sizeDif > 0)
  95. {
  96. write_lock();
  97. create_new_chunk(sizeDif, (intptr_t)((intptr_t)selectedChunk.memory_address + (intptr_t)sizeDif), true);
  98. write_unlock();
  99. }
  100.  
  101. //If it didnt find a usable chunk, create a new one
  102. if (return_address == nullptr)
  103. {
  104. read_unlock();
  105. //increase the program edge by the requested amount
  106. sbrk(chunk_size + 1);
  107. write_lock();
  108. create_new_chunk((size_t)chunk_size, (intptr_t)old_address, false); //create and add a new chunk
  109. write_unlock();
  110. return_address = (void *)((intptr_t)old_address);
  111. }
  112.  
  113. //Return the address
  114. return return_address;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement