Index: include/__config =================================================================== --- include/__config +++ include/__config @@ -126,6 +126,13 @@ # define _LIBCPP_ABI_ITANIUM #endif +#if defined(__APPLE__) +# include +# if __MAC_OS_X_VERSION_MIN_REQUIRED < 1060 +# define _LIBCPP_HAS_NO_ALIGNED_ALLOCATION +# endif +#endif // defined(__APPLE__) + // Need to detect which libc we're using if we're on Linux. #if defined(__linux__) #include Index: include/new =================================================================== --- include/new +++ include/new @@ -101,8 +101,9 @@ # define _LIBCPP_HAS_NO_SIZED_DEALLOCATION #endif -#if !(defined(_LIBCPP_BUILDING_NEW) || _LIBCPP_STD_VER > 14 || \ - (defined(__cpp_aligned_new) && __cpp_aligned_new >= 201606)) +#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) && \ + (!(defined(_LIBCPP_BUILDING_NEW) || _LIBCPP_STD_VER > 14 || \ + (defined(__cpp_aligned_new) && __cpp_aligned_new >= 201606))) # define _LIBCPP_HAS_NO_ALIGNED_ALLOCATION #endif Index: src/new.cpp =================================================================== --- src/new.cpp +++ src/new.cpp @@ -64,38 +64,6 @@ } _LIBCPP_WEAK -void * -operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC -{ - if (size == 0) - size = 1; - if (static_cast(alignment) < sizeof(void*)) - alignment = std::align_val_t(sizeof(void*)); - void* p; -#if defined(_LIBCPP_MSVCRT) - while ((p = _aligned_malloc(size, static_cast(alignment))) == nullptr) -#else - while (::posix_memalign(&p, static_cast(alignment), size) != 0) -#endif - { - // If posix_memalign 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 - p = nullptr; // posix_memalign doesn't initialize 'p' on failure - break; -#endif - } - } - return p; -} - -_LIBCPP_WEAK void* operator new(size_t size, const std::nothrow_t&) _NOEXCEPT { @@ -116,14 +84,21 @@ _LIBCPP_WEAK void* -operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT +operator new[](size_t size) _THROW_BAD_ALLOC +{ + return ::operator new(size); +} + +_LIBCPP_WEAK +void* +operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT { void* p = 0; #ifndef _LIBCPP_NO_EXCEPTIONS try { #endif // _LIBCPP_NO_EXCEPTIONS - p = ::operator new(size, alignment); + p = ::operator new[](size); #ifndef _LIBCPP_NO_EXCEPTIONS } catch (...) @@ -134,29 +109,92 @@ } _LIBCPP_WEAK -void* -operator new[](size_t size) _THROW_BAD_ALLOC +void +operator delete(void* ptr) _NOEXCEPT { - return ::operator new(size); + if (ptr) + ::free(ptr); } _LIBCPP_WEAK -void* -operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC +void +operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT { - return ::operator new(size, alignment); + ::operator delete(ptr); +} + +_LIBCPP_WEAK +void +operator delete(void* ptr, size_t) _NOEXCEPT +{ + ::operator delete(ptr); +} + +_LIBCPP_WEAK +void +operator delete[] (void* ptr) _NOEXCEPT +{ + ::operator delete(ptr); +} + +_LIBCPP_WEAK +void +operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT +{ + ::operator delete[](ptr); +} + +_LIBCPP_WEAK +void +operator delete[] (void* ptr, size_t) _NOEXCEPT +{ + ::operator delete[](ptr); +} + +#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) + +_LIBCPP_WEAK +void * +operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC +{ + if (size == 0) + size = 1; + if (static_cast(alignment) < sizeof(void*)) + alignment = std::align_val_t(sizeof(void*)); + void* p; +#if defined(_LIBCPP_MSVCRT) + while ((p = _aligned_malloc(size, static_cast(alignment))) == nullptr) +#else + while (::posix_memalign(&p, static_cast(alignment), size) != 0) +#endif + { + // If posix_memalign 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 + p = nullptr; // posix_memalign doesn't initialize 'p' on failure + break; +#endif + } + } + return p; } _LIBCPP_WEAK void* -operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT +operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT { void* p = 0; #ifndef _LIBCPP_NO_EXCEPTIONS try { #endif // _LIBCPP_NO_EXCEPTIONS - p = ::operator new[](size); + p = ::operator new(size, alignment); #ifndef _LIBCPP_NO_EXCEPTIONS } catch (...) @@ -168,6 +206,13 @@ _LIBCPP_WEAK void* +operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC +{ + return ::operator new(size, alignment); +} + +_LIBCPP_WEAK +void* operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT { void* p = 0; @@ -187,14 +232,6 @@ _LIBCPP_WEAK void -operator delete(void* ptr) _NOEXCEPT -{ - if (ptr) - ::free(ptr); -} - -_LIBCPP_WEAK -void operator delete(void* ptr, std::align_val_t) _NOEXCEPT { if (ptr) @@ -207,13 +244,6 @@ _LIBCPP_WEAK void -operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT -{ - ::operator delete(ptr); -} - -_LIBCPP_WEAK -void operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT { ::operator delete(ptr, alignment); @@ -221,13 +251,6 @@ _LIBCPP_WEAK void -operator delete(void* ptr, size_t) _NOEXCEPT -{ - ::operator delete(ptr); -} - -_LIBCPP_WEAK -void operator delete(void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT { ::operator delete(ptr, alignment); @@ -235,13 +258,6 @@ _LIBCPP_WEAK void -operator delete[] (void* ptr) _NOEXCEPT -{ - ::operator delete(ptr); -} - -_LIBCPP_WEAK -void operator delete[] (void* ptr, std::align_val_t alignment) _NOEXCEPT { ::operator delete(ptr, alignment); @@ -249,13 +265,6 @@ _LIBCPP_WEAK void -operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT -{ - ::operator delete[](ptr); -} - -_LIBCPP_WEAK -void operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT { ::operator delete[](ptr, alignment); @@ -263,18 +272,13 @@ _LIBCPP_WEAK void -operator delete[] (void* ptr, size_t) _NOEXCEPT -{ - ::operator delete[](ptr); -} - -_LIBCPP_WEAK -void operator delete[] (void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT { ::operator delete[](ptr, alignment); } +#endif // !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) + #endif // !__GLIBCXX__ namespace std