如果内存管理标志位使能,那么系统初始化内存管理功能
- /*
- *********************************************************************************************************
- * INITIALIZE MEMORY PARTITION MANAGER
- *
- * Description : This function is called by uC/OS-II to initialize the memory partition manager. Your
- * application MUST NOT call this function.
- *
- * Arguments : none
- *
- * Returns : none
- *
- * Note(s) : This function is INTERNAL to uC/OS-II and your application should not call it.
- *********************************************************************************************************
- */
- void OS_MemInit (void)
- {
- #if OS_MAX_MEM_PART == 1
- OS_MemClr((INT8U *)&OSMemTbl[0], sizeof(OSMemTbl)); /* Clear the memory partition table */
- OSMemFreeList = (OS_MEM *)&OSMemTbl[0]; /* Point to beginning of free list */
- #if OS_MEM_NAME_SIZE > 1
- OSMemFreeList->OSMemName[0] = '?'; /* Unknown name */
- OSMemFreeList->OSMemName[1] = OS_ASCII_NUL;
- #endif
- #endif
- #if OS_MAX_MEM_PART >= 2
- OS_MEM *pmem;
- INT16U i;
- OS_MemClr((INT8U *)&OSMemTbl[0], sizeof(OSMemTbl)); /* Clear the memory partition table */
- pmem = &OSMemTbl[0]; /* Point to memory control block (MCB) */
- for (i = 0; i < (OS_MAX_MEM_PART - 1); i++) { /* Init. list of free memory partitions */
- pmem->OSMemFreeList = (void *)&OSMemTbl[i+1]; /* Chain list of free partitions */
- #if OS_MEM_NAME_SIZE > 1
- pmem->OSMemName[0] = '?'; /* Unknown name */
- pmem->OSMemName[1] = OS_ASCII_NUL;
- #endif
- pmem++;
- }
- pmem->OSMemFreeList = (void *)0; /* Initialize last node */
- #if OS_MEM_NAME_SIZE > 1
- pmem->OSMemName[0] = '?'; /* Unknown name */
- pmem->OSMemName[1] = OS_ASCII_NUL;
- #endif
- OSMemFreeList = &OSMemTbl[0]; /* Point to beginning of free list */
- #endif
- }
核心数据结构为OSMemTbl[0]内存分区表
MCB内存控制数据块的结构如下所示
- /*
- *********************************************************************************************************
- * MEMORY PARTITION DATA STRUCTURES
- *********************************************************************************************************
- */
- #if (OS_MEM_EN > 0) && (OS_MAX_MEM_PART > 0)
- typedef struct os_mem { /* MEMORY CONTROL BLOCK */
- void *OSMemAddr; /* Pointer to beginning of memory partition */
- void *OSMemFreeList; /* Pointer to list of free memory blocks */
- INT32U OSMemBlkSize; /* Size (in bytes) of each block of memory */
- INT32U OSMemNBlks; /* Total number of blocks in this partition */
- INT32U OSMemNFree; /* Number of memory blocks remaining in this partition */
- #if OS_MEM_NAME_SIZE > 1
- INT8U OSMemName[OS_MEM_NAME_SIZE]; /* Memory partition name */
- #endif
- } OS_MEM;
- typedef struct os_mem_data {
- void *OSAddr; /* Pointer to the beginning address of the memory partition */
- void *OSFreeList; /* Pointer to the beginning of the free list of memory blocks */
- INT32U OSBlkSize; /* Size (in bytes) of each memory block */
- INT32U OSNBlks; /* Total number of blocks in the partition */
- INT32U OSNFree; /* Number of memory blocks free */
- INT32U OSNUsed; /* Number of memory blocks used */
- } OS_MEM_DATA;
- #endif
OSMemAddr指向内存分区头地址的void型指针
OSMemFreeList指向空闲内存分区列表的void型指针
OSMemBlkSize内存分区块以字节为单位的大小
OSMemNBlks内存分区中块的个数
OSMemNFree内存分区中未使用的块的个数
初始化函数中,当只有一个内存分区时,我们把OSMemTbl[0]赋值给OSMemFreeList就完成了
当有多个内存分区时,初始化OSMemTbl[0]链表,将未使用的MCB单元的指向内存分区地址的指针OSMemFreeList赋值数组的下一个元素,其本质与以前的链表初始化相同,只是只用了一个指针
最后一个节点的指针指向NULL指针
然后将OSMemTbl[0]数组返回给操作系统的OSMemFreeList值
阅读(2282) | 评论(0) | 转发(0) |