Advertisement
Guest User

Untitled

a guest
Feb 18th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1.  
  2. /** @brief Request a block of memory of the given size
  3. *
  4. * Uses the 'Worst Fit' algorithm to choose a suitable block of available memory
  5. *
  6. * Returns the memory address of the start of the requested memory
  7. * If no block is big enough, it returns nullptr.
  8. */
  9. char * allocateMemoryWorstFit(size_t requested) {
  10. // TODO: your code for allocateMemory memory goes here
  11.  
  12. //Round up to nearest multiple of 4
  13. if(requested%4 != 0){
  14. requested += (4 - (requested % 4));
  15. }
  16.  
  17. //Use worst fit algorithm to choose mem block
  18. MemControlBlock * curr = startOfHeap;
  19.  
  20. cout << "running" << requested <<endl;
  21. MemControlBlock * max = startOfHeap;
  22. for (int i = 0; curr; ++i, curr = curr->next) {
  23.  
  24. if (curr->size > max->size && curr-> available && curr->size >= requested){
  25. max = curr;
  26. cout << "Found "<< max << endl;
  27. }
  28. }
  29. if(max == NULL) return nullptr;
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36. return nullptr;
  37.  
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement