source: rtems/cpukit/libcsupport/src/posix_memalign.c @ 2c5199b

Last change on this file since 2c5199b was 2c5199b, checked in by Sebastian Huber <sebastian.huber@…>, on 05/05/21 at 06:49:52

Return NULL for zero size allocations

In POSIX, zero size memory allocations are implementation-defined
behaviour. The implementation has two options:

https://pubs.opengroup.org/onlinepubs/9699919799/functions/malloc.html

https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_memalign.html

Linux and FreeBSD return a unique pointer for zero size memory
allocations. Return NULL on RTEMS to more likely catch the use of a
zero size memory area by erroneous applications.

Update #4390.

  • Property mode set to 100644
File size: 946 bytes
Line 
1/**
2 * @file
3 *
4 * @ingroup libcsupport
5 */
6
7/*
8 *  COPYRIGHT (c) 1989-2008.
9 *  On-Line Applications Research Corporation (OAR).
10 *
11 *  The license and distribution terms for this file may be
12 *  found in the file LICENSE in this distribution or at
13 *  http://www.rtems.org/license/LICENSE.
14 */
15
16#ifdef HAVE_CONFIG_H
17#include "config.h"
18#endif
19
20#ifdef RTEMS_NEWLIB
21#include "malloc_p.h"
22
23#include <stdlib.h>
24#include <errno.h>
25
26int posix_memalign(
27  void   **memptr,
28  size_t   alignment,
29  size_t   size
30)
31{
32  RTEMS_OBFUSCATE_VARIABLE( memptr );
33
34  if ( memptr == NULL ) {
35    return EINVAL;
36  }
37
38  *memptr = NULL;
39
40  if ( size == 0 ) {
41    return 0;
42  }
43
44  if ( alignment < sizeof( void * ) ) {
45    return EINVAL;
46  }
47
48  if ( ( ( alignment - 1 ) & alignment ) != 0 ) {
49    return EINVAL;
50  }
51
52  *memptr = rtems_heap_allocate_aligned_with_boundary( size, alignment, 0 );
53
54  if ( *memptr == NULL ) {
55    return ENOMEM;
56  }
57
58  return 0;
59}
60#endif
Note: See TracBrowser for help on using the repository browser.