#undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
\r
/* Block sizes must not get too small. */\r
-#define heapMINIMUM_BLOCK_SIZE ( ( size_t ) ( heapSTRUCT_SIZE * 2 ) )\r
+#define heapMINIMUM_BLOCK_SIZE ( ( size_t ) ( xHeapStructSize * 2 ) )\r
\r
/* Assumes 8bit bytes! */\r
#define heapBITS_PER_BYTE ( ( size_t ) 8 )\r
\r
-/* A few bytes might be lost to byte aligning the heap start address. */\r
-#define heapADJUSTED_HEAP_SIZE ( configTOTAL_HEAP_SIZE - portBYTE_ALIGNMENT )\r
-\r
/* Allocate the memory for the heap. */\r
static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];\r
\r
\r
/* The size of the structure placed at the beginning of each allocated memory\r
block must by correctly byte aligned. */\r
-static const uint16_t heapSTRUCT_SIZE = ( ( sizeof ( BlockLink_t ) + ( portBYTE_ALIGNMENT - 1 ) ) & ~portBYTE_ALIGNMENT_MASK );\r
-\r
-/* Ensure the pxEnd pointer will end up on the correct byte alignment. */\r
-static const size_t xTotalHeapSize = ( ( size_t ) heapADJUSTED_HEAP_SIZE ) & ( ( size_t ) ~portBYTE_ALIGNMENT_MASK );\r
+static const size_t xHeapStructSize = ( ( sizeof( BlockLink_t ) + ( portBYTE_ALIGNMENT - 1 ) ) & ~portBYTE_ALIGNMENT_MASK );\r
\r
/* Create a couple of list links to mark the start and end of the list. */\r
static BlockLink_t xStart, *pxEnd = NULL;\r
\r
/* Keeps track of the number of free bytes remaining, but says nothing about\r
fragmentation. */\r
-static size_t xFreeBytesRemaining = ( ( size_t ) heapADJUSTED_HEAP_SIZE ) & ( ( size_t ) ~portBYTE_ALIGNMENT_MASK );\r
-static size_t xMinimumEverFreeBytesRemaining = ( ( size_t ) heapADJUSTED_HEAP_SIZE ) & ( ( size_t ) ~portBYTE_ALIGNMENT_MASK );\r
+static size_t xFreeBytesRemaining = 0U;\r
+static size_t xMinimumEverFreeBytesRemaining = 0U;\r
\r
/* Gets set to the top bit of an size_t type. When this bit in the xBlockSize\r
member of an BlockLink_t structure is set then the block belongs to the\r
structure in addition to the requested amount of bytes. */\r
if( xWantedSize > 0 )\r
{\r
- xWantedSize += heapSTRUCT_SIZE;\r
+ xWantedSize += xHeapStructSize;\r
\r
/* Ensure that blocks are always aligned to the required number\r
of bytes. */\r
{\r
/* Byte alignment required. */\r
xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );\r
+ configASSERT( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) == 0 );\r
}\r
else\r
{\r
{\r
/* Return the memory space pointed to - jumping over the\r
BlockLink_t structure at its start. */\r
- pvReturn = ( void * ) ( ( ( uint8_t * ) pxPreviousBlock->pxNextFreeBlock ) + heapSTRUCT_SIZE );\r
+ pvReturn = ( void * ) ( ( ( uint8_t * ) pxPreviousBlock->pxNextFreeBlock ) + xHeapStructSize );\r
\r
/* This block is being returned for use so must be taken out\r
of the list of free blocks. */\r
cast is used to prevent byte alignment warnings from the\r
compiler. */\r
pxNewBlockLink = ( void * ) ( ( ( uint8_t * ) pxBlock ) + xWantedSize );\r
+ configASSERT( ( ( ( uint32_t ) pxNewBlockLink ) & portBYTE_ALIGNMENT_MASK ) == 0 );\r
\r
/* Calculate the sizes of two blocks split from the\r
single block. */\r
}\r
#endif\r
\r
+ configASSERT( ( ( ( uint32_t ) pvReturn ) & portBYTE_ALIGNMENT_MASK ) == 0 );\r
return pvReturn;\r
}\r
/*-----------------------------------------------------------*/\r
{\r
/* The memory being freed will have an BlockLink_t structure immediately\r
before it. */\r
- puc -= heapSTRUCT_SIZE;\r
+ puc -= xHeapStructSize;\r
\r
/* This casting is to keep the compiler from issuing warnings. */\r
pxLink = ( void * ) puc;\r
static void prvHeapInit( void )\r
{\r
BlockLink_t *pxFirstFreeBlock;\r
-uint8_t *pucHeapEnd, *pucAlignedHeap;\r
+uint8_t *pucAlignedHeap;\r
+uint32_t ulAddress;\r
+size_t xTotalHeapSize = configTOTAL_HEAP_SIZE;\r
\r
/* Ensure the heap starts on a correctly aligned boundary. */\r
- pucAlignedHeap = ( uint8_t * ) ( ( ( portPOINTER_SIZE_TYPE ) &ucHeap[ portBYTE_ALIGNMENT ] ) & ( ( portPOINTER_SIZE_TYPE ) ~portBYTE_ALIGNMENT_MASK ) );\r
+ ulAddress = ( uint32_t ) ucHeap;\r
+\r
+ if( ( ulAddress & portBYTE_ALIGNMENT_MASK ) != 0 )\r
+ {\r
+ ulAddress += ( portBYTE_ALIGNMENT - 1 );\r
+ ulAddress &= ~portBYTE_ALIGNMENT_MASK;\r
+ xTotalHeapSize -= ulAddress - ( uint32_t ) ucHeap;\r
+ }\r
+\r
+ pucAlignedHeap = ( uint8_t * ) ulAddress;\r
\r
/* xStart is used to hold a pointer to the first item in the list of free\r
blocks. The void cast is used to prevent compiler warnings. */\r
\r
/* pxEnd is used to mark the end of the list of free blocks and is inserted\r
at the end of the heap space. */\r
- pucHeapEnd = pucAlignedHeap + xTotalHeapSize;\r
- pucHeapEnd -= heapSTRUCT_SIZE;\r
- pxEnd = ( void * ) pucHeapEnd;\r
- configASSERT( ( ( ( uint32_t ) pxEnd ) & ( ( uint32_t ) portBYTE_ALIGNMENT_MASK ) ) == 0UL );\r
+ ulAddress = ( ( uint32_t ) pucAlignedHeap ) + xTotalHeapSize;\r
+ ulAddress -= xHeapStructSize;\r
+ ulAddress &= ~portBYTE_ALIGNMENT_MASK;\r
+ pxEnd = ( void * ) ulAddress;\r
pxEnd->xBlockSize = 0;\r
pxEnd->pxNextFreeBlock = NULL;\r
\r
/* To start with there is a single free block that is sized to take up the\r
entire heap space, minus the space taken by pxEnd. */\r
pxFirstFreeBlock = ( void * ) pucAlignedHeap;\r
- pxFirstFreeBlock->xBlockSize = xTotalHeapSize - heapSTRUCT_SIZE;\r
+ pxFirstFreeBlock->xBlockSize = ulAddress - ( uint32_t ) pxFirstFreeBlock;\r
pxFirstFreeBlock->pxNextFreeBlock = pxEnd;\r
\r
- /* The heap now contains pxEnd. */\r
- xFreeBytesRemaining -= heapSTRUCT_SIZE;\r
+ /* Only one block exists - and it covers the entire usable heap space. */\r
+ xMinimumEverFreeBytesRemaining = pxFirstFreeBlock->xBlockSize;\r
+ xFreeBytesRemaining = pxFirstFreeBlock->xBlockSize;\r
\r
/* Work out the position of the top bit in a size_t variable. */\r
xBlockAllocatedBit = ( ( size_t ) 1 ) << ( ( sizeof( size_t ) * heapBITS_PER_BYTE ) - 1 );\r
traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );\r
pxTCB->uxPriority = pxTCB->uxBasePriority;\r
\r
- /* Only reset the event list item value if the value is not\r
- being used for anything else. */\r
- if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )\r
- {\r
- listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxTCB->uxPriority ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */\r
- }\r
- else\r
- {\r
-#warning Is it possible to come through here?\r
- mtCOVERAGE_TEST_MARKER();\r
- }\r
+ /* Reset the event list item value. It cannot be in use for\r
+ any other purpose if this task is running, and it must be\r
+ running to give back the mutex. */\r
+ listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxTCB->uxPriority ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */\r
prvAddTaskToReadyList( pxTCB );\r
\r
/* Return true to indicate that a context switch is required.\r
\r
void vTaskIncrementMutexHeldCount( void )\r
{\r
- ( pxCurrentTCB->uxMutexesHeld )++;\r
+ /* If xSemaphoreCreateMutex() is called before any tasks have been created\r
+ then pxCurrentTCB will be NULL. */\r
+ if( pxCurrentTCB != NULL )\r
+ {\r
+ ( pxCurrentTCB->uxMutexesHeld )++;\r
+ }\r
}\r
/*-----------------------------------------------------------*/\r
\r
void vTaskDecrementMutexHeldCount( void )\r
{\r
- configASSERT( pxCurrentTCB->uxMutexesHeld );\r
- ( pxCurrentTCB->uxMutexesHeld )--;\r
+ /* If xSemaphoreCreateMutex() is called before any tasks have been created\r
+ then pxCurrentTCB will be NULL. */\r
+ if( pxCurrentTCB != NULL )\r
+ {\r
+ configASSERT( pxCurrentTCB->uxMutexesHeld );\r
+ ( pxCurrentTCB->uxMutexesHeld )--;\r
+ }\r
}\r
\r
#ifdef FREERTOS_MODULE_TEST\r