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,10 +275,10 @@ unique_ptr __stat_hold(nullptr, free); if (__nkw > sizeof(__statbuf)) { - __status = (unsigned char*)malloc(__nkw); - if (__status == nullptr) - __throw_bad_alloc(); - __stat_hold.reset(__status); + __status = (unsigned char*)std::__libcpp_malloc(__nkw); + if (__status == nullptr) + __throw_bad_alloc(); + __stat_hold.reset(__status); } size_t __n_might_match = __nkw; // At this point, any keyword might match size_t __n_does_match = 0; // but none of them definitely do @@ -1606,10 +1606,10 @@ unique_ptr __obh(0, free); if (__nb != __nar) { - __ob = (char_type*)malloc(2*static_cast(__nc)*sizeof(char_type)); - if (__ob == 0) - __throw_bad_alloc(); - __obh.reset(__ob); + __ob = (char_type*)std::__libcpp_malloc(2*static_cast(__nc)*sizeof(char_type)); + if (__ob == 0) + __throw_bad_alloc(); + __obh.reset(__ob); } char_type* __op; // pad here char_type* __oe; // end of output @@ -1657,10 +1657,10 @@ unique_ptr __obh(0, free); if (__nb != __nar) { - __ob = (char_type*)malloc(2*static_cast(__nc)*sizeof(char_type)); - if (__ob == 0) - __throw_bad_alloc(); - __obh.reset(__ob); + __ob = (char_type*)std::__libcpp_malloc(2*static_cast(__nc)*sizeof(char_type)); + if (__ob == 0) + __throw_bad_alloc(); + __obh.reset(__ob); } char_type* __op; // pad here char_type* __oe; // end of output @@ -3156,10 +3156,10 @@ unique_ptr __h(nullptr, free); if (__wn - __wb.get() > __bz-2) { - __h.reset((char*)malloc(static_cast(__wn - __wb.get() + 2))); - if (__h.get() == nullptr) - __throw_bad_alloc(); - __nc = __h.get(); + __h.reset((char*)std::__libcpp_malloc(static_cast(__wn - __wb.get() + 2))); + if (__h.get() == nullptr) + __throw_bad_alloc(); + __nc = __h.get(); } if (__neg) *__nc++ = '-'; @@ -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,10 +3483,10 @@ : __sn.size() + __sym.size() + static_cast(__fd) + 2; if (__exn > __bs) { - __hw.reset((char_type*)malloc(__exn * sizeof(char_type))); - __mb = __hw.get(); - if (__mb == 0) - __throw_bad_alloc(); + __hw.reset((char_type*)std::__libcpp_malloc(__exn * sizeof(char_type))); + __mb = __hw.get(); + if (__mb == 0) + __throw_bad_alloc(); } // format char_type* __mi; @@ -3525,10 +3525,10 @@ : __sn.size() + __sym.size() + static_cast(__fd) + 2; if (__exn > 100) { - __h.reset((char_type*)malloc(__exn * sizeof(char_type))); - __mb = __h.get(); - if (__mb == 0) - __throw_bad_alloc(); + __h.reset((char_type*)std::__libcpp_malloc(__exn * sizeof(char_type))); + __mb = __h.get(); + if (__mb == 0) + __throw_bad_alloc(); } // format char_type* __mi; 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 them 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,10 +823,10 @@ unique_ptr<_CharT, void(*)(void*)> __h(0, free); if (__len > __bs) { - __wb = (_CharT*)malloc(__len*sizeof(_CharT)); - if (__wb == 0) - __throw_bad_alloc(); - __h.reset(__wb); + __wb = (_CharT*)std::__libcpp_malloc(__len * sizeof(_CharT)); + if (__wb == 0) + __throw_bad_alloc(); + __h.reset(__wb); } for (_CharT* __p = __wb; *__strn != '\0'; ++__strn, ++__p) *__p = __os.widen(*__strn); 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); @@ -489,8 +489,7 @@ size_t nc = 2*static_cast(cap_ - beg_); if (nc == 0) nc = 1; - __i_node** beg = - static_cast<__i_node**>(malloc(nc * sizeof(__i_node*))); + __i_node** beg = static_cast<__i_node**>(std::__libcpp_malloc(nc * sizeof(__i_node*))); if (beg == nullptr) __throw_bad_alloc(); @@ -513,7 +512,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(); @@ -535,8 +534,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))); + __i_node* r = __ibeg_[hi] = 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,19 +64,18 @@ if (size == 0) size = 1; void* p; - while ((p = ::malloc(size)) == nullptr) - { - // If malloc fails and there is a new_handler, - // call it to try free up memory. - std::new_handler nh = std::get_new_handler(); - if (nh) - nh(); - else -#ifndef _LIBCPP_NO_EXCEPTIONS - throw std::bad_alloc(); -#else - break; -#endif + while ((p = std::__libcpp_malloc(size)) == nullptr) { + // If malloc fails and there is a new_handler, + // call it to try free up memory. + std::new_handler nh = std::get_new_handler(); + if (nh) + nh(); + else +# ifndef _LIBCPP_NO_EXCEPTIONS + throw std::bad_alloc(); +# else + break; +# endif } return p; } 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,14 +27,13 @@ if (size == 0) size = 1; void* p; - while ((p = ::malloc(size)) == nullptr) - { - // If malloc fails and there is a new_handler, - // call it to try free up memory. - std::new_handler nh = std::get_new_handler(); - if (nh) - nh(); - else + while ((p = std::__libcpp_malloc(size)) == nullptr) { + // If malloc fails and there is a new_handler, + // call it to try free up memory. + std::new_handler nh = std::get_new_handler(); + if (nh) + nh(); + else #ifndef _LIBCXXABI_NO_EXCEPTIONS throw std::bad_alloc(); #else