Question: I need help with modifying my best,worst,delete, and compact function #include #include #include #include #include #include #include memory _ interface.cpp using namespace std; const
I need help with modifying my best,worst,delete, and compact function
#include
#include
#include
#include
#include
#include
#include "memoryinterface.cpp
using namespace std;
const char FIRSTFIT F;
const char WORSTFIT W;
const char BESTFIT B;
struct allocentryt
int start;
int length;
char contents;
;
GLOBALS
char nAllocatorType;
list freelist;
list usedlist;
TEMPLATES
MemoryInterface mem;
string promptUser;
void deleteEntrychar ch;
void showhelp;
void compactMemory;
bool storeint char;
END TEMPLATES
Finds the first free section of memory to fit 'length' many characters.
this function should return the location of the beginning of
a sufficiently large hole. Specifically, the first on big enough.
Returns if there isn't enough free memory.
TODO: Finish this function!
int firstfitint requestedLength
NOTE: this file does NOT need to add the record to the usedlist,
because store takes care of that for you!
forlist::iterator it freelist.begin; it freelist.end; it
ifitlength requestedLength
can fit here
int loc itstart;
ifitlength requestedLength
itlength requestedLength;
itstart requestedLength;
else
freelist.eraseit;
return loc;
if you get here then I guess we couldn't find a big enough hole
return ;
TODO: IMPLEMENT THIS!!
DESCRIPTION: find the worst hole for a length of
size length as defined by the worstfit algorithm
and return that location. Return if no location
exists.
int worstfitint length
find the largest blockhole
int largestSize ;
int worstIndex ;
forlist::iterator it freelist.begin; it freelist.end; it
find largest hole
ifitlength length && itlength largestSize
largestSize itlength;
worstIndex itstart;
ifworstIndex
if we found a hole that can fit the requested size
and it is the largest hole, return the start of that hole
forlist::iterator it freelist.begin; it freelist.end; it
ifitstart worstIndex
ifitlength length
itlength length;
itstart length;
else
freelist.eraseit;
return worstIndex;
return ;
TODO: IMPLEMENT THIS!!
DESCRIPTION: find the best hole for a length of
size length as defined by the worstfit algorithm
and return that location. Return if no location
exists.
This function should find the smallest hole that can fit the
requested block of memory, but it must still be larger than or equal to the requested size.
int bestfitint requestedLength
int bestLoc ; This will store the location of the best fitting hole
int bestSize ; This will store the size of the best fitting hole, initially set to zero for comparison
Loop through the free list to find the smallest hole that fits the requested length
for list::iterator it freelist.begin; it freelist.end; it
if itlength requestedLength
If it's the first fitting hole found or a smaller fitting hole than previously found
if bestSize itlength bestSize
bestSize itlength;
bestLoc itstart;
If a best fit was found, adjust the hole or remove it
if bestLoc
for list::iterator it freelist.begin; it freelist.end; it
if itstart bestLoc
if itlength requestedLength
itlength requestedLength;
itstart requestedLength;
else
freelist.eraseit;
break; Exit the loop after modifying the list to avoid invalid iterator usage
return bestLoc;
return ; Return if no suitable hole is found
TODO: IMPLEMENT THIS!!
DESCRIPTION: should compact the holes in the memory ie defrag
void compactMemory
void deleteEntrychar item
find it in usedlist
forlist::iterator it usedlist.begin; it usedlist.end; it
ifitcontents item
create a new hole
allocentryt
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
