Author: mturk
Date: Sat Sep 3 10:56:57 2011
New Revision: 1164845
URL: http://svn.apache.org/viewvc?rev=1164845&view=rev
Log:
Implement win32 exec memory
Modified:
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
commons/sandbox/runtime/trunk/src/main/native/os/win32/winapi.c
commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestMemory.java
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=1164845&r1=1164844&r2=1164845&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 10:56:57 2011
@@ -36,7 +36,7 @@ final class ExecutableMemoryPointer exte
/*
* Only created from JNI code.
*/
- private ExecutableMemoryPointer(long ptr, long len)
+ public ExecutableMemoryPointer(long ptr, long len)
{
POINTER = ptr;
PLENGTH = len;
@@ -55,7 +55,7 @@ final class ExecutableMemoryPointer exte
if (POINTER == 0L)
throw new NullPointerException();
try {
- int rc = Win32.VirtualFree(POINTER, PLENGTH, Win32.MEM_RELEASE);
+ int rc = Win32.VirtualFree(POINTER, 0L, Win32.MEM_RELEASE);
if (rc != 0) {
throw new SystemException(Status.describe(rc));
}
@@ -75,11 +75,12 @@ final class ExecutableMemoryPointer exte
throws Throwable
{
try {
- Win32.VirtualFree(POINTER, PLENGTH, Win32.MEM_RELEASE);
+ if (POINTER != 0L)
+ Win32.VirtualFree(POINTER, 0L, Win32.MEM_RELEASE);
} finally {
POINTER = 0L;
}
}
-
+
}
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=1164845&r1=1164844&r2=1164845&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 10:56:57 2011
@@ -13,8 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.commons.runtime.platform.unix;
+package org.apache.commons.runtime.platform.windows;
+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 +40,41 @@ final class WindowsExecutableMemoryImpl
public final Pointer malloc(long size)
throws OutOfMemoryError, InvalidArgumentException
{
- return null;
+ 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);
+ } catch (Exception ex) {
+ Win32.VirtualFree(mem, 0L, Win32.MEM_RELEASE);
+ // XXX: Is this a correct exception to throw?
+ throw new OutOfMemoryError();
+ }
+ return ptr;
}
@Override
public final Pointer calloc(long size)
throws OutOfMemoryError, InvalidArgumentException
{
- return null;
+ 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();
+ }
+ return ptr;
}
}
Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/winapi.c
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/winapi.c?rev=1164845&r1=1164844&r2=1164845&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/winapi.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/winapi.c Sat Sep 3
10:56:57 2011
@@ -348,7 +348,7 @@ ACR_WIN_EXPORT(jlong, Win32, CreateServi
WITH_DWCS(ldgrp) {
WITH_DWCS(user) {
WITH_DWCS(password) {
- wchar_t *wdeps = 0;
+ wchar_t *wdeps = 0;
if (deps != 0)
wdeps = (wchar_t *)(*env)->GetPrimitiveArrayCritical(env, deps, 0);
@@ -393,7 +393,7 @@ ACR_WIN_EXPORT(jint, Win32, ChangeServic
WITH_DWCS(ldgrp) {
WITH_DWCS(user) {
WITH_DWCS(password) {
- wchar_t *wdeps = 0;
+ wchar_t *wdeps = 0;
if (deps != 0)
wdeps = (wchar_t *)(*env)->GetPrimitiveArrayCritical(env, deps, 0);
/* Call the real API */
Modified:
commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestMemory.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestMemory.java?rev=1164845&r1=1164844&r2=1164845&view=diff
==============================================================================
---
commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestMemory.java
(original)
+++
commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestMemory.java
Sat Sep 3 10:56:57 2011
@@ -104,4 +104,13 @@ public class TestMemory extends Assert
p.free();
}
+ @Test(groups = { "private" })
+ public void allocExec()
+ throws Throwable
+ {
+ Pointer p = ExecutableMemory.malloc(65536);
+ assertNotNull(p);
+ p.free();
+ }
+
}