Bug 1413475 - Reorganize #includes in mozjemalloc.cpp. r?njn
--- a/memory/build/mozjemalloc.cpp
+++ b/memory/build/mozjemalloc.cpp
@@ -102,29 +102,55 @@
//
// Huge : Each allocation is backed by a dedicated contiguous set of chunks.
// Metadata are stored in a separate red-black tree.
//
// *****************************************************************************
#include "mozmemory_wrap.h"
#include "mozjemalloc.h"
+#include "mozjemalloc_types.h"
+
+#include <cstring>
+#include <cerrno>
+#ifdef XP_WIN
+#include <io.h>
+#include <windows.h>
+#else
+#include <sys/mman.h>
+#include <unistd.h>
+#endif
+#ifdef XP_DARWIN
+#include <libkern/OSAtomic.h>
+#include <mach/mach_init.h>
+#include <mach/vm_map.h>
+#endif
+
#include "mozilla/Atomics.h"
#include "mozilla/Alignment.h"
+#include "mozilla/Assertions.h"
+#include "mozilla/Attributes.h"
#include "mozilla/CheckedInt.h"
#include "mozilla/DoublyLinkedList.h"
#include "mozilla/GuardObjects.h"
#include "mozilla/Likely.h"
#include "mozilla/MathAlgorithms.h"
#include "mozilla/Sprintf.h"
+// Note: MozTaggedAnonymousMmap() could call an LD_PRELOADed mmap
+// instead of the one defined here; use only MozTagAnonymousMemory().
+#include "mozilla/TaggedAnonymousMemory.h"
+#include "mozilla/ThreadLocal.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/Unused.h"
#include "mozilla/fallible.h"
+#include "rb.h"
#include "Utils.h"
+using namespace mozilla;
+
// On Linux, we use madvise(MADV_DONTNEED) to release memory back to the
// operating system. If we release 1MB of live pages with MADV_DONTNEED, our
// RSS will decrease by 1MB (almost) immediately.
//
// On Mac, we use madvise(MADV_FREE). Unlike MADV_DONTNEED on Linux, MADV_FREE
// on Mac doesn't cause the OS to release the specified pages immediately; the
// OS keeps them in our process until the machine comes under memory pressure.
//
@@ -139,34 +165,18 @@
//
// 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 XP_DARWIN
#define MALLOC_DOUBLE_PURGE
#endif
-#include <sys/types.h>
-
-#include <errno.h>
-#include <stdlib.h>
-#include <limits.h>
-#include <stdarg.h>
-#include <stdio.h>
-#include <string.h>
-#include <algorithm>
-
-using namespace mozilla;
-
#ifdef XP_WIN
-#include <io.h>
-#include <windows.h>
-#include <intrin.h>
-
#define STDERR_FILENO 2
// Implement getenv without using malloc.
static char mozillaMallocOptionsBuf[64];
#define getenv xgetenv
static char*
getenv(const char* name)
@@ -179,58 +189,24 @@ getenv(const char* name)
return nullptr;
}
#define MALLOC_DECOMMIT
#endif
#ifndef XP_WIN
-#ifndef XP_SOLARIS
-#include <sys/cdefs.h>
-#endif
-#include <sys/mman.h>
#ifndef MADV_FREE
#define MADV_FREE MADV_DONTNEED
#endif
#ifndef MAP_NOSYNC
#define MAP_NOSYNC 0
#endif
-#include <sys/param.h>
-#include <sys/time.h>
-#include <sys/types.h>
-#if !defined(XP_SOLARIS) && !defined(ANDROID)
-#include <sys/sysctl.h>
#endif
-#include <sys/uio.h>
-
-#include <errno.h>
-#include <limits.h>
-#include <pthread.h>
-#include <sched.h>
-#include <stdarg.h>
-#include <stdio.h>
-#include <stdbool.h>
-#include <stdint.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-#ifdef XP_DARWIN
-#include <libkern/OSAtomic.h>
-#include <mach/mach_error.h>
-#include <mach/mach_init.h>
-#include <mach/vm_map.h>
-#include <malloc/malloc.h>
-#endif
-
-#endif
-
-#include "mozilla/ThreadLocal.h"
-#include "mozjemalloc_types.h"
+
// Some tools, such as /dev/dsp wrappers, LD_PRELOAD libraries that
// happen to override mmap() and call dlsym() from their overridden
// mmap(). The problem is that dlsym() calls malloc(), and this ends
// up in a dead lock in jemalloc.
// On these systems, we prefer to directly use the system call.
// We do that for Linux systems and kfreebsd with GNU userland.
// Note sanity checks are not done (alignment of offset, ...) because
@@ -276,18 +252,16 @@ static inline void*
#endif
// Size of stack-allocated buffer passed to strerror_r().
#define STRERROR_BUF 64
// Minimum alignment of non-tiny allocations is 2^QUANTUM_2POW_MIN bytes.
#define QUANTUM_2POW_MIN 4
-#include "rb.h"
-
// sizeof(int) == (1U << SIZEOF_INT_2POW).
#ifndef SIZEOF_INT_2POW
#define SIZEOF_INT_2POW 2
#endif
// Size and alignment of memory chunks that are allocated by the OS's virtual
// memory system.
#define CHUNK_2POW_DEFAULT 20
@@ -1274,22 +1248,16 @@ static void
template<typename... Args>
static void
_malloc_message(const char* p, Args... args)
{
_malloc_message(p);
_malloc_message(args...);
}
-#include "mozilla/Assertions.h"
-#include "mozilla/Attributes.h"
-#include "mozilla/TaggedAnonymousMemory.h"
- // Note: MozTaggedAnonymousMmap() could call an LD_PRELOADed mmap
- // instead of the one defined here; use only MozTagAnonymousMemory().
-
#ifdef ANDROID
// Android's pthread.h does not declare pthread_atfork() until SDK 21.
extern "C" MOZ_EXPORT int
pthread_atfork(void (*)(void), void (*)(void), void (*)(void));
#endif
// ***************************************************************************
// Begin mutex. We can't use normal pthread mutexes in all places, because