freeRTOS源码解析1--堆栈管理2
下图为当前的堆使用情况:

各全局变量的值:
xFreeBytesRemaining = 0x00011988;
xMinimumEverFreeBytesRemaining = 0x00011988;
xNumberOfSuccessfulAllocations = 9;
xNumberOfSuccessfulFrees = 0;
接下来解析释放红圈圈出来的内存的情况,首先是vPortFree的源码:
1 void vPortFree( void * pv ) 2 { 3 uint8_t * puc = ( uint8_t * ) pv; 4 BlockLink_t * pxLink; 5 6 /* 此例pv = 0x20001180 */ 7 if( pv != NULL ) 8 { 9 /* The memory being freed will have an BlockLink_t structure immediately 10 * before it. */ 11 /* 被释放的内存前有一个BlockLink_t结构体 */ 12 // puc = 0x20001180 - 8 = 0x20001178 13 puc -= xHeapStructSize; 14 15 /* This casting is to keep the compiler from issuing warnings. */ 16 /* 强转成void是用于防止编译器报告警。 */ 17 pxLink = ( void * ) puc; 18 19 /* Check the block is actually allocated. */ 20 /* 检查该块的确是被申请来的 */ 21 configASSERT( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 ); 22 configASSERT( pxLink->pxNextFreeBlock == NULL ); 23 24 // pxLink->xBlockSize = 0x80000200 25 if( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 ) 26 { 27 if( pxLink->pxNextFreeBlock == NULL ) 28 { 29 /* The block is being returned to the heap - it is no longer 30 * allocated. */ 31 /* 块被返回到堆-它被释放了。 */ 32 // pxLink->xBlockSize = 0x00000200 33 pxLink->xBlockSize &= ~xBlockAllocatedBit; 34 35 vTaskSuspendAll(); 36 { 37 /* Add this block to the list of free blocks. */ 38 // 将此块加入到空闲块链表中 39 // xFreeBytesRemaining = 0x00011988 + 0x00000200 = 0x11B88 40 xFreeBytesRemaining += pxLink->xBlockSize; 41 traceFREE( pv, pxLink->xBlockSize ); 42 // 上次已解析过一次了,这里直接看最终结果 43 prvInsertBlockIntoFreeList( ( ( BlockLink_t * ) pxLink ) ); 44 xNumberOfSuccessfulFrees++; 45 } 46 ( void ) xTaskResumeAll(); 47 } 48 else 49 { 50 mtCOVERAGE_TEST_MARKER(); 51 } 52 } 53 else 54 { 55 mtCOVERAGE_TEST_MARKER(); 56 } 57 } 58 }
下图是释放后的堆使用情况:

接下来是在当前状态下继续申请100字节后的堆使用情况:

xFreeBytesRemaining = 0x00011B18;
xMinimumEverFreeBytesRemaining = 0x00011988;
xNumberOfSuccessfulAllocations = 10;
xNumberOfSuccessfulFrees = 1;
至此基本上heap_4.c的源码已经解析清楚了,下次开始解析freeRTOS的链表。

浙公网安备 33010602011771号