📄 lib.java
字号:
// PART OF THE MACHINE SIMULATION. DO NOT CHANGE.package nachos.machine;import java.lang.reflect.Constructor;import java.lang.reflect.Method;import java.lang.reflect.Field;import java.lang.reflect.Modifier;import java.security.PrivilegedAction;import java.util.Random;/** * Thrown when an assertion fails. */class AssertionFailureError extends Error { AssertionFailureError() { super(); } AssertionFailureError(String message) { super(message); }}/** * Provides miscellaneous library routines. */public final class Lib { /** * Prevent instantiation. */ private Lib() { } private static Random random = null; /** * Seed the random number generater. May only be called once. * * @param seed the seed for the random number generator. */ public static void seedRandom(long randomSeed) { assert(random == null); random = new Random(randomSeed); } /** * Return a random integer between 0 and <i>range - 1</i>. Must not be * called before <tt>seedRandom()</tt> seeds the random number generator. * * @param range a positive value specifying the number of possible * return values. * @return a random integer in the specified range. */ public static int random(int range) { assert(range > 0); return random.nextInt(range); } /** * Return a random double between 0.0 (inclusive) and 1.0 (exclusive). * * @return a random double between 0.0 and 1.0. */ public static double random() { return random.nextDouble(); } /** * Asserts that <i>expression</i> is <tt>true</tt>. If not, then Nachos * exits with an error message. * * @param expression the expression to assert. */ public static void assert(boolean expression) { if (!expression) throw new AssertionFailureError(); } /** * Asserts that <i>expression</i> is <tt>true</tt>. If not, then Nachos * exits with the specified error message. * * @param expression the expression to assert. * @param message the error message. */ public static void assert(boolean expression, String message) { if (!expression) throw new AssertionFailureError(message); } /** * Asserts that this call is never made. Same as <tt>assert(false)</tt>. */ public static void assertNotReached() { assert(false); } /** * Asserts that this call is never made, with the specified error messsage. * Same as <tt>assert(false, message)</tt>. * * @param message the error message. */ public static void assertNotReached(String message) { assert(false, message); } /** * Print <i>message</i> if <i>flag</i> was enabled on the command line. To * specify which flags to enable, use the -d command line option. For * example, to enable flags a, c, and e, do the following: * * <p> * <pre>nachos -d ace</pre> * * <p> * Nachos uses several debugging flags already, but you are encouraged to * add your own. * * @param flag the debug flag that must be set to print this message. * @param message the debug message. */ public static void debug(char flag, String message) { if (test(flag)) System.out.println(message); } /** * Tests if <i>flag</i> was enabled on the command line. * * @param flag the debug flag to test. * * @return <tt>true</tt> if this flag was enabled on the command line. */ public static boolean test(char flag) { if (debugFlags == null) return false; else if (debugFlags[(int) '+']) return true; else if (flag >= 0 && flag < 0x80 && debugFlags[(int) flag]) return true; else return false; } /** * Enable all the debug flags in <i>flagsString</i>. * * @param flagsString the flags to enable. */ public static void enableDebugFlags(String flagsString) { if (debugFlags == null) debugFlags = new boolean[0x80]; char[] newFlags = flagsString.toCharArray(); for (int i=0; i<newFlags.length; i++) { char c = newFlags[i]; if (c >= 0 && c < 0x80) debugFlags[(int) c] = true; } } /** Debug flags specified on the command line. */ private static boolean debugFlags[]; /** * Read a file, verifying that the requested number of bytes is read, and * verifying that the read operation took a non-zero amount of time. * * @param file the file to read. * @param position the file offset at which to start reading. * @param buf the buffer in which to store the data. * @param offset the buffer offset at which storing begins. * @param length the number of bytes to read. */ public static void strictReadFile(OpenFile file, int position, byte[] buf, int offset, int length) { long startTime = Machine.timer().getTime(); assert(file.read(position, buf, offset, length) == length); long finishTime = Machine.timer().getTime(); assert(finishTime>startTime); } /** * Load an entire file into memory. * * @param file the file to load. * @return an array containing the contents of the entire file, or * <tt>null</tt> if an error occurred. */ public static byte[] loadFile(OpenFile file) { int startOffset = file.tell(); int length = file.length(); if (length < 0) return null; byte[] data = new byte[length]; file.seek(0); int amount = file.read(data, 0, length); file.seek(startOffset); if (amount == length) return data; else return null; } /** * Take a read-only snapshot of a file. * * @param file the file to take a snapshot of. * @return a read-only snapshot of the file. */ public static OpenFile cloneFile(OpenFile file) { OpenFile clone = new ArrayFile(loadFile(file)); clone.seek(file.tell()); return clone; } /** * Convert a short into its little-endian byte string representation. * * @param array the array in which to store the byte string. * @param offset the offset in the array where the string will start. * @param value the value to convert. */ public static void bytesFromShort(byte[] array, int offset, short value) { array[offset+0] = (byte) ((value>>0)&0xFF); array[offset+1] = (byte) ((value>>8)&0xFF); } /** * Convert an int into its little-endian byte string representation. * * @param array the array in which to store the byte string. * @param offset the offset in the array where the string will start. * @param value the value to convert. */ public static void bytesFromInt(byte[] array, int offset, int value) { array[offset+0] = (byte) ((value>>0) &0xFF); array[offset+1] = (byte) ((value>>8) &0xFF); array[offset+2] = (byte) ((value>>16)&0xFF); array[offset+3] = (byte) ((value>>24)&0xFF); } /** * Convert an int into its little-endian byte string representation, and * return an array containing it. * * @param value the value to convert. * @return an array containing the byte string. */ public static byte[] bytesFromInt(int value) { byte[] array = new byte[4]; bytesFromInt(array, 0, value); return array; } /** * Convert an int into a little-endian byte string representation of the * specified length. * * @param array the array in which to store the byte string. * @param offset the offset in the array where the string will start. * @param length the number of bytes to store (must be 1, 2, or 4). * @param value the value to convert. */ public static void bytesFromInt(byte[] array, int offset, int length, int value) { assert(length==1 || length==2 || length==4); switch (length) { case 1: array[offset] = (byte) value; break; case 2: bytesFromShort(array, offset, (short) value); break; case 4: bytesFromInt(array, offset, value); break; } } /** * Convert to a short from its little-endian byte string representation. * * @param array the array containing the byte string. * @param offset the offset of the byte string in the array. * @return the corresponding short value. */ public static short bytesToShort(byte[] array, int offset) { return (short) ((((short) array[offset+0] & 0xFF) << 0) | (((short) array[offset+1] & 0xFF) << 8)); } /** * Convert to an unsigned short from its little-endian byte string * representation. * * @param array the array containing the byte string. * @param offset the offset of the byte string in the array. * @return the corresponding short value. */ public static int bytesToUnsignedShort(byte[] array, int offset) { return (((int) bytesToShort(array, offset)) & 0xFFFF); } /** * Convert to an int from its little-endian byte string representation. * * @param array the array containing the byte string. * @param offset the offset of the byte string in the array. * @return the corresponding int value.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -