117117#define heapALLOCATE_BLOCK ( pxBlock ) ( ( pxBlock->xBlockSize ) |= heapBLOCK_ALLOCATED_BITMASK )
118118#define heapFREE_BLOCK ( pxBlock ) ( ( pxBlock->xBlockSize ) &= ~heapBLOCK_ALLOCATED_BITMASK )
119119
120- /*-----------------------------------------------------------*/
121-
122- /* Define the linked list structure. This is used to link free blocks in order
123- * of their memory address. */
124- typedef struct A_BLOCK_LINK
125- {
126- struct A_BLOCK_LINK * pxNextFreeBlock ; /**< The next free block in the list. */
127- size_t xBlockSize ; /**< The size of the free block. */
128- } BlockLink_t ;
129-
130120/* Setting configENABLE_HEAP_PROTECTOR to 1 enables heap block pointers
131121 * protection using an application supplied canary value to catch heap
132122 * corruption should a heap buffer overflow occur.
133123 */
134124#if ( configENABLE_HEAP_PROTECTOR == 1 )
135125
136- /**
137- * @brief Application provided function to get a random value to be used as canary.
138- *
139- * @param pxHeapCanary [out] Output parameter to return the canary value.
140- */
141- extern void vApplicationGetRandomHeapCanary ( portPOINTER_SIZE_TYPE * pxHeapCanary );
142-
143- /* Canary value for protecting internal heap pointers. */
144- PRIVILEGED_DATA static portPOINTER_SIZE_TYPE xHeapCanary ;
145-
146126/* Macro to load/store BlockLink_t pointers to memory. By XORing the
147127 * pointers with a random canary value, heap overflows will result
148128 * in randomly unpredictable pointer values which will be caught by
@@ -156,10 +136,6 @@ typedef struct A_BLOCK_LINK
156136 ( ( uint8_t * ) ( pxBlock ) >= pucHeapLowAddress ) && \
157137 ( ( uint8_t * ) ( pxBlock ) < pucHeapHighAddress ) )
158138
159- /* Highest and lowest heap addresses used for heap block bounds checking. */
160- PRIVILEGED_DATA static uint8_t * pucHeapHighAddress = NULL ;
161- PRIVILEGED_DATA static uint8_t * pucHeapLowAddress = NULL ;
162-
163139#else /* if ( configENABLE_HEAP_PROTECTOR == 1 ) */
164140
165141 #define heapPROTECT_BLOCK_POINTER ( pxBlock ) ( pxBlock )
@@ -168,6 +144,15 @@ typedef struct A_BLOCK_LINK
168144
169145#endif /* configENABLE_HEAP_PROTECTOR */
170146
147+ /*-----------------------------------------------------------*/
148+
149+ /* Define the linked list structure. This is used to link free blocks in order
150+ * of their memory address. */
151+ typedef struct A_BLOCK_LINK
152+ {
153+ struct A_BLOCK_LINK * pxNextFreeBlock ; /**< The next free block in the list. */
154+ size_t xBlockSize ; /**< The size of the free block. */
155+ } BlockLink_t ;
171156
172157/*-----------------------------------------------------------*/
173158
@@ -179,6 +164,17 @@ typedef struct A_BLOCK_LINK
179164 */
180165static void prvInsertBlockIntoFreeList ( BlockLink_t * pxBlockToInsert ) PRIVILEGED_FUNCTION ;
181166void vPortDefineHeapRegions ( const HeapRegion_t * const pxHeapRegions ) PRIVILEGED_FUNCTION ;
167+
168+ #if ( configENABLE_HEAP_PROTECTOR == 1 )
169+
170+ /**
171+ * @brief Application provided function to get a random value to be used as canary.
172+ *
173+ * @param pxHeapCanary [out] Output parameter to return the canary value.
174+ */
175+ extern void vApplicationGetRandomHeapCanary ( portPOINTER_SIZE_TYPE * pxHeapCanary );
176+ #endif /* configENABLE_HEAP_PROTECTOR */
177+
182178/*-----------------------------------------------------------*/
183179
184180/* The size of the structure placed at the beginning of each allocated memory
@@ -196,6 +192,17 @@ PRIVILEGED_DATA static size_t xMinimumEverFreeBytesRemaining = 0U;
196192PRIVILEGED_DATA static size_t xNumberOfSuccessfulAllocations = 0 ;
197193PRIVILEGED_DATA static size_t xNumberOfSuccessfulFrees = 0 ;
198194
195+ #if ( configENABLE_HEAP_PROTECTOR == 1 )
196+
197+ /* Canary value for protecting internal heap pointers. */
198+ PRIVILEGED_DATA static portPOINTER_SIZE_TYPE xHeapCanary ;
199+
200+ /* Highest and lowest heap addresses used for heap block bounds checking. */
201+ PRIVILEGED_DATA static uint8_t * pucHeapHighAddress = NULL ;
202+ PRIVILEGED_DATA static uint8_t * pucHeapLowAddress = NULL ;
203+
204+ #endif /* configENABLE_HEAP_PROTECTOR */
205+
199206/*-----------------------------------------------------------*/
200207
201208void * pvPortMalloc ( size_t xWantedSize )
0 commit comments