From 3c56f1186ad66abcf5ea4cc68dbb8ba026374b82 Mon Sep 17 00:00:00 2001 From: Cobus van Eeden <35851496+cobusve@users.noreply.github.com> Date: Sat, 5 Dec 2020 16:14:04 -0800 Subject: [PATCH] adjustments for consistency --- portable/MemMang/heap_2.c | 32 +++++++++++++------------ portable/MemMang/heap_4.c | 50 ++++++++++++++++----------------------- 2 files changed, 38 insertions(+), 44 deletions(-) diff --git a/portable/MemMang/heap_2.c b/portable/MemMang/heap_2.c index 1eed436ce..dbd8e2f54 100644 --- a/portable/MemMang/heap_2.c +++ b/portable/MemMang/heap_2.c @@ -134,26 +134,28 @@ void * pvPortMalloc( size_t xWantedSize ) /* The wanted size must be increased so it can contain a BlockLink_t * structure in addition to the requested amount of bytes. */ - if( xWantedSize > 0 ) + if( ( xWantedSize > 0 ) && + ( ( xWantedSize + xHeapStructSize ) > xWantedSize ) ) /* Overflow check */ { - /* Ensure xWantedSize will never wrap after adjustment, even if we need - * an alignment adjustment */ - if( ( xWantedSize > 0 ) && ( ( xWantedSize + xHeapStructSize ) > xWantedSize ) ) - { - xWantedSize += heapSTRUCT_SIZE; + xWantedSize += heapSTRUCT_SIZE; - /* Ensure that blocks are always aligned to the required number of bytes. */ - if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0 ) - { - /* Byte alignment required. */ - xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) ); - } - } else { - /* If the requested size is too large to handle we force an error */ - xWantedSize = 0; + /* Byte alignment required. (check for overflow again) */ + if( ( xWantedSize + ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) ) ) + > xWantedSize ) + { + xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) ); + configASSERT( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) == 0 ); } + else + { + xWantedSize = 0; + } + } else { + /* If the requested size is too large to handle we force an error */ + xWantedSize = 0; } + if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) ) { /* Blocks are stored in byte order - traverse the list from the start diff --git a/portable/MemMang/heap_4.c b/portable/MemMang/heap_4.c index a86052c20..7a333f207 100644 --- a/portable/MemMang/heap_4.c +++ b/portable/MemMang/heap_4.c @@ -136,44 +136,36 @@ void * pvPortMalloc( size_t xWantedSize ) * kernel, so it must be free. */ if( ( xWantedSize & xBlockAllocatedBit ) == 0 ) { - /* The wanted size is increased so it can contain a BlockLink_t + /* The wanted size must be increased so it can contain a BlockLink_t * structure in addition to the requested amount of bytes. */ - if( xWantedSize > 0 ) + if( ( xWantedSize > 0 ) && + ( ( xWantedSize + xHeapStructSize ) > xWantedSize ) ) /* Overflow check */ { - /* Check for overflow */ - if ( ( xWantedSize + xHeapStructSize ) > xWantedSize ) - { - xWantedSize += xHeapStructSize; + xWantedSize += xHeapStructSize; - /* Ensure that blocks are always aligned to the required number - * of bytes. */ - if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 ) + /* Ensure that blocks are always aligned */ + if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 ) + { + /* Byte alignment required. (check for overflow again) */ + if( ( xWantedSize + ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) ) ) + > xWantedSize ) { - /* Byte alignment required. (check for overflow again) */ - if( ( xWantedSize + ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) ) ) - > xWantedSize ) - { - xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) ); - configASSERT( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) == 0 ); - } - else - { - xWantedSize = 0; - } + xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) ); + configASSERT( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) == 0 ); } else { - mtCOVERAGE_TEST_MARKER(); - } - } - else - { - xWantedSize = 0; + xWantedSize = 0; + } } - } - else + else + { + mtCOVERAGE_TEST_MARKER(); + } + } + else { - mtCOVERAGE_TEST_MARKER(); + xWantedSize = 0; } if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) )