From 82f9f9459875847c38728cd6d12a6aa9a72dd9ad Mon Sep 17 00:00:00 2001 From: David Weber Date: Sat, 24 Nov 2012 04:29:34 -0500 Subject: [PATCH] Adjusted list declarations in Best Fit/Worst Fit algorithm code --- BestFitAlgorithm.java | 69 +++++++++++++++++++++++----------------- WorstFitAlgorithm.java | 71 ++++++++++++++++++++++++------------------ 2 files changed, 80 insertions(+), 60 deletions(-) diff --git a/BestFitAlgorithm.java b/BestFitAlgorithm.java index 889e8a6..9138be3 100644 --- a/BestFitAlgorithm.java +++ b/BestFitAlgorithm.java @@ -9,10 +9,10 @@ import java.util.*; public class BestFitAlgorithm implements baseAlgorithm{ - int memoryBlock[]; + private int memoryBlock[]; private Job[] jobArray = new Job[memoryManagement.JOBAMOUNT+10]; - List indices; - List blocks; + ArrayList indices; + ArrayList blocks; int memoryLocation; int bestSize; //The most suitable block size for the job int bestSizeIndex; //The most suitable block size starting index for the job @@ -21,8 +21,8 @@ public class BestFitAlgorithm implements baseAlgorithm{ { //Initialize memory block to whatever the size is memoryBlock = new int[memorySize]; - 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); + 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); } public int getBestIndex(int jobSize) @@ -31,30 +31,31 @@ public class BestFitAlgorithm implements baseAlgorithm{ indices.clear(); blocks.clear(); - - while (memoryLocation < this.memoryBlock.length) + synchronized(memoryBlock) { - if (memoryBlock[memoryLocation] != 0){ - memoryLocation++; - continue; - } - - int beginningLoc = memoryLocation; - int free = 0; - - while (memoryLocation < this.memoryBlock.length && memoryBlock[memoryLocation] == 0) + while (memoryLocation < this.memoryBlock.length) { - memoryLocation++; - free++; - } + if (memoryBlock[memoryLocation] != 0){ + memoryLocation++; + continue; + } - if (free >= jobSize){ - //System.out.println("Found a block of size " + free + " at " + beginningLoc); - blocks.add(free); - indices.add(beginningLoc); + int beginningLoc = memoryLocation; + int free = 0; + + while (memoryLocation < this.memoryBlock.length && memoryBlock[memoryLocation] == 0) + { + memoryLocation++; + free++; + } + + 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()); @@ -148,7 +149,7 @@ public class BestFitAlgorithm implements baseAlgorithm{ */ public void compact() { - List takenBlocks = new ArrayList<>(); + ArrayList takenBlocks = new ArrayList(); memoryLocation = 0; @@ -161,12 +162,19 @@ public class BestFitAlgorithm implements baseAlgorithm{ for(int i = 0; i < takenBlocks.size(); i++) { - this.memoryBlock[i] = takenBlocks.get(i).intValue(); + + synchronized(memoryBlock) + { + this.memoryBlock[i] = takenBlocks.get(i).intValue(); + } } for(int i = takenBlocks.size(); i < this.memoryBlock.length; i++) { - this.memoryBlock[i] = 0; + synchronized(memoryBlock) + { + this.memoryBlock[i] = 0; + } } /*System.out.println("Successfully compacted!"); @@ -181,9 +189,12 @@ public class BestFitAlgorithm implements baseAlgorithm{ @Override public void deallocate(int jobSize, int beginningLocation) { - for(int i = beginningLocation; i < jobSize + beginningLocation; i++) + synchronized(memoryBlock) { - memoryBlock[i] = 0; + for(int i = beginningLocation; i < jobSize + beginningLocation; i++) + { + memoryBlock[i] = 0; + } } } diff --git a/WorstFitAlgorithm.java b/WorstFitAlgorithm.java index 9e5701e..5e1fa40 100644 --- a/WorstFitAlgorithm.java +++ b/WorstFitAlgorithm.java @@ -11,8 +11,8 @@ public class WorstFitAlgorithm implements baseAlgorithm{ int memoryBlock[]; private Job[] jobArray = new Job[memoryManagement.JOBAMOUNT+10]; - List indices; - List blocks; + ArrayList indices; + ArrayList 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 @@ -21,8 +21,8 @@ public class WorstFitAlgorithm implements baseAlgorithm{ { //Initialize memory block to whatever the size is memoryBlock = new int[memorySize]; - 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); + 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); } public int getWorstIndex(int jobSize) @@ -31,27 +31,29 @@ public class WorstFitAlgorithm implements baseAlgorithm{ indices.clear(); blocks.clear(); - - while (memoryLocation < this.memoryBlock.length) + synchronized(memoryBlock) { - if (memoryBlock[memoryLocation] != 0){ - memoryLocation++; - continue; - } - - int beginningLoc = memoryLocation; - int free = 0; - - while (memoryLocation < this.memoryBlock.length && memoryBlock[memoryLocation] == 0) + while (memoryLocation < this.memoryBlock.length) { - memoryLocation++; - free++; - } + if (memoryBlock[memoryLocation] != 0){ + memoryLocation++; + continue; + } - if (free >= jobSize){ - //System.out.println("Found a block of size " + free + " at " + beginningLoc); - blocks.add(free); - indices.add(beginningLoc); + int beginningLoc = memoryLocation; + int free = 0; + + while (memoryLocation < this.memoryBlock.length && memoryBlock[memoryLocation] == 0) + { + memoryLocation++; + free++; + } + + if (free >= jobSize){ + //System.out.println("Found a block of size " + free + " at " + beginningLoc); + blocks.add(free); + indices.add(beginningLoc); + } } } @@ -148,7 +150,7 @@ public class WorstFitAlgorithm implements baseAlgorithm{ */ public void compact() { - List takenBlocks = new ArrayList<>(); + ArrayList takenBlocks = new ArrayList(); memoryLocation = 0; @@ -158,15 +160,19 @@ public class WorstFitAlgorithm implements baseAlgorithm{ takenBlocks.add(memoryBlock[memoryLocation]); memoryLocation++; } - - for(int i = 0; i < takenBlocks.size(); i++) + synchronized(memoryBlock) { - this.memoryBlock[i] = takenBlocks.get(i).intValue(); + 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++) + synchronized(memoryBlock) { - this.memoryBlock[i] = 0; + for(int i = takenBlocks.size(); i < this.memoryBlock.length; i++) + { + this.memoryBlock[i] = 0; + } } /* @@ -182,9 +188,12 @@ public class WorstFitAlgorithm implements baseAlgorithm{ @Override public void deallocate(int jobSize, int beginningLocation) { - for(int i = beginningLocation; i < jobSize + beginningLocation; i++) + synchronized(memoryBlock) { - memoryBlock[i] = 0; + for(int i = beginningLocation; i < jobSize + beginningLocation; i++) + { + memoryBlock[i] = 0; + } } }