⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmalloc.java

📁 学习JAVA编程的示例
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * Indirect the C pointer to <code>malloc</code> space, a la
     * <code>CPtr.getByte</code>.  But this method performs a bounds checks to
     * ensure that the indirection does not cause memory outside the
     * <code>malloc</code>ed space to be accessed.
     *
     * @see CPtr#getByte(int)
     */
    public byte getByte(int offset) {
        boundsCheck(offset, 1);
	return super.getByte(offset);
    }

    /**
     * Indirect the C pointer to <code>malloc</code> space, a la
     * <code>CPtr.getShort</code>.  But this method performs a bounds checks to
     * ensure that the indirection does not cause memory outside the
     * <code>malloc</code>ed space to be accessed.
     *
     * @see CPtr#getShort(int)
     */
    public short getShort(int offset) {
        boundsCheck(offset, 2);
	return super.getShort(offset);
    }

    /**
     * Indirect the C pointer to <code>malloc</code> space, a la
     * <code>CPtr.getInt</code>.  But this method performs a bounds checks to
     * ensure that the indirection does not cause memory outside the
     * <code>malloc</code>ed space to be accessed.
     *
     * @see CPtr#getInt(int)
     */
    public int getInt(int offset) {
        boundsCheck(offset, 4);
	return super.getInt(offset);
    }

    /**
     * Indirect the C pointer to <code>malloc</code> space, a la
     * <code>CPtr.getLong</code>.  But this method performs a bounds checks to
     * ensure that the indirection does not cause memory outside the
     * <code>malloc</code>ed space to be accessed.
     *
     * @see CPtr#getLong(int)
     */
    public long getLong(int offset) {
        boundsCheck(offset, 8);
	return super.getLong(offset);
    }

    /**
     * Indirect the C pointer to <code>malloc</code> space, a la
     * <code>CPtr.getFloat</code>.  But this method performs a bounds checks to
     * ensure that the indirection does not cause memory outside the
     * <code>malloc</code>ed space to be accessed.
     *
     * @see CPtr#getFloat(int)
     */
    public float getFloat(int offset) {
        boundsCheck(offset, 4);
	return super.getFloat(offset);
    }

    /**
     * Indirect the C pointer to <code>malloc</code> space, a la
     * <code>CPtr.getDouble</code>.  But this method performs a bounds checks
     * to ensure that the indirection does not cause memory outside the
     * <code>malloc</code>ed space to be accessed.
     *
     * @see CPtr#getDouble(int)
     */
    public double getDouble(int offset) {
        boundsCheck(offset, 8);
	return super.getDouble(offset);
    }

    /**
     * Indirect the C pointer to <code>malloc</code> space, a la
     * <code>CPtr.getCPtr</code>.  But this method performs a bounds checks to
     * ensure that the indirection does not cause memory outside the
     * <code>malloc</code>ed space to be accessed.
     *
     * @see CPtr#getCPtr(int)
     */
    public CPtr getCPtr(int offset) {
        boundsCheck(offset, SIZE);
	return super.getCPtr(offset);
    }

    /**
     * Indirect the C pointer to <code>malloc</code> space, a la
     * <code>CPtr.getString</code>.  But this method performs a bounds checks
     * to ensure that the indirection does not cause memory outside the
     * <code>malloc</code>ed space to be accessed.
     *
     * @see CPtr#getString(int)
     */
    public String getString(int offset) {
        boundsCheck(offset, 0);
	return super.getString(offset);
    }

    /**
     * Indirect the C pointer to <code>malloc</code> space, a la
     * <code>CPtr.setByte</code>.  But this method performs a bounds checks to
     * ensure that the indirection does not cause memory outside the
     * <code>malloc</code>ed space to be accessed.
     *
     * @see CPtr#setByte(int)
     */
    public void setByte(int offset, byte value) {
        boundsCheck(offset, 1);
	super.setByte(offset, value);
    }

    /**
     * Indirect the C pointer to <code>malloc</code> space, a la
     * <code>CPtr.setShort</code>.  But this method performs a bounds checks to
     * ensure that the indirection does not cause memory outside the
     * <code>malloc</code>ed space to be accessed.
     *
     * @see CPtr#setShort(int)
     */
    public void setShort(int offset, short value) {
        boundsCheck(offset, 2);
	super.setShort(offset, value);
    }

    /**
     * Indirect the C pointer to <code>malloc</code> space, a la
     * <code>CPtr.setInt</code>.  But this method performs a bounds checks to
     * ensure that the indirection does not cause memory outside the
     * <code>malloc</code>ed space to be accessed.
     *
     * @see CPtr#setInt(int)
     */
    public void setInt(int offset, int value) {
        boundsCheck(offset, 4);
	super.setInt(offset, value);
    }

    /**
     * Indirect the C pointer to <code>malloc</code> space, a la
     * <code>CPtr.setLong</code>.  But this method performs a bounds checks to
     * ensure that the indirection does not cause memory outside the
     * <code>malloc</code>ed space to be accessed.
     *
     * @see CPtr#setLong(int)
     */
    public void setLong(int offset, long value) {
        boundsCheck(offset, 8);
	super.setLong(offset, value);
    }

    /**
     * Indirect the C pointer to <code>malloc</code> space, a la
     * <code>CPtr.setFloat</code>.  But this method performs a bounds checks to
     * ensure that the indirection does not cause memory outside the
     * <code>malloc</code>ed space to be accessed.
     *
     * @see CPtr#setFloat(int)
     */
    public void setFloat(int offset, float value) {
        boundsCheck(offset, 4);
	super.setFloat(offset, value);
    }

    /**
     * Indirect the C pointer to <code>malloc</code> space, a la
     * <code>CPtr.setDouble</code>.  But this method performs a bounds checks
     * to ensure that the indirection does not cause memory outside the
     * <code>malloc</code>ed space to be accessed.
     *
     * @see CPtr#setDouble(int)
     */
    public void setDouble(int offset, double value) {
        boundsCheck(offset, 8);
	super.setDouble(offset, value);
    }

    /**
     * Indirect the C pointer to <code>malloc</code> space, a la
     * <code>CPtr.setCPtr</code>.  But this method performs a bounds checks to
     * ensure that the indirection does not cause memory outside the
     * <code>malloc</code>ed space to be accessed.
     *
     * @see CPtr#setCPtr(int)
     */
    public void setCPtr(int offset, CPtr value) {
        boundsCheck(offset, SIZE);
	super.setCPtr(offset, value);
    }

    /**
     * Indirect the C pointer to <code>malloc</code> space, a la
     * <code>CPtr.setString</code>.  But this method performs a bounds checks
     * to ensure that the indirection does not cause memory outside the
     * <code>malloc</code>ed space to be accessed.
     *
     * @see CPtr#setString(int)
     */
    public void setString(int offset, String value) {
        byte[] bytes = value.getBytes();
	int length = bytes.length;
        boundsCheck(offset, length + 1);
	super.copyIn(offset, bytes, 0, length);
	super.setByte(offset + length, (byte)0);
    }

    /* Size of the malloc'ed space. */
    private int size;

    /* Call the real C malloc. */
    private static native long malloc(int size);

    /* Call the real C free. */
    private static native void free(long ptr);

    /* Private to prevent creation of uninitialized malloc space. */
    private CMalloc() {}
    
    /* Check that indirection won't cause us to write outside the malloc'ed
       space. */
    private void boundsCheck(int off, int sz) {
        if (off < 0 || off + sz > size) {
	    throw new IndexOutOfBoundsException();
	}
    }
}



⌨️ 快捷键说明

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