I am trying to change this header file without changing the class so that it has 2048 locations, but 0-511 are for operating system and memory blocks less than 30 are from 512 to 1023 using best fit and bigger than 30 start at 1024 using worst fit. I am really kind of confused on how this works so if someone can at least give me some ideas about how to do this it would be great.
Thanks!!
Code:
struct process
{
int id;
char name[21];
int start;
int size;
char state[6];
char starttime[15];
int cputime;
};
struct hole
{
int start;
int size;
};
class memman
{
private:
list<process> plist;
list<hole> freelist;
public:
memman( int memsize = 2048 );
bool allocate( int processid, int locations );
bool free( int processid );
void displayfree();
void process_list();
};
memman::memman(int memsize)
{
hole h;
h.size = memsize - 512 - 1024; // changed, used to read h.size = memsize
h.start = 512; // changed, used to read h.start = 0
freelist.push_front(h);
h.size = memsize - 512 - 512; //added this line
h.start = 1024; //added this line
freelist.push_front(h); //added this line
}
bool memman::allocate( int processid, int locations )
{
int mystart,mysize,newsize, newstart;
hole h;
list<hole>::iterator p;
process ptemp;
ptemp.id = processid;
p = freelist.begin();
bool found = false;
while ( p != freelist.end() && !found)
if (locations <= (*p).size )
found = true;
else
p++;
if( found )
{
mystart = (*p).start;
mysize = (*p).size;
freelist.erase(p);
newsize = mysize - locations;
newstart = mystart + locations;
if( newsize != 0 )
{
h.start = newstart;
h.size = newsize;
p = freelist.begin();
// error corrected 6/19/03
// while( p != freelist.end() && mysize > (*p).size )
while( p != freelist.end() && newsize > (*p).size )
p++;
freelist.insert(p,h);
}
ptemp.start = mystart;
ptemp.size = locations;
plist.push_front(ptemp);
return true;
}
else
{
cout << "Memory Allocation Is Not Possible" << endl;
return false;
}
}
bool memman::free( int processid )
{
list<process>::iterator p;
list<hole>::iterator q;
p = plist.begin();
bool found = false, found2;
int mystart,mysize;
while( p != plist.end() && !found )
if( (*p).id == processid )
found = true;
else
p++;
if( found )
{
mystart=(*p).start;
mysize=(*p).size;
q = freelist.begin();
found2 = false;
while(q != freelist.end() && !found2 )
// error corrected 6/19/03
// if( mysize <= (*q).start )
if( mysize <= (*q).size )
found2 = true;
else
q++;
hole h;
h.start=mystart;
h.size=mysize;
freelist.insert(q,h);
plist.erase(p);
}
else
cout << "No process for " << processid << endl;
return 1;
}
void memman::displayfree()
{
list<hole>::iterator p;
p = freelist.begin();
cout << endl << "Free List: " << endl;
while( p != freelist.end() )
{
cout << (*p).start << setw(8) << (*p).size << endl;
p++;
}
}
void memman::process_list()
{
list<process>::iterator p;
p = plist.begin();
cout << endl << "Process List: " << endl;
while( p != plist.end() )
{
cout << (*p).id << " " << (*p).start << endl;
p++;
}
}