diff --git a/libcxx/include/__support/ibm/xlocale.h b/libcxx/include/__support/ibm/xlocale.h --- a/libcxx/include/__support/ibm/xlocale.h +++ b/libcxx/include/__support/ibm/xlocale.h @@ -104,7 +104,7 @@ static inline int vasprintf(char **strp, const char *fmt, va_list ap) { const size_t buff_size = 256; - if ((*strp = (char *)malloc(buff_size)) == NULL) { + if ((*strp = (char *)std::__libcpp_malloc(buff_size)) == NULL) { return -1; } diff --git a/libcxx/include/locale b/libcxx/include/locale --- a/libcxx/include/locale +++ b/libcxx/include/locale @@ -275,7 +275,7 @@ unique_ptr __stat_hold(nullptr, free); if (__nkw > sizeof(__statbuf)) { - __status = (unsigned char*)malloc(__nkw); + __status = (unsigned char*)std::__libcpp_malloc(__nkw); if (__status == nullptr) __throw_bad_alloc(); __stat_hold.reset(__status); @@ -1606,7 +1606,7 @@ unique_ptr __obh(0, free); if (__nb != __nar) { - __ob = (char_type*)malloc(2*static_cast(__nc)*sizeof(char_type)); + __ob = (char_type*)std::__libcpp_malloc(2*static_cast(__nc)*sizeof(char_type)); if (__ob == 0) __throw_bad_alloc(); __obh.reset(__ob); @@ -1657,7 +1657,7 @@ unique_ptr __obh(0, free); if (__nb != __nar) { - __ob = (char_type*)malloc(2*static_cast(__nc)*sizeof(char_type)); + __ob = (char_type*)std::__libcpp_malloc(2*static_cast(__nc)*sizeof(char_type)); if (__ob == 0) __throw_bad_alloc(); __obh.reset(__ob); @@ -3156,7 +3156,7 @@ unique_ptr __h(nullptr, free); if (__wn - __wb.get() > __bz-2) { - __h.reset((char*)malloc(static_cast(__wn - __wb.get() + 2))); + __h.reset((char*)std::__libcpp_malloc(static_cast(__wn - __wb.get() + 2))); if (__h.get() == nullptr) __throw_bad_alloc(); __nc = __h.get(); @@ -3455,7 +3455,7 @@ if (__n == -1) __throw_bad_alloc(); __hn.reset(__bb); - __hd.reset((char_type*)malloc(static_cast(__n) * sizeof(char_type))); + __hd.reset((char_type*)std::__libcpp_malloc(static_cast(__n) * sizeof(char_type))); if (__hd == nullptr) __throw_bad_alloc(); __db = __hd.get(); @@ -3483,7 +3483,7 @@ : __sn.size() + __sym.size() + static_cast(__fd) + 2; if (__exn > __bs) { - __hw.reset((char_type*)malloc(__exn * sizeof(char_type))); + __hw.reset((char_type*)std::__libcpp_malloc(__exn * sizeof(char_type))); __mb = __hw.get(); if (__mb == 0) __throw_bad_alloc(); @@ -3525,7 +3525,7 @@ : __sn.size() + __sym.size() + static_cast(__fd) + 2; if (__exn > 100) { - __h.reset((char_type*)malloc(__exn * sizeof(char_type))); + __h.reset((char_type*)std::__libcpp_malloc(__exn * sizeof(char_type))); __mb = __h.get(); if (__mb == 0) __throw_bad_alloc(); diff --git a/libcxx/include/new b/libcxx/include/new --- a/libcxx/include/new +++ b/libcxx/include/new @@ -340,6 +340,20 @@ } #endif // !_LIBCPP_HAS_NO_ALIGNED_ALLOCATION +#if defined(_AIX) +// AIX system header maps malloc and calloc to vec_malloc and +// vec_calloc under macro __VEC__ which is pre-defined by the compiler when +// the target architecture supports Altivec. The vec_ version always aligns +// to 16-byte and causes some applications to run out of heap space +// unexpectedly. Define __libcpp_malloc/__libcpp_calloc and use it in +// libcxx and libcxxabi in places of the plain malloc/calloc where a +// specific alignment is not required. +void *__libcpp_malloc(std::size_t) __asm__("malloc"); +void *__libcpp_calloc(std::size_t, std::size_t) __asm__("calloc"); +#else +#define __libcpp_malloc(size) malloc((size)) +#define __libcpp_calloc(count, size) calloc((count), (size)) +#endif template _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_HIDE_FROM_ABI diff --git a/libcxx/include/ostream b/libcxx/include/ostream --- a/libcxx/include/ostream +++ b/libcxx/include/ostream @@ -823,7 +823,7 @@ unique_ptr<_CharT, void(*)(void*)> __h(0, free); if (__len > __bs) { - __wb = (_CharT*)malloc(__len*sizeof(_CharT)); + __wb = (_CharT*)std::__libcpp_malloc(__len*sizeof(_CharT)); if (__wb == 0) __throw_bad_alloc(); __h.reset(__wb); diff --git a/libcxx/src/debug.cpp b/libcxx/src/debug.cpp --- a/libcxx/src/debug.cpp +++ b/libcxx/src/debug.cpp @@ -176,7 +176,7 @@ if (__csz_ + 1 > static_cast(__cend_ - __cbeg_)) { size_t nc = __next_prime(2*static_cast(__cend_ - __cbeg_) + 1); - __c_node** cbeg = static_cast<__c_node**>(calloc(nc, sizeof(__c_node*))); + __c_node** cbeg = static_cast<__c_node**>(std::__libcpp_calloc(nc, sizeof(__c_node*))); if (cbeg == nullptr) __throw_bad_alloc(); @@ -198,7 +198,7 @@ } size_t hc = hash()(__c) % static_cast(__cend_ - __cbeg_); __c_node* p = __cbeg_[hc]; - void *buf = malloc(sizeof(__c_node)); + void *buf = std::__libcpp_malloc(sizeof(__c_node)); if (buf == nullptr) __throw_bad_alloc(); __cbeg_[hc] = __fn(buf, __c, p); @@ -490,7 +490,7 @@ if (nc == 0) nc = 1; __i_node** beg = - static_cast<__i_node**>(malloc(nc * sizeof(__i_node*))); + static_cast<__i_node**>(std::__libcpp_malloc(nc * sizeof(__i_node*))); if (beg == nullptr) __throw_bad_alloc(); @@ -513,7 +513,7 @@ if (__isz_ + 1 > static_cast(__iend_ - __ibeg_)) { size_t nc = __next_prime(2*static_cast(__iend_ - __ibeg_) + 1); - __i_node** ibeg = static_cast<__i_node**>(calloc(nc, sizeof(__i_node*))); + __i_node** ibeg = static_cast<__i_node**>(std::__libcpp_calloc(nc, sizeof(__i_node*))); if (ibeg == nullptr) __throw_bad_alloc(); @@ -536,7 +536,7 @@ size_t hi = hash()(__i) % static_cast(__iend_ - __ibeg_); __i_node* p = __ibeg_[hi]; __i_node* r = __ibeg_[hi] = - static_cast<__i_node*>(malloc(sizeof(__i_node))); + static_cast<__i_node*>(std::__libcpp_malloc(sizeof(__i_node))); if (r == nullptr) __throw_bad_alloc(); diff --git a/libcxx/src/filesystem/posix_compat.h b/libcxx/src/filesystem/posix_compat.h --- a/libcxx/src/filesystem/posix_compat.h +++ b/libcxx/src/filesystem/posix_compat.h @@ -356,12 +356,12 @@ } size_t buff_size = MAX_PATH + 10; std::unique_ptr buff( - static_cast(malloc(buff_size * sizeof(wchar_t))), &::free); + static_cast(std::__libcpp_malloc(buff_size * sizeof(wchar_t))), &::free); DWORD retval = GetFinalPathNameByHandleW( h, buff.get(), buff_size, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS); if (retval > buff_size) { buff_size = retval; - buff.reset(static_cast(malloc(buff_size * sizeof(wchar_t)))); + buff.reset(static_cast(std::__libcpp_malloc(buff_size * sizeof(wchar_t)))); retval = GetFinalPathNameByHandleW(h, buff.get(), buff_size, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS); } diff --git a/libcxx/src/ios.cpp b/libcxx/src/ios.cpp --- a/libcxx/src/ios.cpp +++ b/libcxx/src/ios.cpp @@ -291,26 +291,26 @@ if (__event_cap_ < rhs.__event_size_) { size_t newesize = sizeof(event_callback) * rhs.__event_size_; - new_callbacks.reset(static_cast(malloc(newesize))); + new_callbacks.reset(static_cast(std::__libcpp_malloc(newesize))); if (!new_callbacks) __throw_bad_alloc(); size_t newisize = sizeof(int) * rhs.__event_size_; - new_ints.reset(static_cast(malloc(newisize))); + new_ints.reset(static_cast(std::__libcpp_malloc(newisize))); if (!new_ints) __throw_bad_alloc(); } if (__iarray_cap_ < rhs.__iarray_size_) { size_t newsize = sizeof(long) * rhs.__iarray_size_; - new_longs.reset(static_cast(malloc(newsize))); + new_longs.reset(static_cast(std::__libcpp_malloc(newsize))); if (!new_longs) __throw_bad_alloc(); } if (__parray_cap_ < rhs.__parray_size_) { size_t newsize = sizeof(void*) * rhs.__parray_size_; - new_pointers.reset(static_cast(malloc(newsize))); + new_pointers.reset(static_cast(std::__libcpp_malloc(newsize))); if (!new_pointers) __throw_bad_alloc(); } diff --git a/libcxx/src/new.cpp b/libcxx/src/new.cpp --- a/libcxx/src/new.cpp +++ b/libcxx/src/new.cpp @@ -64,7 +64,7 @@ if (size == 0) size = 1; void* p; - while ((p = ::malloc(size)) == nullptr) + while ((p = std::__libcpp_malloc(size)) == nullptr) { // If malloc fails and there is a new_handler, // call it to try free up memory. diff --git a/libcxx/src/support/win32/support.cpp b/libcxx/src/support/win32/support.cpp --- a/libcxx/src/support/win32/support.cpp +++ b/libcxx/src/support/win32/support.cpp @@ -28,7 +28,7 @@ if (count < 0) return count; size_t buffer_size = static_cast(count) + 1; - char* p = static_cast(malloc(buffer_size)); + char* p = static_cast(std::__libcpp_malloc(buffer_size)); if ( ! p ) return -1; // If we haven't used exactly what was required, something is wrong. diff --git a/libcxxabi/src/cxa_demangle.cpp b/libcxxabi/src/cxa_demangle.cpp --- a/libcxxabi/src/cxa_demangle.cpp +++ b/libcxxabi/src/cxa_demangle.cpp @@ -255,7 +255,7 @@ BlockMeta* BlockList = nullptr; void grow() { - char* NewMeta = static_cast(std::malloc(AllocSize)); + char* NewMeta = static_cast(std::__libcpp_malloc(AllocSize)); if (NewMeta == nullptr) std::terminate(); BlockList = new (NewMeta) BlockMeta{BlockList, 0}; @@ -263,7 +263,7 @@ void* allocateMassive(size_t NBytes) { NBytes += sizeof(BlockMeta); - BlockMeta* NewMeta = reinterpret_cast(std::malloc(NBytes)); + BlockMeta* NewMeta = reinterpret_cast(std::__libcpp_malloc(NBytes)); if (NewMeta == nullptr) std::terminate(); BlockList->Next = new (NewMeta) BlockMeta{BlockList->Next, 0}; diff --git a/libcxxabi/src/cxa_thread_atexit.cpp b/libcxxabi/src/cxa_thread_atexit.cpp --- a/libcxxabi/src/cxa_thread_atexit.cpp +++ b/libcxxabi/src/cxa_thread_atexit.cpp @@ -126,7 +126,7 @@ dtors_alive = true; } - auto head = static_cast(::malloc(sizeof(DtorList))); + auto head = static_cast(std::__libcpp_malloc(sizeof(DtorList))); if (!head) { return -1; } diff --git a/libcxxabi/src/fallback_malloc.cpp b/libcxxabi/src/fallback_malloc.cpp --- a/libcxxabi/src/fallback_malloc.cpp +++ b/libcxxabi/src/fallback_malloc.cpp @@ -208,7 +208,7 @@ if (void* dest = std::__libcpp_aligned_alloc(alignof(__aligned_type), size)) return dest; #elif defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION) - if (void* dest = ::malloc(size)) + if (void* dest = std::__libcpp_malloc(size)) return dest; #else if (size == 0) @@ -220,7 +220,7 @@ } void* __calloc_with_fallback(size_t count, size_t size) { - void* ptr = ::calloc(count, size); + void* ptr = std::__libcpp_calloc(count, size); if (NULL != ptr) return ptr; // if calloc fails, fall back to emergency stash diff --git a/libcxxabi/src/stdlib_new_delete.cpp b/libcxxabi/src/stdlib_new_delete.cpp --- a/libcxxabi/src/stdlib_new_delete.cpp +++ b/libcxxabi/src/stdlib_new_delete.cpp @@ -27,7 +27,7 @@ if (size == 0) size = 1; void* p; - while ((p = ::malloc(size)) == nullptr) + while ((p = std::__libcpp_malloc(size)) == nullptr) { // If malloc fails and there is a new_handler, // call it to try free up memory.