2012-11-05 21:04:21 -05:00
|
|
|
/*
|
2012-11-14 23:54:24 -05:00
|
|
|
* Best Fit Algorithm by David Weber
|
|
|
|
* ITCS 3146
|
|
|
|
* 11/9/2012
|
2012-11-05 21:04:21 -05:00
|
|
|
*/
|
|
|
|
|
2012-11-17 05:07:03 -05:00
|
|
|
import java.lang.reflect.Method;
|
2012-11-14 23:54:24 -05:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
public class BestFitAlgorithm implements baseAlgorithm{
|
|
|
|
|
|
|
|
int memoryBlock[];
|
2012-11-17 05:07:03 -05:00
|
|
|
private Job[] jobArray = new Job[memoryManagement.JOBAMOUNT+10];
|
|
|
|
ArrayList<Integer> candidates;
|
2012-11-14 23:54:24 -05:00
|
|
|
|
|
|
|
public BestFitAlgorithm(int memorySize)
|
|
|
|
{
|
|
|
|
//Initialize memory block to whatever the size is
|
|
|
|
memoryBlock = new int[memorySize];
|
2012-11-17 05:07:03 -05:00
|
|
|
System.out.println("The size of the memory is: " + memoryBlock.length);
|
2012-11-14 23:54:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getBestSizeIndex(int jobSize)
|
|
|
|
{
|
2012-11-17 05:07:03 -05:00
|
|
|
int bestSize; //The most suitable block size for the job
|
|
|
|
int bestSizeIndex; //The most suitable block size starting index for the job
|
2012-11-14 23:54:24 -05:00
|
|
|
|
2012-11-17 05:07:03 -05:00
|
|
|
System.out.println("The size of the job is: " + jobSize);
|
|
|
|
|
|
|
|
candidates = new ArrayList<Integer>(); //Dynamically resizable array list for allocation candidates (interleaved with index and memory size)
|
2012-11-14 23:54:24 -05:00
|
|
|
|
|
|
|
int counter = 0; //Counter for measuring unallocated memory
|
2012-11-17 05:07:03 -05:00
|
|
|
//Scan through memory block and get free blocks. Add candidates for allocation to array list
|
2012-11-14 23:54:24 -05:00
|
|
|
for(int i = 0; i < memoryBlock.length; i++)
|
|
|
|
{
|
2012-11-17 05:07:03 -05:00
|
|
|
//If position in memory block here is 0, iterate from that index and count up sequential 0's (free space)
|
2012-11-14 23:54:24 -05:00
|
|
|
if(memoryBlock[i] == 0)
|
|
|
|
{
|
|
|
|
for(int j = i; j < memoryBlock.length - i; j++)
|
|
|
|
{
|
|
|
|
if(memoryBlock[j] == 0)
|
|
|
|
{
|
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
}
|
2012-11-17 05:07:03 -05:00
|
|
|
if(counter == jobSize)
|
2012-11-14 23:54:24 -05:00
|
|
|
{
|
|
|
|
candidates.add(i); //Store index
|
|
|
|
candidates.add(counter); //Store size of free memory chunk
|
|
|
|
}
|
2012-11-17 05:07:03 -05:00
|
|
|
else if(counter >= jobSize)
|
|
|
|
{
|
|
|
|
candidates.add(i); //Store index
|
|
|
|
candidates.add(counter); //Store size of free memory chunk
|
|
|
|
}
|
|
|
|
//System.out.println("The size of the counter is: " + counter);
|
2012-11-14 23:54:24 -05:00
|
|
|
counter = 0;
|
|
|
|
}
|
|
|
|
}
|
2012-11-17 05:07:03 -05:00
|
|
|
for(int i = 0; i < candidates.size(); i++)
|
|
|
|
{
|
|
|
|
System.out.println("Candidate index: " + candidates.get(i));
|
|
|
|
System.out.println("Candidate size: " + candidates.get(i+1));
|
|
|
|
}
|
2012-11-14 23:54:24 -05:00
|
|
|
|
|
|
|
//Iterate through candidate sizes
|
2012-11-17 05:07:03 -05:00
|
|
|
bestSizeIndex = candidates.get(0).intValue(); //Initialize best index to first spot in array list
|
2012-11-14 23:54:24 -05:00
|
|
|
|
|
|
|
bestSize = candidates.get(1).intValue(); //Initialize bestSize to first space size in candidate
|
|
|
|
|
|
|
|
//Iterate through sizes and find the best fit
|
|
|
|
for(int i = 1; i < candidates.size(); i=i+2)
|
|
|
|
{
|
|
|
|
//Best possible case: job size = free block size (you're done)
|
|
|
|
if(candidates.get(i).intValue() == jobSize)
|
|
|
|
{
|
|
|
|
bestSizeIndex = i - 1;
|
2012-11-17 05:07:03 -05:00
|
|
|
System.out.println("The best size index is: " + bestSizeIndex);
|
2012-11-14 23:54:24 -05:00
|
|
|
return bestSizeIndex; //You're done. Return the value.
|
|
|
|
}
|
|
|
|
//If the current size is less than the previous best size, make this the new best size
|
|
|
|
else if(candidates.get(i).intValue() < bestSize)
|
|
|
|
{
|
|
|
|
bestSize = candidates.get(i+1).intValue();
|
|
|
|
bestSizeIndex = i - 1;
|
2012-11-17 05:07:03 -05:00
|
|
|
System.out.println("The best size index is: " + bestSizeIndex);
|
2012-11-14 23:54:24 -05:00
|
|
|
}
|
|
|
|
}
|
2012-11-17 05:07:03 -05:00
|
|
|
System.out.println("The best size is: " + bestSize);
|
2012-11-14 23:54:24 -05:00
|
|
|
|
2012-11-17 05:07:03 -05:00
|
|
|
//No candidates
|
2012-11-14 23:54:24 -05:00
|
|
|
if(candidates.isEmpty())
|
|
|
|
{
|
2012-11-17 05:07:03 -05:00
|
|
|
System.out.println("No best size index");
|
2012-11-14 23:54:24 -05:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bestSizeIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void allocate(int jobID, int jobSize, int jobTime)
|
|
|
|
{
|
2012-11-17 05:07:03 -05:00
|
|
|
try
|
2012-11-14 23:54:24 -05:00
|
|
|
{
|
2012-11-17 05:07:03 -05:00
|
|
|
Method deallocateMethod = this.getClass().getMethod("deallocate", new Class[]{int.class, int.class});
|
|
|
|
|
|
|
|
//checks to see if the job will fit in memory
|
|
|
|
if(jobSize>memoryBlock.length)
|
2012-11-14 23:54:24 -05:00
|
|
|
{
|
2012-11-17 05:07:03 -05:00
|
|
|
System.out.println("This job is too large");
|
|
|
|
System.exit(0);
|
2012-11-14 23:54:24 -05:00
|
|
|
}
|
2012-11-17 05:07:03 -05:00
|
|
|
|
|
|
|
int bestSizeIndex = getBestSizeIndex(jobSize);
|
|
|
|
|
|
|
|
//No candidates found
|
|
|
|
if(bestSizeIndex == -1)
|
2012-11-17 02:24:16 -05:00
|
|
|
{
|
2012-11-17 05:07:03 -05:00
|
|
|
System.out.println("No candidates found...attempting to compact");
|
|
|
|
//Try compacting, then attempt to get an index again
|
|
|
|
compact();
|
|
|
|
|
|
|
|
bestSizeIndex = getBestSizeIndex(jobSize);
|
|
|
|
|
|
|
|
//Compacting still didn't produce an appropriate block
|
|
|
|
if(bestSizeIndex == -1)
|
|
|
|
{
|
|
|
|
//TODO .....
|
|
|
|
}
|
2012-11-17 02:24:16 -05:00
|
|
|
}
|
2012-11-17 05:07:03 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
//Allocate the memory
|
|
|
|
for(int i = bestSizeIndex; i < jobSize; i++)
|
|
|
|
{
|
|
|
|
memoryBlock[i] = jobID;
|
|
|
|
}
|
|
|
|
|
|
|
|
System.out.println("Successfully allocated!");
|
|
|
|
|
|
|
|
for(int i = 0; i < memoryBlock.length; i++)
|
|
|
|
{
|
|
|
|
System.out.println("Job at position " + i + "in memoryblock: " + memoryBlock[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
System.out.println("Could not allocate job with ID " + jobID);
|
2012-11-14 23:54:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void compact()
|
|
|
|
{
|
|
|
|
//TODO: Compact memory if no suitable allocation candidates are found on the first pass
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void deallocate(int jobSize, int beginningLocation)
|
2012-11-05 21:08:12 -05:00
|
|
|
{
|
2012-11-14 23:54:24 -05:00
|
|
|
for(int i = beginningLocation; i < jobSize; i++)
|
|
|
|
{
|
|
|
|
memoryBlock[i] = 0;
|
|
|
|
}
|
2012-11-05 21:08:12 -05:00
|
|
|
}
|
2012-11-14 23:54:24 -05:00
|
|
|
|
2012-11-05 21:04:21 -05:00
|
|
|
}
|