Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libstdc++: Use of non-standard memalign instead of C11 aligned_alloc #675

Open
stephanosio opened this issue May 18, 2023 · 2 comments
Open
Labels
area: GCC Issues related to GCC (GNU Compiler Collection) area: libstdc++ Issues related to libstdc++ (GNU C++ Library) bug priority: medium Medium impact/importance issue

Comments

@stephanosio
Copy link
Member

libstdc++ seems to internally make use of the GNU memalign function instead of the ISO C aligned_alloc function.

Looking at the libstdc++ internals, it should be using the aligned_alloc instead of memalign when aligned_alloc is detected as available:

namespace __gnu_cxx {
#if _GLIBCXX_HAVE_ALIGNED_ALLOC
using ::aligned_alloc;
[...]
#elif _GLIBCXX_HAVE_MEMALIGN
static inline void*
aligned_alloc (std::size_t al, std::size_t sz)
{
  // Solaris requires al >= sizeof a word and QNX requires >= sizeof(void*)
  // but they both provide posix_memalign, so will use the definition above.
  return memalign (al, sz);
}
[...]
  while ((p = __gnu_cxx::aligned_alloc (align, sz)) == nullptr)

Find out why _GLIBCXX_HAVE_ALIGNED_ALLOC is not being defined and make sure it is defined and the non-standard memalign function is not used by libstdc++.

@stephanosio stephanosio added area: libstdc++ Issues related to libstdc++ (GNU C++ Library) area: GCC Issues related to GCC (GNU Compiler Collection) labels May 18, 2023
@stephanosio stephanosio added bug priority: medium Medium impact/importance issue labels May 18, 2023
@keith-packard
Copy link
Collaborator

Good debugging -- it looks like gcc is just missing an AC_DEFINE(HAVE_ALIGNED_ALLOC) in libstdc++-v3/configure.ac in the newlib section. It has no mechanism to autodetect the available libc API, so it hard-codes the list of available functions and aligned_alloc() is just missing here. Super easy to fix; I'll get a patch made for the SDK.

However, I don't think that eliminates the need for the memalign() alias in the common C library -- every gcc-based toolchain which doesn't have the necessary patch will fail unless that alias is provided. If we can detect when a fixed toolchain is in use, we can skip the definition there.

@keith-packard
Copy link
Collaborator

The proposed change is here: zephyrproject-rtos/gcc#22
I haven't tested it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: GCC Issues related to GCC (GNU Compiler Collection) area: libstdc++ Issues related to libstdc++ (GNU C++ Library) bug priority: medium Medium impact/importance issue
Projects
None yet
Development

No branches or pull requests

2 participants