From e5fdaa4f9d102461a4d8a865e6ca84666893b9e7 Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Sat, 14 Jun 2014 01:04:31 +0000 Subject: [PATCH] Revert "Backing this one out since the counterpart needs to be sent upstream." This reverts commit a04d2bc28e7d7fcaf34ad71e4a6608a13cf84197. Change-Id: I1b49165ca5d4bafdba7948818256a6167a363aca --- libc/bionic/malloc_debug_common.cpp | 21 +++++++++++++++++++-- libc/bionic/malloc_debug_common.h | 15 +++++++++++++++ libc/include/malloc.h | 3 --- tests/Android.mk | 9 +++++++++ tests/malloc_test.cpp | 9 +++++++++ tests/string_test.cpp | 7 ++++--- 6 files changed, 56 insertions(+), 8 deletions(-) diff --git a/libc/bionic/malloc_debug_common.cpp b/libc/bionic/malloc_debug_common.cpp index 6677c22e3..cbca160eb 100644 --- a/libc/bionic/malloc_debug_common.cpp +++ b/libc/bionic/malloc_debug_common.cpp @@ -61,9 +61,13 @@ static const MallocDebug __libc_malloc_default_dispatch __attribute__((aligned(3 Malloc(malloc_usable_size), Malloc(memalign), Malloc(posix_memalign), +#if defined(HAVE_DEPRECATED_MALLOC_FUNCS) Malloc(pvalloc), +#endif Malloc(realloc), +#if defined(HAVE_DEPRECATED_MALLOC_FUNCS) Malloc(valloc), +#endif }; // Selector of dispatch table to use for dispatching malloc calls. @@ -258,17 +262,21 @@ extern "C" int posix_memalign(void** memptr, size_t alignment, size_t size) { return __libc_malloc_dispatch->posix_memalign(memptr, alignment, size); } +#if defined(HAVE_DEPRECATED_MALLOC_FUNCS) extern "C" void* pvalloc(size_t bytes) { return __libc_malloc_dispatch->pvalloc(bytes); } +#endif extern "C" void* realloc(void* oldMem, size_t bytes) { return __libc_malloc_dispatch->realloc(oldMem, bytes); } +#if defined(HAVE_DEPRECATED_MALLOC_FUNCS) extern "C" void* valloc(size_t bytes) { return __libc_malloc_dispatch->valloc(bytes); } +#endif // We implement malloc debugging only in libc.so, so the code below // must be excluded if we compile this file for static libc.a @@ -299,9 +307,13 @@ static void InitMalloc(void* malloc_impl_handler, MallocDebug* table, const char InitMallocFunction(malloc_impl_handler, &table->malloc_usable_size, prefix, "malloc_usable_size"); InitMallocFunction(malloc_impl_handler, &table->memalign, prefix, "memalign"); InitMallocFunction(malloc_impl_handler, &table->posix_memalign, prefix, "posix_memalign"); +#if defined(HAVE_DEPRECATED_MALLOC_FUNCS) InitMallocFunction(malloc_impl_handler, &table->pvalloc, prefix, "pvalloc"); +#endif InitMallocFunction(malloc_impl_handler, &table->realloc, prefix, "realloc"); +#if defined(HAVE_DEPRECATED_MALLOC_FUNCS) InitMallocFunction(malloc_impl_handler, &table->valloc, prefix, "valloc"); +#endif } // Initializes memory allocation framework once per process. @@ -447,9 +459,14 @@ static void malloc_init_impl() { (malloc_dispatch_table.malloc_usable_size == NULL) || (malloc_dispatch_table.memalign == NULL) || (malloc_dispatch_table.posix_memalign == NULL) || +#if defined(HAVE_DEPRECATED_MALLOC_FUNCS) (malloc_dispatch_table.pvalloc == NULL) || - (malloc_dispatch_table.realloc == NULL) || - (malloc_dispatch_table.valloc == NULL)) { +#endif + (malloc_dispatch_table.realloc == NULL) +#if defined(HAVE_DEPRECATED_MALLOC_FUNCS) + || (malloc_dispatch_table.valloc == NULL) +#endif + ) { error_log("%s: some symbols for libc.debug.malloc level %d were not found (see above)", getprogname(), g_malloc_debug_level); dlclose(malloc_impl_handle); diff --git a/libc/bionic/malloc_debug_common.h b/libc/bionic/malloc_debug_common.h index 8052a1775..a0f9972dd 100644 --- a/libc/bionic/malloc_debug_common.h +++ b/libc/bionic/malloc_debug_common.h @@ -61,6 +61,12 @@ #define Malloc(function) dl ## function #endif +// valloc(3) and pvalloc(3) were removed from POSIX 2004. We do not include them +// for LP64, but the symbols remain in LP32 for binary compatibility. +#ifndef __LP64__ +#define HAVE_DEPRECATED_MALLOC_FUNCS 1 +#endif + // ============================================================================= // Structures // ============================================================================= @@ -90,9 +96,14 @@ typedef void* (*MallocDebugMalloc)(size_t); typedef size_t (*MallocDebugMallocUsableSize)(const void*); typedef void* (*MallocDebugMemalign)(size_t, size_t); typedef int (*MallocDebugPosixMemalign)(void**, size_t, size_t); +#if defined(HAVE_DEPRECATED_MALLOC_FUNCS) typedef void* (*MallocDebugPvalloc)(size_t); +#endif typedef void* (*MallocDebugRealloc)(void*, size_t); +#if defined(HAVE_DEPRECATED_MALLOC_FUNCS) typedef void* (*MallocDebugValloc)(size_t); +#endif + struct MallocDebug { MallocDebugCalloc calloc; MallocDebugFree free; @@ -101,9 +112,13 @@ struct MallocDebug { MallocDebugMallocUsableSize malloc_usable_size; MallocDebugMemalign memalign; MallocDebugPosixMemalign posix_memalign; +#if defined(HAVE_DEPRECATED_MALLOC_FUNCS) MallocDebugPvalloc pvalloc; +#endif MallocDebugRealloc realloc; +#if defined(HAVE_DEPRECATED_MALLOC_FUNCS) MallocDebugValloc valloc; +#endif }; typedef bool (*MallocDebugInit)(HashTable*); diff --git a/libc/include/malloc.h b/libc/include/malloc.h index 9a4e32472..e6ea27606 100644 --- a/libc/include/malloc.h +++ b/libc/include/malloc.h @@ -35,9 +35,6 @@ extern void free(void* p); extern void* memalign(size_t alignment, size_t byte_count) __mallocfunc __wur __attribute__((alloc_size(2))); extern size_t malloc_usable_size(const void* p); -extern void* valloc(size_t byte_count) __mallocfunc __wur __attribute__((alloc_size(1))); -extern void* pvalloc(size_t byte_count) __mallocfunc __wur __attribute__((alloc_size(1))); - #ifndef STRUCT_MALLINFO_DECLARED #define STRUCT_MALLINFO_DECLARED 1 struct mallinfo { diff --git a/tests/Android.mk b/tests/Android.mk index c2e76b6bc..ce0c5f211 100644 --- a/tests/Android.mk +++ b/tests/Android.mk @@ -115,9 +115,18 @@ libBionicStandardTests_src_files := \ libBionicStandardTests_cflags := \ $(test_cflags) \ +ifeq ($(MALLOC_IMPL),jemalloc) + libBionicStandardTests_cflags += -DUSE_JEMALLOC +else + libBionicStandardTests_cflags += -DUSE_DLMALLOC +endif + libBionicStandardTests_cppflags := \ $(test_cppflags) \ +libBionicStandardTests_c_includes := \ + bionic/libc \ + libBionicStandardTests_ldlibs_host := \ -lrt \ diff --git a/tests/malloc_test.cpp b/tests/malloc_test.cpp index ed98f15d3..d70136496 100644 --- a/tests/malloc_test.cpp +++ b/tests/malloc_test.cpp @@ -22,6 +22,10 @@ #include #include +#if defined(__BIONIC__) +#include +#endif + TEST(malloc, malloc_std) { // Simple malloc test. void *ptr = malloc(100); @@ -291,6 +295,10 @@ TEST(malloc, realloc_overflow) { free(ptr); } +#if defined(HAVE_DEPRECATED_MALLOC_FUNCS) +extern "C" void* pvalloc(size_t); +extern "C" void* valloc(size_t); + TEST(malloc, pvalloc_std) { size_t pagesize = sysconf(_SC_PAGESIZE); void* ptr = pvalloc(100); @@ -315,3 +323,4 @@ TEST(malloc, valloc_std) { TEST(malloc, valloc_overflow) { ASSERT_EQ(NULL, valloc(SIZE_MAX)); } +#endif diff --git a/tests/string_test.cpp b/tests/string_test.cpp index a468b97fc..2ab60d771 100644 --- a/tests/string_test.cpp +++ b/tests/string_test.cpp @@ -17,6 +17,7 @@ #include #include +#include #include #include @@ -143,9 +144,9 @@ struct StringTestState { int max_alignment = 64; // TODO: fix the tests to not sometimes use twice their specified "MAX_LEN". - glob_ptr = reinterpret_cast(valloc(2 * sizeof(Character) * MAX_LEN + max_alignment)); - glob_ptr1 = reinterpret_cast(valloc(2 * sizeof(Character) * MAX_LEN + max_alignment)); - glob_ptr2 = reinterpret_cast(valloc(2 * sizeof(Character) * MAX_LEN + max_alignment)); + glob_ptr = reinterpret_cast(memalign(sysconf(_SC_PAGESIZE), 2 * sizeof(Character) * MAX_LEN + max_alignment)); + glob_ptr1 = reinterpret_cast(memalign(sysconf(_SC_PAGESIZE), 2 * sizeof(Character) * MAX_LEN + max_alignment)); + glob_ptr2 = reinterpret_cast(memalign(sysconf(_SC_PAGESIZE), 2 * sizeof(Character) * MAX_LEN + max_alignment)); InitLenArray();