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/09/01 08:39:57 UTC

svn commit: r809848 - in /commons/sandbox/runtime/trunk/src: main/java/org/apache/commons/runtime/io/ main/native/os/unix/ main/native/os/win32/ test/org/apache/commons/runtime/

Author: mturk
Date: Tue Sep  1 06:39:56 2009
New Revision: 809848

URL: http://svn.apache.org/viewvc?rev=809848&view=rev
Log:
Implement map create API

Added:
    commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestMemoryMap.java   (with props)
Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/MemoryMapProvider.java
    commons/sandbox/runtime/trunk/src/main/native/os/unix/pmmap.c
    commons/sandbox/runtime/trunk/src/main/native/os/win32/pmmap.c
    commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/MemoryMapProvider.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/MemoryMapProvider.java?rev=809848&r1=809847&r2=809848&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/MemoryMapProvider.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/MemoryMapProvider.java Tue Sep  1 06:39:56 2009
@@ -55,6 +55,48 @@
         this.map = map;
     }
 
+    private static native Descriptor create0(String fname, int flags)
+        throws IOException, SecurityException, OutOfMemoryError;
+    /**
+     * Create new MemoryMapPrivider from the abstract {@code File}.
+     * <p>
+     * </p>
+     * @param path Abstract path name of the existing file to map.
+     * @param mode Open mode flags.
+     */
+    public  MemoryMapProvider(File path, EnumSet<FileOpenMode> mode)
+        throws IOException, IllegalArgumentException, OutOfMemoryError
+    {
+        int flags = FileOpenMode.bitmapOf(mode);
+        if (flags == 0)
+            throw new IllegalArgumentException();
+        map = create0(path.getPath(), flags);
+    }
+
+    private static native Descriptor create1(int fd, int flags)
+        throws IOException, SecurityException, OutOfMemoryError;
+    /**
+     * Create new MemoryMapPrivider from the esisting filr {@code Descriptor}.
+     * <p>
+     * </p>
+     * @param fd Valid file descriptor.
+     * @param mode Open mode flags.
+     */
+    public  MemoryMapProvider(Descriptor fd, EnumSet<FileOpenMode> mode)
+        throws IOException, IllegalArgumentException, OutOfMemoryError,
+               ClosedDescriptorException
+    {
+        int flags = FileOpenMode.bitmapOf(mode);
+        if (flags == 0)
+            throw new IllegalArgumentException();
+        if (fd.valid()) {
+            map = create1(fd.fd(), flags);
+        }
+        else {
+            throw new ClosedDescriptorException();
+        }
+    }
+
     private static native Pointer map0(int md, long offset, long size)
         throws IOException, IllegalArgumentException;
     /**

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/pmmap.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/pmmap.c?rev=809848&r1=809847&r2=809848&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/pmmap.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/pmmap.c Tue Sep  1 06:39:56 2009
@@ -329,6 +329,42 @@
     return mapo;
 }
 
+ACR_IO_EXPORT_DECLARE(jobject, MemoryMapProvider, create0)(ACR_JNISTDARGS,
+                                                           jstring fname,
+                                                           jint flags)
+{
+
+    jobject mapd = NULL;
+    int     imap;
+
+    UNREFERENCED_O;
+    WITH_CSTR(fname) {
+        imap = ACR_MMapOpen(_E, J2S(fname), flags);
+        if (imap > 0) {
+            /* Create Descriptor Object */
+            mapd = ACR_DescriptorCreate(_E, ACR_DT_MMAP, imap, NULL,
+                                        mmap_descriptor_cleanup);
+        }
+    } END_WITH_CSTR(fname);
+    return mapd;
+}
+
+ACR_IO_EXPORT_DECLARE(jobject, MemoryMapProvider, create1)(ACR_JNISTDARGS,
+                                                           jint fd,
+                                                           jint flags)
+{
+    jobject mapd = NULL;
+    int     imap;
+    UNREFERENCED_O;
+
+    imap = ACR_MMapCreate(_E, fd, flags);
+    if (imap > 0) {
+        mapd = ACR_DescriptorCreate(_E, ACR_DT_MMAP, imap, NULL,
+                                    mmap_descriptor_cleanup);
+    }
+    return mapd;
+}
+
 ACR_IO_EXPORT_DECLARE(jobject, MemoryMapProvider, map0)(ACR_JNISTDARGS,
                                                         jint map,
                                                         jlong offset,

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/pmmap.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/pmmap.c?rev=809848&r1=809847&r2=809848&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/pmmap.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/pmmap.c Tue Sep  1 06:39:56 2009
@@ -370,6 +370,42 @@
     return mapo;
 }
 
