Bug 1369626 - Remove MALLOC_XMALLOC. r?njn draft
authorMike Hommey <mh+mozilla@glandium.org>
Fri, 02 Jun 2017 15:19:20 +0900
changeset 588109 f9f82311a21acf794be8fd22bb4f21489381477f
parent 587906 b138d2f271fdb598bf8a66c2dcb7fe391ca2a96f
child 631465 d60c21de5f498b3fe240b79fd92f6de705ac9cd3
push id61913
push userbmo:mh+mozilla@glandium.org
push dateFri, 02 Jun 2017 06:24:23 +0000
reviewersnjn
bugs1369626
milestone55.0a1
Bug 1369626 - Remove MALLOC_XMALLOC. r?njn Setting MALLOC_XMALLOC enables a runtime toggle that allows to make allocations abort() on OOM instead of returning NULL. In other words, it enables a toggle that allows to turn all allocations into infallible allocations. The toggle however still defaults to being disabled, which means even when MALLOC_XMALLOC is defined when building mozjemalloc, there is no change in behavior, unless a MALLOC_OPTIONS is set. Even if this were useful to anyone (MALLOC_XMALLOC is only defined on debug builds, limiting the usefulness), this is something replace-malloc, in Firefox, is meant to be used for. So let's remove this feature, and possibly add an equivalent replace-malloc later if deemed necessary.
memory/mozjemalloc/mozjemalloc.cpp
memory/mozjemalloc/mozjemalloc_types.h
--- a/memory/mozjemalloc/mozjemalloc.cpp
+++ b/memory/mozjemalloc/mozjemalloc.cpp
@@ -127,21 +127,16 @@
  *
  * The jemalloc_purge_freed_pages definition in memory/build/mozmemory.h needs
  * to be adjusted if MALLOC_DOUBLE_PURGE is ever enabled on Linux.
  */
 #ifdef MOZ_MEMORY_DARWIN
 #define MALLOC_DOUBLE_PURGE
 #endif
 
-#ifdef MOZ_DEBUG
-   /* Support optional abort() on OOM. */
-#  define MALLOC_XMALLOC
-#endif
-
 #include <sys/types.h>
 
 #include <errno.h>
 #include <stdlib.h>
 #include <limits.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <string.h>
@@ -1021,19 +1016,16 @@ static bool	opt_print_stats = false;
 #define opt_quantum_2pow	QUANTUM_2POW_MIN
 #define opt_small_max_2pow	SMALL_MAX_2POW_DEFAULT
 #define opt_chunk_2pow		CHUNK_2POW_DEFAULT
 #else
 static size_t	opt_quantum_2pow = QUANTUM_2POW_MIN;
 static size_t	opt_small_max_2pow = SMALL_MAX_2POW_DEFAULT;
 static size_t	opt_chunk_2pow = CHUNK_2POW_DEFAULT;
 #endif
-#ifdef MALLOC_XMALLOC
-static bool	opt_xmalloc = false;
-#endif
 
 /******************************************************************************/
 /*
  * Begin forward declarations.
  */
 
 #ifdef MOZ_MEMORY_DARWIN
 /* Avoid namespace collision with OS X's malloc APIs. */
@@ -4382,19 +4374,16 @@ malloc_print_stats(void)
 #else
 		    "enabled",
 #endif
 		    "\n", "");
 		_malloc_message("Boolean MALLOC_OPTIONS: ",
 		    opt_abort ? "A" : "a", "", "");
 		_malloc_message(opt_junk ? "J" : "j", "", "", "");
 		_malloc_message("P", "", "", "");
-#ifdef MALLOC_XMALLOC
-		_malloc_message(opt_xmalloc ? "X" : "x", "", "", "");
-#endif
 		_malloc_message(opt_zero ? "Z" : "z", "", "", "");
 		_malloc_message("\n", "", "", "");
 
 		_malloc_message("Max arenas: ", umax2s(narenas, s), "\n",
 		    "");
 		_malloc_message("Pointer size: ", umax2s(sizeof(void *), s),
 		    "\n", "");
 		_malloc_message("Quantum size: ", umax2s(quantum, s), "\n",
@@ -4647,24 +4636,16 @@ MALLOC_OUT:
 						opt_small_max_2pow--;
 					break;
 				case 'S':
 					if (opt_small_max_2pow < pagesize_2pow
 					    - 1)
 						opt_small_max_2pow++;
 					break;
 #endif
-#ifdef MALLOC_XMALLOC
-				case 'x':
-					opt_xmalloc = false;
-					break;
-				case 'X':
-					opt_xmalloc = true;
-					break;
-#endif
 #ifdef MOZ_DEBUG
 				case 'z':
 					opt_zero = false;
 					break;
 				case 'Z':
 					opt_zero = true;
 					break;
 #endif
@@ -4831,24 +4812,16 @@ malloc_impl(size_t size)
 	if (size == 0) {
 		size = 1;
 	}
 
 	ret = imalloc(size);
 
 RETURN:
 	if (ret == NULL) {
-#ifdef MALLOC_XMALLOC
-		if (opt_xmalloc) {
-			_malloc_message(_getprogname(),
-			    ": (malloc) Error in malloc(): out of memory\n", "",
-			    "");
-			moz_abort();
-		}
-#endif
 		errno = ENOMEM;
 	}
 
 	return (ret);
 }
 
 /*
  * In ELF systems the default visibility allows symbols to be preempted at
@@ -4889,46 +4862,31 @@ MEMALIGN(size_t alignment, size_t size)
 	if (size == 0) {
 		size = 1;
 	}
 
 	alignment = alignment < sizeof(void*) ? sizeof(void*) : alignment;
 	ret = ipalloc(alignment, size);
 
 RETURN:
-#ifdef MALLOC_XMALLOC
-	if (opt_xmalloc && ret == NULL) {
-		_malloc_message(_getprogname(),
-		": (malloc) Error in memalign(): out of memory\n", "", "");
-		moz_abort();
-	}
-#endif
 	return (ret);
 }
 
 #ifdef MOZ_MEMORY_ELF
 extern void *
 memalign_impl(size_t alignment, size_t size) __attribute__((alias ("memalign_internal"), visibility ("default")));
 #endif
 
 MOZ_MEMORY_API int
 posix_memalign_impl(void **memptr, size_t alignment, size_t size)
 {
 	void *result;
 
 	/* Make sure that alignment is a large enough power of 2. */
 	if (((alignment - 1) & alignment) != 0 || alignment < sizeof(void *)) {
-#ifdef MALLOC_XMALLOC
-		if (opt_xmalloc) {
-			_malloc_message(_getprogname(),
-			    ": (malloc) Error in posix_memalign(): "
-			    "invalid alignment\n", "", "");
-			moz_abort();
-		}
-#endif
 		return (EINVAL);
 	}
 
 	/* The 0-->1 size promotion is done in the memalign() call below */
 
 	result = MEMALIGN(alignment, size);
 
 	if (result == NULL)
