unsafe.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 1,063 行 · 第 1/2 页

JAVA
1,063
字号
/**
 * $Id: Unsafe.java,v 1.9 2004/01/20 19:30:18 epr Exp $
 */
package org.jnode.vm;

import org.jnode.assembler.ObjectResolver;
import org.jnode.vm.classmgr.VmClassType;
import org.jnode.vm.classmgr.VmMethod;
import org.jnode.vm.classmgr.VmType;

/**
 * Class that allows directy hardware access.
 * 
 * @author epr
 */
public final class Unsafe {

	public static class UnsafeObjectResolver extends ObjectResolver {

		public UnsafeObjectResolver() {
		}

		/**
		 * @param object
		 * @see org.jnode.assembler.ObjectResolver#addressOf32(Object)
		 * @return int
		 */
		public int addressOf32(Object object) {
			return Unsafe.addressToInt(Unsafe.addressOf(object));
		}

		/**
		 * @param object
		 * @see org.jnode.assembler.ObjectResolver#addressOf64(Object)
		 * @return long
		 */
		public long addressOf64(Object object) {
			return Unsafe.addressToLong(Unsafe.addressOf(object));
		}

		/**
		 * @param ptr
		 * @see org.jnode.assembler.ObjectResolver#objectAt32(int)
		 * @return Object
		 */
		public Object objectAt32(int ptr) {
			return Unsafe.objectAt(Unsafe.intToAddress(ptr));
		}

		/**
		 * @param ptr
		 * @see org.jnode.assembler.ObjectResolver#objectAt64(int)
		 * @return Object
		 */
		public Object objectAt64(long ptr) {
			return Unsafe.objectAt(Unsafe.longToAddress(ptr));
		}

		/**
		 * Gets the address of the given object.
		 * 
		 * @param object
		 * @return Address
		 */
		public Address addressOf(Object object) {
			return Unsafe.addressOf(object);
		}

		/**
		 * Gets the object at a given address.
		 * 
		 * @param ptr
		 * @return Object
		 */
		public Object objectAt(Address ptr) {
			return Unsafe.objectAt(ptr);
		}

		/**
		 * Gets the given address incremented by the given offset.
		 * 
		 * @param address
		 * @param offset
		 * @return Address
		 */
		public Address add(Address address, int offset) {
			return Unsafe.add(address, offset);
		}

		/**
		 * Gets the address of the first element of the given array.
		 * 
		 * @param array
		 * @return The address of the array data.
		 */
		public Address addressOfArrayData(Object array) {
			return Address.addressOfArrayData(array);
		}
	}

	/**
	 * Gets the memory address of the given object.
	 * 
	 * @param object
	 * @return int
	 */
	protected static native Address addressOf(Object object);

	/**
	 * Gets object at the given memory address
	 * 
	 * @param memPtr
	 * @return Object
	 */
	protected static native Object objectAt(Address memPtr);

	/**
	 * Gets the VmClass of the given object.
	 * 
	 * @param object
	 * @return VmClass
	 */
	protected static native VmClassType getVmClass(Object object);

	/**
	 * Gets the Type Information Block of the given object.
	 * 
	 * @param object
	 * @return VmClass
	 */
	protected static native Object[] getTIB(Object object);

	/**
	 * Gets the Super Classes Array of the given object.
	 * 
	 * @param object
	 * @return VmType[]
	 */
	protected static native VmType[] getSuperClasses(Object object);

	/**
	 * Gets all of the flags of the given object.
	 * 
	 * @param object
	 * @return int
	 */
	protected static native int getObjectFlags(Object object);

	/**
	 * Sets all of the flags of the given object.
	 * 
	 * @param object
	 * @param flags
	 */
	protected static native void setObjectFlags(Object object, int flags);

	/**
	 * Gets the GC flags of the given object
	 * 
	 * @param object
	 * @return int
	 * @see org.jnode.classmgr.VMObject#GC_MARKED
	 * @see org.jnode.classmgr.VMObject#GC_FROMSTACK
	 */
	//protected static native int getObjectGCFlags(Object object);

	/**
	 * Sets the GC flags of the given object
	 * 
	 * @param object
	 * @param newFlags
	 *            The new GC flags. Old flags not included are removed.
	 * @see org.jnode.classmgr.VMObject#GC_MARKED
	 * @see org.jnode.classmgr.VMObject#GC_FROMSTACK
	 */
	//protected static native void setObjectGCFlags(Object object, int newFlags);

