📄 cmalloc.java
字号:
/* * %W% %E% * * Copyright (c) 1998 Sun Microsystems, Inc. All Rights Reserved. * * See also the LICENSE file in this distribution. *//** * A <code>CPointer</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 CPointer */public class CMalloc extends CPointer { /** * 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 native void free(); /** * Indirect the C pointer to <code>malloc</code> space, a la * <code>CPointer.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 CPointer#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>CPointer.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 CPointer#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>CPointer.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 CPointer#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>CPointer.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 CPointer#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>CPointer.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 CPointer#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>CPointer.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 CPointer#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>CPointer.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 CPointer#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>CPointer.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 CPointer#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>CPointer.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 CPointer#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>CPointer.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 CPointer#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>CPointer.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 CPointer#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>CPointer.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 CPointer#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>CPointer.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 CPointer#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>CPointer.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 CPointer#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); } /** * Indirect the C pointer to <code>malloc</code> space, a la
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -