use tcmalloc

It doesn't get used with tsan because tsan wants to hook malloc and
friends itself.

Change-Id: I0286d9e0053ecac40a30760a81ca5df1dd6e8e0d
diff --git a/aos/build/aos.gypi b/aos/build/aos.gypi
index cb2ca33..2051d63 100644
--- a/aos/build/aos.gypi
+++ b/aos/build/aos.gypi
@@ -111,6 +111,12 @@
           ],
         },
       },
+    ], ['SANITIZER!="thread"', {
+        'libraries': [
+          '<!(readlink -f <(AOS)/../output/compiled-<(ARCHITECTURE)<(EXTERNALS_EXTRA)/gperftools-2.3-prefix/lib/libtcmalloc.a)',
+          '<!(readlink -f <(AOS)/../output/compiled-<(ARCHITECTURE)<(EXTERNALS_EXTRA)/libunwind-1.1-prefix/lib/libunwind.a)',
+        ],
+      },
     ], ['EXTERNALS_EXTRA=="-fPIE"', {
         'target_defaults': {
           'cflags': [
diff --git a/aos/build/download_externals.sh b/aos/build/download_externals.sh
index ecd1b01..674dc3a 100755
--- a/aos/build/download_externals.sh
+++ b/aos/build/download_externals.sh
@@ -191,7 +191,7 @@
 [ -f ${LIBCDD_LIB} ] || \
 	bash -c "cd ${LIBCDD_DIR} && ./configure \
 	--disable-shared ${CONFIGURE_FLAGS} \
-	LDFLAGS=\"${ALL_LDFLAGS} -L${GMP_PREFIX}/lib\"\
+	LDFLAGS=\"${ALL_LDFLAGS} -L${GMP_PREFIX}/lib\" \
 	--prefix=$(readlink -f ${LIBCDD_PREFIX}) \
 	&& make gmpdir=${GMP_PREFIX} && make install"
 
@@ -203,4 +203,39 @@
 	git clone ${STM32FLASH_URL} ${STM32FLASH_DIR}/stm32flash && \
 	cd ${STM32FLASH_DIR}/stm32flash && git checkout ${STM32FLASH_COMMIT} )
 
+# get and build libunwind
+LIBUNWIND_VERSION=1.1
+LIBUNWIND_TAR=${EXTERNALS}/libunwind-${LIBUNWIND_VERSION}.tar.gz
+LIBUNWIND_DIR=${COMPILED}/libunwind-${LIBUNWIND_VERSION}
+LIBUNWIND_PREFIX=${LIBUNWIND_DIR}-prefix
+LIBUNWIND_LIB=${LIBUNWIND_PREFIX}/lib/libunwind.a
+LIBUNWIND_URL=http://download.savannah.gnu.org/releases/libunwind/libunwind-${LIBUNWIND_VERSION}.tar.gz
+[ -f ${LIBUNWIND_TAR} ] || \
+	wget ${LIBUNWIND_URL} -O ${LIBUNWIND_TAR}
+[ -d ${LIBUNWIND_DIR} ] || ( mkdir ${LIBUNWIND_DIR} && tar \
+	--strip-components=1 -C ${LIBUNWIND_DIR} -xf ${LIBUNWIND_TAR} )
+[ -f ${LIBUNWIND_LIB} ] || \
+	bash -c "cd ${LIBUNWIND_DIR} && ./configure \
+	--prefix=$(readlink -f ${LIBUNWIND_PREFIX}) \
+	${CONFIGURE_FLAGS} \
+	&& make && make install"
+
+# get and build gperftools
+GPERFTOOLS_VERSION=2.3
+GPERFTOOLS_TAR=${EXTERNALS}/gperftools-${GPERFTOOLS_VERSION}.tar.gz
+GPERFTOOLS_DIR=${COMPILED}/gperftools-${GPERFTOOLS_VERSION}
+GPERFTOOLS_PREFIX=${GPERFTOOLS_DIR}-prefix
+TCMALLOC_LIB=${GPERFTOOLS_PREFIX}/lib/libtcmalloc.a
+GPERFTOOLS_URL=https://googledrive.com/host/0B6NtGsLhIcf7MWxMMF9JdTN3UVk/gperftools-2.3.tar.gz
+[ -f ${GPERFTOOLS_TAR} ] || \
+	wget ${GPERFTOOLS_URL} -O ${GPERFTOOLS_TAR}
+[ -d ${GPERFTOOLS_DIR} ] || ( mkdir ${GPERFTOOLS_DIR} && tar \
+	--strip-components=1 -C ${GPERFTOOLS_DIR} -xf ${GPERFTOOLS_TAR} )
+[ -f ${TCMALLOC_LIB} ] || \
+	bash -c "cd ${GPERFTOOLS_DIR} && ./configure \
+	${CONFIGURE_FLAGS} --prefix=$(readlink -f ${GPERFTOOLS_PREFIX}) \
+	LDFLAGS=\"${ALL_LDFLAGS} -L${LIBUNWIND_PREFIX}/lib -Wl,-rpath-link=${LIBUNWIND_PREFIX}/lib\" \
+	CPPFLAGS=\"-I ${LIBUNWIND_PREFIX}/include\" \
+	&& make && make install"
+
 rm -rf ${TMPDIR}
diff --git a/output/downloaded/.gitignore b/output/downloaded/.gitignore
index 07b809d..53de81a 100644
--- a/output/downloaded/.gitignore
+++ b/output/downloaded/.gitignore
@@ -20,3 +20,5 @@
 /libcdd-094g/
 /gmp-5.1.3.tar.lz
 /stm32flash-8399fbe1baf2b7d097746786458021d92895d71b/
+/gperftools-2.3.tar.gz
+/libunwind-1.1.tar.gz