/* David P. Turnbull ITCS3146 group project this class sets up a First Fit memory scheme */ import java.lang.reflect.*; //this section sets up the Car class class NextFit implements baseAlgorithm { //this section sets up the private elements of the class private int jobId, jobSize, jobTime, startLoc, fillLoc, endLoc, blkSize, memSize = memoryManagement.MEMORYSIZE, active, noJobs=0, s1=0, currentPosition=0, positionToCompress=0, loopCount, compMemTest=0, jobLoaded=0, tableEntries=1; private int[] tempVal = new int[6]; private int[][] memTable = new int[memSize+2][6]; private int[] memory = new int[memSize]; private Job[] jobArray = new Job[memoryManagement.JOBAMOUNT+10]; //this is a no argument constructor public NextFit() { memTable[0][0]=0; //job number memTable[0][1]=0; //job size memTable[0][2]=0; //start location in memory memTable[0][3]=memSize-1; //end location in memory memTable[0][4]=memSize; //mem blk size size memTable[0][5]=-1; //status, 0=not active, 1=active, -1=special } //this method sets the job up public void allocate(int ID, int size, int jTime) { //synchronized(memTable){ jobId = ID; jobSize = size; jobTime = jTime; noJobs++; s1=0; loopCount=0; //Bradlee's code *********************************************************************************** try { Method deallocateMethod = this.getClass().getMethod("deallocate", new Class[]{int.class, int.class}); //checks to see if the job will fit in memory if(jobSize>memSize) { System.out.println("\n\n*********************************************************"+ " THIS JOB IS TOO LARGE TO FIT INTO MEMORY"+ "*********************************************************"); System.exit(0); } //this will loop until job is loaded into memory do { //this section looks for a place to put the new job do { if(memTable[currentPosition][5]==-1 && memTable[currentPosition][4]>=jobSize && memTable[currentPosition][3]==memSize-1) { //runs only for the first job if(noJobs==1) { synchronized(memTable){ memTable[currentPosition][0] = jobId; memTable[currentPosition][1] = jobSize; memTable[currentPosition][2] = 0; memTable[currentPosition][3] = jobSize-1; memTable[currentPosition][4] = memTable[0][3]-memTable[0][2]+1; memTable[currentPosition][5] = 1; Job newJob = new Job(jobTime, jobId, jobSize, memTable[currentPosition][2], deallocateMethod, this); fillMemory(jobId, jobSize, memTable[currentPosition][2]); jobArray[jobId - 1] = newJob; newJob.start(); memTable[currentPosition+1][0] = 0; memTable[currentPosition+1][1] = 0; memTable[currentPosition+1][2] = memTable[currentPosition][3]+1; memTable[currentPosition+1][3] = memSize-1; memTable[currentPosition+1][4] = memSize-memTable[currentPosition+1][2]; memTable[currentPosition+1][5] = -1; currentPosition++; positionToCompress=currentPosition; tableEntries++; jobLoaded=1; s1=memSize*2; } } //runs after the first job and if the only available slot is at the end of memory else { synchronized(memTable){ memTable[currentPosition][0] = jobId; memTable[currentPosition][1] = jobSize; memTable[currentPosition][2] = memTable[currentPosition-1][3]+1; memTable[currentPosition][3] = jobSize+memTable[currentPosition][2]-1; memTable[currentPosition][4] = memTable[currentPosition][3]-memTable[currentPosition][2]+1; memTable[currentPosition][5] = 1; Job newJob = new Job(jobTime, jobId, jobSize, memTable[currentPosition][2], deallocateMethod, this); fillMemory(jobId, jobSize, memTable[currentPosition][2]); jobArray[jobId - 1] = newJob; newJob.start(); memTable[currentPosition+1][0] = 0; memTable[currentPosition+1][1] = 0; memTable[currentPosition+1][2] = memTable[currentPosition][3]+1; memTable[currentPosition+1][3] = memSize-1; memTable[currentPosition+1][4] = memSize-memTable[currentPosition+1][2]; memTable[currentPosition+1][5] = -1; tableEntries++; currentPosition++; jobLoaded=1; positionToCompress=currentPosition; s1=memSize*2; } } } //checks for first available free block that has been deallocated else if(memTable[currentPosition][4]>=jobSize && memTable[currentPosition][5]==0) { synchronized(memTable){ memTable[currentPosition][0] = jobId; memTable[currentPosition][1] = jobSize; memTable[currentPosition][5] = 1; Job newJob = new Job(jobTime, jobId, jobSize, memTable[currentPosition][2], deallocateMethod, this); fillMemory(jobId, jobSize, memTable[currentPosition][2]); jobArray[jobId - 1] = newJob; newJob.start(); currentPosition++; jobLoaded=1; positionToCompress=currentPosition; s1=memSize*2; } } else if (currentPosition==tableEntries-1) { currentPosition=0; s1++; } else { s1++; currentPosition++; } }while(s1