Merge change I3628c655

* changes:
  OSMemorySystem.mmap fixes.
diff --git a/libcore/luni/src/main/java/org/apache/harmony/luni/platform/IMemorySystem.java b/libcore/luni/src/main/java/org/apache/harmony/luni/platform/IMemorySystem.java
index 6e8028a..f7a0209 100644
--- a/libcore/luni/src/main/java/org/apache/harmony/luni/platform/IMemorySystem.java
+++ b/libcore/luni/src/main/java/org/apache/harmony/luni/platform/IMemorySystem.java
@@ -515,7 +515,7 @@
      *
      * @param fileDescriptor
      *            a handle to the file that is to be memory mapped.
-     * @param alignment
+     * @param offset
      *            the offset in the file where the mapping should begin.
      * @param size
      *            the number of bytes that are requested to map.
@@ -528,8 +528,10 @@
      * @throws IOException
      *             if an exception occurs mapping the file into memory.
      */
-    public int mmap(int fileDescriptor, long alignment, long size,  int mapMode)
+    // BEGIN android-changed: rename 'alignment' to 'offset'.
+    public int mmap(int fileDescriptor, long offset, long size, int mapMode)
             throws IOException;
+    // END android-changed
 
     /**
      * TODO: JavaDoc
diff --git a/libcore/luni/src/main/java/org/apache/harmony/luni/platform/OSMemory.java b/libcore/luni/src/main/java/org/apache/harmony/luni/platform/OSMemory.java
index 8d06b1c..b490da5 100644
--- a/libcore/luni/src/main/java/org/apache/harmony/luni/platform/OSMemory.java
+++ b/libcore/luni/src/main/java/org/apache/harmony/luni/platform/OSMemory.java
@@ -608,18 +608,17 @@
      */
     public native void setAddress(int address, int value);
 
-    /*
-         * Memory mapped file
-         */
-    private native int mmapImpl(int fileDescriptor, long alignment,
-            long size, int mapMode);
+    // BEGIN android-changed: more error checking, rename 'alignment' to 'offset'.
+    private native int mmapImpl(int fd, long offset, long size, int mapMode);
 
-    public int mmap(int fileDescriptor, long alignment, long size,
-            int mapMode) throws IOException {
-        // No need to check mmapImpl return as it throws IOException in error cases
-        int address = mmapImpl(fileDescriptor, alignment, size, mapMode);
-        return address;
+    public int mmap(int fd, long offset, long size, int mapMode) throws IOException {
+        // Check just those errors mmap(2) won't detect.
+        if (offset < 0 || size < 0 || offset > Integer.MAX_VALUE || size > Integer.MAX_VALUE) {
+            throw new IllegalArgumentException("offset=" + offset + " size=" + size);
+        }
+        return mmapImpl(fd, offset, size, mapMode);
     }
+    // END android-changed
 
     private native void unmapImpl(int addr, long size);
 
diff --git a/libcore/luni/src/main/native/org_apache_harmony_luni_platform_OSMemory.cpp b/libcore/luni/src/main/native/org_apache_harmony_luni_platform_OSMemory.cpp
index b1493f8..1d63faf 100644
--- a/libcore/luni/src/main/native/org_apache_harmony_luni_platform_OSMemory.cpp
+++ b/libcore/luni/src/main/native/org_apache_harmony_luni_platform_OSMemory.cpp
@@ -34,12 +34,9 @@
     jobject runtimeInstance;
 } gIDCache;
 
-#undef MMAP_READ_ONLY
-#define MMAP_READ_ONLY 1L
-#undef MMAP_READ_WRITE
-#define MMAP_READ_WRITE 2L
-#undef MMAP_WRITE_COPY
-#define MMAP_WRITE_COPY 4L
+static const int MMAP_READ_ONLY = 1;
+static const int MMAP_READ_WRITE = 2;
+static const int MMAP_WRITE_COPY = 4;
 
 /*
  * Class:     org_apache_harmony_luni_platform_OSMemory
@@ -429,36 +426,33 @@
  * Method:    mmapImpl
  * Signature: (IJJI)I
  */
-static jint harmony_nio_mmapImpl(JNIEnv *_env, jobject _this, jint fd, 
-        jlong alignment, jlong size, jint mmode) {
-    void *mapAddress = NULL;
+static jint harmony_nio_mmapImpl(JNIEnv* env, jobject, jint fd, 
+        jlong offset, jlong size, jint mapMode) {
     int prot, flags;
-          
-    // Convert from Java mapping mode to port library mapping mode.
-    switch (mmode) {
-      case MMAP_READ_ONLY:
-              prot = PROT_READ;
-              flags = MAP_SHARED;
-              break;
-      case MMAP_READ_WRITE:
-              prot = PROT_READ|PROT_WRITE;
-              flags = MAP_SHARED;
-              break;
-      case MMAP_WRITE_COPY:
-              prot = PROT_READ|PROT_WRITE;
-              flags = MAP_PRIVATE;
-              break;
-      default:
-              return -1;
-    }
-
-    mapAddress = mmap(0, (size_t)(size&0x7fffffff), prot, flags,fd,
-            (off_t)(alignment&0x7fffffff));
-    if (mapAddress == MAP_FAILED) {
+    switch (mapMode) {
+    case MMAP_READ_ONLY:
+        prot = PROT_READ;
+        flags = MAP_SHARED;
+        break;
+    case MMAP_READ_WRITE:
+        prot = PROT_READ|PROT_WRITE;
+        flags = MAP_SHARED;
+        break;
+    case MMAP_WRITE_COPY:
+        prot = PROT_READ|PROT_WRITE;
+        flags = MAP_PRIVATE;
+        break;
+    default:
+        jniThrowIOException(env, EINVAL);
+        LOGE("bad mapMode %i", mapMode);
         return -1;
     }
-    
-    return (jint) mapAddress;
+
+    void* mapAddress = mmap(0, size, prot, flags, fd, offset);
+    if (mapAddress == MAP_FAILED) {
+        jniThrowIOException(env, errno);
+    }
+    return reinterpret_cast<uintptr_t>(mapAddress);
 }
 
 /*
@@ -494,15 +488,6 @@
     return -1;
 }
 
-int getPageSize() {
-    static int page_size = 0;
-    if(page_size==0)
-    {
-        page_size=getpagesize();
-    }
-    return page_size;
-}
-
 /*
  * Class:     org_apache_harmony_luni_platform_OSMemory
  * Method:    isLoadedImpl
@@ -511,18 +496,16 @@
 static jboolean harmony_nio_isLoadedImpl(JNIEnv *_env, jobject _this, 
         jint address, jlong size) {
 
+    static int page_size = getpagesize();
     jboolean result = 0;
     jint m_addr = (jint)address;
-    int page_size = getPageSize();
-    unsigned char* vec = NULL;
-    int page_count = 0;
     
     int align_offset = m_addr%page_size;// addr should align with the boundary of a page.
     m_addr -= align_offset;
     size   += align_offset;
-    page_count = (size+page_size-1)/page_size;
+    int page_count = (size+page_size-1)/page_size;
     
-    vec = (unsigned char *) malloc(page_count*sizeof(char));
+    unsigned char* vec = (unsigned char *) malloc(page_count*sizeof(char));
     
     if (mincore((void *)m_addr, size, (MINCORE_POINTER_TYPE) vec)==0) {
         // or else there is error about the mincore and return false;