[rtems commit] libc: Reimplement posix_memlign()

Sebastian Huber sebh at rtems.org
Thu May 6 13:14:56 UTC 2021


Module:    rtems
Branch:    master
Commit:    5580b93f367e057ef0184231e7619beea4ae3734
Changeset: http://git.rtems.org/rtems/commit/?id=5580b93f367e057ef0184231e7619beea4ae3734

Author:    Sebastian Huber <sebastian.huber at embedded-brains.de>
Date:      Wed May  5 08:46:24 2021 +0200

libc: Reimplement posix_memlign()

Move all error checks into posix_memalign() so that the returned memory
pointer is set to NULL under all error conditions except
memptr == NULL.

Use parameter names of POSIX documentation.

---

 cpukit/libcsupport/src/posix_memalign.c | 29 ++++++++++++++++++++++-------
 1 file changed, 22 insertions(+), 7 deletions(-)

diff --git a/cpukit/libcsupport/src/posix_memalign.c b/cpukit/libcsupport/src/posix_memalign.c
index b2bef3e..316ed73 100644
--- a/cpukit/libcsupport/src/posix_memalign.c
+++ b/cpukit/libcsupport/src/posix_memalign.c
@@ -24,18 +24,33 @@
 #include <errno.h>
 
 int posix_memalign(
-  void   **pointer,
+  void   **memptr,
   size_t   alignment,
   size_t   size
 )
 {
-  if (((alignment - 1) & alignment) != 0 || (alignment < sizeof(void *)))
+  RTEMS_OBFUSCATE_VARIABLE( memptr );
+
+  if ( memptr == NULL ) {
+    return EINVAL;
+  }
+
+  *memptr = NULL;
+
+  if ( alignment < sizeof( void * ) ) {
     return EINVAL;
+  }
+
+  if ( ( ( alignment - 1 ) & alignment ) != 0 ) {
+    return EINVAL;
+  }
+
+  *memptr = rtems_heap_allocate_aligned_with_boundary( size, alignment, 0 );
+
+  if ( *memptr == NULL ) {
+    return ENOMEM;
+  }
 
-  /*
-   *  rtems_memalign does all of the error checking work EXCEPT
-   *  for adding restrictionso on the alignment.
-   */
-  return rtems_memalign( pointer, alignment, size );
+  return 0;
 }
 #endif



More information about the vc mailing list