LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 31448 - libc++ fails to compile on systems without posix_memalign
Summary: libc++ fails to compile on systems without posix_memalign
Status: RESOLVED FIXED
Alias: None
Product: libc++
Classification: Unclassified
Component: All Bugs (show other bugs)
Version: unspecified
Hardware: PC All
: P normal
Assignee: Eric Fiselier
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-12-21 12:09 PST by Jeremy Huddleston Sequoia
Modified: 2018-11-07 00:17 PST (History)
4 users (show)

See Also:
Fixed By Commit(s):


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jeremy Huddleston Sequoia 2016-12-21 12:09:56 PST
On older systems without posix_memalign, libcxx now fails to compile with:

new.cpp:73:14: error: no member named 'posix_memalign' in the global namespace
    while (::posix_memalign(&p, static_cast<size_t>(alignment), size) != 0)
           ~~^
1 error generated.

This regressed in 9acbffa370bd03e4e0ed742110e4c780b99c28ac with:

commit 9acbffa370bd03e4e0ed742110e4c780b99c28ac
Author: Eric Fiselier <eric@efcs.ca>
Date:   Fri Oct 14 06:46:30 2016 +0000

    Implement P0035R4 -- Add C++17 aligned allocation functions
    
    Summary:
    This patch implements the library side of P0035R4. The implementation is thanks to @rsmith.
    
    In addition to the C++17 implementation, the library implementation can be explicitly turned on using `-faligned-allocation` in all dialects.
    
    
    Reviewers: mclow.lists, rsmith
    
    Subscribers: rsmith, cfe-commits
    
    Differential Revision: https://reviews.llvm.org/D25591
    
    git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@284206 91177308-0d34-0410-b5e6-96231b3b80d8

Can we test for the existence of posix_memalign
Comment 1 Jeremy Huddleston Sequoia 2016-12-21 12:11:27 PST
Can we test for the existence of posix_memalign and disable support for C++17 aligned allocation functions if it doesn't exist?  I'm not suggesting we implement that internally to libcxx, but that would obviously be an option as well.
Comment 2 Saleem Abdulrasool 2016-12-21 21:31:42 PST
On Windows, posix_memalign wouldn't be available, but _aligned_malloc would.
Comment 3 Eric Fiselier 2016-12-23 14:10:15 PST
@Jeremy Can you be more specific about the platform causing issues? Is it just an older OS X version? If so do you know how to detect the availability of posix_memalign or Apple's libc version on that platform?


> On Windows, posix_memalign wouldn't be available, but _aligned_malloc would.

Ack. I'll make that change right away.
Comment 4 Eric Fiselier 2016-12-23 14:29:52 PST
Windows fix made in r290448.
Comment 5 Jeremy Huddleston Sequoia 2016-12-23 21:24:00 PST
posix_memalign was added in Snow Leopard, and we've (MacPorts) got some users still on Leopard that we try to support.  Yes, that's a bit far back.

Something like this should do it in CMakeLists.txt:

CHECK_SYMBOL_EXISTS(posix_memalign "stdlib.h" HAVE_POSIX_MEMALIGN)
CHECK_SYMBOL_EXISTS(_aligned_malloc "malloc.h" HAVE_ALIGNED_MALLOC)

Then something like this in new.cpp:

#if defined(HAVE_POSIX_MEMALIGN) || defined(HAVE_ALIGNED_MALLOC)
#define SUPPORT_ALIGNED_ALLOCATION 1
#else
#define SUPPORT_ALIGNED_ALLOCATION 0
#endif
Comment 6 Eric Fiselier 2016-12-23 21:25:58 PST
Using CMake to detect it doesn't work.

We need to be able to detect it inside <new> so we know if we can offer the aligned new/delete overloads.
Comment 7 Jeremy Huddleston Sequoia 2016-12-24 03:08:30 PST
Oh, well you could do something like this in <new>:

#if defined(__APPLE__)
# include <Availability.h>
# if __MAC_OS_X_VERSION_MIN_REQUIRED < 1060
#  define _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
# endif
#endif
Comment 8 Eric Fiselier 2016-12-24 04:09:13 PST
Awesome thanks!

Can you point to the OS X documentation that specifies the min required version for posix_memalign? I can't seem to find it anywhere.
Comment 9 Jeremy Huddleston Sequoia 2016-12-24 12:29:40 PST
It's declared in stdlib.h as:

int      posix_memalign(void **, size_t, size_t) __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_0);

And I've also verified as such through nm.
Comment 10 Eric Fiselier 2017-01-19 19:18:45 PST
Fix up for review in D28931. I'll backport it to Clang 4.0 as well.

https://reviews.llvm.org/D28931
Comment 11 Eric Fiselier 2017-01-19 19:57:06 PST
Fixed in r292564 and merged into the 4.0 release branch.
Comment 12 Jeremy Huddleston Sequoia 2017-01-20 00:00:23 PST
Patch LGTM.  Thanks.