Copyright (c) 1998 Sun Microsystems, Inc. All Rights Reserved.
Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
modify and redistribute this software in source and binary code form,
provided that i) this copyright notice and license appear on all copies of
the software; and ii) Licensee does not utilize the software in a manner
which is disparaging to Sun.
This software is provided "AS IS," without a warranty of any kind. ALL
EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
This software is not designed or intended for use in on-line control of
aircraft, air traffic, aircraft navigation or aircraft communications; or in
the design, construction, operation or maintenance of any nuclear
facility. Licensee represents and warrants that it will not use or
redistribute the Software for such purposes.
/*
* @(#)CMalloc.java 1.6 98/03/22
*
* Copyright (c) 1998 Sun Microsystems, Inc. All Rights Reserved.
*
* See also the LICENSE file in this distribution.
*/
/**
* ACPtr
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 tofree
anymalloc
space
* explicitly. This class could perhaps contain afinalize
* method that does thefree
, 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'smalloc
.
*
* @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 tomalloc
.
*/
public void free() {
free(peer);
peer = 0;
}
/**
* Indirect the C pointer tomalloc
space, a la
*CPtr.copyIn
. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
*malloc
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 tomalloc
space, a la
*CPtr.copyIn
. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
*malloc
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 tomalloc
space, a la
*CPtr.copyIn
. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
*malloc
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 tomalloc
space, a la
*CPtr.copyIn
. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
*malloc
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 tomalloc
space, a la
*CPtr.copyIn
. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
*malloc
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 tomalloc
space, a la
*CPtr.copyIn
. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
*malloc
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 tomalloc
space, a la
*CPtr.copyIn
. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
*malloc
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 tomalloc
space, a la
*CPtr.copyOut
. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
*malloc
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 tomalloc
space, a la
*CPtr.copyOut
. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
*malloc
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 tomalloc
space, a la
*CPtr.copyOut
. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
*malloc
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 tomalloc
space, a la
*CPtr.copyOut
. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
*malloc
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 tomalloc
space, a la
*CPtr.copyOut
. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
*malloc
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 tomalloc
space, a la
*CPtr.copyOut
. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
*malloc
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 tomalloc
space, a la
*CPtr.copyOut
. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
*malloc
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);
}
/**
* Indirect the C pointer tomalloc
space, a la
*CPtr.getByte
. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
*malloc
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 tomalloc
space, a la
*CPtr.getShort
. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
*malloc
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 tomalloc
space, a la
*CPtr.getInt
. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
*malloc
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 tomalloc
space, a la
*CPtr.getLong
. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
*malloc
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 tomalloc
space, a la
*CPtr.getFloat
. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
*malloc
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 tomalloc
space, a la
*CPtr.getDouble
. But this method performs a bounds checks
* to ensure that the indirection does not cause memory outside the
*malloc
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 tomalloc
space, a la
*CPtr.getCPtr
. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
*malloc
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 tomalloc
space, a la
*CPtr.getString
. But this method performs a bounds checks
* to ensure that the indirection does not cause memory outside the
*malloc
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 tomalloc
space, a la
*CPtr.setByte
. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
*malloc
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 tomalloc
space, a la
*CPtr.setShort
. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
*malloc
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 tomalloc
space, a la
*CPtr.setInt
. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
*malloc
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 tomalloc
space, a la
*CPtr.setLong
. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
*malloc
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 tomalloc
space, a la
*CPtr.setFloat
. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
*malloc
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 tomalloc
space, a la
*CPtr.setDouble
. But this method performs a bounds checks
* to ensure that the indirection does not cause memory outside the
*malloc
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 tomalloc
space, a la
*CPtr.setCPtr
. But this method performs a bounds checks to
* ensure that the indirection does not cause memory outside the
*malloc
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 tomalloc
space, a la
*CPtr.setString
. But this method performs a bounds checks
* to ensure that the indirection does not cause memory outside the
*malloc
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();
}
}
}
feedme java project
is free software, licensed under the terms of the GNU General Public License, that feeds/imports media from anywhere to xbox360 media center extenders, ipods, web servers, file servers, etc. using Java.
Please read the license here.
Please read the license here.
Thursday, September 6, 2007
CMalloc.java
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment