How to implement quick fit memory allocation algorithm

Hello,
I cant make an implementation of quick fit and I need someone to help me how to do it.
Below is the code for best fit, which finds the minimum block size, and stores the process there.
QuickFit relies on lazy coalescing. If you free a block of size n, you are very likely, in the near future, to allocate a block of size n, because what you really did was free an object of type A (sizeof(A) == n) and you are therefore fairly likely in the near future to allocate a new object of type A. So instead of returning the block to the general heap, and possibly coalescing it with nearby blocks, you just hang onto it, keeping it in a list of blocks of size n. The next time you allocate an object of size n, the allocator looks on the free list[n] to see if there are any blocks laying aroud to be reallocated, and if one is, your allocation is essentially instantaneous. Only if the list is empty do you revert to one of the slower algorithms


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
 #include <iostream>
 
using namespace std;
 
int main()
{
    int nBlocks,nProcess,blockSize[20],processSize[20];
     
    cout<<" Enter the number of blocks: ";
    cin>>nBlocks;
    cout<<" Enter the number of processes: ";
    cin>>nProcess;
     
    cout<<" Enter the size of "<<nBlocks<<" blocks: ";
    for(int i=0;i<nBlocks;i++)
      cin>>blockSize[i];
       
    cout<<" Enter the size of "<<nProcess<<" processes: ";
    for(int i=0;i<nProcess;i++)
      cin>>processSize[i];
     
    for(int i=0;i<nProcess;i++)
    {
        int min = 9999;
        bool flag = false;
        int pos=0;
        for(int j=0;j<nBlocks;j++) {
           if(min > blockSize[j] && processSize[i]<=blockSize[j])
            {
                min = blockSize[j];
                flag = true;
                pos = j;
            }
             
        }
         
        if(flag)
        {
            blockSize[pos] = blockSize[pos] - processSize[i];
            cout<<"\n\n Process "<<i+1<<" is allocated to Block "<<pos+1;
        }
        else
        {
            cout<<"\n Process "<<i+1<<" cannot be allocated";
        }
         
        cout<<"\n Remaining Block size\n";
        for(int j=0;j<nBlocks;j++)
        cout<<" "<<blockSize[j];
         
         
    }
     
     
    return 0;
}
Last edited on
Topic archived. No new replies allowed.