	/**
	 * Gets a boolean at the given memory address
	 * 
	 * @param memPtr
	 * @return boolean
	 */
	protected static native boolean getBoolean(Address memPtr);

	/**
	 * Gets a boolean at the given offset in a given object.
	 * 
	 * @param object
	 * @param offset
	 * @return boolean
	 */
	protected static native boolean getBoolean(Object object, int offset);

	/**
	 * Gets a 8-bit signed byte at the given memory address
	 * 
	 * @param memPtr
	 * @return byte
	 */
	protected static native byte getByte(Address memPtr);

	/**
	 * Gets a 8-bit signed byte at the given offset in the given object.
	 * 
	 * @param object
	 * @param offset
	 * @return byte
	 */
	protected static native byte getByte(Object object, int offset);

	/**
	 * Gets a 16-bit signed short at the given memory address
	 * 
	 * @param memPtr
	 * @return short
	 */
	protected static native short getShort(Address memPtr);

	/**
	 * Gets a 16-bit signed short at the given memory address
	 * 
	 * @param object
	 * @param offset
	 * @return short
	 */
	protected static native short getShort(Object object, int offset);

	/**
	 * Gets a 16-bit unsigned char at the given memory address
	 * 
	 * @param memPtr
	 * @return char
	 */
	protected static native char getChar(Address memPtr);

	/**
	 * Gets a 16-bit unsigned char at the given memory address
	 * 
	 * @param object
	 * @param offset
	 * @return char
	 */
	protected static native char getChar(Object object, int offset);

	/**
	 * Gets a 32-bit signed int at the given memory address
	 * 
	 * @param memPtr
	 * @return int
	 */
	protected static native int getInt(Address memPtr);

	/**
	 * Gets a 32-bit signed int at the given memory address
	 * 
	 * @param object
	 * @param offset
	 * @return int
	 */
	protected static native int getInt(Object object, int offset);

	/**
	 * Gets a 64-bit signed long at the given memory address
	 * 
	 * @param memPtr
	 * @return long
	 */
	protected static native long getLong(Address memPtr);

	/**
	 * Gets a 64-bit signed long at the given memory address
	 * 
	 * @param object
	 * @param offset
	 * @return long
	 */
	protected static native long getLong(Object object, int offset);

	/**
	 * Gets a float at the given memory address
	 * 
	 * @param memPtr
	 * @return float
	 */
	protected static native float getFloat(Address memPtr);

	/**
	 * Gets a float at the given memory address
	 * 
	 * @param object
	 * @param offset
	 * @return float
	 */
	protected static native float getFloat(Object object, int offset);

	/**
	 * Gets a double at the given memory address
	 * 
	 * @param memPtr
	 * @return double
	 */
	protected static native double getDouble(Address memPtr);

	/**
	 * Gets a double at the given memory address
	 * 
	 * @param object
	 * @param offset
	 * @return double
	 */
	protected static native double getDouble(Object object, int offset);

	/**
	 * Gets a object reference at the given memory address
	 * 
	 * @param memPtr
	 * @return Object
	 */
	protected static native Object getObject(Address memPtr);

	/**
	 * Gets a address at the given memory address
	 * 
	 * @param memPtr
	 * @return Address
	 */
	protected static native Address getAddress(Address memPtr);

	/**
	 * Gets a object reference at the given memory address
	 * 
	 * @param object
	 * @param offset
	 * @return Object
	 */
	protected static native Object getObject(Object object, int offset);

	/**
	 * Gets an address at the given memory address
	 * 
	 * @param object
	 * @param offset
	 * @return Object
	 */
	public static native Address getAddress(Object object, int offset);

	/**
	 * Sets a boolean at a given memory address
	 * 
	 * @param memPtr
	 * @param value
	 */
	protected static native void setBoolean(Address memPtr, boolean value);

	/**
	 * Sets a boolean at a given memory address
	 * 
	 * @param object
	 * @param offset
	 * @param value
	 */
	protected static native void setBoolean(Object object, int offset, boolean value);

	/**
	 * Sets a byte at a given memory address
	 * 
	 * @param memPtr
	 * @param value
	 */
	protected static native void setByte(Address memPtr, byte value);

