Skip to content

Commit 94b2916

Browse files
committed
Only add alignment padding when needed
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>
1 parent 99797e1 commit 94b2916

File tree

2 files changed

+48
-12
lines changed

2 files changed

+48
-12
lines changed

portable/MemMang/heap_4.c

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,31 @@ void * pvPortMalloc( size_t xWantedSize )
159159
if( xWantedSize > 0 )
160160
{
161161
/* The wanted size must be increased so it can contain a BlockLink_t
162-
* structure in addition to the requested amount of bytes. Some
163-
* additional increment may also be needed for alignment. */
164-
xAdditionalRequiredSize = xHeapStructSize + portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK );
165-
166-
if( heapADD_WILL_OVERFLOW( xWantedSize, xAdditionalRequiredSize ) == 0 )
162+
* structure in addition to the requested amount of bytes. */
163+
if( heapADD_WILL_OVERFLOW( xWantedSize, xHeapStructSize ) == 0 )
167164
{
168-
xWantedSize += xAdditionalRequiredSize;
165+
xWantedSize += xHeapStructSize;
166+
167+
/* Ensure that blocks are always aligned to the required number
168+
* of bytes. */
169+
if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 )
170+
{
171+
/* Byte alignment required. */
172+
xAdditionalRequiredSize = portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK );
173+
174+
if( heapADD_WILL_OVERFLOW( xWantedSize, xAdditionalRequiredSize ) == 0 )
175+
{
176+
xWantedSize += xAdditionalRequiredSize;
177+
}
178+
else
179+
{
180+
xWantedSize = 0;
181+
}
182+
}
183+
else
184+
{
185+
mtCOVERAGE_TEST_MARKER();
186+
}
169187
}
170188
else
171189
{

portable/MemMang/heap_5.c

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,31 @@ void * pvPortMalloc( size_t xWantedSize )
170170
if( xWantedSize > 0 )
171171
{
172172
/* The wanted size must be increased so it can contain a BlockLink_t
173-
* structure in addition to the requested amount of bytes. Some
174-
* additional increment may also be needed for alignment. */
175-
xAdditionalRequiredSize = xHeapStructSize + portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK );
176-
177-
if( heapADD_WILL_OVERFLOW( xWantedSize, xAdditionalRequiredSize ) == 0 )
173+
* structure in addition to the requested amount of bytes. */
174+
if( heapADD_WILL_OVERFLOW( xWantedSize, xHeapStructSize ) == 0 )
178175
{
179-
xWantedSize += xAdditionalRequiredSize;
176+
xWantedSize += xHeapStructSize;
177+
178+
/* Ensure that blocks are always aligned to the required number
179+
* of bytes. */
180+
if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 )
181+
{
182+
/* Byte alignment required. */
183+
xAdditionalRequiredSize = portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK );
184+
185+
if( heapADD_WILL_OVERFLOW( xWantedSize, xAdditionalRequiredSize ) == 0 )
186+
{
187+
xWantedSize += xAdditionalRequiredSize;
188+
}
189+
else
190+
{
191+
xWantedSize = 0;
192+
}
193+
}
194+
else
195+
{
196+
mtCOVERAGE_TEST_MARKER();
197+
}
180198
}
181199
else
182200
{

0 commit comments

Comments
 (0)