Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/com/ibm/platform/OSMemory.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/com/ibm/platform/OSMemory.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/com/ibm/platform/OSMemory.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/com/ibm/platform/OSMemory.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,558 @@
+/* Copyright 2004 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.ibm.platform;
+
+// import com.ibm.oti.vm.VM;
+
+/**
+ * This class enables direct access to OS memory.
+ * <p>
+ * Methods that take OS addresses define such parameters as a Java
+ * <code>long</code>. The <code>long</code> value is interpreted based on
+ * the underlying platform pointer size, such that only the lowest significant
+ * <code>POINTER_SIZE</code> bytes of the <code>long</code> 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.
+ * </p>
+ * <p>
+ * Methods that return OS addresses define the return type to be a Java
+ * <code>long</code>. 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.
+ * </p>
+ */
+final class OSMemory extends OSComponent implements IMemorySystem {
+
+ /**
+ * 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 int POINTER_SIZE;
+
+ /**
+ * Defines the natural byte order for this machine.
+ */
+ public static final Endianness NATIVE_ORDER;
+
+ private static final OSMemory singleton = new OSMemory();
+
+ static {
+ POINTER_SIZE = getPointerSizeImpl();
+
+ if (isLittleEndianImpl()) {
+ NATIVE_ORDER = Endianness.LITTLE_ENDIAN;
+ } else {
+ NATIVE_ORDER = Endianness.BIG_ENDIAN;
+ }
+ }
+
+ public static OSMemory getOSMemory() {
+ /*
+ * if (VM.callerClassLoader() != null) { throw new SecurityException(); }
+ */
+ return singleton;
+ }
+
+ /**
+ * This class is not designed to be publically instantiated.
+ *
+ * @see #getOSMemory()
+ */
+ OSMemory() {
+ super();
+ }
+
+ /**
+ * Answers whether the byte order of this machine is little endian or not..
+ *
+ * @return <code>false</code> for Big Endian, and
+ * <code>true</code. for Little Endian.
+ */
+ public static native boolean isLittleEndianImpl();
+
+ public boolean isLittleEndian() {
+ return isLittleEndianImpl();
+ }
+
+ /**
+ * Answers the natural byte order for this machine.
+ *
+ * @return the native byte order for the current platform.
+ */
+ public Endianness getNativeOrder() {
+ return NATIVE_ORDER;
+ }
+
+ /**
+ * Answers the size of a native pointer type for the underlying platform.
+ *
+ * @return the size of a pointer, in bytes.
+ */
+ private static native int getPointerSizeImpl();
+
+ public int getPointerSize() {
+ return POINTER_SIZE;
+ }
+
+ /**
+ * Allocates and returns a pointer to space for a memory block of
+ * <code>length</code> bytes. The space is uninitialized and may be larger
+ * than the number of bytes requested; however, the guaranteed usable memory
+ * block is exactly <code>length</code> bytes long.
+ *
+ * @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 long malloc(long length) throws OutOfMemoryError;
+
+ /**
+ * Deallocates space for a memory block that was previously allocated by a
+ * call to {@link #malloc(long) malloc(long)}. The number of bytes freed is
+ * identical to the number of bytes acquired when the memory block was
+ * allocated. If <code>address</code> is zero the method does nothing.
+ * <p>
+ * Freeing a pointer to a memory block that was not allocated by
+ * <code>malloc()</code> has unspecified effect.
+ * </p>
+ *
+ * @param address
+ * the address of the memory block to deallocate.
+ */
+ public native void free(long address);
+
+ /**
+ * Places <code>value</code> into first <code>length</code> bytes of the
+ * memory block starting at <code>address</code>.
+ * <p>
+ * The behavior is unspecified if
+ * <code>(address ... address + length)</code> is not wholly within the
+ * range that was previously allocated using <code>malloc()</code>.
+ * </p>
+ *
+ * @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 native void memset(long address, byte value, long length);
+
+ /**
+ * Copies <code>length</code> bytes from <code>srcAddress</code> to
+ * <code>destAddress</code>. Where any part of the source memory block
+ * and the destination memory block overlap <code>memmove()</code> ensures
+ * that the original source bytes in the overlapping region are copied
+ * before being overwritten.
+ * <p>
+ * The behavior is unspecified if
+ * <code>(srcAddress ... srcAddress + length)</code> and
+ * <code>(destAddress ... destAddress + length)</code> are not both wholly
+ * within the range that was previously allocated using
+ * <code>malloc()</code>.
+ * </p>
+ *
+ * @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 void memmove(long destAddress, long srcAddress, long length);
+
+ /**
+ * Copies <code>length</code> bytes from the memory block at
+ * <code>address</code> into the byte array <code>bytes</code> starting
+ * at element <code>offset</code> within the byte array.
+ * <p>
+ * The behavior of this method is undefined if the range
+ * <code>(address ... address + length)</code> is not within a memory
+ * block that was allocated using {@link #malloc(long) malloc(long)}.
+ * </p>
+ *
+ * @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 <code>bytes</code> that
+ * will be overwritten.
+ * @param length
+ * the total number of bytes to copy into the byte array.
+ * @throws NullPointerException
+ * if <code>bytes</code> is <code>null</code>.
+ * @throws IndexOutOfBoundsException
+ * if <code>offset + length > bytes.length</code>.
+ */
+ public native void getByteArray(long address, byte[] bytes, int offset,
+ int length) throws NullPointerException, IndexOutOfBoundsException;
+
+ /**
+ * Copies <code>length</code> bytes from the byte array <code>bytes</code>
+ * into the memory block at <code>address</code>, starting at element
+ * <code>offset</code> within the byte array.
+ * <p>
+ * The behavior of this method is undefined if the range
+ * <code>(address ... address + length)</code> is not within a memory
+ * block that was allocated using {@link #malloc(long) malloc(long)}.
+ * </p>
+ *
+ * @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 <code>bytes</code> that
+ * will be read.
+ * @param length
+ * the total number of bytes to copy from <code>bytes</code>
+ * into the memory block.
+ * @throws NullPointerException
+ * if <code>bytes</code> is <code>null</code>.
+ * @throws IndexOutOfBoundsException
+ * if <code>offset + length > bytes.length</code>.
+ */
+ public native void setByteArray(long address, byte[] bytes, int offset,
+ int length) throws NullPointerException, IndexOutOfBoundsException;
+
+ // Primitive get & set methods
+
+ /**
+ * Gets the value of the single byte at the given address.
+ * <p>
+ * The behavior is unspecified if <code>address</code> is not in the range
+ * that was previously allocated using <code>malloc()</code>.
+ * </p>
+ *
+ * @param address
+ * the platform address of the byte.
+ * @return the byte value.
+ */
+ public native byte getByte(long address);
+
+ /**
+ * Sets the given single byte value at the given address.
+ * <p>
+ * The behavior is unspecified if <code>address</code> is not in the range
+ * that was previously allocated using <code>malloc()</code>.
+ * </p>
+ *
+ * @param address
+ * the address at which to set the byte value.
+ * @param value
+ * the value to set.
+ */
+ public native void setByte(long address, byte value);
+
+ /**
+ * Gets the value of the signed two-byte integer stored in platform byte
+ * order at the given address.
+ * <p>
+ * The behavior is unspecified if <code>(address ... address + 2)</code>
+ * is not wholly within the range that was previously allocated using
+ * <code>malloc()</code>.
+ * </p>
+ *
+ * @param address
+ * the platform address of the start of the two-byte value.
+ * @return the value of the two-byte integer as a Java <code>short</code>.
+ */
+ public native short getShort(long address);
+
+ public short getShort(long address, Endianness endianness) {
+ return (endianness == NATIVE_ORDER) ? getShort(address)
+ : swap(getShort(address));
+ }
+
+ /**
+ * Sets the value of the signed two-byte integer at the given address in
+ * platform byte order.
+ * <p>
+ * The behavior is unspecified if <code>(address ... address + 2)</code>
+ * is not wholly within the range that was previously allocated using
+ * <code>malloc()</code>.
+ * </p>
+ *
+ * @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 <code>short</code>.
+ */
+ public native void setShort(long address, short value);
+
+ public void setShort(long address, short value, Endianness endianness) {
+ if (endianness == NATIVE_ORDER) {
+ setShort(address, value);
+ } else {
+ setShort(address, swap(value));
+ }
+ }
+
+ /**
+ * Gets the value of the signed four-byte integer stored in platform
+ * byte-order at the given address.
+ * <p>
+ * The behavior is unspecified if <code>(address ... address + 4)</code>
+ * is not wholly within the range that was previously allocated using
+ * <code>malloc()</code>.
+ * </p>
+ *
+ * @param address
+ * the platform address of the start of the four-byte value.
+ * @return the value of the four-byte integer as a Java <code>int</code>.
+ */
+ public native int getInt(long address);
+
+ public int getInt(long address, Endianness endianness) {
+ return (endianness == NATIVE_ORDER) ? getInt(address)
+ : swap(getInt(address));
+ }
+
+ /**
+ * Sets the value of the signed four-byte integer at the given address in
+ * platform byte order.
+ * <p>
+ * The behavior is unspecified if <code>(address ... address + 4)</code>
+ * is not wholly within the range that was previously allocated using
+ * <code>malloc()</code>.
+ * </p>
+ *
+ * @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 <code>int</code>.
+ */
+ public native void setInt(long address, int value);
+
+ public void setInt(long address, int value, Endianness endianness) {
+ if (endianness == NATIVE_ORDER) {
+ setInt(address, value);
+ } else {
+ setInt(address, swap(value));
+ }
+ }
+
+ /**
+ * Gets the value of the signed eight-byte integer stored in platform byte
+ * order at the given address.
+ * <p>
+ * The behavior is unspecified if <code>(address ... address + 8)</code>
+ * is not wholly within the range that was previously allocated using
+ * <code>malloc()</code>.
+ * </p>
+ *
+ * @param address
+ * the platform address of the start of the eight-byte value.
+ * @return the value of the eight-byte integer as a Java <code>long</code>.
+ */
+ public native long getLong(long address);
+
+ public long getLong(long address, Endianness endianness) {
+ return (endianness == NATIVE_ORDER) ? getLong(address)
+ : swap(getLong(address));
+ }
+
+ /**
+ * Sets the value of the signed eight-byte integer at the given address in
+ * the platform byte order.
+ * <p>
+ * The behavior is unspecified if <code>(address ... address + 8)</code>
+ * is not wholly within the range that was previously allocated using
+ * <code>malloc()</code>.
+ * </p>
+ *
+ * @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
+ * <code>long</code>.
+ */
+ public native void setLong(long address, long value);
+
+ public void setLong(long address, long value, Endianness endianness) {
+ if (endianness == NATIVE_ORDER) {
+ setLong(address, value);
+ } else {
+ setLong(address, swap(value));
+ }
+ }
+
+ /**
+ * Gets the value of the IEEE754-format four-byte float stored in platform
+ * byte order at the given address.
+ * <p>
+ * The behavior is unspecified if <code>(address ... address + 4)</code>
+ * is not wholly within the range that was previously allocated using
+ * <code>malloc()</code>.
+ * </p>
+ *
+ * @param address
+ * the platform address of the start of the eight-byte value.
+ * @return the value of the four-byte float as a Java <code>float</code>.
+ */
+ public native float getFloat(long address);
+
+ public float getFloat(long address, Endianness endianness) {
+ if (endianness == NATIVE_ORDER) {
+ return getFloat(address);
+ }
+ int floatBits = swap(getInt(address));
+ return Float.intBitsToFloat(floatBits);
+ }
+
+ /**
+ * Sets the value of the IEEE754-format four-byte float stored in platform
+ * byte order at the given address.
+ * <p>
+ * The behavior is unspecified if <code>(address ... address + 4)</code>
+ * is not wholly within the range that was previously allocated using
+ * <code>malloc()</code>.
+ * </p>
+ *
+ * @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 <code>float</code>.
+ */
+ public native void setFloat(long address, float value);
+
+ public void setFloat(long address, float value, Endianness endianness) {
+ if (endianness == NATIVE_ORDER) {
+ setFloat(address, value);
+ } else {
+ int floatBits = Float.floatToIntBits(value);
+ setInt(address, swap(floatBits));
+ }
+ }
+
+ /**
+ * Gets the value of the IEEE754-format eight-byte float stored in platform
+ * byte order at the given address.
+ * <p>
+ * The behavior is unspecified if <code>(address ... address + 8)</code>
+ * is not wholly within the range that was previously allocated using
+ * <code>malloc()</code>.
+ * </p>
+ *
+ * @param address
+ * the platform address of the start of the eight-byte value.
+ * @return the value of the eight-byte float as a Java <code>double</code>.
+ */
+ public native double getDouble(long address);
+
+ public double getDouble(long address, Endianness endianness) {
+ if (endianness == NATIVE_ORDER) {
+ return getDouble(address);
+ }
+ long doubleBits = swap(getLong(address));
+ return Double.longBitsToDouble(doubleBits);
+ }
+
+ /**
+ * Sets the value of the IEEE754-format eight-byte float store in platform
+ * byte order at the given address.
+ * <p>
+ * The behavior is unspecified if <code>(address ... address + 8)</code>
+ * is not wholly within the range that was previously allocated using
+ * <code>malloc()</code>.
+ * </p>
+ *
+ * @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
+ * <code>double</code>.
+ */
+ public native void setDouble(long address, double value);
+
+ public void setDouble(long address, double value, Endianness endianness) {
+ if (endianness == NATIVE_ORDER) {
+ setDouble(address, value);
+ } else {
+ long doubleBits = Double.doubleToLongBits(value);
+ setLong(address, swap(doubleBits));
+ }
+ }
+
+ /**
+ * Gets the value of the platform pointer at the given address.
+ * <p>
+ * The length of the platform pointer is defined by
+ * <code>POINTER_SIZE</code>.
+ * </p>
+ * The behavior is unspecified if
+ * <code>(address ... address + POINTER_SIZE)</code> is not wholly within
+ * the range that was previously allocated using <code>malloc()</code>.
+ * </p>
+ *
+ * @param address
+ * the platform address of the start of the platform pointer.
+ * @return the value of the platform pointer as a Java <code>long</code>.
+ */
+ public native long getAddress(long address);
+
+ /**
+ * Sets the value of the platform pointer at the given address.
+ * <p>
+ * The length of the platform pointer is defined by
+ * <code>POINTER_SIZE</code>. This method only sets
+ * <code>POINTER_SIZE</code> bytes at the given address.
+ * </p>
+ * The behavior is unspecified if
+ * <code>(address ... address + POINTER_SIZE)</code> is not wholly within
+ * the range that was previously allocated using <code>malloc()</code>.
+ * </p>
+ *
+ * @param address
+ * the platform address of the start of the platform pointer.
+ * @param value
+ * the value of the platform pointer as a Java <code>long</code>.
+ */
+ public native void setAddress(long address, long value);
+
+ /*
+ * Helper methods to change byte order.
+ */
+ private short swap(short value) {
+ int topEnd = value << 8;
+ int btmEnd = (value >> 8) & 0xFF;
+ return (short) (topEnd | btmEnd);
+ }
+
+ private int swap(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 long swap(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;
+ }
+}
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/com/ibm/platform/Platform.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/com/ibm/platform/Platform.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/com/ibm/platform/Platform.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/com/ibm/platform/Platform.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,53 @@
+/* Copyright 2004 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.ibm.platform;
+
+
+import com.ibm.oti.vm.VM;
+
+/**
+ * Platform
+ *
+ */
+public class Platform {
+
+ static final IAdapterManager ADAPTER_MANAGER = new AdapterManager();
+
+ static final IFileSystem FILE_SYSTEM = OSComponentFactory.getFileSystem();
+
+ static final IMemorySystem MEMORY_SYSTEM = OSComponentFactory
+ .getMemorySystem();
+
+ public static IAdapterManager getAdapterManager() {
+ return ADAPTER_MANAGER;
+ }
+
+ private static final void accessCheck() {
+ if (VM.callerClassLoader() != null) {
+ throw new SecurityException();
+ }
+ }
+
+ public static IFileSystem getFileSystem() {
+ accessCheck();
+ return FILE_SYSTEM;
+ }
+
+ public static IMemorySystem getMemorySystem() {
+ accessCheck();
+ return MEMORY_SYSTEM;
+ }
+}
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/com/ibm/platform/struct/AbstractMemorySpy.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/com/ibm/platform/struct/AbstractMemorySpy.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/com/ibm/platform/struct/AbstractMemorySpy.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/com/ibm/platform/struct/AbstractMemorySpy.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,118 @@
+/* Copyright 2004 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.ibm.platform.struct;
+
+
+import java.lang.ref.ReferenceQueue;
+import java.lang.ref.WeakReference;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Abstract implementation for the OS memory spies.
+ *
+ */
+abstract class AbstractMemorySpy implements IMemorySpy {
+
+ // TODO: figure out how to prevent this being a synchronization bottleneck
+ protected Map memoryInUse = new HashMap(); // Shadow to Wrapper
+
+ protected Map refToShadow = new HashMap(); // Reference to Shadow
+
+ protected ReferenceQueue notifyQueue = new ReferenceQueue();
+
+ protected Object lock = new Object();
+
+ final class AddressWrapper {
+ final PlatformAddress shadow;
+
+ final long size;
+
+ final WeakReference wrAddress;
+
+ volatile boolean autoFree = false;
+
+ AddressWrapper(PlatformAddress address, long size) {
+ super();
+ this.shadow = PlatformAddress.on(address);
+ this.size = size;
+ this.wrAddress = new WeakReference(address, notifyQueue);
+ }
+ }
+
+ public AbstractMemorySpy() {
+ super();
+ }
+
+ public void alloc(PlatformAddress address, long size) {
+ AddressWrapper wrapper = new AddressWrapper(address, size);
+ synchronized (lock) {
+ memoryInUse.put(wrapper.shadow, wrapper);
+ refToShadow.put(wrapper.wrAddress, wrapper.shadow);
+ }
+ }
+
+ public boolean free(PlatformAddress address) {
+ AddressWrapper wrapper;
+ synchronized (lock) {
+ wrapper = (AddressWrapper) memoryInUse.remove(address);
+ }
+ if (wrapper == null) {
+ // Attempt to free memory we didn't alloc
+ System.err
+ .println("Memory Spy! Fixed attempt to free memory that was not allocated " + address); //$NON-NLS-1$
+ }
+ return wrapper != null;
+ }
+
+ public void rangeCheck(PlatformAddress address, int offset, int length)
+ throws IndexOutOfBoundsException {
+ // Do nothing
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see com.ibm.platform.struct.IMemorySpy#autoFree(com.ibm.platform.struct.PlatformAddress)
+ */
+ public void autoFree(PlatformAddress address) {
+ AddressWrapper wrapper;
+ synchronized (lock) {
+ wrapper = (AddressWrapper) memoryInUse.remove(address);
+ }
+ if (wrapper != null) {
+ wrapper.autoFree = true;
+ }
+ }
+
+ protected void orphanedMemory(Object ref) {
+ AddressWrapper wrapper;
+ synchronized (lock) {
+ Object shadow = refToShadow.remove(ref);
+ wrapper = (AddressWrapper) memoryInUse.remove(shadow);
+ }
+ if (wrapper != null) {
+ // There is a leak if we were not auto-freeing this memory.
+ if (!wrapper.autoFree) {
+ System.err
+ .println("Memory Spy! Fixed memory leak by freeing " + wrapper.shadow); //$NON-NLS-1$
+ }
+ memoryInUse.put(wrapper.shadow, wrapper);
+ wrapper.shadow.free();
+ }
+
+ }
+}
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/com/ibm/platform/struct/DebugMemorySpy.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/com/ibm/platform/struct/DebugMemorySpy.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/com/ibm/platform/struct/DebugMemorySpy.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/com/ibm/platform/struct/DebugMemorySpy.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,92 @@
+/* Copyright 2004 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.ibm.platform.struct;
+
+
+/**
+ * This memory spy tries to be as protective as possible, and fail as early as
+ * posible for misbehaving allocators.
+ *
+ */
+public final class DebugMemorySpy extends AbstractMemorySpy {
+
+ private final boolean stackDump = true;
+
+ public DebugMemorySpy() {
+ super();
+ startSpy();
+ }
+
+ public boolean free(PlatformAddress address) {
+ boolean vetoed = super.free(address);
+ if (!vetoed && stackDump) {
+ Thread.dumpStack();
+ }
+ return vetoed;
+ }
+
+ /*
+ * If this is memory that we allocated then we can check the access is
+ * within range. However, we cannot check the range for memory that has been
+ * allocated by the OS, or for addresses that we have computed. i.e. it is
+ * quite possible that the range checker does <em>not</em> catch some
+ * buffer overruns.
+ */
+ public void rangeCheck(PlatformAddress address, int offset, int length)
+ throws IndexOutOfBoundsException {
+ AddressWrapper wrapper = (AddressWrapper) memoryInUse.get(address);
+ if (wrapper != null) {
+ PlatformAddress accessStart = address.offsetBytes(offset);
+ PlatformAddress accessEnd = accessStart.offsetBytes(length);
+ PlatformAddress allocStart = wrapper.shadow;
+ PlatformAddress allocEnd = allocStart.offsetBytes(wrapper.size);
+ boolean under = (accessStart.compareTo(allocStart)) == -1;
+ boolean over = (accessEnd.compareTo(allocEnd)) == 1;
+ if (under || over) {
+ System.err.println("Memory Spy! Access out of allocated range"); //$NON-NLS-1$
+ System.err
+ .println("\tAlloc range : [" + allocStart + " ... " + allocEnd + "]"); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
+ System.err
+ .println("\tAccess range : [" + accessStart + " ... " + accessEnd + "]"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
+ if (stackDump) {
+ Thread.dumpStack();
+ }
+ // throw new IndexOutOfBoundsException();
+ }
+ }
+ }
+
+ private void startSpy() {
+ Thread spy = new Thread(new Runnable() {
+ public void run() {
+ while (true) {
+ try {
+ orphanedMemory(notifyQueue.remove()); // Blocks until
+ // notified of
+ // collected
+ // reference
+ } catch (InterruptedException e) {
+ // Ignore interruptions
+ }
+ }
+ }
+ });
+ spy.setDaemon(true);
+ spy.setName("Platform Interface Memory Spy"); //$NON-NLS-1$
+ spy.setPriority(Thread.MAX_PRIORITY);
+ spy.start();
+ }
+}
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/com/ibm/platform/struct/ICommonDataTypes.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/com/ibm/platform/struct/ICommonDataTypes.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/com/ibm/platform/struct/ICommonDataTypes.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/com/ibm/platform/struct/ICommonDataTypes.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,120 @@
+/* Copyright 2004 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.ibm.platform.struct;
+
+
+/**
+ * Defines some C scalar types.
+ *
+ */
+
+public interface ICommonDataTypes {
+
+ // Nothing
+ public static final int SIZEOF_NO_BYTES = 0;
+
+ public static final int SIZEOF_VOID = SIZEOF_NO_BYTES;
+
+ // One byte
+ public static final int SIZEOF_ONE_BYTE = 1;
+
+ public static final int SIZEOF_UNSIGNED_CHAR = SIZEOF_ONE_BYTE; // ANSI C
+
+ public static final int SIZEOF_JBOOLEAN = SIZEOF_ONE_BYTE; // Java language
+
+ public static final int SIZEOF_BOOLEAN = SIZEOF_ONE_BYTE;
+
+ public static final int SIZEOF_BOOL = SIZEOF_ONE_BYTE;
+
+ public static final int SIZEOF_SIGNED_8_BITS = SIZEOF_ONE_BYTE;
+
+ public static final int SIZEOF_SIGNED_CHAR = SIZEOF_ONE_BYTE; // ANSI C
+
+ public static final int SIZEOF_JBYTE = SIZEOF_ONE_BYTE; // Java language
+
+ // Two bytes
+ public static final int SIZEOF_TWO_BYTES = 2;
+
+ public static final int SIZEOF_UNSIGNED_SHORT = SIZEOF_TWO_BYTES; // ANSI
+ // C
+
+ public static final int SIZEOF_JCHAR = SIZEOF_TWO_BYTES; // Java language
+
+ public static final int SIZEOF_UINT16 = SIZEOF_TWO_BYTES; // Common
+ // abbreviation
+
+ public static final int SIZEOF_SIGNED_16_BITS = SIZEOF_TWO_BYTES;
+
+ public static final int SIZEOF_SIGNED_SHORT = SIZEOF_TWO_BYTES; // ANSI C
+
+ public static final int SIZEOF_JSHORT = SIZEOF_TWO_BYTES; // Java language
+
+ public static final int SIZEOF_SHORT = SIZEOF_TWO_BYTES; // Common
+ // convention
+
+ public static final int SIZEOF_INT16 = SIZEOF_TWO_BYTES; // Common
+ // abbreviation
+
+ // Four bytes
+ public static final int SIZE_OF_FOUR_BYTES = 4;
+
+ public static final int SIZEOF_UNSIGNED_32_BITS = SIZE_OF_FOUR_BYTES;
+
+ public static final int SIZEOF_UINT32 = SIZE_OF_FOUR_BYTES; // Common
+ // abbreviation
+
+ public static final int SIZEOF_UNSIGNED_LONG = SIZE_OF_FOUR_BYTES; // Common
+ // convention
+
+ public static final int SIZEOF_SIGNED_32_BITS = SIZE_OF_FOUR_BYTES;
+
+ public static final int SIZEOF_SIGNED_LONG = SIZE_OF_FOUR_BYTES; // ANSI
+ // C
+
+ public static final int SIZEOF_JINT = SIZE_OF_FOUR_BYTES; // Java language
+
+ public static final int SIZEOF_JSIZE = SIZE_OF_FOUR_BYTES; // Java language
+
+ public static final int SIZEOF_LONG = SIZE_OF_FOUR_BYTES; // Common
+ // abbreviation
+
+ public static final int SIZEOF_INT32 = SIZE_OF_FOUR_BYTES; // Common
+ // abbreviation
+
+ public static final int SIZEOF_IEEE754_32_BITS = SIZE_OF_FOUR_BYTES;
+
+ public static final int SIZEOF_JFLOAT = SIZE_OF_FOUR_BYTES; // Java language
+
+ public static final int SIZEOF_FLOAT = SIZE_OF_FOUR_BYTES; // Common
+ // convention
+
+ // Eight bytes
+ public static final int SIZEOF_EIGHT_BYTES = 8;
+
+ public static final int SIZEOF_UNSIGNED_64_BITS = SIZEOF_EIGHT_BYTES;
+
+ public static final int SIZEOF_SIGNED_64_BITS = SIZEOF_EIGHT_BYTES;
+
+ public static final int SIZEOF_JLONG = SIZEOF_EIGHT_BYTES; // Java language
+
+ public static final int SIZEOF_IEEE754_64_BITS = SIZEOF_EIGHT_BYTES;
+
+ public static final int SIZEOF_JDOUBLE = SIZEOF_EIGHT_BYTES; // Java
+ // language
+
+ public static final int SIZEOF_DOUBLE = SIZEOF_EIGHT_BYTES; // Common
+ // convention
+}
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/com/ibm/platform/struct/IMemorySpy.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/com/ibm/platform/struct/IMemorySpy.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/com/ibm/platform/struct/IMemorySpy.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/com/ibm/platform/struct/IMemorySpy.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,44 @@
+/* Copyright 2004 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.ibm.platform.struct;
+
+
+/**
+ * This is the interface that the memory watchers implement -- what they do with
+ * this information is largely undefined.
+ *
+ */
+public interface IMemorySpy {
+
+ public void alloc(PlatformAddress address, long size);
+
+ // Has a veto: true == do free,false = don't
+ public boolean free(PlatformAddress address);
+
+ public void rangeCheck(PlatformAddress address, int offset, int length)
+ throws IndexOutOfBoundsException;
+
+ /**
+ * Requests that the given address is freed automatically when it becomes
+ * garbage. If the address is alredy freed, or has not been notified as
+ * allocated via this memory spy, then this call has no effect and completes
+ * quietly.
+ *
+ * @param address
+ * the address to be freed.
+ */
+ public void autoFree(PlatformAddress address);
+}
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/com/ibm/platform/struct/IPlatformConstants.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/com/ibm/platform/struct/IPlatformConstants.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/com/ibm/platform/struct/IPlatformConstants.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/com/ibm/platform/struct/IPlatformConstants.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,26 @@
+/* Copyright 2004 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.ibm.platform.struct;
+
+
+public interface IPlatformConstants {
+
+ public static final int NULL = 0;
+
+ public static final byte TRUE = 1;
+
+ public static final byte FALSE = 0;
+}
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/com/ibm/platform/struct/PlatformAddress.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/com/ibm/platform/struct/PlatformAddress.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/com/ibm/platform/struct/PlatformAddress.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/com/ibm/platform/struct/PlatformAddress.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,253 @@
+/* Copyright 2004 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.ibm.platform.struct;
+
+
+import com.ibm.platform.Endianness;
+import com.ibm.platform.IMemorySystem;
+import com.ibm.platform.Platform;
+
+/**
+ * The platform address class is an unsafe virtualization of an OS memory block.
+ *
+ */
+public final class PlatformAddress implements ICommonDataTypes, Comparable {
+
+ /**
+ * This final field defines the size of an address on this platform.
+ */
+ static final int SIZEOF = Platform.getMemorySystem().getPointerSize();
+
+ /**
+ * NULL is the canonical address with address value zero.
+ */
+ public static final PlatformAddress NULL = new PlatformAddress(0);
+
+ public static final IMemorySpy memorySpy = new RuntimeMemorySpy();
+
+ private static final IMemorySystem osMemory = Platform.getMemorySystem();
+
+ private final long osaddr;
+
+ public static PlatformAddress on(PlatformAddress other) {
+ return new PlatformAddress(other.osaddr);
+ }
+
+ public static PlatformAddress on(long value) {
+ return (value == 0) ? NULL : new PlatformAddress(value);
+ }
+
+ public static PlatformAddress alloc(long size) {
+ long osAddress = osMemory.malloc(size);
+ PlatformAddress newMemory = PlatformAddress.on(osAddress);
+ memorySpy.alloc(newMemory, size);
+ return newMemory;
+ }
+
+ public PlatformAddress(long address) {
+ super();
+ this.osaddr = address;
+ }
+
+ /**
+ * Sending auto free to an address means that, when this subsystem has
+ * allocated the memory, it will automatically be freed when this object is
+ * collected by the garbage collector if the memory has not already been
+ * freed explicitly.
+ *
+ */
+ public void autoFree() {
+ memorySpy.autoFree(this);
+ }
+
+ public PlatformAddress offsetBytes(int offset) {
+ return PlatformAddress.on(osaddr + offset);
+ }
+
+ public PlatformAddress offsetBytes(long offset) {
+ return PlatformAddress.on(osaddr + offset);
+ }
+
+ public void moveTo(PlatformAddress dest, long numBytes) {
+ osMemory.memmove(dest.osaddr, osaddr, numBytes);
+ }
+
+ public boolean equals(Object other) {
+ return (other instanceof PlatformAddress)
+ && (((PlatformAddress) other).osaddr == osaddr);
+ }
+
+ public int hashCode() {
+ return (int) osaddr;
+ }
+
+ public boolean isNULL() {
+ return this == NULL;
+ }
+
+ public void free() {
+ // Memory spys can veto the basic free if they determine the memory was
+ // not allocated.
+ if (memorySpy.free(this)) {
+ osMemory.free(osaddr);
+ }
+ }
+
+ public void setAddress(int offset, PlatformAddress address) {
+ osMemory.setAddress(osaddr + offset, address.osaddr);
+ }
+
+ public PlatformAddress getAddress(int offset) {
+ int addr = getInt(offset);
+ if (addr == 0) {
+ return NULL;
+ }
+ return PlatformAddress.on(addr);
+ }
+
+ public void setByte(int offset, byte value) {
+ memorySpy.rangeCheck(this, offset, SIZEOF_JBYTE);
+ osMemory.setByte(osaddr + offset, value);
+ }
+
+ public byte getByte(int offset) {
+ memorySpy.rangeCheck(this, offset, SIZEOF_JBYTE);
+ return osMemory.getByte(osaddr + offset);
+ }
+
+ public void setShort(int offset, short value, Endianness order) {
+ memorySpy.rangeCheck(this, offset, SIZEOF_JSHORT);
+ osMemory.setShort(osaddr + offset, value, order);
+ }
+
+ public void setShort(int offset, short value) {
+ memorySpy.rangeCheck(this, offset, SIZEOF_JSHORT);
+ osMemory.setShort(osaddr + offset, value);
+ }
+
+ public short getShort(int offset, Endianness order) {
+ memorySpy.rangeCheck(this, offset, SIZEOF_JSHORT);
+ return osMemory.getShort(osaddr + offset, order);
+ }
+
+ public short getShort(int offset) {
+ memorySpy.rangeCheck(this, offset, SIZEOF_JSHORT);
+ return osMemory.getShort(osaddr + offset);
+ }
+
+ public void setInt(int offset, int value, Endianness order) {
+ memorySpy.rangeCheck(this, offset, SIZEOF_JINT);
+ osMemory.setInt(osaddr + offset, value, order);
+ }
+
+ public void setInt(int offset, int value) {
+ memorySpy.rangeCheck(this, offset, SIZEOF_JINT);
+ osMemory.setInt(osaddr + offset, value);
+ }
+
+ public int getInt(int offset, Endianness order) {
+ memorySpy.rangeCheck(this, offset, SIZEOF_JINT);
+ return osMemory.getInt(osaddr + offset, order);
+ }
+
+ public int getInt(int offset) {
+ memorySpy.rangeCheck(this, offset, SIZEOF_JINT);
+ return osMemory.getInt(osaddr + offset);
+ }
+
+ public void setLong(int offset, long value, Endianness order) {
+ memorySpy.rangeCheck(this, offset, SIZEOF_JLONG);
+ osMemory.setLong(osaddr + offset, value, order);
+ }
+
+ public void setLong(int offset, long value) {
+ memorySpy.rangeCheck(this, offset, SIZEOF_JLONG);
+ osMemory.setLong(osaddr + offset, value);
+ }
+
+ public long getLong(int offset, Endianness order) {
+ memorySpy.rangeCheck(this, offset, SIZEOF_JLONG);
+ return osMemory.getLong(osaddr + offset, order);
+ }
+
+ public long getLong(int offset) {
+ memorySpy.rangeCheck(this, offset, SIZEOF_JLONG);
+ return osMemory.getLong(osaddr + offset);
+ }
+
+ public void setFloat(int offset, float value, Endianness order) {
+ memorySpy.rangeCheck(this, offset, SIZEOF_JFLOAT);
+ osMemory.setFloat(osaddr + offset, value, order);
+ }
+
+ public void setFloat(int offset, float value) {
+ memorySpy.rangeCheck(this, offset, SIZEOF_JFLOAT);
+ osMemory.setFloat(osaddr + offset, value);
+ }
+
+ public float getFloat(int offset, Endianness order) {
+ memorySpy.rangeCheck(this, offset, SIZEOF_JFLOAT);
+ return osMemory.getFloat(osaddr + offset, order);
+ }
+
+ public float getFloat(int offset) {
+ memorySpy.rangeCheck(this, offset, SIZEOF_JFLOAT);
+ return osMemory.getFloat(osaddr + offset);
+ }
+
+ public void setDouble(int offset, double value, Endianness order) {
+ memorySpy.rangeCheck(this, offset, SIZEOF_JDOUBLE);
+ osMemory.setDouble(osaddr + offset, value, order);
+ }
+
+ public void setDouble(int offset, double value) {
+ memorySpy.rangeCheck(this, offset, SIZEOF_JDOUBLE);
+ osMemory.setDouble(osaddr + offset, value);
+ }
+
+ public double getDouble(int offset, Endianness order) {
+ memorySpy.rangeCheck(this, offset, SIZEOF_JDOUBLE);
+ return osMemory.getDouble(osaddr + offset, order);
+ }
+
+ public double getDouble(int offset) {
+ memorySpy.rangeCheck(this, offset, SIZEOF_JDOUBLE);
+ return osMemory.getDouble(osaddr + offset);
+ }
+
+ public long toLong() {
+ return osaddr;
+ }
+
+ public String toString() {
+ return "PlatformAddress[" + osaddr + "]"; //$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public int compareTo(Object other) {
+ if (other == null) {
+ throw new NullPointerException(); // per spec.
+ }
+ if (other instanceof PlatformAddress) {
+ long otherPA = ((PlatformAddress) other).osaddr;
+ if (osaddr == otherPA) {
+ return 0;
+ }
+ return osaddr < otherPA ? -1 : 1;
+ }
+
+ throw new ClassCastException(); // per spec.
+ }
+}
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/com/ibm/platform/struct/RuntimeMemorySpy.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/com/ibm/platform/struct/RuntimeMemorySpy.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/com/ibm/platform/struct/RuntimeMemorySpy.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/com/ibm/platform/struct/RuntimeMemorySpy.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,34 @@
+/* Copyright 2004 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.ibm.platform.struct;
+
+
+public final class RuntimeMemorySpy extends AbstractMemorySpy {
+
+ public RuntimeMemorySpy() {
+ super();
+ }
+
+ public void alloc(PlatformAddress address, long size) {
+ super.alloc(address, size);
+ // Pay a tax on the allocation to see if there are any frees pending.
+ Object ref = notifyQueue.poll(); // non-blocking check
+ while (ref != null) {
+ orphanedMemory(ref);
+ ref = notifyQueue.poll();
+ }
+ }
+}
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/Buffer.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/Buffer.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/Buffer.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/Buffer.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,288 @@
+/* Copyright 2004 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.nio;
+
+
+/**
+ * A buffer is a list of elements of a specific primitive type.
+ * <p>
+ * A buffer can be described by following properties:
+ * <ul>
+ * <li>Capacity, is the number of elements a buffer can hold. Capacity is no
+ * less than zero and never changes.</li>
+ * <li>Posotion, is a cursor of this buffer. Elements are read or write at the
+ * position if you do not specify an index explicitly. Position is no less than
+ * zero and no greater than the limit.</li>
+ * <li>Limit controls the scope of accessible elements. You can only read or
+ * write elements from index zero to <code>limit - 1</code>. Accessing
+ * elements out of the scope will cause exception. Limit is no less than zero
+ * and no greater than capacity.</li>
+ * <li>Mark, is used to remember the current position, so that you can reset
+ * the position later. Mark is no less than zero and no greater than position.</li>
+ * <li>A buffer can be readonly or read-write. Trying to modify the elements of
+ * a readonly buffer will cause <code>ReadOnlyBufferException</code>, while
+ * changing the position, limit and mark of a readonly buffer is OK.</li>
+ * <li>A buffer can be direct or indirect. A direct buffer will try its best to
+ * take advantage of native memory APIs and it may not stay in java heap, thus
+ * not affected by GC.</li>
+ * </ul>
+ * </p>
+ * <p>
+ * Buffers are not thread-safe. If concurrent access to a buffer instance is
+ * required, then the callers are responsible to take care of the
+ * synchronization issues.
+ * </p>
+ *
+ */
+public abstract class Buffer {
+
+ /**
+ * <code>UNSET_MARK</code> means the mark has not been set.
+ */
+ protected final static int UNSET_MARK = -1;
+
+ /**
+ * The capacity of this buffer, which never change.
+ */
+ protected final int capacity;
+
+ /**
+ * <code>limit - 1</code> is the last element that can be read or write.
+ * Limit must be no less than zero and no greater than <code>capacity</code>.
+ */
+ protected int limit;
+
+ /**
+ * Mark is the position will be set when <code>reset()</code> is called.
+ * Mark is not set by default. Mark is always no less than zero and no
+ * greater than <code>position</code>.
+ */
+ protected int mark = UNSET_MARK;
+
+ /**
+ * The current position of this buffer. Position is always no less than zero
+ * and no greater than <code>limit</code>.
+ */
+ protected int position = 0;
+
+ /**
+ * Construct a buffer with the specified capacity.
+ *
+ * @param capacity
+ * The capacity of this buffer
+ */
+ protected Buffer(int capacity) {
+ super();
+ if (capacity < 0) {
+ throw new IllegalArgumentException();
+ }
+ this.capacity = this.limit = capacity;
+ }
+
+ /**
+ * Returns the capacity of this buffer.
+ *
+ * @return The number of elements that are contained in this buffer.
+ */
+ public final int capacity() {
+ return capacity;
+ }
+
+ /**
+ * Clears this buffer.
+ * <p>
+ * While the content of this buffer is not changed the following internal
+ * changes take place : the current position is reset back to the start of the buffer,
+ * the value of the buffer limit is made equal to the capacity and mark is unset.
+ * </p>
+ *
+ * @return This buffer
+ */
+ public final Buffer clear() {
+ position = 0;
+ mark = UNSET_MARK;
+ limit = capacity;
+ return this;
+ }
+
+ /**
+ * Flips this buffer.
+ * <p>
+ * The limit is set to the current position, then the position is set to
+ * zero, and the mark is cleared.
+ * </p>
+ * <p>
+ * The content of this buffer is not changed.
+ * </p>
+ *
+ * @return This buffer
+ */
+ public final Buffer flip() {
+ limit = position;
+ position = 0;
+ mark = UNSET_MARK;
+ return this;
+ }
+
+ /**
+ * Returns true if there are remaining element(s) in this buffer.
+ * <p>
+ * Or more precisely, returns <code>position < limit</code>.
+ * </p>
+ *
+ * @return True if there are remaining element(s) in this buffer.
+ */
+ public final boolean hasRemaining() {
+ return position < limit;
+ }
+
+ /**
+ * Returns whether this buffer is readonly or not.
+ *
+ * @return Whether this buffer is readonly or not.
+ */
+ public abstract boolean isReadOnly();
+
+ /**
+ * Returns the limit of this buffer.
+ *
+ * @return The limit of this buffer.
+ */
+ public final int limit() {
+ return limit;
+ }
+
+ /**
+ * Sets the limit of this buffer.
+ * <p>
+ * If the current position in the buffer is in excess of
+ * <code>newLimit</code> then, on returning from this call, it will have
+ * been adjusted to be equivalent to <code>newLimit</code>. If the mark
+ * is set and is greater than the new limit, then it is cleared.
+ * </p>
+ *
+ * @param newLimit
+ * The new limit, must be no less than zero and no greater than
+ * capacity
+ * @return This buffer
+ * @exception IllegalArgumentException
+ * If <code>newLimit</code> is invalid.
+ */
+ public final Buffer limit(int newLimit) {
+ if (newLimit < 0 || newLimit > capacity) {
+ throw new IllegalArgumentException();
+ }
+
+ limit = newLimit;
+ if (position > newLimit) {
+ position = newLimit;
+ }
+ if ((mark != UNSET_MARK) && (mark > newLimit)) {
+ mark = UNSET_MARK;
+ }
+ return this;
+ }
+
+ /**
+ * Mark the current position, so that the position may return to this point
+ * later by calling <code>reset()</code>.
+ *
+ * @return This buffer
+ */
+ public final Buffer mark() {
+ mark = position;
+ return this;
+ }
+
+ /**
+ * Returns the position of this buffer.
+ *
+ * @return The value of this buffer's current position.
+ */
+ public final int position() {
+ return position;
+ }
+
+ /**
+ * Sets the position of this buffer.
+ * <p>
+ * If the mark is set and is greater than the new position, then it is
+ * cleared.
+ * </p>
+ *
+ * @param newPosition
+ * The new position, must be no less than zero and no greater
+ * than limit
+ * @return This buffer
+ * @exception IllegalArgumentException
+ * If <code>newPosition</code> is invalid
+ */
+ public final Buffer position(int newPosition) {
+ if (newPosition < 0 || newPosition > limit) {
+ throw new IllegalArgumentException();
+ }
+
+ position = newPosition;
+ if ((mark != UNSET_MARK) && (mark > position)) {
+ mark = UNSET_MARK;
+ }
+ return this;
+ }
+
+ /**
+ * Returns the number of remaining elements in this buffer.
+ * <p>
+ * Or more precisely, returns <code>limit - position</code>.
+ * </p>
+ *
+ * @return The number of remaining elements in this buffer.
+ */
+ public final int remaining() {
+ return limit - position;
+ }
+
+ /**
+ * Reset the position of this buffer to the <code>mark</code>.
+ *
+ * @return This buffer
+ * @exception InvalidMarkException
+ * If the mark is not set
+ */
+ public final Buffer reset() {
+ if (mark == UNSET_MARK) {
+ throw new InvalidMarkException();
+ }
+ position = mark;
+ return this;
+ }
+
+ /**
+ * Rewinds this buffer.
+ * <p>
+ * The position is set to zero, and the mark is cleared.
+ * </p>
+ * <p>
+ * The content of this buffer is not changed.
+ * </p>
+ *
+ * @return This buffer
+ */
+ public final Buffer rewind() {
+ position = 0;
+ mark = UNSET_MARK;
+ return this;
+ }
+}
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/BufferOverflowException.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/BufferOverflowException.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/BufferOverflowException.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/BufferOverflowException.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,35 @@
+/* Copyright 2004 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.nio;
+
+
+/**
+ * A <code>BufferOverflowException</code> is thrown when you try to write
+ * elements to a buffer, but there is not enough remaining space in the
+ * buffer.
+ *
+ */
+public class BufferOverflowException extends RuntimeException {
+
+ static final long serialVersionUID = -5484897634319144535L;
+
+ /**
+ * Construts a <code>BufferOverflowException</code>.
+ */
+ public BufferOverflowException() {
+ super();
+ }
+}
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/BufferUnderflowException.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/BufferUnderflowException.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/BufferUnderflowException.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/BufferUnderflowException.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,35 @@
+/* Copyright 2004 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.nio;
+
+
+/**
+ * A <code>BufferUnderflowException</code> is thrown when you try to read
+ * elements from a buffer, but there is not enough remaining elements in the
+ * buffer.
+ *
+ */
+public class BufferUnderflowException extends RuntimeException {
+
+ static final long serialVersionUID = -1713313658691622206L;
+
+ /**
+ * Constructs a <code>BufferUnderflowException</code>.
+ */
+ public BufferUnderflowException() {
+ super();
+ }
+}
|