commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r1164846 - in /commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform: unix/ExecutableMemoryPointer.java unix/PosixExecutableMemoryImpl.java windows/ExecutableMemoryPointer.java windows/WindowsExecutableMemoryImpl.java
Date Sat, 03 Sep 2011 11:12:18 GMT
Author: mturk
Date: Sat Sep  3 11:12:17 2011
New Revision: 1164846

URL: http://svn.apache.org/viewvc?rev=1164846&view=rev
Log:
Implement Posix exec memory alloc

Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/ExecutableMemoryPointer.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/PosixExecutableMemoryImpl.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/ExecutableMemoryPointer.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsExecutableMemoryImpl.java

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/ExecutableMemoryPointer.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/ExecutableMemoryPointer.java?rev=1164846&r1=1164845&r2=1164846&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/ExecutableMemoryPointer.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/ExecutableMemoryPointer.java
Sat Sep  3 11:12:17 2011
@@ -33,10 +33,10 @@ final class ExecutableMemoryPointer exte
         // No instance
     }
 
-    /*
-     * Only created from JNI code.
+    /**
+     * Create new Pointer
      */
-    private ExecutableMemoryPointer(long ptr, long len)
+    public ExecutableMemoryPointer(long ptr, long len)
     {
         POINTER = ptr;
         PLENGTH = len;

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/PosixExecutableMemoryImpl.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/PosixExecutableMemoryImpl.java?rev=1164846&r1=1164845&r2=1164846&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/PosixExecutableMemoryImpl.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/PosixExecutableMemoryImpl.java
Sat Sep  3 11:12:17 2011
@@ -15,6 +15,8 @@
  */
 package org.apache.commons.runtime.platform.unix;
 
+import org.apache.commons.runtime.Errno;
+import org.apache.commons.runtime.Memory;
 import org.apache.commons.runtime.Pointer;
 import org.apache.commons.runtime.ExecutableMemoryImpl;
 import org.apache.commons.runtime.InvalidArgumentException;
@@ -39,14 +41,33 @@ final class PosixExecutableMemoryImpl ex
     public final Pointer malloc(long size)
         throws OutOfMemoryError, InvalidArgumentException
     {
-        return null;
+        if (size < 1L)
+            throw new InvalidArgumentException();
+        long mem = Posix.mmap(0L, size,
+                              Posix.PROT_EXEC | Posix.PROT_READ | Posix.PROT_WRITE,
+                              Posix.MAP_ANONYMOUS | Posix.MAP_EXECUTABLE | Posix.MAP_PRIVATE,
+                              -1, 0L);
+        if (mem == 0L) {
+            throw new OutOfMemoryError();
+        }
+        Pointer ptr;
+        try {
+            ptr = new ExecutableMemoryPointer(mem, size);
+        } catch (Exception ex) {
+            Posix.munmap(mem, size);
+            // XXX: Is this a correct exception to throw?
+            throw new OutOfMemoryError(Errno.msg());
+        }
+        return ptr;
     }
 
     @Override
     public final Pointer calloc(long size)
         throws OutOfMemoryError, InvalidArgumentException
     {
-        return null;
+        Pointer ptr = malloc(size);
+        Memory.clear(ptr, 0L, size);
+        return ptr;
     }
 
 }

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/ExecutableMemoryPointer.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/ExecutableMemoryPointer.java?rev=1164846&r1=1164845&r2=1164846&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/ExecutableMemoryPointer.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/ExecutableMemoryPointer.java
Sat Sep  3 11:12:17 2011
@@ -33,8 +33,8 @@ final class ExecutableMemoryPointer exte
         // No instance
     }
 
-    /*
-     * Only created from JNI code.
+    /**
+     * Create new Pointer
      */
     public ExecutableMemoryPointer(long ptr, long len)
     {

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsExecutableMemoryImpl.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsExecutableMemoryImpl.java?rev=1164846&r1=1164845&r2=1164846&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsExecutableMemoryImpl.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsExecutableMemoryImpl.java
Sat Sep  3 11:12:17 2011
@@ -15,6 +15,7 @@
  */
 package org.apache.commons.runtime.platform.windows;
 
+import org.apache.commons.runtime.Errno;
 import org.apache.commons.runtime.Memory;
 import org.apache.commons.runtime.Pointer;
 import org.apache.commons.runtime.ExecutableMemoryImpl;
@@ -51,7 +52,7 @@ final class WindowsExecutableMemoryImpl 
         } catch (Exception ex) {
             Win32.VirtualFree(mem, 0L, Win32.MEM_RELEASE);
             // XXX: Is this a correct exception to throw?
-            throw new OutOfMemoryError();
+            throw new OutOfMemoryError(Errno.msg());
         }
         return ptr;
     }
@@ -60,20 +61,8 @@ final class WindowsExecutableMemoryImpl 
     public final Pointer calloc(long size)
         throws OutOfMemoryError, InvalidArgumentException
     {
-        if (size < 1L)
-            throw new InvalidArgumentException();
-        long mem = Win32.VirtualAlloc(0L, size, Win32.MEM_COMMIT | Win32.MEM_RESERVE, AccessRights.PAGE_EXECUTE_READWRITE);
-        if (mem == 0L)
-            throw new OutOfMemoryError();
-        Pointer ptr;
-        try {
-            ptr = new ExecutableMemoryPointer(mem, size);
-            Memory.clear(ptr, 0L, size);
-        } catch (Exception ex) {
-            Win32.VirtualFree(mem, 0L, Win32.MEM_RELEASE);
-            // XXX: Is this a correct exception to throw?
-            throw new OutOfMemoryError();
-        }
+        Pointer ptr = malloc(size);
+        Memory.clear(ptr, 0L, size);
         return ptr;
     }
 



Mime
View raw message