Return-Path: X-Original-To: apmail-commons-commits-archive@minotaur.apache.org Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 283578797 for ; Sat, 3 Sep 2011 11:13:01 +0000 (UTC) Received: (qmail 93592 invoked by uid 500); 3 Sep 2011 11:12:59 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 93218 invoked by uid 500); 3 Sep 2011 11:12:43 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 93211 invoked by uid 99); 3 Sep 2011 11:12:41 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 03 Sep 2011 11:12:41 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 03 Sep 2011 11:12:38 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 7608523889E2 for ; Sat, 3 Sep 2011 11:12:18 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: commits@commons.apache.org From: mturk@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110903111218.7608523889E2@eris.apache.org> 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; }