diff --git a/group19/1294642551/src/LRU/LRUPageFrame.java b/group19/1294642551/src/LRU/LRUPageFrame.java new file mode 100644 index 0000000000..f2419e0278 --- /dev/null +++ b/group19/1294642551/src/LRU/LRUPageFrame.java @@ -0,0 +1,142 @@ +package LRU; + +/** + * 使用双向链表实现LRU算法 + * @author 12946 + * + */ + +public class LRUPageFrame { + + private static class Node { + + Node prev = null; + Node next = null; + int pageNum; + + Node(Object data) { + if(data != null){ + this.pageNum = (Integer)data; + } + } + } + + private int capacity;//链表总元素个数 + private int currentSize;//链表当前元素个数 + private Node first;// 链表头 + private Node last;// 链表尾 + + + public LRUPageFrame(int capacity) { + + first = new Node(null);//栈底,元素最先放入的地方 + last = new Node(null);//栈顶 + + this.currentSize = 0; + this.capacity = capacity; + + } + + /** + * 获取缓存中对象 + * + * @param key + * @return + */ + public void access(int pageNum) { + Node newNode = new Node(pageNum); + + if(currentSize == capacity){ + Node tempNode = null; + if(getIndex(pageNum) == -1){//不包含 + tempNode = first.next; + }else{//包含 + tempNode = getNode(pageNum); + } + (tempNode.prev).next = tempNode.next; + (tempNode.next).prev = tempNode.prev; + + + newNode.next = last; + newNode.prev = last.prev; + last.prev = newNode; + newNode.prev.next = newNode; + } + + if(currentSize < capacity){ + Node point = first; + for(int i = 0; i < currentSize; i++){ + point = point.next; + } + point.next = newNode; + newNode.prev = point; + + newNode.next = last; + last.prev = newNode; + + currentSize += 1; + } + + + } + + public Node getNode(int data){ + + Node point = first.next; + for(int i = 0; i < capacity; i++){ + if(point.pageNum == data){ + return point; + } + point = point.next; + } + + return null; + + } + + + public int getIndex(int data){ + Node point = first.next; + for(int i = 0; i < capacity; i++){ + if(point.pageNum == data){ + return i; + } + point = point.next; + } + + return -1; + } + +// 原toString +// public String toString(){ +// StringBuilder buffer = new StringBuilder(); +// Node node = first; +// while(node != null){ +// buffer.append(node.pageNum); +// +// node = node.next; +// if(node != null){ +// buffer.append(","); +// } +// } +// return buffer.toString(); +// } + + public String toString(){ + StringBuilder sb = new StringBuilder(); + Node point = last.prev; + while(point != first){ + sb.append(point.pageNum); + if(point.prev != first){ + sb.append(","); + } + point = point.prev; + } + + return sb.toString(); + + } + + + +} diff --git a/group19/1294642551/src/jvm/loader/ClassFileLoader.java b/group19/1294642551/src/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..8e133e40cd --- /dev/null +++ b/group19/1294642551/src/jvm/loader/ClassFileLoader.java @@ -0,0 +1,140 @@ +package jvm.loader; + +import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; + + +import jvm.clz.ClassFile; + + + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) { + + className = className.replace('.', File.separatorChar) + ".class"; + + for(String path : clzPaths){ + String clzFileName = path + File.separatorChar + className; + byte[] codes = loadClassFile(clzFileName); + if(codes != null){ + return codes; + } + } + + return null; + + } + + private byte[] loadClassFile(String clzFileName) { + + try { + InputStream input = new FileInputStream(new File(clzFileName)); + return IOUtils.toByteArray(input); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + return null; + } + + + + public void addClassPath(String path) { + if(this.clzPaths.contains(path)){ + return; + } + + this.clzPaths.add(path); + + } + + + + public String getClassPath(){ + return StringUtils.join(this.clzPaths,";"); + } + + public ClassFile loadClass(String className) { +// byte[] codes = this.readBinaryCode(className); +// ClassFileParser parser = new ClassFileParser(); +// return parser.parse(codes); + return null; + + } + + + + // ------------------------------backup------------------------ + public String getClassPath_V1(){ + + StringBuffer buffer = new StringBuffer(); + for(int i=0;i