package com.jstatcom.engine;
/*
* @(#)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 CPtr to memory obtained from the C heap via a call to
* malloc.
*
* In some cases it might be necessary to use memory obtained from
* malloc. For example, CMalloc helps accomplish
* the following idiom:
*
* void *buf = malloc(BUF_LEN * sizeof(char));
* call_some_function(buf);
* free(buf);
*
*
* Remember to free any malloc space
* explicitly. This class could perhaps contain a finalize
* method that does the free, but note that in Java you should
* not use finalizers to free resources.
*
* @author Sheng Liang
* @see CPtr
*/
public final class CMalloc extends CPtr {
/**
* Allocate space in the C heap via a call to C's malloc.
*
* @param size number of bytes 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 malloc.
*/
public void free() {
free(peer);
peer = 0;
}
/**
* Indirect the C pointer to malloc space, a la
* CPtr.copyIn. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
* malloced 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 malloc space, a la
* CPtr.copyIn. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
* malloced 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 malloc space, a la
* CPtr.copyIn. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
* malloced 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 malloc space, a la
* CPtr.copyIn. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
* malloced 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 malloc space, a la
* CPtr.copyIn. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
* malloced 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 malloc space, a la
* CPtr.copyIn. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
* malloced 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 malloc space, a la
* CPtr.copyIn. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
* malloced 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 malloc space, a la
* CPtr.copyOut. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
* malloced 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 malloc space, a la
* CPtr.copyOut. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
* malloced 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 malloc space, a la
* CPtr.copyOut. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
* malloced 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 malloc space, a la
* CPtr.copyOut. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
* malloced 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 malloc space, a la
* CPtr.copyOut. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
* malloced 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 malloc space, a la
* CPtr.copyOut. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
* malloced 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 malloc space, a la
* CPtr.copyOut. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
* malloced 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);
}
/**
* Indirect the C pointer to malloc space, a la
* CPtr.getByte. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
* malloced 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 malloc space, a la
* CPtr.getShort. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
* malloced 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 malloc space, a la
* CPtr.getInt. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
* malloced 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 malloc space, a la
* CPtr.getLong. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
* malloced 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 malloc space, a la
* CPtr.getFloat. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
* malloced 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 malloc space, a la
* CPtr.getDouble. But this method performs a bounds checks
* to ensure that the indirection does not cause memory outside the
* malloced 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 malloc space, a la
* CPtr.getCPtr. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
* malloced 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 malloc space, a la
* CPtr.getString. But this method performs a bounds checks
* to ensure that the indirection does not cause memory outside the
* malloced 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 malloc space, a la
* CPtr.setByte. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
* malloced space to be accessed.
*
* @see CPtr#setByte
*/
public void setByte(int offset, byte value) {
boundsCheck(offset, 1);
super.setByte(offset, value);
}
/**
* Indirect the C pointer to malloc space, a la
* CPtr.setShort. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
* malloced space to be accessed.
*
* @see CPtr#setShort
*/
public void setShort(int offset, short value) {
boundsCheck(offset, 2);
super.setShort(offset, value);
}
/**
* Indirect the C pointer to malloc space, a la
* CPtr.setInt. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
* malloced space to be accessed.
*
* @see CPtr#setInt
*/
public void setInt(int offset, int value) {
boundsCheck(offset, 4);
super.setInt(offset, value);
}
/**
* Indirect the C pointer to malloc space, a la
* CPtr.setLong. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
* malloced space to be accessed.
*
* @see CPtr#setLong
*/
public void setLong(int offset, long value) {
boundsCheck(offset, 8);
super.setLong(offset, value);
}
/**
* Indirect the C pointer to malloc space, a la
* CPtr.setFloat. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
* malloced space to be accessed.
*
* @see CPtr#setFloat
*/
public void setFloat(int offset, float value) {
boundsCheck(offset, 4);
super.setFloat(offset, value);
}
/**
* Indirect the C pointer to malloc space, a la
* CPtr.setDouble. But this method performs a bounds checks
* to ensure that the indirection does not cause memory outside the
* malloced space to be accessed.
*
* @see CPtr#setDouble
*/
public void setDouble(int offset, double value) {
boundsCheck(offset, 8);
super.setDouble(offset, value);
}
/**
* Indirect the C pointer to malloc space, a la
* CPtr.setCPtr. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
* malloced space to be accessed.
*
* @see CPtr#setCPtr
*/
public void setCPtr(int offset, CPtr value) {
boundsCheck(offset, SIZE);
super.setCPtr(offset, value);
}
/**
* Indirect the C pointer to malloc space, a la
* CPtr.setString. But this method performs a bounds checks
* to ensure that the indirection does not cause memory outside the
* malloced space to be accessed.
*
* @see CPtr#setString
*/
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();
}
}
}
Ads help maintain this website.