FileDocCategorySizeDatePackage
OSMemory.javaAPI DocAndroid 1.5 API24235Wed May 06 22:41:04 BST 2009org.apache.harmony.luni.platform

OSMemory

public final class OSMemory extends OSComponent implements IMemorySystem
This class enables direct access to OS memory.

Methods that take OS addresses define such parameters as a Java int. The int value is interpreted based on the underlying platform pointer size, such that only the lowest significant POINTER_SIZE bytes of the long value are used. In practice this means that methods on 64-bit platforms use the full eight bytes of the address parameter, and on 32-bit platforms the same methods are truncated to use only the low four bytes.

Methods that return OS addresses define the return type to be a Java long. If the platform pointer size is less than eight bytes the OS address value is zero-extended to an eight-byte int to correspond to the subsequent interpretation of that jlong as an OS address as defined above.

Fields Summary
public static final int
POINTER_SIZE
Defines the size, in bytes, of a native pointer type for the underlying platform. This will be 4 (for 32-bit machines) or 8 (for 64-bit machines).
public static final Endianness
NATIVE_ORDER
Defines the natural byte order for this machine.
private static final OSMemory
singleton
Constructors Summary
OSMemory()
This class is not designed to be publically instantiated.

see
#getOSMemory()

        super();
    
Methods Summary
public voidflush(int addr, long size)

        flushImpl(addr, size);
    
private native intflushImpl(int l, long size)

