mirror of
				https://github.com/FreeRTOS/FreeRTOS-Kernel.git
				synced 2025-11-04 11:09:01 +01:00 
			
		
		
		
	Only add alignment padding when needed (#650)
Heap 4 and Heap 5 add some padding to ensure that the allocated blocks are always aligned to portBYTE_ALIGNMENT bytes. The code until now was adding padding always even if the resulting block was already aligned. This commits updates the code to only add padding if the resulting block is not aligned. Signed-off-by: Gaurav Aggarwal <aggarg@amazon.com>
This commit is contained in:
		
							parent
							
								
									97e58da313
								
							
						
					
					
						commit
						68f105375f
					
				@ -159,13 +159,31 @@ void * pvPortMalloc( size_t xWantedSize )
 | 
			
		||||
        if( xWantedSize > 0 )
 | 
			
		||||
        {
 | 
			
		||||
            /* The wanted size must be increased so it can contain a BlockLink_t
 | 
			
		||||
             * structure in addition to the requested amount of bytes. Some
 | 
			
		||||
             * additional increment may also be needed for alignment. */
 | 
			
		||||
            xAdditionalRequiredSize = xHeapStructSize + portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK );
 | 
			
		||||
 | 
			
		||||
            if( heapADD_WILL_OVERFLOW( xWantedSize, xAdditionalRequiredSize ) == 0 )
 | 
			
		||||
             * structure in addition to the requested amount of bytes. */
 | 
			
		||||
            if( heapADD_WILL_OVERFLOW( xWantedSize, xHeapStructSize ) == 0 )
 | 
			
		||||
            {
 | 
			
		||||
                xWantedSize += xAdditionalRequiredSize;
 | 
			
		||||
                xWantedSize += xHeapStructSize;
 | 
			
		||||
 | 
			
		||||
                /* Ensure that blocks are always aligned to the required number
 | 
			
		||||
                 * of bytes. */
 | 
			
		||||
                if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 )
 | 
			
		||||
                {
 | 
			
		||||
                    /* Byte alignment required. */
 | 
			
		||||
                    xAdditionalRequiredSize = portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK );
 | 
			
		||||
 | 
			
		||||
                    if( heapADD_WILL_OVERFLOW( xWantedSize, xAdditionalRequiredSize ) == 0 )
 | 
			
		||||
                    {
 | 
			
		||||
                        xWantedSize += xAdditionalRequiredSize;
 | 
			
		||||
                    }
 | 
			
		||||
                    else
 | 
			
		||||
                    {
 | 
			
		||||
                        xWantedSize = 0;
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
                else
 | 
			
		||||
                {
 | 
			
		||||
                    mtCOVERAGE_TEST_MARKER();
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
 | 
			
		||||
@ -170,13 +170,31 @@ void * pvPortMalloc( size_t xWantedSize )
 | 
			
		||||
        if( xWantedSize > 0 )
 | 
			
		||||
        {
 | 
			
		||||
            /* The wanted size must be increased so it can contain a BlockLink_t
 | 
			
		||||
             * structure in addition to the requested amount of bytes. Some
 | 
			
		||||
             * additional increment may also be needed for alignment. */
 | 
			
		||||
            xAdditionalRequiredSize = xHeapStructSize + portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK );
 | 
			
		||||
 | 
			
		||||
            if( heapADD_WILL_OVERFLOW( xWantedSize, xAdditionalRequiredSize ) == 0 )
 | 
			
		||||
             * structure in addition to the requested amount of bytes. */
 | 
			
		||||
            if( heapADD_WILL_OVERFLOW( xWantedSize, xHeapStructSize ) == 0 )
 | 
			
		||||
            {
 | 
			
		||||
                xWantedSize += xAdditionalRequiredSize;
 | 
			
		||||
                xWantedSize += xHeapStructSize;
 | 
			
		||||
 | 
			
		||||
                /* Ensure that blocks are always aligned to the required number
 | 
			
		||||
                 * of bytes. */
 | 
			
		||||
                if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 )
 | 
			
		||||
                {
 | 
			
		||||
                    /* Byte alignment required. */
 | 
			
		||||
                    xAdditionalRequiredSize = portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK );
 | 
			
		||||
 | 
			
		||||
                    if( heapADD_WILL_OVERFLOW( xWantedSize, xAdditionalRequiredSize ) == 0 )
 | 
			
		||||
                    {
 | 
			
		||||
                        xWantedSize += xAdditionalRequiredSize;
 | 
			
		||||
                    }
 | 
			
		||||
                    else
 | 
			
		||||
                    {
 | 
			
		||||
                        xWantedSize = 0;
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
                else
 | 
			
		||||
                {
 | 
			
		||||
                    mtCOVERAGE_TEST_MARKER();
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user