Message ID SN6PR05MB4703CE6447F08CFA3C8A4EBED64E9@SN6PR05MB4703.namprd05.prod.outlook.com
State New
Headers show
Series
  • Untitled series #36690
Related show

Commit Message

Hongtao Liu via Gcc-patches April 14, 2021, 7:41 a.m.
From b1774ab1c8aad82b7a5d975ef90c6d3f633780ee Mon Sep 17 00:00:00 2001
From: expnkx <unlvsur@live.com>

Date: Wed, 14 Apr 2021 03:14:28 -0400
Subject: [PATCH] Fix intrinsics mm_malloc.h in freestanding [PR100057]

C does not have stdlib.h and C++ cstdint in freestanding does not malloc either. This leads
to fail of compilation even with -ffrestanding flag

Only gmm_malloc checks errno, everything else does not. So we remove the errno
in gmm_malloc too. There is no reason freestanding should behave differently with hosted.

gcc/ChangeLog
   PR/100057:
      gcc/config/i386/gmm_malloc.h: use __builtin_malloc and __builtin_free instead
      gcc/config/i386/pmm_malloc.h: use __builtin_malloc and __builtin_free instead
      gcc/config/rs6000/mm_malloc.h: use __builtin_malloc and __builtin_free instead

---
gcc/config/i386/gmm_malloc.h  | 13 ++++---------
gcc/config/i386/pmm_malloc.h  | 13 +++++++++----
gcc/config/rs6000/mm_malloc.h | 13 +++++++++----
3 files changed, 22 insertions(+), 17 deletions(-)

-    return malloc (size);
+    return __builtin_malloc (size);
   if (alignment < vec_align)
     alignment = vec_align;
   if (posix_memalign (&ptr, alignment, size) == 0)
@@ -56,7 +61,7 @@ _mm_malloc (size_t size, size_t alignment)
static __inline void
_mm_free (void * ptr)
{
-  free (ptr);
+  __builtin_free (ptr);
}
 #endif /* _MM_MALLOC_H_INCLUDED */
--
2.25.1


Sent from Mail<https://go.microsoft.com/fwlink/?LinkId=550986> for Windows 10

Patch

diff --git a/gcc/config/i386/gmm_malloc.h b/gcc/config/i386/gmm_malloc.h
index 70b38ab557b..276a5f50023 100644
--- a/gcc/config/i386/gmm_malloc.h
+++ b/gcc/config/i386/gmm_malloc.h
@@ -24,10 +24,7 @@ 
#ifndef _MM_MALLOC_H_INCLUDED
#define _MM_MALLOC_H_INCLUDED
-#include <stdlib.h>
-#if __STDC_HOSTED__
-#include <errno.h>
-#endif
+#include <stddef.h>
 static __inline__ void *
 _mm_malloc (size_t __size, size_t __align)
@@ -38,9 +35,6 @@  _mm_malloc (size_t __size, size_t __align)
   /* Error if align is not a power of two.  */
   if (__align & (__align - 1))
     {
-#if __STDC_HOSTED__
-      errno = EINVAL;
-#endif
       return ((void *) 0);
     }
@@ -54,7 +48,7 @@  _mm_malloc (size_t __size, size_t __align)
     if (__align < 2 * sizeof (void *))
       __align = 2 * sizeof (void *);
-  __malloc_ptr = malloc (__size + __align);
+  __malloc_ptr = __builtin_malloc (__size + __align);
   if (!__malloc_ptr)
     return ((void *) 0);
@@ -72,7 +66,8 @@  static __inline__ void
_mm_free (void *__aligned_ptr)
{
   if (__aligned_ptr)
-    free (((void **) __aligned_ptr)[-1]);
+    __builtin_free (((void **) __aligned_ptr)[-1]);
}
+
#endif /* _MM_MALLOC_H_INCLUDED */
diff --git a/gcc/config/i386/pmm_malloc.h b/gcc/config/i386/pmm_malloc.h
index 1b0bfe37852..3b97107ccfc 100644
--- a/gcc/config/i386/pmm_malloc.h
+++ b/gcc/config/i386/pmm_malloc.h
@@ -24,14 +24,19 @@ 
#ifndef _MM_MALLOC_H_INCLUDED
#define _MM_MALLOC_H_INCLUDED
-#include <stdlib.h>
+#include <stddef.h>
 /* We can't depend on <stdlib.h> since the prototype of posix_memalign
    may not be visible.  */
#ifndef __cplusplus
extern int posix_memalign (void **, size_t, size_t);
#else
-extern "C" int posix_memalign (void **, size_t, size_t) throw ();
+extern "C" int posix_memalign (void **, size_t, size_t)
+#if __cplusplus >= 201103L
+noexcept;
+#else
+throw ();
+#endif
#endif
 static __inline void *
@@ -39,7 +44,7 @@  _mm_malloc (size_t __size, size_t __alignment)
{
   void *__ptr;
   if (__alignment == 1)
-    return malloc (__size);
+    return __builtin_malloc (__size);
   if (__alignment == 2 || (sizeof (void *) == 8 && __alignment == 4))
     __alignment = sizeof (void *);
   if (posix_memalign (&__ptr, __alignment, __size) == 0)
@@ -51,7 +56,7 @@  _mm_malloc (size_t __size, size_t __alignment)
static __inline void
_mm_free (void *__ptr)
{
-  free (__ptr);
+  __builtin_free (__ptr);
}
 #endif /* _MM_MALLOC_H_INCLUDED */
diff --git a/gcc/config/rs6000/mm_malloc.h b/gcc/config/rs6000/mm_malloc.h
index c04348068e0..82aaab411da 100644
--- a/gcc/config/rs6000/mm_malloc.h
+++ b/gcc/config/rs6000/mm_malloc.h
@@ -24,14 +24,19 @@ 
#ifndef _MM_MALLOC_H_INCLUDED
#define _MM_MALLOC_H_INCLUDED
-#include <stdlib.h>
+#include <stddef.h>
 /* We can't depend on <stdlib.h> since the prototype of posix_memalign
    may not be visible.  */
#ifndef __cplusplus
extern int posix_memalign (void **, size_t, size_t);
#else
-extern "C" int posix_memalign (void **, size_t, size_t) throw ();
+extern "C" int posix_memalign (void **, size_t, size_t)
+#if __cplusplus >= 201103L
+noexcept;
+#else
+throw ();
+#endif
#endif
 static __inline void *
@@ -44,7 +49,7 @@  _mm_malloc (size_t size, size_t alignment)
   void *ptr;
   if (alignment == malloc_align && alignment == vec_align)