// First Fit Algorithm voidFF(int* inMemSize, int *inHoleSize, int inM, int inH){ memAllocFF = (int*) malloc(sizeof(int)*inM); int i; for (i=0;i<inM;i++) { memAllocFF[i]=-1; } for (i=0;i<inM;i++) { int j; for (j=0;j<inH;j++) { if (inMemSize[i]<=inHoleSize[j]) { // first fit memAllocFF[i]=j; inHoleSize[j]-=inMemSize[i]; break; } } } }
// Best Fit Algorithm voidBF(int* inMemSize, int *inHoleSize, int inM, int inH){ memAllocBF = (int*) malloc(sizeof(int)*inM); int i; for (i=0;i<inM;i++) { memAllocBF[i]=-1; } for (i=0;i<inM;i++) { int j; int bfIndex=-1; for (j=0;j<inH;j++) { if (inMemSize[i]<=inHoleSize[j]) { // fit if (bfIndex==-1) { // first time fit bfIndex=j; } elseif (inHoleSize[j]<inHoleSize[bfIndex]) { // best fit bfIndex=j; } } } if (bfIndex!=-1) { memAllocBF[i]=bfIndex; inHoleSize[bfIndex]-=inMemSize[i]; } } }
// Worst Fit Algorithm voidWF(int* inMemSize, int *inHoleSize, int inM, int inH){ memAllocWF = (int*) malloc(sizeof(int)*inM); int i; for (i=0;i<inM;i++) { memAllocWF[i]=-1; } for (i=0;i<inM;i++) { int j; int wfIndex=-1; for (j=0;j<inH;j++) { if (inMemSize[i]<=inHoleSize[j]) { // fit if (wfIndex==-1) { // first time fit wfIndex=j; } elseif (inHoleSize[j]>inHoleSize[wfIndex]) { // worst fit wfIndex=j; } } } if (wfIndex!=-1) { memAllocWF[i]=wfIndex; inHoleSize[wfIndex]-=inMemSize[i]; } } }