Build jemalloc for host, too. am: fa9301657a
am: 5c0935fcb1

Change-Id: I826495f533c8448ca84457d8b9c936621be8cad0
diff --git a/Android.bp b/Android.bp
index e7d9ea0..a2c8fb5 100644
--- a/Android.bp
+++ b/Android.bp
@@ -44,9 +44,10 @@
 //     usually decreases the amount of PSS used, but can increase
 //     fragmentation.
 
-// Default to a single arena for svelte configurations to minimize
-// PSS consumed by jemalloc.
-common_cflags += [
+android_common_cflags = [
+    // Default to a single arena for svelte configurations to minimize
+    // PSS. This will be overridden by android_product_variables for
+    // non-svelte configs.
     "-DANDROID_MAX_ARENAS=1",
     "-DANDROID_LG_TCACHE_MAXCLASS_DEFAULT=16",
 ]
@@ -56,7 +57,7 @@
     "include",
 ]
 
-common_product_variables = {
+android_product_variables = {
     // Only enable the tcache on non-svelte configurations, to save PSS.
     malloc_not_svelte: {
         cflags: [
@@ -72,9 +73,20 @@
 cc_defaults {
     name: "jemalloc_defaults",
     defaults: ["linux_bionic_supported"],
+    host_supported: true,
     cflags: common_cflags,
 
-    product_variables: common_product_variables,
+    target: {
+        android: {
+            cflags: android_common_cflags + [
+                "-include android/include/log.h",
+            ],
+            product_variables: android_product_variables,
+        },
+        linux_glibc: {
+            enabled: true,
+        },
+    },
 
     multilib: {
         lib32: {
@@ -132,13 +144,19 @@
 //-----------------------------------------------------------------------
 // jemalloc static library
 //-----------------------------------------------------------------------
-cc_library_static {
+cc_library {
     name: "libjemalloc",
     recovery_available: true,
 
     defaults: ["jemalloc_defaults"],
 
-    cflags: ["-include bionic/libc/async_safe/include/async_safe/log.h"],
+    target: {
+        android: {
+            shared: {
+                enabled: false,
+            },
+        },
+    },
 
     srcs: lib_src_files,
 }
@@ -153,7 +171,6 @@
 
     cflags: [
         "-DJEMALLOC_JET",
-        "-include android/include/log.h",
     ],
 
     srcs: lib_src_files,
@@ -180,10 +197,8 @@
     name: "libjemalloc_unittest",
 
     defaults: ["jemalloc_defaults"],
-
     cflags: [
         "-DJEMALLOC_UNIT_TEST",
-        "-include android/include/log.h",
     ],
 
     local_include_dirs: [
@@ -194,7 +209,6 @@
     srcs: jemalloc_testlib_srcs,
 
     whole_static_libs: ["libjemalloc_jet"],
-
 }
 
 //-----------------------------------------------------------------------
@@ -247,13 +261,12 @@
 cc_test {
     name: "jemalloc_unittests",
 
-    gtest: false,
+    defaults: ["jemalloc_defaults"],
 
-    product_variables: common_product_variables,
+    gtest: false,
 
     cflags: common_cflags + [
         "-DJEMALLOC_UNIT_TEST",
-        "-include android/include/log.h",
     ],
 
     local_include_dirs: common_c_local_includes + [
@@ -280,7 +293,6 @@
 
     cflags: [
         "-DJEMALLOC_INTEGRATION_TEST",
-        "-include android/include/log.h",
     ],
 
     local_include_dirs: [
@@ -299,7 +311,6 @@
     "test/integration/aligned_alloc.c",
     "test/integration/allocated.c",
     "test/integration/chunk.c",
-    "test/integration/iterate.c",
     "test/integration/MALLOCX_ARENA.c",
     "test/integration/mallocx.c",
     "test/integration/overflow.c",
@@ -311,17 +322,19 @@
     "test/integration/xallocx.c",
 ]
 
-cc_test {
+android_integration_tests = [
+    "test/integration/iterate.c",
+]
 
+cc_test {
     name: "jemalloc_integrationtests",
 
+    defaults: ["jemalloc_defaults"],
+
     gtest: false,
 
-    product_variables: common_product_variables,
-
     cflags: common_cflags + [
         "-DJEMALLOC_INTEGRATION_TEST",
-        "-include android/include/log.h",
     ],
 
     local_include_dirs: common_c_local_includes + [
@@ -330,6 +343,11 @@
     ],
 
     srcs: integration_tests,
+    target: {
+        android: {
+            srcs: android_integration_tests,
+        },
+    },
 
     static_libs: ["libjemalloc_integrationtest"],
 
diff --git a/include/jemalloc/internal/arena.h b/include/jemalloc/internal/arena.h
index 9e4bf04..0e4b361 100644
--- a/include/jemalloc/internal/arena.h
+++ b/include/jemalloc/internal/arena.h
@@ -29,6 +29,7 @@
 
 	purge_mode_limit = 2
 } purge_mode_t;
+#if defined(__ANDROID__)
 /* ANDROID change */
 /* Use the decay mode purge method.
  * Setting this value to zero results in performance issues because it
@@ -41,6 +42,11 @@
 /* Default decay time in seconds. */
 #define	DECAY_TIME_DEFAULT	0
 /* End ANDROID change */
+#else
+#define PURGE_DEFAULT           purge_mode_ratio
+/* Default decay time in seconds. */
+#define DECAY_TIME_DEFAULT      10
+#endif
 /* Number of event ticks between time checks. */
 #define	DECAY_NTICKS_PER_UPDATE	1000
 
diff --git a/include/jemalloc/internal/chunk.h b/include/jemalloc/internal/chunk.h
index 9b47f1c..f9e8433 100644
--- a/include/jemalloc/internal/chunk.h
+++ b/include/jemalloc/internal/chunk.h
@@ -5,7 +5,7 @@
  * Size and alignment of memory chunks that are allocated by the OS's virtual
  * memory system.
  */
-#ifdef ANDROID_LG_CHUNK_DEFAULT
+#if defined(__ANDROID__) && defined(ANDROID_LG_CHUNK_DEFAULT)
 #define	LG_CHUNK_DEFAULT	ANDROID_LG_CHUNK_DEFAULT
 #else
 #define	LG_CHUNK_DEFAULT	21
diff --git a/include/jemalloc/internal/jemalloc_internal_defs.h b/include/jemalloc/internal/jemalloc_internal_defs.h
index 1899bf3..9ffe26c 100644
--- a/include/jemalloc/internal/jemalloc_internal_defs.h
+++ b/include/jemalloc/internal/jemalloc_internal_defs.h
@@ -1,3 +1,6 @@
+#if !defined(__ANDROID__)
+#include "jemalloc_internal_defs_host.h"
+#else
 /* include/jemalloc/internal/jemalloc_internal_defs.h.  Generated from jemalloc_internal_defs.h.in by configure.  */
 #ifndef JEMALLOC_INTERNAL_DEFS_H_
 #define	JEMALLOC_INTERNAL_DEFS_H_
@@ -323,3 +326,4 @@
 #define JEMALLOC_CONFIG_MALLOC_CONF ""
 
 #endif /* JEMALLOC_INTERNAL_DEFS_H_ */
+#endif
diff --git a/include/jemalloc/internal/jemalloc_internal_defs_host.h b/include/jemalloc/internal/jemalloc_internal_defs_host.h
new file mode 100644
index 0000000..789e8a4
--- /dev/null
+++ b/include/jemalloc/internal/jemalloc_internal_defs_host.h
@@ -0,0 +1,332 @@
+/* include/jemalloc/internal/jemalloc_internal_defs.h.  Generated from jemalloc_internal_defs.h.in by configure.  */
+#ifndef JEMALLOC_INTERNAL_DEFS_H_
+#define	JEMALLOC_INTERNAL_DEFS_H_
+/*
+ * If JEMALLOC_PREFIX is defined via --with-jemalloc-prefix, it will cause all
+ * public APIs to be prefixed.  This makes it possible, with some care, to use
+ * multiple allocators simultaneously.
+ */
+/* #undef JEMALLOC_PREFIX */
+/* #undef JEMALLOC_CPREFIX */
+
+/*
+ * JEMALLOC_PRIVATE_NAMESPACE is used as a prefix for all library-private APIs.
+ * For shared libraries, symbol visibility mechanisms prevent these symbols
+ * from being exported, but for static libraries, naming collisions are a real
+ * possibility.
+ */
+#define JEMALLOC_PRIVATE_NAMESPACE je_
+
+/*
+ * Hyper-threaded CPUs may need a special instruction inside spin loops in
+ * order to yield to another virtual CPU.
+ */
+#ifdef __x86_64__
+#define CPU_SPINWAIT __asm__ volatile("pause")
+#else
+#define CPU_SPINWAIT 
+#endif
+
+/* Defined if C11 atomics are available. */
+#define JEMALLOC_C11ATOMICS 1
+
+/* Defined if the equivalent of FreeBSD's atomic(9) functions are available. */
+/* #undef JEMALLOC_ATOMIC9 */
+
+/*
+ * Defined if OSAtomic*() functions are available, as provided by Darwin, and
+ * documented in the atomic(3) manual page.
+ */
+/* #undef JEMALLOC_OSATOMIC */
+
+/*
+ * Defined if __sync_add_and_fetch(uint32_t *, uint32_t) and
+ * __sync_sub_and_fetch(uint32_t *, uint32_t) are available, despite
+ * __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4 not being defined (which means the
+ * functions are defined in libgcc instead of being inlines).
+ */
+/* #undef JE_FORCE_SYNC_COMPARE_AND_SWAP_4 */
+
+/*
+ * Defined if __sync_add_and_fetch(uint64_t *, uint64_t) and
+ * __sync_sub_and_fetch(uint64_t *, uint64_t) are available, despite
+ * __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8 not being defined (which means the
+ * functions are defined in libgcc instead of being inlines).
+ */
+/* #undef JE_FORCE_SYNC_COMPARE_AND_SWAP_8 */
+
+/*
+ * Defined if __builtin_clz() and __builtin_clzl() are available.
+ */
+#define JEMALLOC_HAVE_BUILTIN_CLZ 
+
+/*
+ * Defined if os_unfair_lock_*() functions are available, as provided by Darwin.
+ */
+/* #undef JEMALLOC_OS_UNFAIR_LOCK */
+
+/*
+ * Defined if OSSpin*() functions are available, as provided by Darwin, and
+ * documented in the spinlock(3) manual page.
+ */
+/* #undef JEMALLOC_OSSPIN */
+
+/* Defined if syscall(2) is usable. */
+#define JEMALLOC_USE_SYSCALL
+
+/*
+ * Defined if secure_getenv(3) is available.
+ */
+/* #undef JEMALLOC_HAVE_SECURE_GETENV */
+
+/*
+ * Defined if issetugid(2) is available.
+ */
+/* #undef JEMALLOC_HAVE_ISSETUGID */
+
+/* Defined if pthread_atfork(3) is available. */
+/* TODO(asmundak): if this is defined for host build, we get:
+ "~/prebuilts/clang/host/linux-x86/clang-r328903/bin/ld.lld: error: can't create dynamic relocation R_X86_64_32
+  against symbol: __dso_handle in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext'
+  to allow text relocations in the output
+  >>> defined in prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.15-4.8/lib/gcc/x86_64-linux/4.8/crtbeginS.o
+  >>> referenced by pthread_atfork.c:57
+  >>>               pthread_atfork.o:(__pthread_atfork) in archive
+  >>>               prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.15-4.8/sysroot/usr/lib/libpthread.a"
+ */
+/* #undef JEMALLOC_HAVE_PTHREAD_ATFORK */
+
+/*
+ * Defined if clock_gettime(CLOCK_MONOTONIC_COARSE, ...) is available.
+ */
+#define JEMALLOC_HAVE_CLOCK_MONOTONIC_COARSE 1
+
+/*
+ * Defined if clock_gettime(CLOCK_MONOTONIC, ...) is available.
+ */
+#define JEMALLOC_HAVE_CLOCK_MONOTONIC 1
+
+/*
+ * Defined if mach_absolute_time() is available.
+ */
+/* #undef JEMALLOC_HAVE_MACH_ABSOLUTE_TIME */
+
+/*
+ * Defined if _malloc_thread_cleanup() exists.  At least in the case of
+ * FreeBSD, pthread_key_create() allocates, which if used during malloc
+ * bootstrapping will cause recursion into the pthreads library.  Therefore, if
+ * _malloc_thread_cleanup() exists, use it as the basis for thread cleanup in
+ * malloc_tsd.
+ */
+/* #undef JEMALLOC_MALLOC_THREAD_CLEANUP */
+
+/*
+ * Defined if threaded initialization is known to be safe on this platform.
+ * Among other things, it must be possible to initialize a mutex without
+ * triggering allocation in order for threaded allocation to be safe.
+ */
+#define JEMALLOC_THREADED_INIT 
+
+/*
+ * Defined if the pthreads implementation defines
+ * _pthread_mutex_init_calloc_cb(), in which case the function is used in order
+ * to avoid recursive allocation during mutex initialization.
+ */
+/* #undef JEMALLOC_MUTEX_INIT_CB */
+
+/* Non-empty if the tls_model attribute is supported. */
+#define JEMALLOC_TLS_MODEL __attribute__((tls_model("initial-exec")))
+
+/* JEMALLOC_CC_SILENCE enables code that silences unuseful compiler warnings. */
+#define JEMALLOC_CC_SILENCE 
+
+/* JEMALLOC_CODE_COVERAGE enables test code coverage analysis. */
+/* #undef JEMALLOC_CODE_COVERAGE */
+
+/*
+ * JEMALLOC_DEBUG enables assertions and other sanity checks, and disables
+ * inline functions.
+ */
+/* #undef JEMALLOC_DEBUG */
+
+/* JEMALLOC_STATS enables statistics calculation. */
+#define JEMALLOC_STATS 
+
+/* JEMALLOC_PROF enables allocation profiling. */
+/* #undef JEMALLOC_PROF */
+
+/* Use libunwind for profile backtracing if defined. */
+/* #undef JEMALLOC_PROF_LIBUNWIND */
+
+/* Use libgcc for profile backtracing if defined. */
+/* #undef JEMALLOC_PROF_LIBGCC */
+
+/* Use gcc intrinsics for profile backtracing if defined. */
+/* #undef JEMALLOC_PROF_GCC */
+
+/*
+ * JEMALLOC_TCACHE enables a thread-specific caching layer for small objects.
+ * This makes it possible to allocate/deallocate objects without any locking
+ * when the cache is in the steady state.
+ */
+#define JEMALLOC_TCACHE 
+
+/*
+ * JEMALLOC_DSS enables use of sbrk(2) to allocate chunks from the data storage
+ * segment (DSS).
+ */
+#define JEMALLOC_DSS 
+
+/* Support memory filling (junk/zero/quarantine/redzone). */
+#define JEMALLOC_FILL 
+
+/* Support utrace(2)-based tracing. */
+/* #undef JEMALLOC_UTRACE */
+
+/* Support Valgrind. */
+/* #undef JEMALLOC_VALGRIND */
+
+/* Support optional abort() on OOM. */
+/* #undef JEMALLOC_XMALLOC */
+
+/* Support lazy locking (avoid locking unless a second thread is launched). */
+/* #undef JEMALLOC_LAZY_LOCK */
+
+/* Minimum size class to support is 2^LG_TINY_MIN bytes. */
+#define LG_TINY_MIN 3
+
+/*
+ * Minimum allocation alignment is 2^LG_QUANTUM bytes (ignoring tiny size
+ * classes).
+ */
+/* #undef LG_QUANTUM */
+
+/* One page is 2^LG_PAGE bytes. */
+#define LG_PAGE 12
+
+/*
+ * If defined, adjacent virtual memory mappings with identical attributes
+ * automatically coalesce, and they fragment when changes are made to subranges.
+ * This is the normal order of things for mmap()/munmap(), but on Windows
+ * VirtualAlloc()/VirtualFree() operations must be precisely matched, i.e.
+ * mappings do *not* coalesce/fragment.
+ */
+#define JEMALLOC_MAPS_COALESCE 
+
+/*
+ * If defined, use munmap() to unmap freed chunks, rather than storing them for
+ * later reuse.  This is disabled by default on Linux because common sequences
+ * of mmap()/munmap() calls will cause virtual memory map holes.
+ */
+/* #undef JEMALLOC_MUNMAP */
+
+/* TLS is used to map arenas and magazine caches to threads. */
+#define JEMALLOC_TLS 
+
+/*
+ * Used to mark unreachable code to quiet "end of non-void" compiler warnings.
+ * Don't use this directly; instead use unreachable() from util.h
+ */
+#define JEMALLOC_INTERNAL_UNREACHABLE __builtin_unreachable
+
+/*
+ * ffs*() functions to use for bitmapping.  Don't use these directly; instead,
+ * use ffs_*() from util.h.
+ */
+#define JEMALLOC_INTERNAL_FFSLL __builtin_ffsll
+#define JEMALLOC_INTERNAL_FFSL __builtin_ffsl
+#define JEMALLOC_INTERNAL_FFS __builtin_ffs
+
+/*
+ * JEMALLOC_IVSALLOC enables ivsalloc(), which verifies that pointers reside
+ * within jemalloc-owned chunks before dereferencing them.
+ */
+/* #undef JEMALLOC_IVSALLOC */
+
+/*
+ * If defined, explicitly attempt to more uniformly distribute large allocation
+ * pointer alignments across all cache indices.
+ */
+#define JEMALLOC_CACHE_OBLIVIOUS 
+
+/*
+ * Darwin (OS X) uses zones to work around Mach-O symbol override shortcomings.
+ */
+/* #undef JEMALLOC_ZONE */
+/* #undef JEMALLOC_ZONE_VERSION */
+
+/*
+ * Methods for determining whether the OS overcommits.
+ * JEMALLOC_PROC_SYS_VM_OVERCOMMIT_MEMORY: Linux's
+ *                                         /proc/sys/vm.overcommit_memory file.
+ * JEMALLOC_SYSCTL_VM_OVERCOMMIT: FreeBSD's vm.overcommit sysctl.
+ */
+/* #undef JEMALLOC_SYSCTL_VM_OVERCOMMIT */
+#define JEMALLOC_PROC_SYS_VM_OVERCOMMIT_MEMORY 
+
+/* Defined if madvise(2) is available. */
+#define JEMALLOC_HAVE_MADVISE 
+
+/*
+ * Methods for purging unused pages differ between operating systems.
+ *
+ *   madvise(..., MADV_FREE) : This marks pages as being unused, such that they
+ *                             will be discarded rather than swapped out.
+ *   madvise(..., MADV_DONTNEED) : This immediately discards pages, such that
+ *                                 new pages will be demand-zeroed if the
+ *                                 address region is later touched.
+ */
+/* #undef JEMALLOC_PURGE_MADVISE_FREE */
+#define JEMALLOC_PURGE_MADVISE_DONTNEED 
+
+/*
+ * Defined if transparent huge pages are supported via the MADV_[NO]HUGEPAGE
+ * arguments to madvise(2).
+ */
+/* ANDROID: Do not enable huge pages because it can increase PSS. */
+#define JEMALLOC_THP 
+
+/* Define if operating system has alloca.h header. */
+#define JEMALLOC_HAS_ALLOCA_H 1
+
+/* C99 restrict keyword supported. */
+#define JEMALLOC_HAS_RESTRICT 1
+
+/* For use by hash code. */
+/* #undef JEMALLOC_BIG_ENDIAN */
+
+/* sizeof(int) == 2^LG_SIZEOF_INT. */
+#define LG_SIZEOF_INT 2
+
+/* sizeof(long) == 2^LG_SIZEOF_LONG. */
+#ifdef __LP64__
+#define LG_SIZEOF_LONG 3
+#else
+#define LG_SIZEOF_LONG 2
+#endif
+
+/* sizeof(long long) == 2^LG_SIZEOF_LONG_LONG. */
+#define LG_SIZEOF_LONG_LONG 3
+
+/* sizeof(intmax_t) == 2^LG_SIZEOF_INTMAX_T. */
+#define LG_SIZEOF_INTMAX_T 3
+
+/* glibc malloc hooks (__malloc_hook, __realloc_hook, __free_hook). */
+#define JEMALLOC_GLIBC_MALLOC_HOOK
+
+/* glibc memalign hook. */
+#define JEMALLOC_GLIBC_MEMALIGN_HOOK
+
+/* Adaptive mutex support in pthreads. */
+#define JEMALLOC_HAVE_PTHREAD_MUTEX_ADAPTIVE_NP
+
+/*
+ * If defined, jemalloc symbols are not exported (doesn't work when
+ * JEMALLOC_PREFIX is not defined).
+ */
+/* #undef JEMALLOC_EXPORT */
+
+/* config.malloc_conf options string. */
+#define JEMALLOC_CONFIG_MALLOC_CONF ""
+
+#endif /* JEMALLOC_INTERNAL_DEFS_H_ */
diff --git a/include/jemalloc/jemalloc.h b/include/jemalloc/jemalloc.h
index bbd3ca5..8f88f6d 100644
--- a/include/jemalloc/jemalloc.h
+++ b/include/jemalloc/jemalloc.h
@@ -64,6 +64,7 @@
  * these macro definitions.
  */
 #ifndef JEMALLOC_NO_RENAME
+#if defined(__ANDROID__)
 #  define je_malloc_conf je_malloc_conf
 #  define je_malloc_message je_malloc_message
 #  define je_malloc je_malloc
@@ -86,6 +87,30 @@
 #  define je_malloc_usable_size je_malloc_usable_size
 #  define je_memalign je_memalign
 #  define je_valloc je_valloc
+#else
+#  define je_malloc_conf malloc_conf
+#  define je_malloc_message malloc_message
+#  define je_malloc malloc
+#  define je_calloc calloc
+#  define je_posix_memalign posix_memalign
+#  define je_aligned_alloc aligned_alloc
+#  define je_realloc realloc
+#  define je_free free
+#  define je_mallocx mallocx
+#  define je_rallocx rallocx
+#  define je_xallocx xallocx
+#  define je_sallocx sallocx
+#  define je_dallocx dallocx
+#  define je_sdallocx sdallocx
+#  define je_nallocx nallocx
+#  define je_mallctl mallctl
+#  define je_mallctlnametomib mallctlnametomib
+#  define je_mallctlbymib mallctlbymib
+#  define je_malloc_stats_print malloc_stats_print
+#  define je_malloc_usable_size malloc_usable_size
+#  define je_memalign memalign
+#  define je_valloc valloc
+#endif
 #endif
 
 #include <stdlib.h>
diff --git a/src/huge.c b/src/huge.c
index 1e5cb00..82e5f99 100644
--- a/src/huge.c
+++ b/src/huge.c
@@ -83,7 +83,7 @@
 	is_zeroed = zero;
 	/* ANDROID change */
 	if (likely(!tsdn_null(tsdn))) {
-#if !defined(__LP64__)
+#if defined(__ANDROID__) && !defined(__LP64__)
 		/* On 32 bit systems, using a per arena cache can exhaust
 		 * virtual address space. Force all huge allocations to
 		 * always take place in the first arena.
diff --git a/src/jemalloc.c b/src/jemalloc.c
index 72f561f..c2bafc5 100644
--- a/src/jemalloc.c
+++ b/src/jemalloc.c
@@ -2930,7 +2930,9 @@
 
 /******************************************************************************/
 
+#if defined(__ANDROID__)
 /* ANDROID extension */
 #include "android_je_iterate.c"
 #include "android_je_mallinfo.c"
 /* End ANDROID extension */
+#endif