[PATCH] Guard aligned_alloc implementation with ALIGNED_ALLOC_PROVIDED define
Daniel Kowalski
dkowalski@n7space.com
Fri Nov 20 13:56:11 GMT 2026
Hello,
Add ALIGNED_ALLOC_PROVIDED and define it for RTEMS
RTEMS provides its own malloc() and family, including aligned_alloc(),
builds with MALLOC_PROVIDED. aligned_alloc() calls _memalign_r() which
is not built when MALLOC_PROVIDED is defined and RTEMS does not have
_memalign_r(). Everything will still link, because RTEMS also has aligned_alloc()
implementation, but which implementation is used depends on link order
and newlib one cannot be linked in this configuration.
I tried using MALLOC_PROVIDED as a guard, but Cygwin also defines it and relies
on aligned_alloc wrapper, but it has _memalign_r() which uses its memalign()
Added ALIGNED_ALLOC_PROVIDED guard working the same way as
MALLOC_PROVIDED and defined it for RTEMS targets.
Best regards/Pozdrawiam
Daniel Kowalski
Software Engineer
N7 Space Sp. z o.o.
https://n7space.com
https://bootloader.space
https://canopen.space
Signed-off-by: Daniel Kowalski <dkowalski@n7space.com>
---
This came up while building LLVM-based toolchains for RTEMS
for SPARC and RISC-V platforms. If program is linked with -lc before -lrtemscpu
newlib aligned_alloc() gets linked and breaks the build because of missing
_memalign_r(). If -lc is after -lrtemscpu, RTEMS own aligned_alloc()
implementation is picked. There should not be two aligned_alloc
implementations available.
newlib/configure.host | 2 +-
newlib/libc/stdlib/aligned_alloc.c | 4 ++++
2 files changed, 5 insertions(+), 1 deletion(-)
diff --git a/newlib/configure.host b/newlib/configure.host
index 3c30bf8a7..861b42ae6 100644
--- a/newlib/configure.host
+++ b/newlib/configure.host
@@ -625,7 +625,7 @@ case "${host}" in
default_newlib_io_long_long="yes"
default_newlib_io_c99_formats="yes"
newlib_cflags="${newlib_cflags} -ffunction-sections -fdata-sections "
-newlib_cflags="${newlib_cflags} -DCLOCK_PROVIDED -DMALLOC_PROVIDED -DEXIT_PROVIDED -DSIGNAL_PROVIDED -DGETREENT_PROVIDED -DREENTRANT_SYSCALLS_PROVIDED -DHAVE_NANOSLEEP -DHAVE_BLKSIZE -DHAVE_FCNTL -DHAVE_ASSERT_FUNC"
+newlib_cflags="${newlib_cflags} -DCLOCK_PROVIDED -DMALLOC_PROVIDED -DALIGNED_ALLOC_PROVIDED -DEXIT_PROVIDED -DSIGNAL_PROVIDED -DGETREENT_PROVIDED -DREENTRANT_SYSCALLS_PROVIDED -DHAVE_NANOSLEEP -DHAVE_BLKSIZE -DHAVE_FCNTL -DHAVE_ASSERT_FUNC"
# turn off unsupported items in posix directory
newlib_cflags="${newlib_cflags} -D_NO_GETLOGIN -D_NO_GETPWENT -D_NO_GETUT -D_NO_GETPASS -D_NO_SIGSET -D_NO_WORDEXP -D_NO_POPEN -D_NO_POSIX_SPAWN"
;;
diff --git a/newlib/libc/stdlib/aligned_alloc.c b/newlib/libc/stdlib/aligned_alloc.c
index 06b3883cf..eedba6e88 100644
--- a/newlib/libc/stdlib/aligned_alloc.c
+++ b/newlib/libc/stdlib/aligned_alloc.c
@@ -26,6 +26,8 @@
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
+#ifndef ALIGNED_ALLOC_PROVIDED
+
#include <reent.h>
#include <stdlib.h>
#include <malloc.h>
@@ -35,3 +37,5 @@ aligned_alloc (size_t align, size_t size)
{
return _memalign_r (_REENT, align, size);
}
+
+#endif /* ! defined (ALIGNED_ALLOC_PROVIDED) */
--
2.56.1
More information about the Newlib
mailing list