Rev 93 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
import java.nio.ByteBuffer;// Memory manager that operates on the memory poolpublic class MemManager {private byte[] memoryPool; // Byte array representing the memory poolprivate DoubleLinkedList emptyBlockList; // List of free blocks// Constructorpublic MemManager(int poolsize) {// Initialize memory pool and list of free blocksmemoryPool = new byte[poolsize];emptyBlockList = new DoubleLinkedList();// Create the first empty block of the pool sizeLinkedListEntry 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 blocksreturn null;}if ((size + 1) > start.getSize()) {// Size to insert is greater than size of largest free blockreturn null;}while (start != null) {if (start.getNextBlock() == null) {// Current block is last block in list, insert into current blockHandle newHandle = insertDataIntoMemPool(start, space, size);return newHandle;} else if ((size + 1) > start.getNextBlock().getSize()) {// Best fit block found, insert into current blockHandle newHandle = insertDataIntoMemPool(start, space, size);return newHandle;}// Otherwise keep searching for best fit blockstart = 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 addressLinkedListEntry 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 arrayint toRead;if (size < shSize) {toRead = size;} else {toRead = shSize;}// Copy bytes from memory pool to byte arrayByteBuffer memPool = ByteBuffer.wrap(memoryPool, handle.getAddress() + 1, toRead);ByteBuffer dest = ByteBuffer.wrap(space);dest.put(memPool);}// Dump a printout of the freeblock listpublic 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 poolprivate Handle insertDataIntoMemPool(LinkedListEntry entry, byte[] space, int size) {// Insert the data at the address of the given free blockByteBuffer memBuffer = ByteBuffer.wrap(memoryPool, entry.getStartAddress(), size + 1);memBuffer.put((byte)size);memBuffer.put(space);// Create a new free block with remaining free spaceif (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;}}