memoryresourceimpl.java

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

JAVA
1,009
字号
	 * @param length
	 */
	public void setFloats(float[] src, int srcOfs, int dstPtr, int length) {
		if (srcOfs < 0) {
			throw new IndexOutOfBoundsException("srcOfs < 0");
		}
		if (length < 0) {
			throw new IndexOutOfBoundsException("length < 0");
		}
		if (srcOfs + length > src.length) {
			throw new IndexOutOfBoundsException("dstOfs + length > dst.length");
		}
		testMemPtr(dstPtr, length * 4);
		Address srcPtr = Unsafe.add(Unsafe.addressOf(src), (VmArray.DATA_OFFSET * slotSize) + (srcOfs * 4));
		Unsafe.copy(srcPtr, Unsafe.add(start, dstPtr), length * 4);
	}

	/**
	 * Sets a long at a given memory address
	 * 
	 * @param memPtr
	 * @param value
	 */
	public void setLong(int memPtr, long value) {
		testMemPtr(memPtr, 8);
		Unsafe.setLong(start, memPtr, value);
	}

	/**
	 * Sets multiple 64-bit signed longs at the given memory address
	 * 
	 * @param src
	 * @param srcOfs
	 * @param dstPtr
	 * @param length
	 */
	public void setLongs(long[] src, int srcOfs, int dstPtr, int length) {
		if (srcOfs < 0) {
			throw new IndexOutOfBoundsException("srcOfs < 0");
		}
		if (length < 0) {
			throw new IndexOutOfBoundsException("length < 0");
		}
		if (srcOfs + length > src.length) {
			throw new IndexOutOfBoundsException("dstOfs + length > dst.length");
		}
		testMemPtr(dstPtr, length * 8);
		Address srcPtr = Unsafe.add(Unsafe.addressOf(src), (VmArray.DATA_OFFSET * slotSize) + (srcOfs * 8));
		Unsafe.copy(srcPtr, Unsafe.add(start, dstPtr), length * 8);
	}

	/**
	 * Sets a double at a given memory address
	 * 
	 * @param memPtr
	 * @param value
	 */
	public void setDouble(int memPtr, double value) {
		testMemPtr(memPtr, 8);
		Unsafe.setDouble(start, memPtr, value);
	}

	/**
	 * Sets multiple 64-bit doubles at the given memory address
	 * 
	 * @param src
	 * @param srcOfs
	 * @param dstPtr
	 * @param length
	 */
	public void setDoubles(double[] src, int srcOfs, int dstPtr, int length) {
		if (srcOfs < 0) {
			throw new IndexOutOfBoundsException("srcOfs < 0");
		}
		if (length < 0) {
			throw new IndexOutOfBoundsException("length < 0");
		}
		if (srcOfs + length > src.length) {
			throw new IndexOutOfBoundsException("dstOfs + length > dst.length");
		}
		testMemPtr(dstPtr, length * 8);
		Address srcPtr = Unsafe.add(Unsafe.addressOf(src), (VmArray.DATA_OFFSET * slotSize) + (srcOfs * 8));
		Unsafe.copy(srcPtr, Unsafe.add(start, dstPtr), length * 8);
	}

	/**
	 * Sets a Object at a given memory address
	 * 
	 * @param memPtr
	 * @param value
	 */
	public void setObject(int memPtr, Object value) {
		if (this.data != null) {
			throw new SecurityException("Cannot set an Object in a byte-array");
		}
		testMemPtr(memPtr, 4);
		Unsafe.setObject(start, memPtr, value);
	}

	/**
	 * Fill the memory at the given memory address with size times 0 bytes.
	 * 
	 * memPtr must be VmObject.SLOT_SIZE aligned
	 * 
	 * size % VmObject.SLOT_SIZE must be 0
	 * 
	 * @param memPtr
	 * @param size
	 */
	public void clear(int memPtr, int size) {
		testMemPtr(memPtr, size);
		Unsafe.clear(Unsafe.add(start, memPtr), size);
	}

	public void copy(int srcMemPtr, int destMemPtr, int size) {
		testMemPtr(srcMemPtr, size);
		testMemPtr(destMemPtr, size);
		Unsafe.copy(Unsafe.add(start, srcMemPtr), Unsafe.add(start, destMemPtr), size);
	}

	/**
	 * Give up this resource. After this method has been called, the resource cannot be used
	 * anymore.
	 */
	public void release() {
		if (data == null) {
			if (!this.released) {
				this.released = true;
				synchronized (getClass()) {
					resources = remove(resources, this);
				}
			}
		}
	}

	private void testMemPtr(int memPtr, int size) {
		if (released) {
			throw new IndexOutOfBoundsException("MemoryResource is released");
		}
		if ((memPtr < 0) || ((memPtr + size) > this.size)) {
			throw new IndexOutOfBoundsException("At " + memPtr + ", this.size=" + this.size);
		}
	}

	/**
	 * Returns the size of this buffer in bytes.
	 * 
	 * @return int
	 */
	public long getSize() {
		return size;
	}

	/**
	 * Gets the address of the first byte of this buffer
	 * @return Address of first byte in buffer
	 */
	public Address getAddress() {
		return start;
	}

	/**
	 * Compare to regions.
	 * 
	 * @param otherRegion
	 * @return a negative integer, zero, or a positive integer as this object is less than, equal
	 *         to, or greater than the specified region. If the regions overlap, 0 is returned.
	 */
	public int compareTo(Region otherRegion) {
		final MemoryResourceImpl other = (MemoryResourceImpl) otherRegion;
		int rc = Unsafe.compare(this.end, other.start);
		if (rc <= 0) {
			// this < other
			return -1;
		}
		rc = Unsafe.compare(this.start, other.end);
		if (rc >= 0) {
			// this > other
			return 1;
		}
		// this overlaps other
		return 0;
	}

	/**
	 * Compare this region with a given address.
	 * 
	 * @param address
	 * @return a negative integer, zero, or a positive integer as this region is less than,
	 *         overlapping, or greater than the address.
	 */
	public int compareTo(Address address) {
		int rc = Unsafe.compare(this.end, address);
		if (rc <= 0) {
			// this < address
			return -1;
		}
		rc = Unsafe.compare(this.start, address);
		if (rc > 0) {
			// this > other
			return 1;
		}
		// this overlaps address
		return 0;
	}

	/**
	 * @param memPtr
	 * @param value
	 * @param count
	 * @see org.jnode.system.MemoryResource#setByte(int, byte, int)
	 */
	public void setByte(int memPtr, byte value, int count) {
		testMemPtr(memPtr, count);
		Unsafe.setBytes(Unsafe.add(start, memPtr), value, count);
	}

	/**
	 * @param memPtr
	 * @param value
	 * @param count
	 * @see org.jnode.system.MemoryResource#setChar(int, char, int)
	 */
	public void setChar(int memPtr, char value, int count) {
		testMemPtr(memPtr, count * 2);
		Unsafe.setChars(Unsafe.add(start, memPtr), value, count);
	}

	/**
	 * @param memPtr
	 * @param value
	 * @param count
	 * @see org.jnode.system.MemoryResource#setDouble(int, double, int)
	 */
	public void setDouble(int memPtr, double value, int count) {
		testMemPtr(memPtr, count * 8);
		Unsafe.setDoubles(Unsafe.add(start, memPtr), value, count);
	}

	/**
	 * @param memPtr
	 * @param value
	 * @param count
	 * @see org.jnode.system.MemoryResource#setFloat(int, float, int)
	 */
	public void setFloat(int memPtr, float value, int count) {
		testMemPtr(memPtr, count * 4);
		Unsafe.setFloats(Unsafe.add(start, memPtr), value, count);
	}

	/**
	 * @param memPtr
	 * @param value
	 * @param count
	 * @see org.jnode.system.MemoryResource#setInt24(int, int, int)
	 */
	public void setInt24(int memPtr, int value, int count) {
		testMemPtr(memPtr, count * 3);
		Unsafe.setInts24(Unsafe.add(start, memPtr), value, count);
	}

	/**
	 * @param memPtr
	 * @param value
	 * @param count
	 * @see org.jnode.system.MemoryResource#setInt(int, int, int)
	 */
	public void setInt(int memPtr, int value, int count) {
		testMemPtr(memPtr, count * 4);
		Unsafe.setInts(Unsafe.add(start, memPtr), value, count);
	}

	/**
	 * @param memPtr
	 * @param value
	 * @param count
	 * @see org.jnode.system.MemoryResource#setLong(int, long, int)
	 */
	public void setLong(int memPtr, long value, int count) {
		testMemPtr(memPtr, count * 8);
		Unsafe.setLongs(Unsafe.add(start, memPtr), value, count);
	}

	/**
	 * @param memPtr
	 * @param value
	 * @param count
	 * @see org.jnode.system.MemoryResource#setObject(int, java.lang.Object, int)
	 */
	public void setObject(int memPtr, Object value, int count) {
		testMemPtr(memPtr, count * slotSize);
		Unsafe.setObjects(Unsafe.add(start, memPtr), value, count);
	}

	/**
	 * @param memPtr
	 * @param value
	 * @param count
	 * @see org.jnode.system.MemoryResource#setShort(int, short, int)
	 */
	public void setShort(int memPtr, short value, int count) {
		testMemPtr(memPtr, count * 2);
		Unsafe.setShorts(Unsafe.add(start, memPtr), value, count);
	}

	/**
	 * @param memPtr
	 * @param value
	 * @param count
	 * @see org.jnode.system.MemoryResource#andByte(int, byte, int)
	 */
	public void andByte(int memPtr, byte value, int count) {
		testMemPtr(memPtr, count);
		Unsafe.andByte(Unsafe.add(start, memPtr), value, count);
	}

	/**
	 * @param memPtr
	 * @param value
	 * @param count
	 * @see org.jnode.system.MemoryResource#andChar(int, char, int)
	 */
	public void andChar(int memPtr, char value, int count) {
		testMemPtr(memPtr, count * 2);
		Unsafe.andChar(Unsafe.add(start, memPtr), value, count);
	}

	/**
	 * @param memPtr
	 * @param value
	 * @param count
	 * @see org.jnode.system.MemoryResource#andInt24(int, int, int)
	 */
	public void andInt24(int memPtr, int value, int count) {
		testMemPtr(memPtr, count * 3);
		Unsafe.andInt24(Unsafe.add(start, memPtr), value, count);
	}

	/**
	 * @param memPtr
	 * @param value
	 * @param count
	 * @see org.jnode.system.MemoryResource#andInt(int, int, int)
	 */
	public void andInt(int memPtr, int value, int count) {
		testMemPtr(memPtr, count * 4);
		Unsafe.andInt(Unsafe.add(start, memPtr), value, count);
	}

	/**
	 * @param memPtr
	 * @param value
	 * @param count
	 * @see org.jnode.system.MemoryResource#andLong(int, long, int)
	 */
	public void andLong(int memPtr, long value, int count) {
		testMemPtr(memPtr, count * 8);
		Unsafe.andLong(Unsafe.add(start, memPtr), value, count);
	}

	/**
	 * @param memPtr
	 * @param value
	 * @param count
	 * @see org.jnode.system.MemoryResource#andShort(int, short, int)
	 */
	public void andShort(int memPtr, short value, int count) {
		testMemPtr(memPtr, count * 2);
		Unsafe.andShort(Unsafe.add(start, memPtr), value, count);
	}

	/**
	 * @param memPtr
	 * @param value
	 * @param count
	 * @see org.jnode.system.MemoryResource#orByte(int, byte, int)
	 */
	public void orByte(int memPtr, byte value, int count) {
		testMemPtr(memPtr, count);
		Unsafe.orByte(Unsafe.add(start, memPtr), value, count);
	}

	/**
	 * @param memPtr
	 * @param value
	 * @param count
	 * @see org.jnode.system.MemoryResource#orChar(int, char, int)
	 */
	public void orChar(int memPtr, char value, int count) {
		testMemPtr(memPtr, count * 2);
		Unsafe.orChar(Unsafe.add(start, memPtr), value, count);
	}

	/**
	 * @param memPtr
	 * @param value
	 * @param count
	 * @see org.jnode.system.MemoryResource#orInt24(int, int, int)
	 */
	public void orInt24(int memPtr, int value, int count) {
		testMemPtr(memPtr, count * 3);
		Unsafe.orInt24(Unsafe.add(start, memPtr), value, count);
	}

	/**
	 * @param memPtr
	 * @param value
	 * @param count
	 * @see org.jnode.system.MemoryResource#orInt(int, int, int)
	 */
	public void orInt(int memPtr, int value, int count) {
		testMemPtr(memPtr, count * 4);
		Unsafe.orInt(Unsafe.add(start, memPtr), value, count);
	}

	/**
	 * @param memPtr
	 * @param value
	 * @param count
	 * @see org.jnode.system.MemoryResource#orLong(int, long, int)
	 */
	public void orLong(int memPtr, long value, int count) {
		testMemPtr(memPtr, count * 8);
		Unsafe.orLong(Unsafe.add(start, memPtr), value, count);
	}

	/**
	 * @param memPtr
	 * @param value
	 * @param count
	 * @see org.jnode.system.MemoryResource#orShort(int, short, int)
	 */
	public void orShort(int memPtr, short value, int count) {
		testMemPtr(memPtr, count * 2);
		Unsafe.orShort(Unsafe.add(start, memPtr), value, count);
	}

	/**
	 * @param memPtr
	 * @param value
	 * @param count
	 * @see org.jnode.system.MemoryResource#xorByte(int, byte, int)
	 */
	public void xorByte(int memPtr, byte value, int count) {
		testMemPtr(memPtr, count);
		Unsafe.xorByte(Unsafe.add(start, memPtr), value, count);
	}

	/**
	 * @param memPtr
	 * @param value
	 * @param count
	 * @see org.jnode.system.MemoryResource#xorChar(int, char, int)
	 */
	public void xorChar(int memPtr, char value, int count) {
		testMemPtr(memPtr, count * 2);
		Unsafe.xorChar(Unsafe.add(start, memPtr), value, count);
	}

	/**
	 * @param memPtr
	 * @param value
	 * @param count
	 * @see org.jnode.system.MemoryResource#xorInt(int, int, int)
	 */
	public void xorInt24(int memPtr, int value, int count) {
		testMemPtr(memPtr, count * 3);
		Unsafe.xorInt24(Unsafe.add(start, memPtr), value, count);
	}

	/**
	 * @param memPtr
	 * @param value
	 * @param count
	 * @see org.jnode.system.MemoryResource#xorInt(int, int, int)
	 */
	public void xorInt(int memPtr, int value, int count) {
		testMemPtr(memPtr, count * 4);
		Unsafe.xorInt(Unsafe.add(start, memPtr), value, count);
	}

	/**
	 * @param memPtr
	 * @param value
	 * @param count
	 * @see org.jnode.system.MemoryResource#xorLong(int, long, int)
	 */
	public void xorLong(int memPtr, long value, int count) {
		testMemPtr(memPtr, count * 8);
		Unsafe.xorLong(Unsafe.add(start, memPtr), value, count);
	}

	/**
	 * @param memPtr
	 * @param value
	 * @param count
	 * @see org.jnode.system.MemoryResource#xorShort(int, short, int)
	 */
	public void xorShort(int memPtr, short value, int count) {
		testMemPtr(memPtr, count * 2);
		Unsafe.xorShort(Unsafe.add(start, memPtr), value, count);
	}
}

⌨️ 快捷键说明

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