1. 程式人生 > >OS-記憶體分配回收(最佳適應,最差適應,首次適應)

OS-記憶體分配回收(最佳適應,最差適應,首次適應)

#include <iostream>

using namespace std;
class freeMemoryTable
{
    public:
    int id;
    int begin;
    int size;
    int status;
};
class Node
{
    public:
    freeMemoryTable data;
    Node *pre;
    Node * next;

};
Node* first;
Node* endm;
int flag;
int InitBlock()
{

    first->pre=NULL;
    first->next=endm;
    first->data.status=1;
    endm->pre=first;
    endm->next=NULL;
    endm->data.id=1;
    endm->data.begin=40;
    endm->data.size=1000;
    endm->data.status=0;
    cout<<"Init success"<<endl;
    return 1;

}
int sortf(int type,int request)
{
    Node*p=first->next;
    Node *q;
    int temp1=999999;
    int temp2=-99999;
    int index=0;
    int index2=0;
    if(type==1)//Sort by adress
    {
         for(;p!=NULL;p=p->next)
         {

                if((p->data.size-request)>=0&&(p->data.status!=1))
                {
                    return index;
                }
                index++;
         }
    }
    else if(type==2)//Sort by memory <
    {
         for(;p!=NULL;p=p->next)
         {

                if((p->data.size-request)>=0&&(temp1>p->data.size)&&(p->data.status!=1))
                {
                             temp1=p->data.size;
                             index2=index;
                }
                index++;
         }
         cout<<index2<<endl;
         return index2;
    }
    else if(type==3)//Sort by memory >
    {
            for(;p!=NULL;p=p->next)
                {

                if((p->data.size-request)>=0&&(temp2<p->data.size)&&(p->data.status!=1))
                {
                             temp1=p->data.size;
                             index2=index;
                }
                index++;
                }
                cout<<index2<<endl;
         return index2;
    }
}
Node* searchM(int index)
{
       Node*p=first->next;
             int j=0;
         for(;p!=NULL;p=p->next)
         {

                if(index==j)
                {
                    return p;
                }
                j++;
         }
}
void add(Node *q)
{
    endm->next=q;
    q->pre=endm;
    q->next=NULL;
    endm=q;
}
void operation(int request,Node*p)
{
       if(p->data.size==request)
       {
           p->data.status=1;
       }
       else
       {
          p->data.status=1;

          Node *q =new Node();
          q->data.id=p->data.id+1;
          q->data.status=0;

          q->data.size=p->data.size-request;
           p->data.size=request;
          add(q);
       }
}
void printM()
{
    Node *p=first->next;
    cout<<"------------------------"<<endl;
     for(;p!=NULL;p=p->next)
         {
           cout<<"id "<<p->data.id<<" Size "<<p->data.size
           <<" Status "<<p->data.status<<endl;
         }
      cout<<"------------------------"<<endl;
}
void MemoryRe(int id)
{
    Node *p=first->next;
    Node *qq=p->pre;
    bool flag=true;
     for(;p!=NULL&&flag;p=p->next)
         {

           if(p->data.id==id)
           {
               flag=false;
               break;
           }
           if(flag)
            qq=p;
         }
    if(p->next!=NULL)
    {
        if(!qq->data.status&&!p->next->data.status)
        {
                cout<<"Enter1"<<endl;
                  Node *q=qq;
                  q->data.size+=p->data.size;
                  q->data.size+=p->next->data.size;
                  q->next=p->next->next;

        }
        else if(!qq->data.status&&p->next->data.status)
        {
            cout<<"Enter2"<<endl;
                  Node *q=qq;
                  q->data.size+=p->data.size;
                  q->next=p->next;
                  p->next->pre=q;
        }
        else if(qq->data.status&&!p->next->data.status)
        {
                cout<<"Enter3"<<endl;
                  p->data.status=0;
                  p->data.size+=p->next->data.size;
                  p->next=p->next->next;

        }
        else
        {
              cout<<"Enter4"<<endl;
              p->data.status=0;
        }
    }
    else
    {
           if(!qq->data.status)
            {
                cout<<"Enter5"<<endl;
                  Node *q=qq;
                  q->data.size+=p->data.size;

                  q->next=p->next->next;
            }
             else
             {  cout<<"Enter6"<<endl;
                  p->data.status=0;
             }
    }
}
void menu()
{
    cout<<"1.first fit"<<endl;
    cout<<"2.best fit" <<endl;
    cout<<"3.worst fit" <<endl;
    cout<<"4.Memory refresh"<<endl;
}
int main()

{
        first =new Node();
        endm=new Node();
     InitBlock();
     while(1)
     {
         menu();
         cout <<"Choose:"<<endl;
         int type;
         cin>>type;
         int req;
         if(type!=4)
         {
            cout<<"Request size"<<endl;
            cin>>req;
            operation(req,searchM(sortf(type,req)));
         }
         else
         {
             cout<<"Memory id"<<endl;
             int id;
             cin>>id;
             MemoryRe(id);
         }

         printM();
     }

    return 0;
}