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

📄 cmalloc.java

📁 学习JAVA编程的示例
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * @(#)CMalloc.java	1.6 98/03/22
 *
 * Copyright (c) 1998 Sun Microsystems, Inc. All Rights Reserved.
 *
 * See also the LICENSE file in this distribution.
 */

/**
 * A <code>CPtr</code> to memory obtained from the C heap via a call to
 * <code>malloc</code>.
 * <p>
 * In some cases it might be necessary to use memory obtained from
 * <code>malloc</code>.  For example, <code>CMalloc</code> helps accomplish
 * the following idiom:
 * <pre>
 * 		void *buf = malloc(BUF_LEN * sizeof(char));
 *		call_some_function(buf);
 *		free(buf);
 * </pre>
 * <p>
 * <b>Remember to <code>free</code> any <code>malloc</code> space
 * explicitly</b>.  This class could perhaps contain a <code>finalize</code>
 * method that does the <code>free</code>, but note that in Java you should
 * not use finalizers to free resources.
 *
 * @author Sheng Liang
 * @see CPtr
 */
public class CMalloc extends CPtr {

    /**
     * Allocate space in the C heap via a call to C's <code>malloc</code>.
     *
     * @param size number of <em>bytes</em> of space to allocate
     */
    public CMalloc(int size) {
        this.size = size;
        peer = malloc(size);
	if (peer == 0) {
	    throw new OutOfMemoryError();
	}
    }

    /**
     * De-allocate space obtained via an earlier call to <code>malloc</code>.
     */
    public void free() {
        free(peer);
	peer = 0;
    }

    /**
     * Indirect the C pointer to <code>malloc</code> space, a la
     * <code>CPtr.copyIn</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#copyIn(int,byte[],int,int) 
     */
    public void copyIn(int bOff, byte[] buf, int index, int length) {
        boundsCheck(bOff, length * 1);
	super.copyIn(bOff, buf, index, length);
    }

    /**
     * Indirect the C pointer to <code>malloc</code> space, a la
     * <code>CPtr.copyIn</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#copyIn(int,short[],int,int)
     */
    public void copyIn(int bOff, short[] buf, int index, int length) {
        boundsCheck(bOff, length * 2);
	super.copyIn(bOff, buf, index, length);
    }

    /**
     * Indirect the C pointer to <code>malloc</code> space, a la
     * <code>CPtr.copyIn</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#copyIn(int,char[],int,int)
     */
    public void copyIn(int bOff, char[] buf, int index, int length) {
        boundsCheck(bOff, length * 2);
	super.copyIn(bOff, buf, index, length);
    }

    /**
     * Indirect the C pointer to <code>malloc</code> space, a la
     * <code>CPtr.copyIn</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#copyIn(int,int[],int,int) 
     */
    public void copyIn(int bOff, int[] buf, int index, int length) {
        boundsCheck(bOff, length * 4);
	super.copyIn(bOff, buf, index, length);
    }

    /**
     * Indirect the C pointer to <code>malloc</code> space, a la
     * <code>CPtr.copyIn</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#copyIn(int,long[],int,int) 
     */
    public void copyIn(int bOff, long[] buf, int index, int length) {
        boundsCheck(bOff, length * 8);
	super.copyIn(bOff, buf, index, length);
    }

    /**
     * Indirect the C pointer to <code>malloc</code> space, a la
     * <code>CPtr.copyIn</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#copyIn(int,float[],int,int)
     */
    public void copyIn(int bOff, float[] buf, int index, int length) {
        boundsCheck(bOff, length * 4);
	super.copyIn(bOff, buf, index, length);
    }

    /**
     * Indirect the C pointer to <code>malloc</code> space, a la
     * <code>CPtr.copyIn</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#copyIn(int,double[],int,int) 
     */
    public void copyIn(int bOff, double[] buf, int index, int length) {
        boundsCheck(bOff, length * 8);
	super.copyIn(bOff, buf, index, length);
    }

    /**
     * Indirect the C pointer to <code>malloc</code> space, a la
     * <code>CPtr.copyOut</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#copyOut(int,byte[],int,int) 
     */
    public void copyOut(int bOff, byte[] buf, int index, int length) {
        boundsCheck(bOff, length * 1);
	super.copyOut(bOff, buf, index, length);
    }

    /**
     * Indirect the C pointer to <code>malloc</code> space, a la
     * <code>CPtr.copyOut</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#copyOut(int,short[],int,int)
     */
    public void copyOut(int bOff, short[] buf, int index, int length) {
        boundsCheck(bOff, length * 2);
	super.copyOut(bOff, buf, index, length);
    }

    /**
     * Indirect the C pointer to <code>malloc</code> space, a la
     * <code>CPtr.copyOut</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#copyOut(int,char[],int,int) 
     */
    public void copyOut(int bOff, char[] buf, int index, int length) {
        boundsCheck(bOff, length * 2);
	super.copyOut(bOff, buf, index, length);
    }

    /**
     * Indirect the C pointer to <code>malloc</code> space, a la
     * <code>CPtr.copyOut</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#copyOut(int,int[],int,int)
     */
    public void copyOut(int bOff, int[] buf, int index, int length) {
        boundsCheck(bOff, length * 4);
	super.copyOut(bOff, buf, index, length);
    }

    /**
     * Indirect the C pointer to <code>malloc</code> space, a la
     * <code>CPtr.copyOut</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#copyOut(int,long[],int,int) 
     */
    public void copyOut(int bOff, long[] buf, int index, int length) {
        boundsCheck(bOff, length * 8);
	super.copyOut(bOff, buf, index, length);
    }

    /**
     * Indirect the C pointer to <code>malloc</code> space, a la
     * <code>CPtr.copyOut</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#copyOut(int,float[],int,int) 
     */
    public void copyOut(int bOff, float[] buf, int index, int length) {
        boundsCheck(bOff, length * 4);
	super.copyOut(bOff, buf, index, length);
    }

    /**
     * Indirect the C pointer to <code>malloc</code> space, a la
     * <code>CPtr.copyOut</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#copyOut(int,double[],int,int) 
     */
    public void copyOut(int bOff, double[] buf, int index, int length) {
        boundsCheck(bOff, length * 8);
	super.copyOut(bOff, buf, index, length);
    }

    /**

⌨️ 快捷键说明

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