public native voidfree(int address)
Deallocates space for a memory block that was previously allocated by a call to {@link #malloc(int) malloc(int)}. The number of bytes freed is identical to the number of bytes acquired when the memory block was allocated. If address is zero the method does nothing.

Freeing a pointer to a memory block that was not allocated by malloc() has unspecified effect.

param
address the address of the memory block to deallocate.

public native intgetAddress(int address)
Gets the value of the platform pointer at the given address.

The length of the platform pointer is defined by POINTER_SIZE.

The behavior is unspecified if (address ... address + POINTER_SIZE) is not wholly within the range that was previously allocated using malloc().

param
address the platform address of the start of the platform pointer.
return
the value of the platform pointer as a Java int.

public native bytegetByte(int address)
Gets the value of the single byte at the given address.

The behavior is unspecified if address is not in the range that was previously allocated using malloc().

param
address the platform address of the byte.
return
the byte value.

public native voidgetByteArray(int address, byte[] bytes, int offset, int length)
Copies length bytes from the memory block at address into the byte array bytes starting at element offset within the byte array.

The behavior of this method is undefined if the range (address ... address + length) is not within a memory block that was allocated using {@link #malloc(int) malloc(int)}.

param
address the address of the OS memory block from which to copy bytes.
param
bytes the byte array into which to copy the bytes.
param
offset the index of the first element in bytes that will be overwritten.
param
length the total number of bytes to copy into the byte array.
throws
NullPointerException if bytes is null.
throws
IndexOutOfBoundsException if offset + length > bytes.length.

public native doublegetDouble(int address)
Gets the value of the IEEE754-format eight-byte float stored in platform byte order at the given address.

The behavior is unspecified if (address ... address + 8) is not wholly within the range that was previously allocated using malloc().

param
address the platform address of the start of the eight-byte value.
return
the value of the eight-byte float as a Java double.

public doublegetDouble(int address, Endianness endianness)

        if (endianness == NATIVE_ORDER) {
            return getDouble(address);
        }
        long doubleBits = swap(getLong(address));
        return Double.longBitsToDouble(doubleBits);
    
public native floatgetFloat(int address)
Gets the value of the IEEE754-format four-byte float stored in platform byte order at the given address.

The behavior is unspecified if (address ... address + 4) is not wholly within the range that was previously allocated using malloc().

param
address the platform address of the start of the eight-byte value.
return
the value of the four-byte float as a Java float.

public floatgetFloat(int address, Endianness endianness)

        if (endianness == NATIVE_ORDER) {
            return getFloat(address);
        }
        int floatBits = swap(getInt(address));
        return Float.intBitsToFloat(floatBits);
    
public native intgetInt(int address)
Gets the value of the signed four-byte integer stored in platform byte-order at the given address.

The behavior is unspecified if (address ... address + 4) is not wholly within the range that was previously allocated using malloc().

param
address the platform address of the start of the four-byte value.
return
the value of the four-byte integer as a Java int.

public intgetInt(int address, Endianness endianness)

        return (endianness == NATIVE_ORDER) ? getInt(address)
                : swap(getInt(address));
    
public native longgetLong(int address)
Gets the value of the signed eight-byte integer stored in platform byte order at the given address.

The behavior is unspecified if (address ... address + 8) is not wholly within the range that was previously allocated using malloc().

param
address the platform address of the start of the eight-byte value.
return
the value of the eight-byte integer as a Java long.

public longgetLong(int address, Endianness endianness)

        return (endianness == NATIVE_ORDER) ? getLong(address)
                : swap(getLong(address));
    
public EndiannessgetNativeOrder()
Returns the natural byte order for this machine.

return
the native byte order for the current platform.

        return NATIVE_ORDER;
    
public static org.apache.harmony.luni.platform.OSMemorygetOSMemory()


     
        POINTER_SIZE = getPointerSizeImpl();

        if (isLittleEndianImpl()) {
            NATIVE_ORDER = Endianness.LITTLE_ENDIAN;
        } else {
            NATIVE_ORDER = Endianness.BIG_ENDIAN;
        }
    
        return singleton;
    
public intgetPointerSize()

        return POINTER_SIZE;
    
private static native intgetPointerSizeImpl()
Returns the size of a native pointer type for the underlying platform.

return
the size of a pointer, in bytes.

public native shortgetShort(int address)
Gets the value of the signed two-byte integer stored in platform byte order at the given address.

The behavior is unspecified if (address ... address + 2) is not wholly within the range that was previously allocated using malloc().

param
address the platform address of the start of the two-byte value.
return
the value of the two-byte integer as a Java short.

public shortgetShort(int address, Endianness endianness)

        return (endianness == NATIVE_ORDER) ? getShort(address)
                : swap(getShort(address));
    
public booleanisLittleEndian()

        return isLittleEndianImpl();
    
private static native booleanisLittleEndianImpl()
Returns whether the byte order of this machine is little endian or not..

return
false for Big Endian, and true

public booleanisLoaded(int addr, long size)

        return size == 0 ? true : isLoadedImpl(addr, size);
    
private native booleanisLoadedImpl(int l, long size)

public voidload(int addr, long size)

        loadImpl(addr, size);
    
private native intloadImpl(int l, long size)

public native intmalloc(int length)
Allocates and returns a pointer to space for a memory block of length bytes. The space is uninitialized and may be larger than the number of bytes requested; however, the guaranteed usable memory block is exactly length bytes int.

param
length number of bytes requested.
return
the address of the start of the memory block.
throws
OutOfMemoryError if the request cannot be satisfied.

public native voidmemmove(int destAddress, int srcAddress, long length)
Copies length bytes from srcAddress to destAddress. Where any part of the source memory block and the destination memory block overlap memmove() ensures that the original source bytes in the overlapping region are copied before being overwritten.

The behavior is unspecified if (srcAddress ... srcAddress + length) and (destAddress ... destAddress + length) are not both wholly within the range that was previously allocated using malloc().

param
destAddress the address of the destination memory block.
param
srcAddress the address of the source memory block.
param
length the number of bytes to move.

public native voidmemset(int address, byte value, long length)
Places value into first length bytes of the memory block starting at address.

The behavior is unspecified if (address ... address + length) is not wholly within the range that was previously allocated using malloc().

param
address the address of the first memory location.
param
value the byte value to set at each location.
param
length the number of byte-length locations to set.

public intmmap(int fileDescriptor, long alignment, long size, int mapMode)

        int address = mmapImpl(fileDescriptor, alignment, size, mapMode);
        if (address == -1) {
            throw new IOException();
        }
        return address;
    
private native intmmapImpl(int fileDescriptor, long alignment, long size, int mapMode)

public native voidsetAddress(int address, int value)
Sets the value of the platform pointer at the given address.

The length of the platform pointer is defined by POINTER_SIZE. This method only sets POINTER_SIZE bytes at the given address.

The behavior is unspecified if (address ... address + POINTER_SIZE) is not wholly within the range that was previously allocated using malloc().

param
address the platform address of the start of the platform pointer.
param
value the value of the platform pointer as a Java int.

public native voidsetByte(int address, byte value)
Sets the given single byte value at the given address.

The behavior is unspecified if address is not in the range that was previously allocated using malloc().

param
address the address at which to set the byte value.
param
value the value to set.

public native voidsetByteArray(int address, byte[] bytes, int offset, int length)
Copies length bytes from the byte array bytes into the memory block at address, starting at element offset within the byte array.

The behavior of this method is undefined if the range (address ... address + length) is not within a memory block that was allocated using {@link #malloc(int) malloc(int)}.

param
address the address of the OS memory block into which to copy the bytes.
param
bytes the byte array from which to copy the bytes.
param
offset the index of the first element in bytes that will be read.
param
length the total number of bytes to copy from bytes into the memory block.
throws
NullPointerException if bytes is null.
throws
IndexOutOfBoundsException if offset + length > bytes.length.

public native voidsetDouble(int address, double value)
Sets the value of the IEEE754-format eight-byte float store in platform byte order at the given address.

The behavior is unspecified if (address ... address + 8) is not wholly within the range that was previously allocated using malloc().

param
address the platform address of the start of the eight-byte value.
param
value the value of the eight-byte float as a Java double.

public voidsetDouble(int address, double value, Endianness endianness)

        if (endianness == NATIVE_ORDER) {
            setDouble(address, value);
        } else {
            long doubleBits = Double.doubleToLongBits(value);
            setLong(address, swap(doubleBits));
        }
    
public native voidsetFloat(int address, float value)
Sets the value of the IEEE754-format four-byte float stored in platform byte order at the given address.

The behavior is unspecified if (address ... address + 4) is not wholly within the range that was previously allocated using malloc().

param
address the platform address of the start of the eight-byte value.
param
value the value of the four-byte float as a Java float.

public voidsetFloat(int address, float value, Endianness endianness)

        if (endianness == NATIVE_ORDER) {
            setFloat(address, value);
        } else {
            int floatBits = Float.floatToIntBits(value);
            setInt(address, swap(floatBits));
        }
    
public native voidsetInt(int address, int value)
Sets the value of the signed four-byte integer at the given address in platform byte order.

The behavior is unspecified if (address ... address + 4) is not wholly within the range that was previously allocated using malloc().

param
address the platform address of the start of the four-byte value.
param
value the value of the four-byte integer as a Java int.

public voidsetInt(int address, int value, Endianness endianness)

        if (endianness == NATIVE_ORDER) {
            setInt(address, value);
        } else {
            setInt(address, swap(value));
        }
    
public native voidsetIntArray(int address, int[] ints, int offset, int length, boolean swap)
Copies length ints from the int array ints into the memory block at address, starting at element offset within the int array.

The behavior of this method is undefined if the range (address ... address + 2*length) is not within a memory block that was allocated using {@link #malloc(int) malloc(int)}.

param
address the address of the OS memory block into which to copy the ints.
param
ints the int array from which to copy the ints.
param
offset the index of the first element in ints that will be read.
param
length the total number of ints to copy from ints into the memory block.
param
swap true if the ints should be written in reverse byte order.
throws
NullPointerException if bytes is null.
throws
IndexOutOfBoundsException if offset + length > bytes.length.

public native voidsetLong(int address, long value)
Sets the value of the signed eight-byte integer at the given address in the platform byte order.

The behavior is unspecified if (address ... address + 8) is not wholly within the range that was previously allocated using malloc().

param
address the platform address of the start of the eight-byte value.
param
value the value of the eight-byte integer as a Java long.

public voidsetLong(int address, long value, Endianness endianness)

        if (endianness == NATIVE_ORDER) {
            setLong(address, value);
        } else {
            setLong(address, swap(value));
        }
    
public native voidsetShort(int address, short value)
Sets the value of the signed two-byte integer at the given address in platform byte order.

The behavior is unspecified if (address ... address + 2) is not wholly within the range that was previously allocated using malloc().

param
address the platform address of the start of the two-byte value.
param
value the value of the two-byte integer as a Java short.

public voidsetShort(int address, short value, Endianness endianness)

        if (endianness == NATIVE_ORDER) {
            setShort(address, value);
        } else {
            setShort(address, swap(value));
        }
    
public native voidsetShortArray(int address, short[] shorts, int offset, int length, boolean swap)
Copies length shorts from the short array shorts into the memory block at address, starting at element offset within the short array.

The behavior of this method is undefined if the range (address ... address + 2*length) is not within a memory block that was allocated using {@link #malloc(int) malloc(int)}.

param
address the address of the OS memory block into which to copy the shorts.
param
shorts the short array from which to copy the shorts.
param
offset the index of the first element in shorts that will be read.
param
length the total number of shorts to copy from shorts into the memory block.
param
swap true if the shorts should be written in reverse byte order.
throws
NullPointerException if bytes is null.
throws
IndexOutOfBoundsException if offset + length > bytes.length.

private shortswap(short value)

        int topEnd = value << 8;
        int btmEnd = (value >> 8) & 0xFF;
        return (short) (topEnd | btmEnd);
    
private intswap(int value)

        short left = (short) (value >> 16);
        short right = (short) value;
        int topEnd = swap(right) << 16;
        int btmEnd = swap(left) & 0xFFFF;
        return topEnd | btmEnd;
    
private longswap(long value)

        int left = (int) (value >> 32);
        int right = (int) value;
        long topEnd = ((long) swap(right)) << 32;
        long btmEnd = swap(left) & 0xFFFFFFFFL;
        return topEnd | btmEnd;
    
public voidunmap(int addr, long size)

        unmapImpl(addr, size);
    
private native voidunmapImpl(int addr, long size)