/* Define the linked list structure. This is used to link free blocks in order * of their size. */ typedefstructA_BLOCK_LINK { structA_BLOCK_LINK * pxNextFreeBlock;/*<< The next free block in the list. */ size_t xBlockSize; /*<< The size of the free block. */ } BlockLink_t;
/* MSB of the xBlockSize member of an BlockLink_t structure is used to track * the allocation status of a block. When MSB of the xBlockSize member of * an BlockLink_t structure is set then the block belongs to the application. * When the bit is free the block is still part of the free heap space. */ #define heapBLOCK_ALLOCATED_BITMASK (((size_t)1) << ((sizeof(size_t) * heapBITS_PER_BYTE) - 1)) #define heapBLOCK_SIZE_IS_VALID( xBlockSize ) (((xBlockSize) & heapBLOCK_ALLOCATED_BITMASK) == 0) #define heapBLOCK_IS_ALLOCATED( pxBlock ) (((pxBlock->xBlockSize) & heapBLOCK_ALLOCATED_BITMASK) != 0) #define heapALLOCATE_BLOCK( pxBlock ) ((pxBlock->xBlockSize) |= heapBLOCK_ALLOCATED_BITMASK) #define heapFREE_BLOCK( pxBlock ) ((pxBlock->xBlockSize) &= ~heapBLOCK_ALLOCATED_BITMASK)
/* Blocks are stored in byte order - traverse the list from the start * (smallest) block until one of adequate size is found. */ pxPreviousBlock = &xStart; pxBlock = xStart.pxNextFreeBlock;
/* * Inserts a block of memory that is being freed into the correct position in * the list of free memory blocks. The block being freed will be merged with * the block in front it and/or the block behind it if the memory blocks are * adjacent to each other. */ staticvoidprvInsertBlockIntoFreeList( BlockLink_t * pxBlockToInsert ) PRIVILEGED_FUNCTION;
/* * Setting configENABLE_HEAP_PROTECTOR to 1 enables heap block pointers * protection using an application supplied canary value to catch heap * corruption should a heap buffer overflow occur. */ #if (configENABLE_HEAP_PROTECTOR == 1)
/** * @brief Application provided function to get a random value to be used as canary. * * @param pxHeapCanary [out] Output parameter to return the canary value. */ externvoidvApplicationGetRandomHeapCanary(portPOINTER_SIZE_TYPE * pxHeapCanary);
/* Canary value for protecting internal heap pointers. */ PRIVILEGED_DATA static portPOINTER_SIZE_TYPE xHeapCanary;
/* Macro to load/store BlockLink_t pointers to memory. By XORing the * pointers with a random canary value, heap overflows will result * in randomly unpredictable pointer values which will be caught by * heapVALIDATE_BLOCK_POINTER assert. */ #define heapPROTECT_BLOCK_POINTER(pxBlock) ((BlockLink_t *) (((portPOINTER_SIZE_TYPE)(pxBlock)) ^ xHeapCanary)) #else #define heapPROTECT_BLOCK_POINTER(pxBlock) (pxBlock) #endif/* configENABLE_HEAP_PROTECTOR */
/* Assert that a heap block pointer is within the heap bounds. */ #define heapVALIDATE_BLOCK_POINTER(pxBlock) configASSERT(((uint8_t *) (pxBlock) >= &(ucHeap[0])) && ((uint8_t *)(pxBlock) <= &(ucHeap[configTOTAL_HEAP_SIZE - 1])))