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 18:14:10 UTC

svn commit: r810107 - in /commons/sandbox/runtime/trunk/src: main/java/org/apache/commons/runtime/io/MemoryMapProvider.java main/native/os/unix/pmmap.c main/native/os/win32/pmmap.c test/org/apache/commons/runtime/TestMemoryMap.java

Author: mturk
Date: Tue Sep  1 16:14:09 2009
New Revision: 810107

URL: http://svn.apache.org/viewvc?rev=810107&view=rev
Log:
Add map entire file

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/TestMemoryMap.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=810107&r1=810106&r2=810107&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 16:14:09 2009
@@ -121,6 +121,29 @@
         }
     }
 
+    private static native Pointer map1(int md, long offset)
+        throws IOException, SecurityException, IllegalArgumentException;
+    /**
+     * Map the entire file starting from offset.
+     * @param offset Offset from the file begin.
+     * @return new {@code Pointer} object
+     */
+    public Pointer map(long offset)
+        throws IOException, SecurityException, IllegalArgumentException,
+               ClosedDescriptorException
+    {
+        /* TODO: Check all provided arguments
+         */
+        if (map.valid()) {
+            Pointer mmap = map1(map.fd(), offset);
+            mappedRegions.add(mmap);
+            return mmap;
+        }
+        else {
+            throw new ClosedDescriptorException();
+        }
+    }
+
     public boolean unmap(Pointer memory)
         throws IOException
     {

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=810107&r1=810106&r2=810107&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 16:14:09 2009
@@ -408,3 +408,35 @@
 
     return mptr;
 }
+
+ACR_IO_EXPORT_DECLARE(jobject, MemoryMapProvider, map1)(ACR_JNISTDARGS,
+                                                        jint map,
+                                                        jlong offset)
+{
+    void   *base  = NULL;
+    jobject mptr  = NULL;
+    struct  stat  s;
+    off_t   size;
+    acr_mmap_t *m = (acr_mmap_t *)ACR_IOH(map);
+
+    if (ACR_IOH_TYPE(map) != ACR_DT_MMAP) {
+        ACR_THROW_EX_IF_ERR(ACR_EX_EINVAL, ACR_EFTYPE);
+        return NULL;
+    }
+    if (fstat(m->fd, &s)) {
+        ACR_THROW_IO_ERRNO();
+        return NULL;
+    }
+    size = s.st_size - offset;
+    base = mmap(NULL, (size_t)size, m->flags, MAP_SHARED, m->fd, (off_t)offset);
+    if (base == MAP_FAILED) {
+        ACR_THROW_IO_ERRNO();
+        return NULL;
+    }
+    if (m->flags & PROT_WRITE)
+        mptr = ACR_NewBasicPointer(_E, base, (size_t)size, mmap_pointer_cleanup);
+    else
+        mptr = ACR_NewConstPointer(_E, base, (size_t)size, mmap_pointer_cleanup);
+
+    return mptr;
+}

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=810107&r1=810106&r2=810107&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 16:14:09 2009
@@ -437,3 +437,37 @@
 
     return mptr;
 }
+
+ACR_IO_EXPORT_DECLARE(jobject, MemoryMapProvider, map1)(ACR_JNISTDARGS,
+                                                        jint map,
+                                                        jlong offset)
+{
+    LARGE_INTEGER off;
+    MEMORY_BASIC_INFORMATION mbi;
+    void   *base  = NULL;
+    jobject mptr  = NULL;
+    size_t  size;
+    acr_mmap_t *m = (acr_mmap_t *)ACR_IOH(map);
+
+    if (ACR_IOH_TYPE(map) != ACR_DT_MMAP) {
+        ACR_THROW_EX_IF_ERR(ACR_EX_EINVAL, ACR_EFTYPE);
+        return NULL;
+    }
+    off.QuadPart = offset;
+    base = MapViewOfFile(m->mh, m->flags, off.HighPart, off.LowPart, 0);
+    if (IS_INVALID_HANDLE(base)) {
+        ACR_THROW_IO_ERRNO();
+        return NULL;
+    }
+    if (!VirtualQuery(base, &mbi, sizeof(MEMORY_BASIC_INFORMATION))) {
+        ACR_THROW_IO_IF_ERR(ACR_EACCES);
+        return NULL;
+    }
+    size = mbi.RegionSize;
+    if (m->flags & FILE_MAP_WRITE)
+        mptr = ACR_NewBasicPointer(_E, base, (size_t)size, mmap_pointer_cleanup);
+    else
+        mptr = ACR_NewConstPointer(_E, base, (size_t)size, mmap_pointer_cleanup);
+
+    return mptr;
+}

Modified: 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=810107&r1=810106&r2=810107&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestMemoryMap.java (original)
+++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestMemoryMap.java Tue Sep  1 16:14:09 2009
@@ -54,6 +54,21 @@
         map.close();
     }
 
+    public void testTestMemoryMapAll()
+        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);
+        System.out.println();
+        System.out.println("Map size is " + p.sizeof());
+        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();
+    }
+
     public void testTestMemoryMapAlignment()
         throws Throwable
     {