Support for jemalloc to replace dlmalloc.

To use jemalloc, add MALLOC_IMPL = jemalloc in a board config file
and you get the new version automatically.

Update the pthread_create_key tests since jemalloc uses a few keys.
Add a new test to verify memalign works as expected.

Bug: 981363

Change-Id: I16eb152b291a95bd2499e90492fc6b4bd7053836
diff --git a/libc/bionic/malloc_debug_check.cpp b/libc/bionic/malloc_debug_check.cpp
index 11578a3..2590ce7 100644
--- a/libc/bionic/malloc_debug_check.cpp
+++ b/libc/bionic/malloc_debug_check.cpp
@@ -47,7 +47,6 @@
 
 #include "debug_mapinfo.h"
 #include "debug_stacktrace.h"
-#include "dlmalloc.h"
 #include "private/libc_logging.h"
 #include "malloc_debug_common.h"
 #include "private/ScopedPthreadMutexLocker.h"
@@ -74,7 +73,7 @@
 
 struct hdr_t {
     uint32_t tag;
-    void* base;  // Always points to the memory allocated using dlmalloc.
+    void* base;  // Always points to the memory allocated using malloc.
                  // For memory allocated in chk_memalign, this value will
                  // not be the same as the location of the start of this
                  // structure.
@@ -321,14 +320,14 @@
     while (backlog_num > g_malloc_debug_backlog) {
         hdr_t* gone = backlog_tail;
         del_from_backlog_locked(gone);
-        dlfree(gone->base);
+        Malloc(free)(gone->base);
     }
 }
 
 extern "C" void* chk_malloc(size_t size) {
 //  log_message("%s: %s\n", __FILE__, __FUNCTION__);
 
-    hdr_t* hdr = static_cast<hdr_t*>(dlmalloc(sizeof(hdr_t) + size + sizeof(ftr_t)));
+    hdr_t* hdr = static_cast<hdr_t*>(Malloc(malloc)(sizeof(hdr_t) + size + sizeof(ftr_t)));
     if (hdr) {
         hdr->base = hdr;
         hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
@@ -356,7 +355,7 @@
         return NULL;
     }
 
-    void* base = dlmalloc(sizeof(hdr_t) + size + sizeof(ftr_t));
+    void* base = Malloc(malloc)(sizeof(hdr_t) + size + sizeof(ftr_t));
     if (base != NULL) {
         // Check that the actual pointer that will be returned is aligned
         // properly.
@@ -453,25 +452,25 @@
                        user(hdr), size);
             log_backtrace(bt, depth);
             // just get a whole new allocation and leak the old one
-            return dlrealloc(0, size);
-            // return dlrealloc(user(hdr), size); // assuming it was allocated externally
+            return Malloc(realloc)(0, size);
+            // return realloc(user(hdr), size); // assuming it was allocated externally
         }
     }
 
     if (hdr->base != hdr) {
         // An allocation from memalign, so create another allocation and
         // copy the data out.
-        void* newMem = dlmalloc(sizeof(hdr_t) + size + sizeof(ftr_t));
+        void* newMem = Malloc(malloc)(sizeof(hdr_t) + size + sizeof(ftr_t));
         if (newMem) {
             memcpy(newMem, hdr, sizeof(hdr_t) + hdr->size);
-            dlfree(hdr->base);
+            Malloc(free)(hdr->base);
             hdr = static_cast<hdr_t*>(newMem);
         } else {
-            dlfree(hdr->base);
+            Malloc(free)(hdr->base);
             hdr = NULL;
         }
     } else {
-        hdr = static_cast<hdr_t*>(dlrealloc(hdr, sizeof(hdr_t) + size + sizeof(ftr_t)));
+        hdr = static_cast<hdr_t*>(Malloc(realloc)(hdr, sizeof(hdr_t) + size + sizeof(ftr_t)));
     }
     if (hdr) {
         hdr->base = hdr;
@@ -486,7 +485,7 @@
 extern "C" void* chk_calloc(int nmemb, size_t size) {
 //  log_message("%s: %s\n", __FILE__, __FUNCTION__);
     size_t total_size = nmemb * size;
-    hdr_t* hdr = static_cast<hdr_t*>(dlcalloc(1, sizeof(hdr_t) + total_size + sizeof(ftr_t)));
+    hdr_t* hdr = static_cast<hdr_t*>(Malloc(calloc)(1, sizeof(hdr_t) + total_size + sizeof(ftr_t)));
     if (hdr) {
         hdr->base = hdr;
         hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
@@ -497,7 +496,7 @@
 }
 
 extern "C" size_t chk_malloc_usable_size(const void* ptr) {
-    // dlmalloc_usable_size returns 0 for NULL and unknown blocks.
+    // malloc_usable_size returns 0 for NULL and unknown blocks.
     if (ptr == NULL)
         return 0;