Chinaunix首页 | 论坛 | 博客
  • 博客访问: 431554
  • 博文数量: 103
  • 博客积分: 1455
  • 博客等级: 上尉
  • 技术积分: 1380
  • 用 户 组: 普通用户
  • 注册时间: 2012-09-15 22:17
文章分类

全部博文(103)

文章存档

2013年(4)

2012年(99)

我的朋友

分类: LINUX

2012-11-07 11:03:45

如果内存管理标志位使能,那么系统初始化内存管理功能

点击(此处)折叠或打开

  1. /*
  2. *********************************************************************************************************
  3. * INITIALIZE MEMORY PARTITION MANAGER
  4. *
  5. * Description : This function is called by uC/OS-II to initialize the memory partition manager. Your
  6. * application MUST NOT call this function.
  7. *
  8. * Arguments : none
  9. *
  10. * Returns : none
  11. *
  12. * Note(s) : This function is INTERNAL to uC/OS-II and your application should not call it.
  13. *********************************************************************************************************
  14. */

  15. void OS_MemInit (void)
  16. {
  17. #if OS_MAX_MEM_PART == 1
  18.     OS_MemClr((INT8U *)&OSMemTbl[0], sizeof(OSMemTbl)); /* Clear the memory partition table */
  19.     OSMemFreeList = (OS_MEM *)&OSMemTbl[0]; /* Point to beginning of free list */
  20. #if OS_MEM_NAME_SIZE > 1
  21.     OSMemFreeList->OSMemName[0] = '?'; /* Unknown name */
  22.     OSMemFreeList->OSMemName[1] = OS_ASCII_NUL;
  23. #endif
  24. #endif

  25. #if OS_MAX_MEM_PART >= 2
  26.     OS_MEM *pmem;
  27.     INT16U i;


  28.     OS_MemClr((INT8U *)&OSMemTbl[0], sizeof(OSMemTbl)); /* Clear the memory partition table */
  29.     pmem = &OSMemTbl[0]; /* Point to memory control block (MCB) */
  30.     for (i = 0; i < (OS_MAX_MEM_PART - 1); i++) { /* Init. list of free memory partitions */
  31.         pmem->OSMemFreeList = (void *)&OSMemTbl[i+1]; /* Chain list of free partitions */
  32. #if OS_MEM_NAME_SIZE > 1
  33.         pmem->OSMemName[0] = '?'; /* Unknown name */
  34.         pmem->OSMemName[1] = OS_ASCII_NUL;
  35. #endif
  36.         pmem++;
  37.     }
  38.     pmem->OSMemFreeList = (void *)0; /* Initialize last node */
  39. #if OS_MEM_NAME_SIZE > 1
  40.     pmem->OSMemName[0] = '?'; /* Unknown name */
  41.     pmem->OSMemName[1] = OS_ASCII_NUL;
  42. #endif

  43.     OSMemFreeList = &OSMemTbl[0]; /* Point to beginning of free list */
  44. #endif
  45. }

核心数据结构为OSMemTbl[0]内存分区表
MCB内存控制数据块的结构如下所示

点击(此处)折叠或打开

  1. /*
  2. *********************************************************************************************************
  3. * MEMORY PARTITION DATA STRUCTURES
  4. *********************************************************************************************************
  5. */

  6. #if (OS_MEM_EN > 0) && (OS_MAX_MEM_PART > 0)
  7. typedef struct os_mem { /* MEMORY CONTROL BLOCK */
  8.     void *OSMemAddr; /* Pointer to beginning of memory partition */
  9.     void *OSMemFreeList; /* Pointer to list of free memory blocks */
  10.     INT32U OSMemBlkSize; /* Size (in bytes) of each block of memory */
  11.     INT32U OSMemNBlks; /* Total number of blocks in this partition */
  12.     INT32U OSMemNFree; /* Number of memory blocks remaining in this partition */
  13. #if OS_MEM_NAME_SIZE > 1
  14.     INT8U OSMemName[OS_MEM_NAME_SIZE]; /* Memory partition name */
  15. #endif
  16. } OS_MEM;


  17. typedef struct os_mem_data {
  18.     void *OSAddr; /* Pointer to the beginning address of the memory partition */
  19.     void *OSFreeList; /* Pointer to the beginning of the free list of memory blocks */
  20.     INT32U OSBlkSize; /* Size (in bytes) of each memory block */
  21.     INT32U OSNBlks; /* Total number of blocks in the partition */
  22.     INT32U OSNFree; /* Number of memory blocks free */
  23.     INT32U OSNUsed; /* Number of memory blocks used */
  24. } OS_MEM_DATA;
  25. #endif

OSMemAddr指向内存分区头地址的void型指针
OSMemFreeList指向空闲内存分区列表的void型指针
OSMemBlkSize内存分区块以字节为单位的大小
OSMemNBlks内存分区中块的个数
OSMemNFree内存分区中未使用的块的个数
 
初始化函数中,当只有一个内存分区时,我们把OSMemTbl[0]赋值给OSMemFreeList就完成了
当有多个内存分区时,初始化OSMemTbl[0]链表,将未使用的MCB单元的指向内存分区地址的指针OSMemFreeList赋值数组的下一个元素,其本质与以前的链表初始化相同,只是只用了一个指针
最后一个节点的指针指向NULL指针
然后将OSMemTbl[0]数组返回给操作系统的OSMemFreeList值
 
 
阅读(2280) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~