Question: Instructions Complete the following functions: MemoryAllocator: :allocate Initialize allocated _ address to 0 . Check for invalid block _ size: If block _ size is

Instructions
Complete the following functions:
MemoryAllocator: :allocate
Initialize allocated_address to 0.
Check for invalid block_size:
If block_size is 0, throw an inval id_argument exception with a message indicating that
the block size for allocation is invalid.
If block_size is greater than total_memory_size_, throw a runtime_error with a
message indicating that the block size exceeds the total memory size.
Find a suitable free memory block:
Use std: : find_if to find a memory block in memory_blocks_ that is free (process ID
is FREE_MEMORY_STRING) and has a size greater than or equal to block_size.
If a suitable memory block is found:
Set allocated_address to the base address of the found memory block.
Calculate free_base_address as the base address of the found memory block,
plus block_size.
Update free_memory_size_ to be the size of the found memory block minus block_size.
Set the process ID of the found memory block to process_id.
Set the size of the found memory block to block_size.
If free_memory_size_ is greater than 0, add a new memory block to memory_blocks_ with:
free_base_address
free_memory_size_
FREE_MEMORY_STRING as the process ID.
If no suitable memory block is found:
Throw a runtime_error with a message indicating that no suitable memory block was
found.
Return allocated_address.
MemoryAllocator: : deallocate
Find the memory block associated with the process ID:
Use std:: find_if to search for a memory block in memory_blocks_ with a
matching process_id.
Update the process ID:
Set the process ID of the found memory block to FREE_MEMORY_STRING.
Merge free blocks:
Call the merge_free_blocks() function to merge adjacent free memory blocks.
Sort the memory layout by the starting address:
Sort the memory_blocks_ vector by the starting address of the memory blocks.
If the process ID is not found:
Throw an exception indicating that the process_id was not found for deallocation.
Instructions Complete the following functions:

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!