+ACR_IO_EXPORT_DECLARE(jobject, MemoryMapProvider, create0)(ACR_JNISTDARGS,
+                                                           jstring fname,
+                                                           jint flags)
+{
+
+    jobject mapd = NULL;
+    int     imap;
+
+    UNREFERENCED_O;
+    WITH_WSTR(fname) {
+        imap = ACR_MMapOpen(_E, J2W(fname), flags);
+        if (imap > 0) {
+            /* Create Descriptor Object */
+            mapd = ACR_DescriptorCreate(_E, ACR_DT_MMAP, imap, NULL,
+                                        mmap_descriptor_cleanup);
+        }
+    } END_WITH_WSTR(fname);
+    return mapd;
+}
+
+ACR_IO_EXPORT_DECLARE(jobject, MemoryMapProvider, create1)(ACR_JNISTDARGS,
+                                                           jint fd,
+                                                           jint flags)
+{
+    jobject mapd = NULL;
+    int     imap;
+    UNREFERENCED_O;
+
+    imap = ACR_MMapCreate(_E, fd, flags);
+    if (imap > 0) {
+        mapd = ACR_DescriptorCreate(_E, ACR_DT_MMAP, imap, NULL,
+                                    mmap_descriptor_cleanup);
+    }
+    return mapd;
+}
+
 ACR_IO_EXPORT_DECLARE(jobject, MemoryMapProvider, map0)(ACR_JNISTDARGS,
                                                         jint map,
                                                         jlong offset,

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=809848&r1=809847&r2=809848&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 Tue Sep  1 06:39:56 2009
@@ -46,6 +46,7 @@
         suite.addTest(TestStrings.suite());
         suite.addTest(TestSemaphore.suite());
         suite.addTest(TestSharedMemory.suite());
+        suite.addTest(TestMemoryMap.suite());
         return suite;
     }
 

Added: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestMemoryMap.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestMemoryMap.java?rev=809848&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestMemoryMap.java (added)
+++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestMemoryMap.java Tue Sep  1 06:39:56 2009
@@ -0,0 +1,57 @@
+/* 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 junit.framework.*;
+import java.util.EnumSet;
+import org.apache.commons.runtime.io.*;
+import org.apache.commons.runtime.exception.*;
+
+/**
+ * SharedMemory Test.
+ *
+ */
+public class TestMemoryMap extends TestCase
+{
+
+    public static Test suite() {
+        TestSuite suite = new TestSuite(TestMemoryMap.class);
+        return suite;
+    }
+
+    protected void setUp()
+        throws Exception
+    {
+        System.loadLibrary("acr");
+    }
+
+    public void testTestMemoryMapOpen()
+        throws Throwable
+    {
+        File f = new File("org/apache/commons/runtime/TestMemoryMap.class");
+        MemoryMapProvider map = new MemoryMapProvider(f, EnumSet.of(FileOpenMode.READ));
+        Pointer p = map.map(0, 128);
+        assertEquals("Class header [0]", (byte)0xCA, (byte)p.peek(0));
+        assertEquals("Class header [1]", (byte)0xFE, (byte)p.peek(1));
+        assertEquals("Class header [2]", (byte)0xBA, (byte)p.peek(2));
+        assertEquals("Class header [3]", (byte)0xBE, (byte)p.peek(3));
+        map.close();
+    }
+
+}
+

Propchange: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestMemoryMap.java
------------------------------------------------------------------------------
    svn:eol-style = native