Bug 1412221 - Use pages_unmap instead of repeating the same code. r?njn draft
authorMike Hommey <mh+mozilla@glandium.org>
Fri, 27 Oct 2017 16:02:22 +0900
changeset 688266 8592a0a8715ae8462e17c730a445446aab82888d
parent 688265 a3707e099c056cd904303929a517ad7daaf3f73b
child 688267 129c557dc13e30b34577d71ed18daa1f5979c7a4
push id86708
push userbmo:mh+mozilla@glandium.org
push dateSun, 29 Oct 2017 11:12:20 +0000
reviewersnjn
bugs1412221
milestone58.0a1
Bug 1412221 - Use pages_unmap instead of repeating the same code. r?njn
memory/build/mozjemalloc.cpp
--- a/memory/build/mozjemalloc.cpp
+++ b/memory/build/mozjemalloc.cpp
@@ -1543,36 +1543,49 @@ using UniqueBaseNode = mozilla::UniquePt
 /******************************************************************************/
 /*
  * Begin chunk management functions.
  */
 
 #ifdef XP_WIN
 
 static void *
-pages_map(void *addr, size_t size)
+pages_map(void *aAddr, size_t aSize)
 {
 	void *ret = nullptr;
-	ret = VirtualAlloc(addr, size, MEM_COMMIT | MEM_RESERVE,
+	ret = VirtualAlloc(aAddr, aSize, MEM_COMMIT | MEM_RESERVE,
 	    PAGE_READWRITE);
 	return ret;
 }
 
 static void
-pages_unmap(void *addr, size_t size)
+pages_unmap(void *aAddr, size_t aSize)
 {
-	if (VirtualFree(addr, 0, MEM_RELEASE) == 0) {
+	if (VirtualFree(aAddr, 0, MEM_RELEASE) == 0) {
 		_malloc_message(_getprogname(),
 		    ": (malloc) Error in VirtualFree()\n");
 	}
 }
 #else
 
+static void
+pages_unmap(void *aAddr, size_t aSize)
+{
+	if (munmap(aAddr, aSize) == -1) {
+		char buf[STRERROR_BUF];
+
+		if (strerror_r(errno, buf, sizeof(buf)) == 0) {
+			_malloc_message(_getprogname(),
+				": (malloc) Error in munmap(): ", buf, "\n");
+		}
+	}
+}
+
 static void *
-pages_map(void *addr, size_t size)
+pages_map(void *aAddr, size_t aSize)
 {
 	void *ret;
 #if defined(__ia64__) || (defined(__sparc__) && defined(__arch64__) && defined(__linux__))
         /*
          * The JS engine assumes that all allocated pointers have their high 17 bits clear,
          * which ia64's mmap doesn't support directly. However, we can emulate it by passing
          * mmap an "addr" parameter with those bits clear. The mmap will return that address,
          * or the nearest available memory above that address, providing a near-guarantee
@@ -1580,109 +1593,88 @@ pages_map(void *addr, size_t size)
          * out-of-memory.
          *
          * The addr is chosen as 0x0000070000000000, which still allows about 120TB of virtual
          * address space.
          *
          * See Bug 589735 for more information.
          */
 	bool check_placement = true;
-        if (!addr) {
-		addr = (void*)0x0000070000000000;
+        if (!aAddr) {
+		aAddr = (void*)0x0000070000000000;
 		check_placement = false;
 	}
 #endif
 
 #if defined(__sparc__) && defined(__arch64__) && defined(__linux__)
     const uintptr_t start = 0x0000070000000000ULL;
     const uintptr_t end   = 0x0000800000000000ULL;
 
     /* Copied from js/src/gc/Memory.cpp and adapted for this source */
 
     uintptr_t hint;
     void* region = MAP_FAILED;
-    for (hint = start; region == MAP_FAILED && hint + size <= end; hint += chunksize) {
-           region = mmap((void*)hint, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
+    for (hint = start; region == MAP_FAILED && hint + aSize <= end; hint += chunksize) {
+           region = mmap((void*)hint, aSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
            if (region != MAP_FAILED) {
-                   if (((size_t) region + (size - 1)) & 0xffff800000000000) {
-                           if (munmap(region, size)) {
+                   if (((size_t) region + (aSize - 1)) & 0xffff800000000000) {
+                           if (munmap(region, aSize)) {
                                    MOZ_ASSERT(errno == ENOMEM);
                            }
                            region = MAP_FAILED;
                    }
            }
     }
     ret = region;
 #else
 
 	/*
 	 * We don't use MAP_FIXED here, because it can cause the *replacement*
 	 * of existing mappings, and we only want to create new mappings.
 	 */
-	ret = mmap(addr, size, PROT_READ | PROT_WRITE,
+	ret = mmap(aAddr, aSize, PROT_READ | PROT_WRITE,
 		MAP_PRIVATE | MAP_ANON, -1, 0);
 	MOZ_ASSERT(ret);
 #endif
 	if (ret == MAP_FAILED) {
 		ret = nullptr;
 	}
 #if defined(__ia64__) || (defined(__sparc__) && defined(__arch64__) && defined(__linux__))
         /*
          * If the allocated memory doesn't have its upper 17 bits clear, consider it
          * as out of memory.
         */
         else if ((long long)ret & 0xffff800000000000) {
-		munmap(ret, size);
+		munmap(ret, aSize);
                 ret = nullptr;
         }
         /* If the caller requested a specific memory location, verify that's what mmap returned. */
-	else if (check_placement && ret != addr) {
+	else if (check_placement && ret != aAddr) {
 #else
-	else if (addr && ret != addr) {
+	else if (aAddr && ret != aAddr) {
 #endif
 		/*
 		 * We succeeded in mapping memory, but not in the right place.
 		 */
-		if (munmap(ret, size) == -1) {
-			char buf[STRERROR_BUF];
-
-			if (strerror_r(errno, buf, sizeof(buf)) == 0) {
-				_malloc_message(_getprogname(),
-					": (malloc) Error in munmap(): ", buf, "\n");
-			}
-		}
+		pages_unmap(ret, aSize);
 		ret = nullptr;
 	}
 	if (ret) {
-		MozTagAnonymousMemory(ret, size, "jemalloc");
+		MozTagAnonymousMemory(ret, aSize, "jemalloc");
 	}
 
 #if defined(__ia64__) || (defined(__sparc__) && defined(__arch64__) && defined(__linux__))
 	MOZ_ASSERT(!ret || (!check_placement && ret)
-	    || (check_placement && ret == addr));
+	    || (check_placement && ret == aAddr));
 #else
-	MOZ_ASSERT(!ret || (!addr && ret != addr)
-	    || (addr && ret == addr));
+	MOZ_ASSERT(!ret || (!aAddr && ret != aAddr)
+	    || (aAddr && ret == aAddr));
 #endif
 	return ret;
 }
-
-static void
-pages_unmap(void *addr, size_t size)
-{
-
-	if (munmap(addr, size) == -1) {
-		char buf[STRERROR_BUF];
-
-		if (strerror_r(errno, buf, sizeof(buf)) == 0) {
-			_malloc_message(_getprogname(),
-				": (malloc) Error in munmap(): ", buf, "\n");
-		}
-	}
-}
 #endif
 
 #ifdef XP_DARWIN
 #define	VM_COPY_MIN (pagesize << 5)
 static inline void
 pages_copy(void *dest, const void *src, size_t n)
 {