@@ -4937,24 +4895,16 @@ posix_memalign_impl(void **memptr, size_
 	*memptr = result;
 	return (0);
 }
 
 MOZ_MEMORY_API void *
 aligned_alloc_impl(size_t alignment, size_t size)
 {
 	if (size % alignment) {
-#ifdef MALLOC_XMALLOC
-		if (opt_xmalloc) {
-			_malloc_message(_getprogname(),
-			    ": (malloc) Error in aligned_alloc(): "
-			    "size is not multiple of alignment\n", "", "");
-			moz_abort();
-		}
-#endif
 		return (NULL);
 	}
 	return MEMALIGN(alignment, size);
 }
 
 MOZ_MEMORY_API void *
 valloc_impl(size_t size)
 {
@@ -4987,24 +4937,16 @@ calloc_impl(size_t num, size_t size)
 		ret = NULL;
 		goto RETURN;
 	}
 
 	ret = icalloc(num_size);
 
 RETURN:
 	if (ret == NULL) {
-#ifdef MALLOC_XMALLOC
-		if (opt_xmalloc) {
-			_malloc_message(_getprogname(),
-			    ": (malloc) Error in calloc(): out of memory\n", "",
-			    "");
-			moz_abort();
-		}
-#endif
 		errno = ENOMEM;
 	}
 
 	return (ret);
 }
 
 MOZ_MEMORY_API void *
 realloc_impl(void *ptr, size_t size)
@@ -5016,41 +4958,25 @@ realloc_impl(void *ptr, size_t size)
 	}
 
 	if (ptr != NULL) {
 		MOZ_ASSERT(malloc_initialized);
 
 		ret = iralloc(ptr, size);
 
 		if (ret == NULL) {
-#ifdef MALLOC_XMALLOC
-			if (opt_xmalloc) {
-				_malloc_message(_getprogname(),
-				    ": (malloc) Error in realloc(): out of "
-				    "memory\n", "", "");
-				moz_abort();
-			}
-#endif
 			errno = ENOMEM;
 		}
 	} else {
 		if (malloc_init())
 			ret = NULL;
 		else
 			ret = imalloc(size);
 
 		if (ret == NULL) {
-#ifdef MALLOC_XMALLOC
-			if (opt_xmalloc) {
-				_malloc_message(_getprogname(),
-				    ": (malloc) Error in realloc(): out of "
-				    "memory\n", "", "");
-				moz_abort();
-			}
-#endif
 			errno = ENOMEM;
 		}
 	}
 
 	return (ret);
 }
 
 MOZ_MEMORY_API void
@@ -5131,21 +5057,16 @@ jemalloc_stats_impl(jemalloc_stats_t *st
 
 	MOZ_ASSERT(stats != NULL);
 
 	/*
 	 * Gather runtime settings.
 	 */
 	stats->opt_abort = opt_abort;
 	stats->opt_junk = opt_junk;
-	stats->opt_xmalloc =
-#ifdef MALLOC_XMALLOC
-	    opt_xmalloc ? true :
-#endif
-	    false;
 	stats->opt_zero = opt_zero;
 	stats->narenas = narenas;
 	stats->quantum = quantum;
 	stats->small_max = small_max;
 	stats->large_max = arena_maxclass;
 	stats->chunksize = chunksize;
 	stats->dirty_max = opt_dirty_max;
 
--- a/memory/mozjemalloc/mozjemalloc_types.h
+++ b/memory/mozjemalloc/mozjemalloc_types.h
@@ -51,17 +51,16 @@ typedef unsigned char jemalloc_bool;
  * file.
  */
 typedef struct {
 	/*
 	 * Run-time configuration settings.
 	 */
 	jemalloc_bool	opt_abort;	/* abort(3) on error? */
 	jemalloc_bool	opt_junk;	/* Fill allocated memory with kAllocJunk? */
-	jemalloc_bool	opt_xmalloc;	/* abort(3) on OOM? */
 	jemalloc_bool	opt_zero;	/* Fill allocated memory with 0x0? */
 	size_t	narenas;	/* Number of arenas. */
 	size_t	quantum;	/* Allocation quantum. */
 	size_t	small_max;	/* Max quantum-spaced allocation size. */
 	size_t	large_max;	/* Max sub-chunksize allocation size. */
 	size_t	chunksize;	/* Size of each virtual memory mapping. */
 	size_t	dirty_max;	/* Max dirty pages per arena. */