Author: mturk
Date: Wed Apr 22 08:55:00 2009
New Revision: 767413
URL: http://svn.apache.org/viewvc?rev=767413&view=rev
Log:
Add Memory class and adapt Pointer so it's usable for it
Added:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Memory.java
(with props)
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestMemory.java
(with props)
Modified:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer.java
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer32.java
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer64.java
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/SystemId.java
commons/sandbox/runtime/trunk/src/main/native/include/acr.h
commons/sandbox/runtime/trunk/src/main/native/include/acr_pointer.h
commons/sandbox/runtime/trunk/src/main/native/include/acr_version.h
commons/sandbox/runtime/trunk/src/main/native/shared/memory.c
commons/sandbox/runtime/trunk/src/main/native/shared/pointer.c
commons/sandbox/runtime/trunk/src/main/native/test/testcase.c
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java
Added:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Memory.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Memory.java?rev=767413&view=auto
==============================================================================
---
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Memory.java
(added)
+++
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Memory.java
Wed Apr 22 08:55:00 2009
@@ -0,0 +1,170 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.commons.runtime;
+
+/**
+ * Memory class.
+ * <p>
+ * <b>Warning:</b><br/>Using this class improperly may crash the running JVM.
+ * </p>
+ *
+ * @see OS
+ * @since Runtime 1.0
+ */
+public final class Memory {
+
+ private Memory()
+ {
+ // No Instance
+ }
+
+ /**
+ * Allocates {...@code size} bytes and returns a {...@link Pointer}
+ * to the allocated memory. The memory is not cleared.
+ *
+ * @param size Size of the memory to allocate.
+ * @return new {...@link Pointer} containing memory area.
+ *
+ * @throws OutOfMemoryError if memory cannot be allocated.
+ * @throws IllegalArgumentException if the size is less then {...@code 1}.
+ */
+ public static native Pointer malloc(long size)
+ throws OutOfMemoryError, IllegalArgumentException;
+
+ /**
+ * Allocates {...@code size} bytes and returns a {...@link Pointer}
+ * to the allocated memory. The memory is cleared.
+ *
+ * @param size Size of the memory to allocate.
+ * @return new {...@link Pointer} containing memory area.
+ *
+ * @throws OutOfMemoryError if memory cannot be allocated.
+ * @throws IllegalArgumentException if the size is less then {...@code 1}.
+ */
+ public static native Pointer calloc(long size)
+ throws OutOfMemoryError, IllegalArgumentException;
+
+ /**
+ * Creates a new {...@link Pointer} object whose content is a shared
+ * subsequence of a {...@code src} memory address.
+ *
+ * @param src {...@code Pointer} whose content to use.
+ * @param offset offset from the {...@code src} memory address.
+ * @param size size of the destination memory.
+ * @return new {...@link Pointer}.
+ *
+ * @throws IllegalArgumentException if the {...@code offset} is
+ * {...@code negative} or {...@code size} is {...@code zero}.
+ * @throws IndexOutOfBoundsException if the operation would cause
+ * access of data outside allocated memory bounds.
+ * @throws NullPointerException if {...@code src} is {...@code null}.
+ */
+ public static native Pointer slice(Pointer src, long offset, long size)
+ throws IndexOutOfBoundsException, IllegalArgumentException,
+ NullPointerException;
+
+ /**
+ * Copy the memory area from {...@code src} to {...@code dst}.
+ * <p>
+ * Method uses the {...@code memcpy} function to do a copying, meaning
+ * that {...@code source} and {...@code destination} memory areas should
+ * not overlap.
+ * </p>
+ *
+ * @param src {...@code Pointer} whose content to use.
+ * @param srcPos starting position in the source memory.
+ * @param dst destination {...@code Pointer}.
+ * @param dstPos starting position in the destination memory.
+ * @param length the number of bytes to be copied.
+ *
+ * @throws IllegalArgumentException if the {...@code srcPos} or
+ * {...@code dstPos} is {...@code negative} or {...@code length}
+ * is {...@code zero}.
+ * @throws IndexOutOfBoundsException if the operation would cause
+ * access of data outside allocated memory bounds.
+ * @throws NullPointerException if {...@code src} or {...@code dst} is
+ * {...@code null}.
+ */
+ public static native void copy(Pointer src, long srcPos, Pointer dst,
+ long dstPos, long length)
+ throws IndexOutOfBoundsException, IllegalArgumentException,
+ NullPointerException;
+
+ /**
+ * Copy the memory area from {...@code src} to {...@code dst}.
+ * <p>
+ * Method uses the {...@code memmove} function to do a copying, meaning
+ * that {...@code src} and {...@code dst} memory areas can overlap.
+ * </p>
+ *
+ * @param src {...@code Pointer} whose content to use.
+ * @param srcPos starting position in the source memory.
+ * @param dst destination {...@code Pointer}.
+ * @param dstPos starting position in the destination memory.
+ * @param length the number of bytes to be copied.
+ *
+ * @throws IllegalArgumentException if the {...@code srcPos} or
+ * {...@code dstPos} is {...@code negative} or {...@code length}
+ * is {...@code zero}.
+ * @throws IndexOutOfBoundsException if the operation would cause
+ * access of data outside allocated memory bounds.
+ * @throws NullPointerException if {...@code src} or {...@code dst} is
+ * {...@code null}.
+ */
+ public static native void move(Pointer src, long srcPos, Pointer dst,
+ long dstPos, long length)
+ throws IndexOutOfBoundsException, IllegalArgumentException,
+ NullPointerException;
+
+ /**
+ * Set the {...@code ptr} memory area from {...@code src} to {...@code
zero}.
+ *
+ * @param ptr {...@code Pointer} whose content to use.
+ * @param offset starting position in the memory.
+ * @param length the number of bytes to be clear.
+ *
+ * @throws IllegalArgumentException if the {...@code offset}
+ * is {...@code negative} or {...@code length}
+ * is {...@code zero}.
+ * @throws IndexOutOfBoundsException if the operation would cause
+ * access of data outside allocated memory bounds.
+ * @throws NullPointerException if {...@code ptr} is {...@code null}.
+ */
+ public static native void clear(Pointer ptr, long offset, long length)
+ throws IndexOutOfBoundsException, IllegalArgumentException,
+ NullPointerException;
+
+ /**
+ * Set the {...@code ptr} memory area from {...@code src} to {...@code
val}.
+ *
+ * @param ptr {...@code Pointer} whose content to use.
+ * @param offset starting position in the memory.
+ * @param length the number of bytes to be set.
+ * @param val value to set.
+ *
+ * @throws IllegalArgumentException if the {...@code offset}
+ * is {...@code negative} or {...@code length}
+ * is {...@code zero}.
+ * @throws IndexOutOfBoundsException if the operation would cause
+ * access of data outside allocated memory bounds.
+ * @throws NullPointerException if {...@code ptr} is {...@code null}.
+ */
+ public static native void set(Pointer ptr, long offset, long length, int
val)
+ throws IndexOutOfBoundsException, IllegalArgumentException,
+ NullPointerException;
+
+}
Propchange:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Memory.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer.java?rev=767413&r1=767412&r2=767413&view=diff
==============================================================================
---
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer.java
(original)
+++
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer.java
Wed Apr 22 08:55:00 2009
@@ -36,22 +36,18 @@
private native void cleanup0()
throws Throwable;
+ private native void cleanup1();
+
/**
* Size in bytes of the storage needed to represent the Pointer.
*/
public static final int SIZEOF = OS.getDataModel() == 64 ? 8 : 4;
- /** Long value of the internal pointer
+ /** Address of the internal pointer
* @return Internal pointer address casted to the {...@code long}.
*/
- public abstract long longValue();
+ public abstract Number address();
- /** Integer value of the internal pointer
- * @return Internal pointer address casted to the {...@code int}.
- * @throws ClassCastException if running on the 64-bit JVM.
- */
- public abstract int intValue()
- throws ClassCastException;
/** Chack if the pointer is valid
* @return true if the internal pointer is not {...@code NULL}.
@@ -78,12 +74,7 @@
protected final void finalize()
throws Throwable
{
- try {
- cleanup0();
- } catch (InstantiationException ex) {
- // Class is alreay uninitialized.
- // Ignore.
- }
+ cleanup1();
}
/**
Modified:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer32.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer32.java?rev=767413&r1=767412&r2=767413&view=diff
==============================================================================
---
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer32.java
(original)
+++
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer32.java
Wed Apr 22 08:55:00 2009
@@ -26,14 +26,16 @@
private int POINTER;
private int CLEANUP;
+ private int PLENGTH;
/*
* Only created from JNI code.
*/
- private Pointer32(int ptr, int clr)
+ private Pointer32(int ptr, int clr, int len)
{
POINTER = ptr;
CLEANUP = clr;
+ PLENGTH = len;
}
public boolean equals(Object other)
@@ -52,15 +54,9 @@
return POINTER == 0;
}
- public long longValue()
+ public Number address()
{
- return POINTER;
- }
-
- public int intValue()
- throws ClassCastException
- {
- return POINTER;
+ return new Integer(POINTER);
}
public String toString()
Modified:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer64.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer64.java?rev=767413&r1=767412&r2=767413&view=diff
==============================================================================
---
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer64.java
(original)
+++
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer64.java
Wed Apr 22 08:55:00 2009
@@ -26,14 +26,16 @@
private long POINTER;
private long CLEANUP;
+ private long PLENGTH;
/*
* Only created from JNI code.
*/
- private Pointer64(long ptr, long clr)
+ private Pointer64(long ptr, long clr, long len)
{
POINTER = ptr;
CLEANUP = clr;
+ PLENGTH = len;
}
public boolean equals(Object other)
@@ -52,15 +54,9 @@
return POINTER == 0L;
}
- public long longValue()
+ public Number address()
{
- return POINTER;
- }
-
- public int intValue()
- throws ClassCastException
- {
- throw new ClassCastException();
+ return new Long(POINTER);
}
public String toString()
Modified:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/SystemId.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/SystemId.java?rev=767413&r1=767412&r2=767413&view=diff
==============================================================================
---
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/SystemId.java
(original)
+++
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/SystemId.java
Wed Apr 22 08:55:00 2009
@@ -127,7 +127,7 @@
* <p>
* Platform Id string is used to name the native libraries inside
* {...@code META-LIB} jar directory. This resource is then dynamically
- * extracted to the {...@code java.temp.path} or system temporary
+ * extracted to the {...@code java.io.tmpdir} or system temporary
* directory and then loaded.
* </p>
* <p>
Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr.h
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr.h?rev=767413&r1=767412&r2=767413&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr.h Wed Apr 22
08:55:00 2009
@@ -39,15 +39,15 @@
*/
#pragma warning(once: 4057 4075 4244)
-#pragma warning(disable: 4053) // An expression of type VOID was used as an
operand
-#pragma warning(disable: 4100) // Unreferenced formal parameter
-#pragma warning(disable: 4115) // Named type definition in parentheses
-#pragma warning(disable: 4127) // Conditional expression is a constant
-#pragma warning(disable: 4163) // '_rotl64' : not available as an intrinsic
function
-#pragma warning(disable: 4189) // Local Variable is initialized but not
referenced
-#pragma warning(disable: 4201) // Nameless struct/union (MS C/C++ specific)
-#pragma warning(disable: 4514) // unreferenced inline function removed
-#pragma warning(disable: 4706) // Assignment within conditional expression
+#pragma warning(disable: 4053) /* An expression of type VOID was used as an
operand */
+#pragma warning(disable: 4100) /* Unreferenced formal parameter
*/
+#pragma warning(disable: 4115) /* Named type definition in parentheses
*/
+#pragma warning(disable: 4127) /* Conditional expression is a constant
*/
+#pragma warning(disable: 4163) /* '_rotl64' : not available as an intrinsic
function */
+#pragma warning(disable: 4189) /* Local Variable is initialized but not
referenced */
+#pragma warning(disable: 4201) /* Nameless struct/union (MS C/C++ specific)
*/
+#pragma warning(disable: 4514) /* unreferenced inline function removed
*/
+#pragma warning(disable: 4706) /* Assignment within conditional expression
*/
/* Ignore Microsoft's interpretation of secure development
* and the POSIX string handling API
@@ -210,8 +210,13 @@
typedef wchar_t acr_pchar_t;
#else
typedef ssize_t acr_ssize_t;
+#ifdef _LP64 /* 64-bit Solaris */
+typedef long acr_int64_t;
+typedef unsigned long acr_uint64_t;
+#else
typedef long long acr_int64_t;
typedef unsigned long long acr_uint64_t;
+#endif
#if CC_SIZEOF_OFF64_T
typedef off64_t acr_off_t;
#else
Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_pointer.h
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_pointer.h?rev=767413&r1=767412&r2=767413&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr_pointer.h
(original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr_pointer.h Wed Apr
22 08:55:00 2009
@@ -42,9 +42,10 @@
* Create new Pointer class instance
* @param env Current JNI environment
* @param p Native pointer to wrap into Pointer class
+ * @param len Length of the pointer.
* @param cb callback function to use on Pointer.finalize()
*/
-ACR_DECLARE(jobject) ACR_PointerCreate(JNIEnv *env, void *p,
+ACR_DECLARE(jobject) ACR_PointerCreate(JNIEnv *env, void *p, size_t len,
acr_pointer_cleanup_fn_t *cb);
/**
@@ -67,18 +68,21 @@
* Get the native pointer from the Pointer object.
* @param env Current JNI environment
* @param ptr Java Pointer object use.
+ * @param len Pointer to receive the length of the pointer.
* @return Underlying wrapped pointer.
*/
-ACR_DECLARE(void *) ACR_PointerGet(JNIEnv *env, jobject ptr);
+ACR_DECLARE(void *) ACR_PointerGet(JNIEnv *env, jobject ptr, size_t *len);
/**
* Set the native pointer to the Pointer object.
* @param env Current JNI environment
* @param ptr Java Pointer object use.
* @param val Value to set.
+ * @param len length of the pointer.
* @return ACR error code on failure.
*/
-ACR_DECLARE(int) ACR_PointerSet(JNIEnv *env, jobject ptr, void *val);
+ACR_DECLARE(int) ACR_PointerSet(JNIEnv *env, jobject ptr, void *val,
+ size_t len);
#ifdef __cplusplus
}
Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_version.h
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_version.h?rev=767413&r1=767412&r2=767413&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr_version.h
(original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr_version.h Wed Apr
22 08:55:00 2009
@@ -94,7 +94,7 @@
/** Return build timestamp
*/
-ACR_DECLARE(const char *) ACR_GetLibraryBuilt();
+ACR_DECLARE(const char *) ACR_GetLibraryBuilt(void);
#ifdef __cplusplus
}
Modified: commons/sandbox/runtime/trunk/src/main/native/shared/memory.c
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/memory.c?rev=767413&r1=767412&r2=767413&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/memory.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/memory.c Wed Apr 22
08:55:00 2009
@@ -18,6 +18,7 @@
#include "acr_private.h"
#include "acr_memory.h"
#include "acr_error.h"
+#include "acr_pointer.h"
#include "acr_vm.h"
struct acr_sbh_t {
@@ -218,3 +219,198 @@
if (sbh->owns)
free(sbh);
}
+
+
+static int ptr_cleanup(void *mem)
+{
+ if (mem) {
+ free(mem);
+ return 0;
+ }
+ else {
+ return ACR_EISNULL;
+ }
+}
+
+ACR_JNI_EXPORT_DECLARE(jobject, Memory, malloc)(ACR_JNISTDARGS, jlong siz)
+{
+ jobject ptr = NULL;
+ void *mem;
+ acr_size_t ass = (acr_size_t)ACR_ALIGN_DEFAULT(siz);
+
+ UNREFERENCED_O;
+ if (siz < 1L) {
+ ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINVAL, 0);
+ return NULL;
+ }
+ mem = ACR_Malloc(_E, THROW_NMARK, ass);
+ if (mem) {
+ /* Create the Pointer class with default cleanup.
+ */
+ ptr = ACR_PointerCreate(_E, mem, (size_t)siz, ptr_cleanup);
+ }
+ return ptr;
+}
+
+ACR_JNI_EXPORT_DECLARE(jobject, Memory, calloc)(ACR_JNISTDARGS, jlong siz)
+{
+ jobject ptr = NULL;
+ void *mem;
+ acr_size_t ass = (acr_size_t)ACR_ALIGN_DEFAULT(siz);
+
+ UNREFERENCED_O;
+ if (siz < 1L) {
+ ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINVAL, 0);
+ return NULL;
+ }
+ mem = ACR_Calloc(_E, THROW_NMARK, ass);
+ if (mem) {
+ /* Create the Pointer class with default cleanup.
+ */
+ ptr = ACR_PointerCreate(_E, mem, (size_t)siz, ptr_cleanup);
+ }
+ return ptr;
+}
+
+ACR_JNI_EXPORT_DECLARE(jobject, Memory, slice)(ACR_JNISTDARGS,
+ jobject src, jlong off,
+ jlong siz)
+{
+ jobject ptr = NULL;
+ size_t so = (size_t)off;
+ size_t ss = (size_t)siz;
+ size_t sl;
+ char *sp = (char *)ACR_PointerGet(_E, src, &sl);
+
+ UNREFERENCED_O;
+
+ if (!sp) {
+ ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ENULL, 0);
+ return NULL;
+ }
+ if (off < 0L || siz < 1L) {
+ ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINVAL, 0);
+ return NULL;
+ }
+ if ((so + ss) > sl) {
+ ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINDEX, 0);
+ return NULL;
+ }
+
+ /* Create the Pointer class without the cleanup.
+ * Original object will clean the entire allocated memory.
+ */
+ ptr = ACR_PointerCreate(_E, sp + so, ss, NULL);
+ return ptr;
+}
+
+ACR_JNI_EXPORT_DECLARE(void, Memory, copy)(ACR_JNISTDARGS, jobject src,
+ jlong soff, jobject dst,
+ jlong doff, jlong size)
+{
+ size_t dl;
+ size_t sl;
+ size_t dn = (size_t)doff;
+ size_t sn = (size_t)soff;
+ size_t cs = (size_t)size;
+ char *d = (char *)ACR_PointerGet(_E, dst, &dl);
+ char *s = (char *)ACR_PointerGet(_E, src, &sl);
+ if (!d || !s) {
+ ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ENULL, 0);
+ return;
+ }
+ if (doff < 0L || soff < 0L || size < 1L) {
+ ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINVAL, 0);
+ return;
+ }
+ if ((dn + cs) > dl) {
+ ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINDEX, 0);
+ return;
+ }
+ if ((sn + cs) > sl) {
+ ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINDEX, 0);
+ return;
+ }
+
+ /* Do a memcpy */
+ memcpy(d + dn, s + sn, cs);
+}
+
+ACR_JNI_EXPORT_DECLARE(void, Memory, move)(ACR_JNISTDARGS, jobject src,
+ jlong soff, jobject dst,
+ jlong doff, jlong size)
+{
+ size_t dl;
+ size_t sl;
+ size_t dn = (size_t)doff;
+ size_t sn = (size_t)soff;
+ size_t cs = (size_t)size;
+ char *d = (char *)ACR_PointerGet(_E, dst, &dl);
+ char *s = (char *)ACR_PointerGet(_E, src, &sl);
+ if (!d || !s) {
+ ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ENULL, 0);
+ return;
+ }
+ if (doff < 0L || soff < 0L || size < 1L) {
+ ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINVAL, 0);
+ return;
+ }
+ if ((dn + cs) > dl) {
+ ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINDEX, 0);
+ return;
+ }
+ if ((sn + cs) > sl) {
+ ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINDEX, 0);
+ return;
+ }
+
+ /* Do a memmove */
+ memmove(d + dn, s + sn, cs);
+}
+
+ACR_JNI_EXPORT_DECLARE(void, Memory, clear)(ACR_JNISTDARGS, jobject dst,
+ jlong doff, jlong size)
+{
+ size_t dl;
+ size_t dn = (size_t)doff;
+ size_t cs = (size_t)size;
+ char *d = (char *)ACR_PointerGet(_E, dst, &dl);
+ if (!d) {
+ ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ENULL, 0);
+ return;
+ }
+ if (doff < 0L || size < 1L) {
+ ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINVAL, 0);
+ return;
+ }
+ if ((doff + size) > dl) {
+ ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINDEX, 0);
+ return;
+ }
+ /* Do a memset */
+ memset(d + dn, 0, cs);
+}
+
+ACR_JNI_EXPORT_DECLARE(void, Memory, set)(ACR_JNISTDARGS, jobject dst,
+ jlong doff, jlong size, jint c)
+{
+ size_t dl;
+ size_t dn = (size_t)doff;
+ size_t cs = (size_t)size;
+ char *d = (char *)ACR_PointerGet(_E, dst, &dl);
+ if (!d) {
+ ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ENULL, 0);
+ return;
+ }
+ if (doff < 0L || size < 1L) {
+ ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINVAL, 0);
+ return;
+ }
+ if ((doff + size) > dl) {
+ ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINDEX, 0);
+ return;
+ }
+ /* Do a memset */
+ memset(d + dn, c, cs);
+}
+
Modified: commons/sandbox/runtime/trunk/src/main/native/shared/pointer.c
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/pointer.c?rev=767413&r1=767412&r2=767413&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/pointer.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/pointer.c Wed Apr 22
08:55:00 2009
@@ -39,7 +39,7 @@
J_DECLARE_M_ID(0000) = {
NULL,
"<init>",
- "(JJ)V"
+ "(JJJ)V"
};
#else
@@ -53,7 +53,7 @@
J_DECLARE_M_ID(0000) = {
NULL,
"<init>",
- "(II)V"
+ "(III)V"
};
#endif
@@ -70,6 +70,12 @@
IFIELD_PTR
};
+J_DECLARE_F_ID(0002) = {
+ NULL,
+ "PLENGTH",
+ IFIELD_PTR
+};
+
ACR_CLASS_LDEF(Pointer)
{
@@ -79,6 +85,7 @@
return rv;
J_LOAD_IFIELD(0000);
J_LOAD_IFIELD(0001);
+ J_LOAD_IFIELD(0002);
J_LOAD_METHOD(0000);
return ACR_SUCCESS;
@@ -121,12 +128,29 @@
}
-ACR_DECLARE(jobject) ACR_PointerCreate(JNIEnv *_E, void *p,
+ACR_JNI_EXPORT_DECLARE(void, Pointer, cleanup1)(ACR_JNISTDARGS)
+{
+ if (_clazzn.i && J4MID(0000)) {
+ acr_pointer_cleanup_fn_t *cleanup;
+ jniptr h = GET_IFIELD_P(0000, _O);
+ jniptr c = GET_IFIELD_P(0001, _O);
+
+ if (h) {
+ SET_IFIELD_P(0000, _O, 0);
+ }
+ cleanup = (acr_pointer_cleanup_fn_t *)((acr_ptr_t)c);
+ if (cleanup) {
+ (*cleanup)((void *)((acr_ptr_t)h));
+ }
+ }
+}
+
+ACR_DECLARE(jobject) ACR_PointerCreate(JNIEnv *_E, void *p, size_t len,
acr_pointer_cleanup_fn_t *cb)
{
if (_clazzn.i && J4MID(0000))
return (*_E)->NewObject(_E, _clazzn.i, J4MID(0000),
- (acr_ptr_t)p, (acr_ptr_t)cb);
+ (acr_ptr_t)p, (acr_ptr_t)cb, (acr_ptr_t)len);
else {
ACR_SET_OS_ERROR(ACR_ECLASSNOTFOUND);
return NULL;
@@ -135,7 +159,7 @@
ACR_DECLARE(int) ACR_PointerCleanup(ACR_JNISTDARGS)
{
- if (_clazzn.i && J4MID(0000)) {
+ if (_clazzn.i && J4MID(0000) && _O) {
acr_pointer_cleanup_fn_t *cleanup;
jniptr h = GET_IFIELD_P(0000, _O);
jniptr c = GET_IFIELD_P(0001, _O);
@@ -164,10 +188,13 @@
return ACR_ECLASSNOTFOUND;
}
-ACR_DECLARE(void *) ACR_PointerGet(ACR_JNISTDARGS)
+ACR_DECLARE(void *) ACR_PointerGet(ACR_JNISTDARGS, size_t *len)
{
- if (_clazzn.i && J4MID(0000)) {
+ if (_clazzn.i && J4MID(0000) && _O) {
jniptr h = GET_IFIELD_P(0000, _O);
+ if (len) {
+ *len = (size_t)GET_IFIELD_P(0002, _O);
+ }
return (void *)((acr_ptr_t)h);
}
else {
@@ -176,19 +203,35 @@
}
}
-ACR_DECLARE(int) ACR_PointerSet(ACR_JNISTDARGS, void *p)
+ACR_DECLARE(int) ACR_PointerSet(ACR_JNISTDARGS, void *p, size_t len)
{
- if (_clazzn.i && J4MID(0000)) {
+ if (_clazzn.i && J4MID(0000) && _O) {
SET_IFIELD_P(0000, _O, (jniptr)((acr_ptr_t)p));
#ifdef _JNI_CHECK_EXCEPTIONS
if ((*_E)->ExceptionCheck(_E)) {
return ACR_EGENERAL;
}
- else
#endif
- return ACR_SUCCESS;
+ if (len) {
+ SET_IFIELD_P(0002, _O, (jniptr)len);
+#ifdef _JNI_CHECK_EXCEPTIONS
+ if ((*_E)->ExceptionCheck(_E)) {
+ return ACR_EGENERAL;
+ }
+#endif
+ }
+ return ACR_SUCCESS;
}
else {
return ACR_ECLASSNOTFOUND;
}
}
+
+#ifdef ACR_ENABLE_TEST
+/* Just for testing the cache table */
+ACR_JNI_EXPORT_DECLARE(int, TestMemory, initp0)(ACR_JNISTDARGS, jint d)
+{
+ ACR_CLASS_LRUN(Pointer);
+ return 0;
+}
+#endif
Modified: commons/sandbox/runtime/trunk/src/main/native/test/testcase.c
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/test/testcase.c?rev=767413&r1=767412&r2=767413&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/test/testcase.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/test/testcase.c Wed Apr 22
08:55:00 2009
@@ -220,7 +220,7 @@
ACR_JNI_EXPORT_DECLARE(jobject, TestPrivate, test017)(ACR_JNISTDARGS, jint d)
{
- return ACR_PointerCreate(_E, I2P(d, void *), callback);
+ return ACR_PointerCreate(_E, I2P(d, void *), 0, callback);
}
ACR_JNI_EXPORT_DECLARE(jint, TestPrivate, test018)(ACR_JNISTDARGS, jobject p)
@@ -231,7 +231,7 @@
ACR_JNI_EXPORT_DECLARE(jint, TestPrivate, test019)(ACR_JNISTDARGS, jobject p)
{
- void *v = ACR_PointerGet(_E, p);
+ void *v = ACR_PointerGet(_E, p, NULL);
return v != NULL;
}
Modified:
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java?rev=767413&r1=767412&r2=767413&view=diff
==============================================================================
---
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java
(original)
+++
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java
Wed Apr 22 08:55:00 2009
@@ -34,6 +34,7 @@
suite.addTest(TestProperties.suite());
suite.addTest(TestOS.suite());
suite.addTest(TestUUID.suite());
+ suite.addTest(TestMemory.suite());
suite.addTest(TestDirectByteBuffer.suite());
suite.addTest(TestPrivate.suite());
suite.addTest(TestFile.suite());
Added:
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestMemory.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestMemory.java?rev=767413&view=auto
==============================================================================
---
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestMemory.java
(added)
+++
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestMemory.java
Wed Apr 22 08:55:00 2009
@@ -0,0 +1,131 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.commons.runtime;
+
+import java.lang.System;
+import java.nio.ByteBuffer;
+import junit.framework.*;
+
+/**
+ * Memory tests
+ */
+public class TestMemory extends TestCase
+{
+
+ private static native int initp0(int d);
+
+ public static Test suite() {
+ TestSuite suite = new TestSuite(TestMemory.class);
+ return suite;
+ }
+
+ protected void setUp()
+ throws Exception
+ {
+ System.loadLibrary("acr");
+ initp0(0);
+ }
+
+
+ public void testMalloc()
+ throws Throwable
+ {
+ Pointer p = Memory.malloc(1000);
+ assertNotNull("Pointer", p);
+ p.free();
+ }
+
+ public void testMallocAddr()
+ throws Throwable
+ {
+ Pointer p = Memory.malloc(1000);
+ assertNotNull("Pointer", p);
+ assertFalse("Null address", 0L == p.address().longValue());
+ p.free();
+ }
+
+ public void testCalloc()
+ throws Throwable
+ {
+ Pointer p = Memory.calloc(1000);
+ assertNotNull("Pointer", p);
+ p.free();
+ }
+
+ public void testSlice()
+ throws Throwable
+ {
+ Pointer src = Memory.calloc(1000);
+ assertNotNull("Pointer", src);
+ Pointer dst = Memory.slice(src, 0, 1000);
+ assertNotNull("Pointer", dst);
+ try {
+ Pointer t = Memory.slice(src, 1, 1000);
+ fail("Missing IndexOutOfBoundsException");
+ } catch (IndexOutOfBoundsException ix) {
+ // Succedded.
+ }
+ try {
+ Pointer t = Memory.slice(null, 0, 0);
+ fail("Missing NullPointerException");
+ } catch (NullPointerException no) {
+ // Succedded.
+ }
+ try {
+ Pointer t = Memory.slice(src, 0, 0);
+ fail("Missing IllegalArgumentException");
+ } catch (IllegalArgumentException ia) {
+ // Succedded.
+ }
+ src.free();
+ dst.free();
+ }
+
+ public void testSet()
+ throws Throwable
+ {
+ Pointer src = Memory.malloc(1000);
+ assertNotNull("Source Pointer", src);
+ Memory.set(src, 0, 1000, 0xFF);
+ src.free();
+ }
+
+ public void testClr()
+ throws Throwable
+ {
+ Pointer src = Memory.malloc(1000);
+ assertNotNull("Source Pointer", src);
+ Memory.clear(src, 0, 1000);
+ src.free();
+ }
+
+ public void testCopy()
+ throws Throwable
+ {
+ Pointer src = Memory.calloc(1000);
+ assertNotNull("Source Pointer", src);
+ Memory.set(src, 0, 1000, 0xFF);
+ Pointer dst = Memory.calloc(1000);
+ assertNotNull("Dest Pointer", dst);
+ Memory.copy(src, 0, dst, 0, 1000);
+
+ src.free();
+ dst.free();
+ }
+
+}
+
Propchange:
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestMemory.java
------------------------------------------------------------------------------
svn:eol-style = native