	/**
	 * Sets a byte at a given memory address While count is greater then 1, the address is incremented and the process repeats.
	 * 
	 * @param memPtr
	 * @param value
	 * @param count
	 */
	protected static native void setBytes(Address memPtr, byte value, int count);

	/**
	 * Perform a bitwise AND of the byte at the given address and the given value. While count is greater then 1, the address is incremented and the process repeats.
	 * 
	 * @param memPtr
	 * @param value
	 * @param count
	 *            The number of times to repeat this operation
	 */
	protected static native void andByte(Address memPtr, byte value, int count);

	/**
	 * Perform a bitwise OR of the byte at the given address and the given value. While count is greater then 1, the address is incremented and the process repeats.
	 * 
	 * @param memPtr
	 * @param value
	 * @param count
	 *            The number of times to repeat this operation
	 */
	protected static native void orByte(Address memPtr, byte value, int count);

	/**
	 * Perform a bitwise XOR of the byte at the given address and the given value While count is greater then 1, the address is incremented and the process repeats.
	 * 
	 * @param memPtr
	 * @param value
	 * @param count
	 *            The number of times to repeat this operation
	 */
	protected static native void xorByte(Address memPtr, byte value, int count);

	/**
	 * Sets a byte at a given memory address
	 * 
	 * @param object
	 * @param offset
	 * @param value
	 */
	protected static native void setByte(Object object, int offset, byte value);

	/**
	 * Sets a short at a given memory address
	 * 
	 * @param memPtr
	 * @param value
	 */
	protected static native void setShort(Address memPtr, short value);

	/**
	 * Sets a short at a given memory address While count is greater then 1, the address is incremented and the process repeats.
	 * 
	 * @param memPtr
	 * @param value
	 * @param count
	 */
	protected static native void setShorts(Address memPtr, short value, int count);

	/**
	 * Perform a bitwise AND of the short at the given address and the given value. While count is greater then 1, the address is incremented and the process repeats.
	 * 
	 * @param memPtr
	 * @param value
	 * @param count
	 *            The number of times to repeat this operation
	 */
	protected static native void andShort(Address memPtr, short value, int count);

	/**
	 * Perform a bitwise OR of the short at the given address and the given value. While count is greater then 1, the address is incremented and the process repeats.
	 * 
	 * @param memPtr
	 * @param value
	 * @param count
	 *            The number of times to repeat this operation
	 */
	protected static native void orShort(Address memPtr, short value, int count);

	/**
	 * Perform a bitwise XOR of the short at the given address and the given value While count is greater then 1, the address is incremented and the process repeats.
	 * 
	 * @param memPtr
	 * @param value
	 * @param count
	 *            The number of times to repeat this operation
	 */
	protected static native void xorShort(Address memPtr, short value, int count);

	/**
	 * Sets a short at a given memory address
	 * 
	 * @param object
	 * @param offset
	 * @param value
	 */
	protected static native void setShort(Object object, int offset, short value);

	/**
	 * Sets a char at a given memory address
	 * 
	 * @param memPtr
	 * @param value
	 */
	protected static native void setChar(Address memPtr, char value);

	/**
	 * Sets a char at a given memory address While count is greater then 1, the address is incremented and the process repeats.
	 * 
	 * @param memPtr
	 * @param value
	 * @param count
	 */
	protected static native void setChars(Address memPtr, char value, int count);

	/**
	 * Perform a bitwise AND of the char at the given address and the given value. While count is greater then 1, the address is incremented and the process repeats.
	 * 
	 * @param memPtr
	 * @param value
	 * @param count
	 *            The number of times to repeat this operation
	 */
	protected static native void andChar(Address memPtr, char value, int count);

	/**
	 * Perform a bitwise OR of the char at the given address and the given value. While count is greater then 1, the address is incremented and the process repeats.
	 * 
	 * @param memPtr
	 * @param value
	 * @param count
	 *            The number of times to repeat this operation
	 */
	protected static native void orChar(Address memPtr, char value, int count);

	/**
	 * Perform a bitwise XOR of the char at the given address and the given value While count is greater then 1, the address is incremented and the process repeats.
	 * 
	 * @param memPtr
	 * @param value
	 * @param count
	 *            The number of times to repeat this operation
	 */
	protected static native void xorChar(Address memPtr, char value, int count);

	/**
	 * Sets a char at a given memory address
	 * 
	 * @param object
	 * @param offset
	 * @param value
	 */
	protected static native void setChar(Object object, int offset, char value);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?