2012-11-05 21:04:21 -05:00
|
|
|
/*
|
2012-11-20 22:03:08 -05:00
|
|
|
* Best Fit Algorithm by David Weber
|
2012-11-14 23:54:24 -05:00
|
|
|
* ITCS 3146
|
|
|
|
* 11/9/2012
|
2012-11-05 21:04:21 -05:00
|
|
|
*/
|
|
|
|
|
2012-11-20 22:03:08 -05:00
|
|
|
import java.lang.reflect.Method;
|
|
|
|
import java.util.*;
|
2012-11-14 23:54:24 -05:00
|
|
|
|
|
|
|
public class WorstFitAlgorithm implements baseAlgorithm{
|
|
|
|
|
|
|
|
int memoryBlock[];
|
2012-11-20 22:03:08 -05:00
|
|
|
private Job[] jobArray = new Job[memoryManagement.JOBAMOUNT+10];
|
|
|
|
List<Integer> indices;
|
|
|
|
List<Integer> blocks;
|
|
|
|
int memoryLocation;
|
|
|
|
int worstSize; //The most suitable block size for the job
|
|
|
|
int worstSizeIndex; //The most suitable block size starting index for the job
|
|
|
|
|
2012-11-14 23:54:24 -05:00
|
|
|
public WorstFitAlgorithm(int memorySize)
|
|
|
|
{
|
|
|
|
//Initialize memory block to whatever the size is
|
|
|
|
memoryBlock = new int[memorySize];
|
2012-11-20 22:03:08 -05:00
|
|
|
blocks = new ArrayList<>(); //Dynamically resizable array list for allocation candidates (interleaved with index and memory size);
|
|
|
|
indices = new ArrayList<>(); //Dynamically resizable array list for allocation candidates (interleaved with index and memory size);
|
2012-11-14 23:54:24 -05:00
|
|
|
}
|
|
|
|
|
2012-11-20 22:03:08 -05:00
|
|
|
public int getWorstIndex(int jobSize)
|
2012-11-14 23:54:24 -05:00
|
|
|
{
|
2012-11-20 22:03:08 -05:00
|
|
|
memoryLocation = 0;
|
2012-11-14 23:54:24 -05:00
|
|
|
|
2012-11-20 22:03:08 -05:00
|
|
|
indices.clear();
|
|
|
|
blocks.clear();
|
|
|
|
|
|
|
|
while (memoryLocation < this.memoryBlock.length)
|
|
|
|
{
|
|
|
|
if (memoryBlock[memoryLocation] != 0){
|
|
|
|
memoryLocation++;
|
|
|
|
continue;
|
|
|
|
}
|
2012-11-14 23:54:24 -05:00
|
|
|
|
2012-11-20 22:03:08 -05:00
|
|
|
int beginningLoc = memoryLocation;
|
|
|
|
int free = 0;
|
|
|
|
|
|
|
|
while (memoryLocation < this.memoryBlock.length && memoryBlock[memoryLocation] == 0)
|
2012-11-14 23:54:24 -05:00
|
|
|
{
|
2012-11-20 22:03:08 -05:00
|
|
|
memoryLocation++;
|
|
|
|
free++;
|
2012-11-14 23:54:24 -05:00
|
|
|
}
|
2012-11-20 22:03:08 -05:00
|
|
|
|
|
|
|
if (free >= jobSize){
|
|
|
|
//System.out.println("Found a block of size " + free + " at " + beginningLoc);
|
|
|
|
blocks.add(free);
|
|
|
|
indices.add(beginningLoc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//System.out.println("Size of indices array: " + indices.size());
|
|
|
|
//System.out.println("Size of sizes array: " + blocks.size());
|
|
|
|
|
|
|
|
for(int i = 0; i < blocks.size(); i++)
|
|
|
|
{
|
|
|
|
//System.out.println("Index: " + indices.get(i));
|
|
|
|
//System.out.println("Size: " + blocks.get(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
int worstIndex = -1;
|
|
|
|
int wSize = blocks.get(0).intValue();
|
|
|
|
|
|
|
|
|
|
|
|
//GET WORST INDEX
|
|
|
|
for(int i = 0; i < blocks.size(); i++)
|
|
|
|
{
|
|
|
|
//Get largest possible free block to allocate to
|
|
|
|
if((blocks.get(i).intValue() >= wSize && blocks.get(i).intValue() >= jobSize) || blocks.get(i).intValue() > -1)
|
2012-11-14 23:54:24 -05:00
|
|
|
{
|
2012-11-20 22:03:08 -05:00
|
|
|
worstIndex = indices.get(i).intValue();
|
2012-11-14 23:54:24 -05:00
|
|
|
}
|
|
|
|
|
2012-11-20 22:03:08 -05:00
|
|
|
//"Worst fit"....same size as job size
|
|
|
|
else if(blocks.get(i).intValue() == jobSize)
|
|
|
|
{
|
|
|
|
//"Worst" possible fit. You're done.
|
|
|
|
//System.out.println("Worst Case");
|
|
|
|
worstIndex = indices.get(i).intValue();
|
|
|
|
}
|
2012-11-14 23:54:24 -05:00
|
|
|
}
|
|
|
|
|
2012-11-20 22:03:08 -05:00
|
|
|
//System.out.println("bestIndex: " + bestIndex);
|
|
|
|
//System.out.println("bSize: " + bSize);
|
|
|
|
|
|
|
|
return worstIndex;
|
2012-11-14 23:54:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void allocate(int jobID, int jobSize, int jobTime)
|
2012-11-20 22:03:08 -05:00
|
|
|
{
|
|
|
|
try
|
2012-11-14 23:54:24 -05:00
|
|
|
{
|
2012-11-20 22:03:08 -05:00
|
|
|
Method deallocateMethod = this.getClass().getMethod("deallocate", new Class[]{int.class, int.class});
|
|
|
|
|
|
|
|
worstSizeIndex = this.getWorstIndex(jobSize);
|
|
|
|
|
|
|
|
if(jobSize > memoryBlock.length)
|
|
|
|
{
|
|
|
|
//System.out.println("Job is too large for current memory size");
|
|
|
|
}
|
2012-11-14 23:54:24 -05:00
|
|
|
|
|
|
|
if(worstSizeIndex == -1)
|
|
|
|
{
|
2012-11-20 22:03:08 -05:00
|
|
|
//Compact and try again
|
|
|
|
//System.out.println("Compacting memory...");
|
|
|
|
this.compact();
|
|
|
|
worstSizeIndex = this.getWorstIndex(jobSize);
|
2012-11-14 23:54:24 -05:00
|
|
|
}
|
2012-11-20 22:03:08 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
//System.out.println("The size of the job is: " + jobSize);
|
|
|
|
//System.out.println("The best size index is: " + bestSizeIndex);
|
|
|
|
synchronized(memoryBlock)
|
|
|
|
{
|
|
|
|
for(int i = worstSizeIndex; i < jobSize + worstSizeIndex; i++)
|
|
|
|
{
|
|
|
|
//System.out.println("Writing jobID: " + jobID + " to position " + i + " in memory block!");
|
|
|
|
this.memoryBlock[i] = jobID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//System.out.println("Successfully allocated! Starting job...");
|
|
|
|
|
|
|
|
Job newJob = new Job(jobSize, jobID, jobSize, worstSizeIndex, deallocateMethod, this);
|
2012-11-14 23:54:24 -05:00
|
|
|
|
2012-11-20 22:03:08 -05:00
|
|
|
jobArray[jobID] = newJob;
|
|
|
|
|
|
|
|
newJob.start();
|
|
|
|
|
|
|
|
//System.out.println("Job started!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
2012-11-14 23:54:24 -05:00
|
|
|
{
|
2012-11-20 22:03:08 -05:00
|
|
|
//System.out.println("Could not allocate job with ID " + jobID);
|
2012-11-14 23:54:24 -05:00
|
|
|
}
|
|
|
|
}
|
2012-11-20 22:03:08 -05:00
|
|
|
/*
|
|
|
|
* This method gathers all occupied memory and stores it contiguously in an array list of blocks.
|
|
|
|
* After that, it rewrites the memoryBlock array by writing the memory in contiguous order, and then
|
|
|
|
* filling in the rest of the memory with 0's
|
|
|
|
*/
|
2012-11-14 23:54:24 -05:00
|
|
|
public void compact()
|
|
|
|
{
|
2012-11-20 22:03:08 -05:00
|
|
|
List<Integer> takenBlocks = new ArrayList<>();
|
|
|
|
|
|
|
|
memoryLocation = 0;
|
|
|
|
|
|
|
|
//Gather allocated memory
|
|
|
|
while(memoryLocation < this.memoryBlock.length && memoryBlock[memoryLocation] != 0)
|
|
|
|
{
|
|
|
|
takenBlocks.add(memoryBlock[memoryLocation]);
|
|
|
|
memoryLocation++;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int i = 0; i < takenBlocks.size(); i++)
|
|
|
|
{
|
|
|
|
this.memoryBlock[i] = takenBlocks.get(i).intValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int i = takenBlocks.size(); i < this.memoryBlock.length; i++)
|
|
|
|
{
|
|
|
|
this.memoryBlock[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
System.out.println("Successfully compacted!");
|
|
|
|
|
|
|
|
if(takenBlocks.isEmpty())
|
|
|
|
{
|
|
|
|
System.out.println("Cannot compact!");
|
|
|
|
}
|
|
|
|
*/
|
2012-11-14 23:54:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void deallocate(int jobSize, int beginningLocation)
|
2012-11-05 21:08:12 -05:00
|
|
|
{
|
2012-11-20 22:03:08 -05:00
|
|
|
for(int i = beginningLocation; i < jobSize + beginningLocation; i++)
|
2012-11-14 23:54:24 -05:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|