2012-11-12 20:04:32 -05:00
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
2012-11-06 20:23:34 -05:00
|
|
|
public class threadedAllocation implements baseAlgorithm{
|
2012-11-06 20:15:10 -05:00
|
|
|
int[] memoryBlock;
|
2012-11-12 20:04:32 -05:00
|
|
|
threadedAllocationGarbage garbageThread;
|
|
|
|
Job[] jobArray;
|
2012-11-06 20:15:10 -05:00
|
|
|
|
|
|
|
threadedAllocation(int memorySize) {
|
2012-10-30 20:53:40 -04:00
|
|
|
/* Constructor specific for this algorithm
|
|
|
|
*
|
|
|
|
* Start the threaded garbage collector, and then begin
|
|
|
|
* our actual operations */
|
2012-11-06 20:15:10 -05:00
|
|
|
memoryBlock = new int[memorySize];
|
2012-11-12 20:04:32 -05:00
|
|
|
|
2012-11-16 22:13:29 -05:00
|
|
|
// Set up the array of job references
|
|
|
|
this.jobArray = new Job[memoryManagement.JOBAMOUNT + 1];
|
|
|
|
|
2012-11-12 20:04:32 -05:00
|
|
|
this.garbageThread = new threadedAllocationGarbage(this.memoryBlock, 20, this.jobArray);
|
2012-11-13 17:22:47 -05:00
|
|
|
this.garbageThread.start();
|
2012-11-12 20:04:32 -05:00
|
|
|
|
2012-10-30 20:53:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int locateBlock(int blockSize){
|
|
|
|
/* Locate a block of size blockSize, return -1 if can't find one */
|
|
|
|
|
|
|
|
//Find an open location
|
|
|
|
int memoryLoc = 0;
|
|
|
|
while (memoryLoc < this.memoryBlock.length)
|
|
|
|
{
|
2012-11-16 22:13:29 -05:00
|
|
|
if (memoryBlock[memoryLoc] != 0){
|
|
|
|
memoryLoc++;
|
2012-10-30 20:53:40 -04:00
|
|
|
//Block isn't free
|
|
|
|
continue;
|
2012-11-16 22:13:29 -05:00
|
|
|
}
|
2012-10-30 20:53:40 -04:00
|
|
|
|
|
|
|
//One location is free, find out total size free
|
|
|
|
//This loop breaks either when we've found the size we need, or
|
|
|
|
//we found the beginning of the next block.
|
|
|
|
int beginningLoc = memoryLoc;
|
|
|
|
int free = 0;
|
2012-11-16 22:13:29 -05:00
|
|
|
while (free < blockSize && memoryLoc < this.memoryBlock.length && memoryBlock[memoryLoc] == 0)
|
2012-10-30 20:53:40 -04:00
|
|
|
{
|
|
|
|
memoryLoc += 1;
|
|
|
|
free += 1;
|
|
|
|
}
|
|
|
|
|
2012-11-13 17:22:47 -05:00
|
|
|
if (free >= blockSize){
|
2012-11-16 22:13:29 -05:00
|
|
|
//System.out.println("Found a block of size " + blockSize + " at " + beginningLoc);
|
2012-10-30 20:53:40 -04:00
|
|
|
return beginningLoc;
|
2012-11-13 17:22:47 -05:00
|
|
|
}
|
2012-10-30 20:53:40 -04:00
|
|
|
}
|
|
|
|
//Once the above loop has exited, we've run out of locations to check
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-11-06 20:15:10 -05:00
|
|
|
public void allocate(int jobID, int jobSize, int jobLength ){
|
2012-10-30 20:53:40 -04:00
|
|
|
/* Over-rides allocate() of baseAlgorithm */
|
2012-11-12 20:04:32 -05:00
|
|
|
try{
|
|
|
|
Method deallocateMethod = this.getClass().getMethod("deallocate", new Class[]{int.class, int.class});
|
|
|
|
|
|
|
|
//Loop until we get a block big enough for our job
|
|
|
|
// Note that this assumes we're not going to race against ourselves
|
|
|
|
int beginningLocation = locateBlock( jobSize );
|
|
|
|
while (beginningLocation == -1)
|
|
|
|
beginningLocation = locateBlock( jobSize );
|
|
|
|
|
2012-11-16 22:13:29 -05:00
|
|
|
|
2012-11-12 20:04:32 -05:00
|
|
|
//We've got a location, mark it as filled, and start the job.
|
2012-11-16 22:13:29 -05:00
|
|
|
synchronized(memoryBlock){
|
|
|
|
for (int x = 0; x < jobSize; x++)
|
|
|
|
{
|
|
|
|
memoryBlock[beginningLocation + x] = jobID;
|
|
|
|
}
|
2012-11-12 20:04:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Job newJob = new Job(jobLength, jobID, jobSize, beginningLocation, deallocateMethod, this);
|
2012-11-16 22:13:29 -05:00
|
|
|
jobArray[jobID] = newJob;
|
|
|
|
|
2012-11-12 20:04:32 -05:00
|
|
|
newJob.start();
|
|
|
|
} catch (Exception e){
|
2012-11-16 22:13:29 -05:00
|
|
|
e.printStackTrace();
|
|
|
|
System.exit(-1);
|
2012-10-30 20:53:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-06 20:15:10 -05:00
|
|
|
public void deallocate(int jobSize, int beginningLocation){
|
2012-10-30 20:53:40 -04:00
|
|
|
/* Over-rides deallocate() of baseAlgorithm */
|
2012-11-16 22:13:29 -05:00
|
|
|
//System.err.println("Deallocation job with ID " + memoryBlock[beginningLocation] + " at time " + System.currentTimeMillis());
|
2012-10-30 20:53:40 -04:00
|
|
|
//Simple algorithm, basically just mark the memory as cleared.
|
2012-11-16 22:13:29 -05:00
|
|
|
synchronized(memoryBlock){
|
|
|
|
for (int x = 0; x < jobSize; x++)
|
|
|
|
{
|
|
|
|
memoryBlock[beginningLocation + x] = 0;
|
|
|
|
}
|
2012-10-30 20:53:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|