You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2009/04/24 10:38:33 UTC

svn commit: r768221 - in /commons/sandbox/runtime/trunk/src: main/java/org/apache/commons/runtime/ main/native/include/ main/native/shared/ test/org/apache/commons/runtime/

Author: mturk
Date: Fri Apr 24 08:38:33 2009
New Revision: 768221

URL: http://svn.apache.org/viewvc?rev=768221&view=rev
Log:
Add Memory.realloc

Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Memory.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer.java
    commons/sandbox/runtime/trunk/src/main/native/include/acr_memory.h
    commons/sandbox/runtime/trunk/src/main/native/shared/memory.c
    commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestMemory.java

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor.java?rev=768221&r1=768220&r2=768221&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor.java Fri Apr 24 08:38:33 2009
@@ -58,7 +58,7 @@
      * Represents a C/C++ NULL {@code descriptor}.
      */
     public static final Descriptor NULL;
-    static {    
+    static {
         NULL = nulld0();
     }
 

Modified: 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=768221&r1=768220&r2=768221&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Memory.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Memory.java Fri Apr 24 08:38:33 2009
@@ -59,6 +59,18 @@
         throws OutOfMemoryError, IllegalArgumentException;
 
     /**
+     * Change the size of memory block pointed by {@code ptr}.
+     *
+     * @param prt Pointer which size to change..
+     * @param size The new size of the memory block.
+     *
+     * @throws OutOfMemoryError if memory cannot be allocated.
+     * @throws IllegalArgumentException if the size is less then {@code 1}.
+     */
+    public static native void realloc(Pointer ptr, long size)
+        throws OutOfMemoryError, IllegalArgumentException;
+
+    /**
      * Creates a new {@link Pointer} object whose content is a shared
      * subsequence of a {@code src} memory address.
      *

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=768221&r1=768220&r2=768221&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 Fri Apr 24 08:38:33 2009
@@ -45,7 +45,7 @@
      * Represents a C/C++ NULL {@code pointer}.
      */
     public static final Pointer NULL;
-    static {    
+    static {
         NULL = nullp0();
     }
 

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_memory.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_memory.h?rev=768221&r1=768220&r2=768221&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr_memory.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr_memory.h Fri Apr 24 08:38:33 2009
@@ -60,6 +60,19 @@
                                acr_size_t size);
 
 /**
+ * Apache's "replacement" for the realloc() function that throws
+ * thejava.lang.OutOfMemoryError in case of failure.
+ * @param env Current JNI environment.
+ * @param org Memory block to reallocate.
+ * @param size New allocation size
+ * @param file Source file where the function was called
+ * @param line Source file line where the function was called
+ * @return Pointer to reallocated memory.
+ */
+ACR_DECLARE(void *) ACR_Realloc(JNIEnv *_E, const char *file, int line,
+                                void *org, acr_size_t size);
+
+/**
  * Apache's "replacement" for the free() function that throws
  * the java.lang.NullPointerException in case of memory is NULL.
  * @param env Current JNI environment.

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=768221&r1=768220&r2=768221&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/memory.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/memory.c Fri Apr 24 08:38:33 2009
@@ -61,6 +61,21 @@
     return mem;
 }
 
+ACR_DECLARE(void *) ACR_Realloc(JNIEnv *_E, const char *file, int line,
+                                void *org, acr_size_t size)
+{
+    void *mem = realloc(org, size);
+
+    if (!mem) {
+        if (_E == NULL)
+            _E = ACR_GetJNIEnv();
+        if (_E != NULL)
+            ACR_ThrowException(_E, file, line, ACR_EX_ENOMEM,
+                               ACR_GET_OS_ERROR());
+    }
+    return mem;
+}
+
 ACR_DECLARE(void) ACR_Free(JNIEnv *_E, const char *file, int line,
                            void *mem)
 {
@@ -272,6 +287,27 @@
     return ptr;
 }
 
+ACR_JNI_EXPORT_DECLARE(void, Memory, realloc)(ACR_JNISTDARGS,
+                                              jobject src,
+                                              jlong siz)
+{
+    void   *np;
+    size_t  ss = (size_t)siz;
+    void   *op = ACR_PointerGet(_E, src, NULL);
+
+    UNREFERENCED_O;
+
+    if (siz < 1L) {
+        ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINVAL, 0);
+        return;
+    }
+    np = ACR_Realloc(_E, THROW_NMARK, op, ss);
+    if (!np) {
+        return;
+    }
+    ACR_PointerSet(_E, src, np, ss);
+}
+
 ACR_JNI_EXPORT_DECLARE(jobject, Memory, slice)(ACR_JNISTDARGS,
                                                jobject src, jlong off,
                                                jlong siz)

Modified: 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=768221&r1=768220&r2=768221&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestMemory.java (original)
+++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestMemory.java Fri Apr 24 08:38:33 2009
@@ -66,6 +66,17 @@
         p.free();
     }
 
+    public void testRealloc()
+        throws Throwable
+    {
+        Pointer p = Memory.calloc(1000);
+        p.poke(999, 23);
+        Memory.realloc(p, 9999);
+        assertNotNull("Pointer", p);
+        assertEquals("Value", 23, p.peek(999));
+        p.free();
+    }
+
     public void testSlice()
         throws Throwable
     {
@@ -177,8 +188,6 @@
     public void testNull()
         throws Throwable
     {
-//        Pointer p = Pointer.NULL;
-//        assertNotNull("Pointer", p);
         assertTrue("Not null", Pointer.NULL.IsNull());
 
     }