/Classwork/CS3114/Project 1/.classpath |
---|
0,0 → 1,6 |
<?xml version="1.0" encoding="UTF-8"?> |
<classpath> |
<classpathentry kind="src" path=""/> |
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/> |
<classpathentry kind="output" path=""/> |
</classpath> |
/Classwork/CS3114/Project 1/.project |
---|
0,0 → 1,17 |
<?xml version="1.0" encoding="UTF-8"?> |
<projectDescription> |
<name>CS3114P1</name> |
<comment></comment> |
<projects> |
</projects> |
<buildSpec> |
<buildCommand> |
<name>org.eclipse.jdt.core.javabuilder</name> |
<arguments> |
</arguments> |
</buildCommand> |
</buildSpec> |
<natures> |
<nature>org.eclipse.jdt.core.javanature</nature> |
</natures> |
</projectDescription> |
/Classwork/CS3114/Project 1/.settings/org.eclipse.jdt.core.prefs |
---|
0,0 → 1,12 |
#Fri Aug 26 18:05:55 EDT 2011 |
eclipse.preferences.version=1 |
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled |
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 |
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve |
org.eclipse.jdt.core.compiler.compliance=1.6 |
org.eclipse.jdt.core.compiler.debug.lineNumber=generate |
org.eclipse.jdt.core.compiler.debug.localVariable=generate |
org.eclipse.jdt.core.compiler.debug.sourceFile=generate |
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error |
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error |
org.eclipse.jdt.core.compiler.source=1.6 |
/Classwork/CS3114/Project 1/ByteStringConverter.class |
---|
Cannot display: file marked as a binary type. |
svn:mime-type = application/octet-stream |
/Classwork/CS3114/Project 1/ByteStringConverter.class |
---|
Property changes: |
Added: svn:mime-type |
+application/octet-stream |
\ No newline at end of property |
/Classwork/CS3114/Project 1/ByteStringConverter.java |
---|
0,0 → 1,40 |
import java.nio.ByteBuffer; |
// Assisting class for MemClient to convert values to a byte array and vice versa |
public class ByteStringConverter { |
// Converts the passed string array into a byte array |
public static byte[] convertToByteArray(int int1, int int2, String str) { |
// Use ByteBuffer to serialize to bytes |
byte[] byteArray = new byte[8 + str.length()]; |
byte[] bstr = str.getBytes(); |
ByteBuffer buffer = ByteBuffer.wrap(byteArray); |
// Insert passed values into the byte array |
buffer.putInt(int1); |
buffer.putInt(int2); |
buffer.put(bstr); |
return byteArray; |
} |
// Converts the byte array into a string array |
public static String[] convertToStringArray(byte[] byteArray) { |
// Array of values to return from byte array |
String[] strArray = new String[3]; |
ByteBuffer buffer = ByteBuffer.wrap(byteArray); |
int int1 = buffer.getInt(); |
int int2 = buffer.getInt(); |
byte[] bstr = new byte[byteArray.length - 8]; |
buffer.get(bstr); |
// Pull values from byte array into string array |
strArray[0] = Integer.toString(int1); |
strArray[1] = Integer.toString(int2); |
strArray[2] = new String(bstr); |
return strArray; |
} |
} |
/Classwork/CS3114/Project 1/DoubleLinkedList.class |
---|
Cannot display: file marked as a binary type. |
svn:mime-type = application/octet-stream |
/Classwork/CS3114/Project 1/DoubleLinkedList.class |
---|
Property changes: |
Added: svn:mime-type |
+application/octet-stream |
\ No newline at end of property |
/Classwork/CS3114/Project 1/DoubleLinkedList.java |
---|
0,0 → 1,138 |
// Class for managing the linked list of free blocks |
public class DoubleLinkedList { |
private LinkedListEntry head; // Start of linked list |
public void insert(LinkedListEntry entry) { |
boolean adjacencyFound = false; // Adjacency flag |
// Create the first entry if there are none existing |
if (head == null) { |
head = entry; |
head.setNextBlock(null); |
head.setPrevBlock(null); |
return; |
} |
LinkedListEntry current = head; |
// Check to see if adjacent entries exist |
while (current != null) { |
if (current.getStartAddress() == entry.getEndAddress() + 1) { |
// Entry is adjacent to start of current |
LinkedListEntry newEntry = new LinkedListEntry(entry.getStartAddress(), |
current.getEndAddress(), entry.getSize() + current.getSize()); |
remove(current.getStartAddress()); |
insert(newEntry); |
adjacencyFound = true; |
break; |
} |
if (current.getEndAddress() == entry.getStartAddress() - 1) { |
// Entry is adjacent to end of current |
LinkedListEntry newEntry = new LinkedListEntry(current.getStartAddress(), |
entry.getEndAddress(), current.getSize() + entry.getSize()); |
remove(current.getStartAddress()); |
insert(newEntry); |
adjacencyFound = true; |
break; |
} |
current = current.getNextBlock(); |
} |
// Otherwise insert entry into sorted list (by size) |
current = head; |
if (!adjacencyFound && (entry.getSize() > current.getSize())) { |
// Insert into beginning of list if size is largest |
entry.setNextBlock(current); |
current.setPrevBlock(entry); |
head = entry; |
return; |
} else if (!adjacencyFound) { |
// Otherwise find location to insert into |
LinkedListEntry prev = head; |
while (prev.getNextBlock() != null) { |
current = prev.getNextBlock(); |
if (entry.getSize() == current.getSize()) { |
// Sort by address location in accending order |
if (entry.getStartAddress() < current.getStartAddress()) { |
// Insert before current |
entry.setNextBlock(current); |
entry.setPrevBlock(prev); |
prev.setNextBlock(entry); |
current.setPrevBlock(entry); |
return; |
} else { |
// Insert after current |
prev = current; |
if (prev.getNextBlock() != null) { |
current = prev.getNextBlock(); |
entry.setNextBlock(current); |
current.setPrevBlock(entry); |
} |
entry.setPrevBlock(prev); |
prev.setNextBlock(entry); |
return; |
} |
} else if (entry.getSize() > current.getSize()) { |
// Insert block between prev and current |
entry.setNextBlock(current); |
entry.setPrevBlock(prev); |
prev.setNextBlock(entry); |
current.setPrevBlock(entry); |
return; |
} else { |
// Otherwise continue searching |
prev = current; |
} |
} |
// Insert into end of list |
entry.setPrevBlock(prev); |
prev.setNextBlock(entry); |
} |
} |
// Deletes an entry from the list, returns true if address is found |
public boolean remove(int address) { |
// First check if list is empty |
if (head == null) { |
return false; |
} else { |
// Otherwise loop through and fine entry |
LinkedListEntry entry = head; |
do { |
if (entry.getStartAddress() == address) { |
if (entry.getPrevBlock() == null && entry.getNextBlock() == null) { |
// Deletes entry (only entry in list) |
head = null; |
} else if (entry.getPrevBlock() == null && entry.getNextBlock() != null) { |
// Deletes entry (first in list) |
head = entry.getNextBlock(); |
head.setPrevBlock(null); |
} else if (entry.getPrevBlock() != null && entry.getNextBlock() == null) { |
// Deletes entry (last in list) |
entry.getPrevBlock().setNextBlock(null); |
} else { |
// Deletes entry (in between two entries) |
LinkedListEntry prev = entry.getPrevBlock(); |
prev.setNextBlock(entry.getNextBlock()); |
entry.getNextBlock().setPrevBlock(prev); |
} |
return true; |
} |
entry = entry.getNextBlock(); |
} while (entry != null); |
} |
return false; |
} |
// Returns an entry from the list with the passed address |
public LinkedListEntry getFirstEntry() { |
// First check if list is empty |
if (head == null) { |
return null; |
} else { |
// Otherwise return head entry |
return head; |
} |
} |
} |
/Classwork/CS3114/Project 1/Handle.class |
---|
Cannot display: file marked as a binary type. |
svn:mime-type = application/octet-stream |
/Classwork/CS3114/Project 1/Handle.class |
---|
Property changes: |
Added: svn:mime-type |
+application/octet-stream |
\ No newline at end of property |
/Classwork/CS3114/Project 1/Handle.java |
---|
0,0 → 1,17 |
// Handle passed to and from MemManager |
public class Handle { |
private int address; // Location of block in memory pool |
public Handle(int addr) { |
this.address = addr; |
} |
public int getAddress() { |
return address; |
} |
public void setAddress(int address) { |
this.address = address; |
} |
} |
/Classwork/CS3114/Project 1/LinkedListEntry.class |
---|
Cannot display: file marked as a binary type. |
svn:mime-type = application/octet-stream |
/Classwork/CS3114/Project 1/LinkedListEntry.class |
---|
Property changes: |
Added: svn:mime-type |
+application/octet-stream |
\ No newline at end of property |
/Classwork/CS3114/Project 1/LinkedListEntry.java |
---|
0,0 → 1,46 |
// Individual entries in the linked list |
public class LinkedListEntry { |
private int startAddress; // Start address of free block |
private int endAddress; // End address of free block |
private int size; // Total size of free block |
private LinkedListEntry nextBlock; // Pointer to next block in list |
private LinkedListEntry prevBlock; // Pointer to previous block in list |
// Constructor |
public LinkedListEntry(int start, int end, int size){ |
this.startAddress = start; |
this.endAddress = end; |
this.size = size; |
setNextBlock(null); |
setPrevBlock(null); |
} |
public int getStartAddress() { |
return startAddress; |
} |
public int getEndAddress() { |
return endAddress; |
} |
public int getSize() { |
return size; |
} |
public LinkedListEntry getNextBlock() { |
return nextBlock; |
} |
public void setNextBlock(LinkedListEntry nextBlock) { |
this.nextBlock = nextBlock; |
} |
public LinkedListEntry getPrevBlock() { |
return prevBlock; |
} |
public void setPrevBlock(LinkedListEntry prevBlock) { |
this.prevBlock = prevBlock; |
} |
} |
/Classwork/CS3114/Project 1/MemClient.class |
---|
Cannot display: file marked as a binary type. |
svn:mime-type = application/octet-stream |
/Classwork/CS3114/Project 1/MemClient.class |
---|
Property changes: |
Added: svn:mime-type |
+application/octet-stream |
\ No newline at end of property |
/Classwork/CS3114/Project 1/MemClient.java |
---|
0,0 → 1,126 |
// Memory client that interfaces with MemManager |
public class MemClient { |
private Handle[] recordArray; // Client's array of handles |
private MemManager memoryManager; // Instance of a memory manager |
public MemClient(int input_numRecs, int input_poolSize) { |
// Initialize the record array and create a new memory manager |
recordArray = new Handle[input_numRecs]; |
memoryManager = new MemManager(input_poolSize); |
} |
public void processLine(String line) { |
// Split the passed line into tokens |
String delim = "[ ]+"; |
String[] tokens = line.split(delim); |
// Parse tokens to determine what action to take |
if (tokens[0].compareToIgnoreCase("insert") == 0) { |
processInsertCommand(tokens, recordArray, memoryManager); |
} else if (tokens[0].compareToIgnoreCase("remove") == 0) { |
processRemoveCommand(tokens, recordArray, memoryManager); |
} else if (tokens[0].compareToIgnoreCase("print") == 0) { |
processPrintCommand(tokens, recordArray, memoryManager); |
} else { |
System.out.printf("\"%s\" command is not supported\n", tokens[0]); |
} |
} |
public void processInsertCommand(String[] tokens, Handle[] recordArray, MemManager memoryManager) { |
// Process insert command |
if (tokens.length != 5) { |
System.out.printf("Arguments must be in format \"insert recnum x y name\"\n"); |
} else { |
try { |
// Check for array bounds for record array |
int recordID = Integer.parseInt(tokens[1]); |
if (recordID < 0 || recordID > recordArray.length - 1) { |
System.out.printf("Arguments out of bound\n"); |
} else { |
// Delete record in record array if it exists |
if (recordArray[recordID] != null) { |
memoryManager.remove(recordArray[recordID]); |
recordArray[recordID] = null; |
} |
// Serialize to byte array and send to memory manager |
int xpos = Integer.parseInt(tokens[2]); |
int ypos = Integer.parseInt(tokens[3]); |
byte[] buffer = ByteStringConverter.convertToByteArray(xpos, ypos, tokens[4]); |
Handle retHandle = memoryManager.insert(buffer, buffer.length); |
if (retHandle == null) { |
System.out.printf("No free space is avaliable to store the record\n"); |
} else { |
// Save returned handle to the record array |
recordArray[recordID] = retHandle; |
} |
} |
} catch (NumberFormatException e) { |
System.out.printf("Error attempting to parse int values\n"); |
} |
} |
} |
public void processRemoveCommand(String[] tokens, Handle[] recordArray, MemManager memoryManager) { |
// Process remove command |
if (tokens.length != 2) { |
System.out.printf("A record ID must be supplied\n"); |
} else { |
try { |
int recordID = Integer.parseInt(tokens[1]); |
if (recordID < 0 || recordID > recordArray.length - 1) { |
System.out.printf("Arguments out of bound\n"); |
} else { |
// Remove record from record array if it exists |
if (recordArray[recordID] != null) { |
memoryManager.remove(recordArray[recordID]); |
recordArray[recordID] = null; |
} else { |
System.out.printf("No record found for record ID %d\n", recordID); |
} |
} |
} catch (NumberFormatException e) { |
System.out.printf("Error attempting to parse int values\n"); |
} |
} |
} |
public void processPrintCommand(String[] tokens, Handle[] recordArray, MemManager memoryManager) { |
byte[] fromPool = new byte[255]; |
// Process print command |
if (tokens.length == 1) { |
// Print out every entry in recordArray |
for (int i = 0; i < recordArray.length; i++) { |
if (recordArray[i] != null) { |
fromPool = new byte[255]; |
System.out.printf("Record #%d at address %d: ", i, recordArray[i].getAddress()); |
memoryManager.get(fromPool, recordArray[i], 255); |
String[] strArray = ByteStringConverter.convertToStringArray(fromPool); |
System.out.printf("%s (%s,%s)\n", strArray[2].trim(), strArray[0], strArray[1]); |
} |
} |
// Print out free block list |
memoryManager.dump(); |
} else if (tokens.length == 2) { |
// Print out specified record |
try { |
int recordID = Integer.parseInt(tokens[1]); |
if (recordID < 0 || recordID > recordArray.length - 1) { |
System.out.printf("Arguments out of bound\n"); |
} else { |
if (recordArray[recordID] != null) { |
// Get and print out record |
memoryManager.get(fromPool, recordArray[recordID], 255); |
String[] strArray = ByteStringConverter.convertToStringArray(fromPool); |
System.out.printf("Record #%d: %s (%s,%s)\n", recordID, strArray[2].trim(), strArray[0], strArray[1]); |
} else { |
System.out.printf("No record found for record ID %d\n", recordID); |
} |
} |
} catch (NumberFormatException e) { |
System.out.printf("Error attempting to parse int values\n"); |
} |
} else { |
System.out.printf("Invalud number of arguments\n"); |
} |
} |
} |
/Classwork/CS3114/Project 1/MemManager.class |
---|
Cannot display: file marked as a binary type. |
svn:mime-type = application/octet-stream |
/Classwork/CS3114/Project 1/MemManager.class |
---|
Property changes: |
Added: svn:mime-type |
+application/octet-stream |
\ No newline at end of property |
/Classwork/CS3114/Project 1/MemManager.java |
---|
0,0 → 1,117 |
import java.nio.ByteBuffer; |
// Memory manager that operates on the memory pool |
public class MemManager { |
private byte[] memoryPool; // Byte array representing the memory pool |
private DoubleLinkedList emptyBlockList; // List of free blocks |
// Constructor |
public MemManager(int poolsize) { |
// Initialize memory pool and list of free blocks |
memoryPool = new byte[poolsize]; |
emptyBlockList = new DoubleLinkedList(); |
// Create the first empty block of the pool size |
LinkedListEntry newEntry = new LinkedListEntry(0, poolsize - 1, poolsize); |
emptyBlockList.insert(newEntry); |
} |
// Insert a record and return its position handle. |
// space contains the record to be inserted, of length size. |
public Handle insert(byte[] space, int size) { |
LinkedListEntry start = emptyBlockList.getFirstEntry(); |
if (start == null) { |
// There are no free blocks |
return null; |
} |
if ((size + 1) > start.getSize()) { |
// Size to insert is greater than size of largest free block |
return null; |
} |
while (start != null) { |
if (start.getNextBlock() == null) { |
// Current block is last block in list, insert into current block |
Handle newHandle = insertDataIntoMemPool(start, space, size); |
return newHandle; |
} else if ((size + 1) > start.getNextBlock().getSize()) { |
// Best fit block found, insert into current block |
Handle newHandle = insertDataIntoMemPool(start, space, size); |
return newHandle; |
} |
// Otherwise keep searching for best fit block |
start = start.getNextBlock(); |
} |
return null; |
} |
// Free a block at address. Merge adjacent blocks if appropriate. |
public void remove(Handle handle) { |
short size = getDataBlockSize(handle.getAddress()); |
// Needs to create a new empty block at the specified address |
LinkedListEntry newEntry = new LinkedListEntry(handle.getAddress(), handle.getAddress() + size, size + 1); |
emptyBlockList.insert(newEntry); |
} |
// Return the record with handle posHandle, up to size bytes. |
// Data to be returned is copied to space. |
public void get(byte[] space, Handle handle, int size) { |
short shSize = getDataBlockSize(handle.getAddress()); |
// Find max number of bytes to copy to byte array |
int toRead; |
if (size < shSize) { |
toRead = size; |
} else { |
toRead = shSize; |
} |
// Copy bytes from memory pool to byte array |
ByteBuffer memPool = ByteBuffer.wrap(memoryPool, handle.getAddress() + 1, toRead); |
ByteBuffer dest = ByteBuffer.wrap(space); |
dest.put(memPool); |
} |
// Dump a printout of the freeblock list |
public void dump() { |
LinkedListEntry head = emptyBlockList.getFirstEntry(); |
while (head != null) { |
System.out.printf("Empty block at address %d of size %d\n", head.getStartAddress(), head.getSize()); |
head = head.getNextBlock(); |
} |
} |
// Returns the value of the first byte for the data entry (size) |
private short getDataBlockSize(int address) { |
byte[] shortArr = new byte[2]; |
ByteBuffer shortBuffer = ByteBuffer.wrap(shortArr, 1, 1); |
shortBuffer.put(memoryPool[address]); |
shortBuffer = ByteBuffer.wrap(shortArr); |
short size = shortBuffer.getShort(); |
return size; |
} |
// Copies the passed byte array into a free block in the memory pool |
private Handle insertDataIntoMemPool(LinkedListEntry entry, byte[] space, int size) { |
// Insert the data at the address of the given free block |
ByteBuffer memBuffer = ByteBuffer.wrap(memoryPool, entry.getStartAddress(), size + 1); |
memBuffer.put((byte)size); |
memBuffer.put(space); |
// Create a new free block with remaining free space |
if (entry.getSize() - (size + 1) != 0) { |
int startAddress = entry.getStartAddress() + size + 1; |
int newSize = entry.getSize() - (size + 1); |
int endAddress = startAddress + newSize - 1; |
LinkedListEntry newEntry = new LinkedListEntry(startAddress, endAddress, newSize); |
emptyBlockList.remove(entry.getStartAddress()); |
emptyBlockList.insert(newEntry); |
} else { |
emptyBlockList.remove(entry.getStartAddress()); |
} |
Handle newHandle = new Handle(entry.getStartAddress()); |
return newHandle; |
} |
} |
/Classwork/CS3114/Project 1/P1 Notes.txt |
---|
0,0 → 1,18 |
P1 Notes |
Components: |
- Array of bytes representing the memory pool |
= byte[size of pool] |
- Doubly linked list that keeps track of the free blocks in the memory pool |
IE, holds the address and size of each free block in the memory pool |
= int (location of block start) |
= int (size of block) |
= handle (next handle) |
= handle (previous handle) |
- Record array that stores the 'handles' to the stored data records in the memory pool |
- Handle class |
= int (location of start address in memory pool) |
/Classwork/CS3114/Project 1/P1.pdf |
---|
Cannot display: file marked as a binary type. |
svn:mime-type = application/octet-stream |
/Classwork/CS3114/Project 1/P1.pdf |
---|
Property changes: |
Added: svn:mime-type |
+application/octet-stream |
\ No newline at end of property |
/Classwork/CS3114/Project 1/P1testOutput.txt |
---|
0,0 → 1,215 |
> Let's assume that this was called with a 10 slot array and a 175 |
> byte memory pool. |
> Freelist: [0, 174] (175 bytes) |
insert 0 4240 7345 Albany |
> 15 bytes taken |
> Freelist: [15, 174] (160 bytes) |
insert 1 3455 13836 Albuquerque |
> 20 bytes taken |
> Freelist: [35, 174] (140 bytes) |
insert 2 3511 10150 Amarillo |
> 17 bytes taken |
> Freelist: [52, 174] (123 bytes) |
insert 3 6113 14954 Anchorage |
> 18 bytes taken |
> Freelist: [70, 174] (105 bytes) |
insert 4 3345 8423 Atlanta |
> 16 bytes taken |
> Freelist: [86, 174] (89 bytes) |
remove 4 |
> This frees up the 16 bytes at the end, which merges with the free block |
> Freelist: [70, 174] (105 bytes) |
> 0: (4240, 7345) Albany |
> 1: (3455, 13836) Albuquerque |
> 2: (3511, 10150) Amarillo |
> 3: (6113, 14954) Anchorage |
> 4: [no record] |
> 5: [no record] |
print 0 |
> 4240 7345 Albany |
print 1 |
> 3455 13836 Albuquerque |
print 2 |
> 3511 10150 Amarillo |
print 3 |
> 6113 14954 Anchorage |
print 4 |
> Slot 4 is empty |
print 5 |
> Slot 5 is empty |
insert 5 2515 5740 Asuncion_Paraguay |
> 26 bytes taken |
> Freelist: [96, 174] (79 bytes) |
print 5 |
> 2515 5740 Asuncion_Paraguay |
insert 6 4447 11750 Baker |
> 14 bytes taken |
> Freelist: [110, 174] (65 bytes) |
insert 7 3918 7638 Baltimore |
> 18 bytes taken |
> Freelist: [128, 174] (47 bytes) |
insert 7 3918 7638 Baltimore |
> Since this is overwriting slot 7, the old message is deleted, |
> freeing 18 bytes, which happen to be at the end and which merge with |
> the existing free block. |
> 18 bytes are then taken for the new message |
> Freelist: [128, 174] (47 bytes) |
insert 8 3955 11625 Beijing_China |
> 22 bytes taken |
> Freelist: [150, 174] (25 bytes) |
insert 9 3330 8650 Birmingham |
> 19 bytes taken |
> Freelist: [169, 174] (6 bytes) |
> [0] --> 0: (4240, 7345) Albany |
> [15] --> 1: (3455, 13836) Albuquerque |
> [35] --> 2: (3511, 10150) Amarillo |
> [52] --> 3: (6113, 14954) Anchorage |
> 4: [no record] |
> [70] --> 5: (2515, 5740) Asuncion_Paraguay |
> [96] --> 6: (4447, 11750) Baker |
> [110] --> 7: (3918, 7638) Baltimore |
> [128] --> 8: (3955, 11625) Beijing_China |
> [150] --> 9: (3330, 8650) Birmingham |
remove 9 |
> This frees 19 bytes, which merge with the free block at the end. |
> Freelist: [150, 174] (25 bytes) |
print 9 |
> Slot 9 is empty |
insert 9 3355 1822 Cape_Town_South_Africa |
> This requires 31 bytes, but there is not a block of this size |
> available. Therefore, nothing is inserted (or otherwise changed) |
> Freelist: [150, 174] (25 bytes) |
print 9 |
> Slot 9 is empty |
remove 3 |
> Free up 18 bytes |
> Freelist: [52, 69] (18 bytes) |
> [150, 174] (25 bytes) |
remove 4 |
> Nothing happens since Slot 4 is empty |
remove 6 |
> Free up 14 bytes starting at position 96 |
> Freelist: [52, 69] (18 bytes); |
> [96, 109] (14 bytes); |
> [150, 174] (25 bytes) |
remove 7 |
> Free up 18 bytes starting at position 110. Since this is adjacent to |
> an existing free block, they merge. |
> Freelist: [52, 69] (18 bytes); |
> [96, 127] (32 bytes); |
> [150, 174] (25 bytes) |
remove 7 |
> Since Slot 7 is empty, nothing happens. |
> Freelist: [52, 69] (18 bytes); |
> [96, 127] (32 bytes); |
> [150, 174] (25 bytes) |
remove 8 |
> Free 22 bytes starting at postion 128. Since there are free blocks |
> to either side, all three merge together. |
> Freelist: [52, 69] (18 bytes); |
> [96, 174] (79 bytes); |
insert 7 2308 8223 Havana_Cuba |
> 20 bytes taken. The first free block is not big enough, so take from |
> the second. |
> Freelist: [52, 69] (18 bytes); |
> [116, 174] (59 bytes); |
insert 7 2946 10634 Chongqing_China |
> First the 20 bytes from the message currently stored is freed, whose |
> space merges with the block at the end of the list. Then 24 bytes |
> are reserved from the second block, since the first is not big enough. |
> Freelist: [52, 69] (18 bytes); |
> [120, 174] (55 bytes); |
insert 8 616 10648 Jakarta_Indonesia |
> 26 bytes taken from the second block |
> Freelist: [52, 69] (18 bytes); |
> [146, 174] (29 bytes); |
> Print out the current records in the array |
> [0] --> 0: 4240 7345 Albany |
> [15] --> 1: 3455 13836 Albuquerque |
> [35] --> 2: 3511 10150 Amarillo |
> 3: [no record] |
> 4: [no record] |
> [70] --> 5: (2515, 5740) Asuncion_Paraguay |
> 6: [no record] |
> [96] --> 7: 2946 10634 Chongqing_China |
> [120] --> 8: 616 10648 Jakarta_Indonesia |
> 9: [no record] |
remove 8 |
> Free 26 bytes and merge with second block. |
> Freelist: [52, 69] (18 bytes); |
> [120, 174] (55 bytes); |
/Classwork/CS3114/Project 1/Schedule.xls |
---|
Cannot display: file marked as a binary type. |
svn:mime-type = application/octet-stream |
/Classwork/CS3114/Project 1/Schedule.xls |
---|
Property changes: |
Added: svn:mime-type |
+application/octet-stream |
\ No newline at end of property |
/Classwork/CS3114/Project 1/memman.class |
---|
Cannot display: file marked as a binary type. |
svn:mime-type = application/octet-stream |
/Classwork/CS3114/Project 1/memman.class |
---|
Property changes: |
Added: svn:mime-type |
+application/octet-stream |
\ No newline at end of property |
/Classwork/CS3114/Project 1/memman.java |
---|
0,0 → 1,108 |
/* |
* CS 3114 Project 1 |
* Author: Kevin Lee |
* Compiler: Eclipse 3.7.0 |
* Operating System: Windows 7 x64 |
* Date Completed: 9/1/2011 |
* |
* The following program is designed to provide an interface to |
* client side code that allows the client to store and retrieve |
* byte arrays of data. The memory manager has no information on |
* the actual data to be stored, other than the size of the byte |
* array. The memory manager allocates a memory pool when the |
* object is created, and executes all operations on the memory |
* pool. The memory manager also keeps a doubly linked list of |
* empty blocks from which it finds the best fit block to store |
* the passed data in. The client is responsible for parsing the |
* command file as well as creating the byte array holding the |
* data to be stored. The client also has a record array which |
* stores handles passed back from the memory manager from which |
* it uses to retrieve stored data from the memory manager. |
* |
* The stored data is saved in the following format: |
* byte[0] = size of data record |
* byte[1-4] = int (x-coordinate) |
* byte[5-8] = int (y-coordinate) |
* byte[9-?] = string (name of city name) |
* The first byte is only used by the memory manager and is not |
* returned to the client when the data record is retrieved. |
* |
* On my honor: |
* |
* - I have not used source code obtained from another student, |
* or any other unauthorized source, either modified or |
* unmodified. |
* |
* - All source code and documentation used in my program is |
* either my original work, or was derived by me from the |
* source code published in the textbook for this course. |
* |
* - I have not discussed coding details about this project with |
* anyone other than my partner (in the case of a joint |
* submission), instructor, ACM/UPE tutors or the TAs assigned |
* to this course. I understand that I may discuss the concepts |
* of this program with other students, and that another student |
* may help me debug my program so long as neither of us writes |
* anything during the discussion or modifies any computer file |
* during the discussion. I have violated neither the spirit nor |
* letter of this restriction. |
*/ |
import java.io.*; |
public class memman { |
public static void main(String[] args) { |
int input_poolSize; // Size of memory pool to allocate |
int input_numRecs; // Total number of records to hold |
String input_commandFile; // Location of file to parse |
// 3 arguments must be supplied to the program |
if (args.length != 3) { |
System.out.println("3 Arguments must be supplied!"); |
return; |
} |
// Parses passed arguments into member variables |
try { |
input_poolSize = Integer.parseInt(args[0]); |
input_numRecs = Integer.parseInt(args[1]); |
input_commandFile = args[2]; |
} catch (NumberFormatException e) { |
System.out.printf("The first two arguments must be an integer"); |
return; |
} |
// Handles error if the passed arguments are negative numbers |
if (input_poolSize < 0 || input_numRecs < 0) { |
System.out.printf("Int arguments cannot be negative"); |
return; |
} |
// Check to make sure the command file exists |
File cmdFile = new File(input_commandFile); |
if (!cmdFile.exists()) { |
System.out.printf("Command file not found"); |
return; |
} |
MemClient memoryClient = new MemClient(input_numRecs, input_poolSize); |
// Parse each command with the memory client |
try { |
BufferedReader reader = new BufferedReader(new FileReader(cmdFile)); |
String cin; |
while ((cin = reader.readLine()) != null) { |
cin = cin.trim(); |
if (cin.length() != 0) { |
memoryClient.processLine(cin); |
} |
} |
} catch (FileNotFoundException e) { |
System.out.printf("Command file not found"); |
return; |
} catch (IOException e) { |
System.out.printf("Unable to read input file"); |
return; |
} |
} |
} |
/Classwork/CS3114/Project 1/p1_testData.txt |
---|
0,0 → 1,54 |
insert 0 4240 7345 Albany |
insert 1 3455 13836 Albuquerque |
insert 2 3511 10150 Amarillo |
insert 3 6113 14954 Anchorage |
insert 4 3345 8423 Atlanta |
remove 4 |
print 0 |
print 1 |
print 2 |
print 3 |
print 4 |
print 5 |
insert 5 2515 5740 Asuncion_Paraguay |
print 5 |
insert 6 4447 11750 Baker |
insert 7 3918 7638 Baltimore |
insert 7 3918 7638 Baltimore |
insert 8 3955 11625 Beijing_China |
insert 9 3330 8650 Birmingham |
remove 9 |
print 9 |
insert 9 3355 1822 Cape_Town_South_Africa |
print 9 |
remove 3 |
remove 4 |
remove 6 |
remove 7 |
remove 7 |
remove 8 |
insert 7 2308 8223 Havana_Cuba |
insert 7 2946 10634 Chongqing_China |
insert 8 616 10648 Jakarta_Indonesia |
remove 8 |