From d692c62dfbf834c16ef7f171ea3161b3f3fac06b Mon Sep 17 00:00:00 2001 From: Sebastian Huber Date: Mon, 3 May 2021 09:17:33 +0200 Subject: Make zero size allocation result consistent The zero size allocations had no consistent behaviour in RTEMS. For example, malloc( 0 ) returned NULL and posix_memalign( &p, align, 0 ) returned in p a unique pointer (or NULL if no memory is available). 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. Use this approach for RTEMS as well throughout the memory allocation directives Close #4390. --- cpukit/libcsupport/src/malloc.c | 6 ------ cpukit/libcsupport/src/malloc_deferred.c | 4 ---- cpukit/libcsupport/src/realloc.c | 8 ++++---- 3 files changed, 4 insertions(+), 14 deletions(-) (limited to 'cpukit/libcsupport/src') diff --git a/cpukit/libcsupport/src/malloc.c b/cpukit/libcsupport/src/malloc.c index e61128bf8c..795254fbab 100644 --- a/cpukit/libcsupport/src/malloc.c +++ b/cpukit/libcsupport/src/malloc.c @@ -30,12 +30,6 @@ void *malloc( { void *return_this; - /* - * Validate the parameters - */ - if ( !size ) - return (void *) 0; - return_this = rtems_heap_allocate_aligned_with_boundary( size, 0, 0 ); if ( !return_this ) { errno = ENOMEM; diff --git a/cpukit/libcsupport/src/malloc_deferred.c b/cpukit/libcsupport/src/malloc_deferred.c index b319d1213e..aab76406c7 100644 --- a/cpukit/libcsupport/src/malloc_deferred.c +++ b/cpukit/libcsupport/src/malloc_deferred.c @@ -106,10 +106,6 @@ void *rtems_heap_allocate_aligned_with_boundary( void *rtems_malloc( size_t size ) { - if ( size == 0 ) { - return NULL; - } - return rtems_heap_allocate_aligned_with_boundary( size, 0, 0 ); } #endif diff --git a/cpukit/libcsupport/src/realloc.c b/cpukit/libcsupport/src/realloc.c index 1d9ae45c5d..e28e3fbc33 100644 --- a/cpukit/libcsupport/src/realloc.c +++ b/cpukit/libcsupport/src/realloc.c @@ -53,15 +53,15 @@ void *realloc( void *ptr, size_t size ) uintptr_t old_size; uintptr_t avail_size; + if ( ptr == NULL ) { + return malloc( size ); + } + if ( size == 0 ) { free( ptr ); return NULL; } - if ( ptr == NULL ) { - return malloc( size ); - } - heap = RTEMS_Malloc_Heap; switch ( _Malloc_System_state() ) { -- cgit v1.2.3