Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** @brief Request a block of memory of the given size
- *
- * Uses the 'Worst Fit' algorithm to choose a suitable block of available memory
- *
- * Returns the memory address of the start of the requested memory
- * If no block is big enough, it returns nullptr.
- */
- char * allocateMemoryWorstFit(size_t requested) {
- // TODO: your code for allocateMemory memory goes here
- //Round up to nearest multiple of 4
- if(requested%4 != 0){
- requested += (4 - (requested % 4));
- }
- //Use worst fit algorithm to choose mem block
- MemControlBlock * curr = startOfHeap;
- cout << "running" << requested <<endl;
- MemControlBlock * max = startOfHeap;
- for (int i = 0; curr; ++i, curr = curr->next) {
- if (curr->size > max->size && curr-> available && curr->size >= requested){
- max = curr;
- cout << "Found "<< max << endl;
- }
- }
- if(max == NULL) return nullptr;
- return nullptr;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement