From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 12EA43858C60; Thu, 8 Feb 2024 21:28:40 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 12EA43858C60 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1707427721; bh=1yMHglxPZrxHgzPouzhpwbPbH0kJO9reJ/BpJy+23zQ=; h=From:To:Subject:Date:From; b=o7A6ZxWgAkZPkBu0fZOD25eeNgmnzw3tEzWhYJ/bF1ABL19ZfY1106VScrgNlh0gE ++inKEcvRZ3WvYxaIWFFoyEPq01Ggm/OcGWdvU9QrBStwCkMAHmIqsRHsmkj/D2E6n nNcjBsfbnwey7aHwoS54+iNOnb9e0JiF6MyvnNIg= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r12-10142] libstdc++: Prefer posix_memalign for aligned-new [PR113258] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-12 X-Git-Oldrev: 6c53d89dae4689dbebf110e1dd427270d167a521 X-Git-Newrev: b255ab901dd0d13ad7f0dc1a823749a5e5f62570 Message-Id: <20240208212841.12EA43858C60@sourceware.org> Date: Thu, 8 Feb 2024 21:28:40 +0000 (GMT) List-Id: https://gcc.gnu.org/g:b255ab901dd0d13ad7f0dc1a823749a5e5f62570 commit r12-10142-gb255ab901dd0d13ad7f0dc1a823749a5e5f62570 Author: Jonathan Wakely Date: Tue Jan 9 15:22:46 2024 +0000 libstdc++: Prefer posix_memalign for aligned-new [PR113258] As described in PR libstdc++/113258 there are old versions of tcmalloc which replace malloc and related APIs, but do not repalce aligned_alloc because it didn't exist at the time they were released. This means that when operator new(size_t, align_val_t) uses aligned_alloc to obtain memory, it comes from libc's aligned_alloc not from tcmalloc. But when operator delete(void*, size_t, align_val_t) uses free to deallocate the memory, that goes to tcmalloc's replacement version of free, which doesn't know how to free it. If we give preference to the older posix_memalign instead of aligned_alloc then we're more likely to use a function that will be compatible with the replacement version of free. Because posix_memalign has been around for longer, it's more likely that old third-party malloc replacements will also replace posix_memalign alongside malloc and free. libstdc++-v3/ChangeLog: PR libstdc++/113258 * libsupc++/new_opa.cc: Prefer to use posix_memalign if available. (cherry picked from commit f50f2efae9fb0965d8ccdb62cfdb698336d5a933) Diff: --- libstdc++-v3/libsupc++/new_opa.cc | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/libstdc++-v3/libsupc++/new_opa.cc b/libstdc++-v3/libsupc++/new_opa.cc index 411cd8d98b26..ae01166cae3b 100644 --- a/libstdc++-v3/libsupc++/new_opa.cc +++ b/libstdc++-v3/libsupc++/new_opa.cc @@ -46,12 +46,12 @@ using std::bad_alloc; using std::size_t; extern "C" { -# if _GLIBCXX_HAVE_ALIGNED_ALLOC +# if _GLIBCXX_HAVE_POSIX_MEMALIGN + void *posix_memalign(void **, size_t alignment, size_t size); +# elif _GLIBCXX_HAVE_ALIGNED_ALLOC void *aligned_alloc(size_t alignment, size_t size); # elif _GLIBCXX_HAVE__ALIGNED_MALLOC void *_aligned_malloc(size_t size, size_t alignment); -# elif _GLIBCXX_HAVE_POSIX_MEMALIGN - void *posix_memalign(void **, size_t alignment, size_t size); # elif _GLIBCXX_HAVE_MEMALIGN void *memalign(size_t alignment, size_t size); # else @@ -63,13 +63,10 @@ extern "C" #endif namespace __gnu_cxx { -#if _GLIBCXX_HAVE_ALIGNED_ALLOC -using ::aligned_alloc; -#elif _GLIBCXX_HAVE__ALIGNED_MALLOC -static inline void* -aligned_alloc (std::size_t al, std::size_t sz) -{ return _aligned_malloc(sz, al); } -#elif _GLIBCXX_HAVE_POSIX_MEMALIGN +// Prefer posix_memalign if available, because it's older than aligned_alloc +// and so more likely to be provided by replacement malloc libraries that +// predate the addition of aligned_alloc. See PR libstdc++/113258. +#if _GLIBCXX_HAVE_POSIX_MEMALIGN static inline void* aligned_alloc (std::size_t al, std::size_t sz) { @@ -83,6 +80,12 @@ aligned_alloc (std::size_t al, std::size_t sz) return ptr; return nullptr; } +#elif _GLIBCXX_HAVE_ALIGNED_ALLOC +using ::aligned_alloc; +#elif _GLIBCXX_HAVE__ALIGNED_MALLOC +static inline void* +aligned_alloc (std::size_t al, std::size_t sz) +{ return _aligned_malloc(sz, al); } #elif _GLIBCXX_HAVE_MEMALIGN static inline void* aligned_alloc (std::size_t al, std::size_t sz) @@ -128,7 +131,8 @@ operator new (std::size_t sz, std::align_val_t al) if (__builtin_expect (sz == 0, false)) sz = 1; -#if _GLIBCXX_HAVE_ALIGNED_ALLOC +#if _GLIBCXX_HAVE_POSIX_MEMALIGN +#elif _GLIBCXX_HAVE_ALIGNED_ALLOC # if defined _AIX || defined __APPLE__ /* AIX 7.2.0.0 aligned_alloc incorrectly has posix_memalign's requirement * that alignment is a multiple of sizeof(void*).