uC/OS-II 源代码中文注释详解: OS_MEM.C



  /*

  /*

*********************************************************************************************************

*                                                uC/OS-II

*                                          The Real-Time Kernel

*                                            MEMORY MANAGEMENT

*

*                        (c) Copyright 1992-1998, Jean J. Labrosse, Plantation, FL

*                                           All Rights Reserved

*

*                                                  V2.00

*

* File : OS_MEM.C

* By   : Jean J. Labrosse

*********************************************************************************************************

*/

 

#ifndef  OS_MASTER_FILE

#include "software\includes.h"

#endif

 

#if OS_MEM_EN && OS_MAX_MEM_PART >= 2

/*

*********************************************************************************************************

*                                         LOCAL GLOBAL VARIABLES

*********************************************************************************************************

*/

 

static  OS_MEM      *OSMemFreeList;            /* Pointer to free list of memory partitions            */

static  OS_MEM       OSMemTbl[OS_MAX_MEM_PART];/* Storage for memory partition manager                 */

/*$PAGE*/

/*

*********************************************************************************************************

*                                        CREATE A MEMORY PARTITION

*

* Description : Create a fixed-sized memory partition that will be managed by uC/OS-II.

*

* Arguments   : addr     is the starting address of the memory partition

*

*               nblks    is the number of memory blocks to create from the partition.

*

*               blksize  is the size (in bytes) of each block in the memory partition.

*

*               err      is a pointer to a variable containing an error message which will be set by

*                        this function to either:

*             

*                        OS_NO_ERR            if the memory partition has been created correctly.

*                        OS_MEM_INVALID_PART  no free partitions available

*                        OS_MEM_INVALID_BLKS  user specified an invalid number of blocks (must be >= 2)

*                        OS_MEM_INVALID_SIZE  user specified an invalid block size 

*                                             (must be greater than the size of a pointer)

* Returns    : != (OS_MEM *)0  is the partition was created 

*              == (OS_MEM *)0  if the partition was not created because of invalid arguments or, no

*                              free partition is available.

*********************************************************************************************************

*/

//建立一个内存分区

OS_MEM *OSMemCreate (void *addr, INT32U nblks, INT32U blksize, INT8U *err) reentrant

{

    OS_MEM  *pmem;

    INT8U   *pblk;

    void   **plink;

    INT32U   i;

 

 

    if (nblks < 2) {                                  /* Must have at least 2 blocks per partition      */

        *err = OS_MEM_INVALID_BLKS;

        return ((OS_MEM *)0);

    }

	

	//块的数目必须大于2

    if (blksize < sizeof(void *)) {                   /* Must contain space for at least a pointer      */

        *err = OS_MEM_INVALID_SIZE;

        return ((OS_MEM *)0);

    }

	//块的大于必须大于一个指针的大小

    OS_ENTER_CRITICAL();

	//进入临界区

    pmem = OSMemFreeList;                             /* Get next free memory partition                */

	//从空列表中摘下一个块

    if (OSMemFreeList != (OS_MEM *)0) {               /* See if pool of free partitions was empty      */

        OSMemFreeList = (OS_MEM *)OSMemFreeList->OSMemFreeList;

		//如果这个块是零,OSMemFreeList指向OSMemFreeList->OSMemFreeList

    }

    OS_EXIT_CRITICAL();

	//退出临界区

    if (pmem == (OS_MEM *)0) {                        /* See if we have a memory partition             */

        *err = OS_MEM_INVALID_PART;

        return ((OS_MEM *)0);

	//如果pmem是零,发送错误码并返回0

    }

    plink = (void **)addr;                            /* Create linked list of free memory blocks      */

    pblk  = (INT8U *)addr + blksize;

	

	

	

	

    for (i = 0; i < (nblks - 1); i++) {

        *plink = (void *)pblk;

        plink  = (void **)pblk;

        pblk   = pblk + blksize;

    }

 

	//所要建立的内存分区内的所有内存块被链接成一个单向的链表

    OS_ENTER_CRITICAL();

	//进入临界区

    pmem->OSMemAddr     = addr;                       /* Store start address of memory partition       */

    pmem->OSMemFreeList = addr;                       /* Initialize pointer to pool of free blocks     */

    pmem->OSMemNFree    = nblks;                      /* Store number of free blocks in MCB            */

    pmem->OSMemNBlks    = nblks;

    pmem->OSMemBlkSize  = blksize;                    /* Store block size of each memory blocks        */

	//填写相应的信息

    OS_EXIT_CRITICAL();

    *err   = OS_NO_ERR;

    return (pmem);

	//退出并返回错误码

}

/*$PAGE*/

/*

*********************************************************************************************************

*                                          GET A MEMORY BLOCK

*

* Description : Get a memory block from a partition

*

* Arguments   : pmem    is a pointer to the memory partition control block

*

*               err     is a pointer to a variable containing an error message which will be set by this

*                       function to either:

*

*                       OS_NO_ERR           if the memory partition has been created correctly.

*                       OS_MEM_NO_FREE_BLKS if there are no more free memory blocks to allocate to caller

*

* Returns     : A pointer to a memory block if no error is detected

*               A pointer to NULL if an error is detected

*********************************************************************************************************

*/

//获得一个内存块

void *OSMemGet (OS_MEM *pmem, INT8U *err) reentrant

{

    void    *pblk;

 

 

    OS_ENTER_CRITICAL();

	//进入临界区

    if (pmem->OSMemNFree > 0) {                       /* See if there are any free memory blocks       */

	//如果有空闲的内存块

        pblk                = pmem->OSMemFreeList;    /* Yes, point to next free memory block          */

		//从空闲内存块链表中删除第一个内存块

        pmem->OSMemFreeList = *(void **)pblk;         /*      Adjust pointer to new free list          */

	//对空闲内存块链表作相应的修改

        pmem->OSMemNFree--;                           /*      One less memory block in this partition  */

		//链表头指针后移一个元素和空闲内存块数减1

        OS_EXIT_CRITICAL();

        *err = OS_NO_ERR;                             /*      No error                                 */

		退出并返回错误码

        return (pblk);                                /*      Return memory block to caller            */

		//返回指向被分配内存块的指针

		

    } else {

        OS_EXIT_CRITICAL();

        *err = OS_MEM_NO_FREE_BLKS;                   /* No,  Notify caller of empty memory partition  */

        return ((void *)0);                           /*      Return NULL pointer to caller            */

		//退出并返回错误码

    }

}

/*$PAGE*/

/*

*********************************************************************************************************

*                                    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

*********************************************************************************************************

*/

//初始化内存分区

void OSMemInit (void) reentrant

{

    OS_MEM  *pmem;

    INT16U   i;

 

 

    pmem = (OS_MEM *)&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                 */

        pmem->OSMemAddr     = (void *)0;              /* Store start address of memory partition       */

        pmem->OSMemNFree    = 0;                      /* No free blocks                                */

        pmem->OSMemNBlks    = 0;                      /* No blocks                                     */

        pmem->OSMemBlkSize  = 0;                      /* Zero size                                     */

        pmem++;

    }

	//对内存的设置

    OSMemTbl[OS_MAX_MEM_PART - 1].OSMemFreeList = (void *)0;

	

    OSMemFreeList                               = (OS_MEM *)&OSMemTbl[0];

	回到开始的内存块链接表

}

/*$PAGE*/

/*

*********************************************************************************************************

*                                         RELEASE A MEMORY BLOCK

*

* Description : Returns a memory block to a partition

*

* Arguments   : pmem    is a pointer to the memory partition control block

*

*               pblk    is a pointer to the memory block being released.

*

* Returns     : OS_NO_ERR         if the memory block was inserted into the partition

*               OS_MEM_FULL       if you are returning a memory block to an already FULL memory partition

*                                 (You freed more blocks than you allocated!)

*********************************************************************************************************

*/

//释放一个内存块

INT8U OSMemPut (OS_MEM  *pmem, void *pblk) reentrant

{

    OS_ENTER_CRITICAL();

	//退出临界区

    if (pmem->OSMemNFree >= pmem->OSMemNBlks) {  /* Make sure all blocks not already returned          */

        OS_EXIT_CRITICAL();

        return (OS_MEM_FULL); 

	//检查内存分区是否已满	

    }

    *(void **)pblk      = pmem->OSMemFreeList;   /* Insert released block into free block list         */

	//释放的内存块被插入到该分区的空闲内存块链表中

    pmem->OSMemFreeList = pblk;

	//????

    pmem->OSMemNFree++;                          /* One more memory block in this partition            */

	//空闲内存块加一

    OS_EXIT_CRITICAL();

	//退出临界区

    return (OS_NO_ERR);                          /* Notify caller that memory block was released       */

}

/*$PAGE*/

/*

*********************************************************************************************************

*                                          QUERY MEMORY PARTITION

*

* Description : This function is used to determine the number of free memory blocks and the number of 

*               used memory blocks from a memory partition.

*

* Arguments   : pmem    is a pointer to the memory partition control block

*

*               pdata   is a pointer to a structure that will contain information about the memory

*                       partition.

*

* Returns     : OS_NO_ERR         Always returns no error.

*********************************************************************************************************

*/

//查询一个内存分区的状态

INT8U OSMemQuery (OS_MEM *pmem, OS_MEM_DATA *ppdata) reentrant

{

    OS_ENTER_CRITICAL();

	//进入临界区

    ppdata->OSAddr     = pmem->OSMemAddr;

    ppdata->OSFreeList = pmem->OSMemFreeList;

    ppdata->OSBlkSize  = pmem->OSMemBlkSize;

    ppdata->OSNBlks    = pmem->OSMemNBlks;

    ppdata->OSNFree    = pmem->OSMemNFree;

	//复制pmem信息到ppdata

    OS_EXIT_CRITICAL();

	//退出临界区

    ppdata->OSNUsed    = ppdata->OSNBlks - ppdata->OSNFree;

	//计算使用量

    return (OS_NO_ERR);      

    //返回错误码	

}

#endif

 

\SOFTWARE The main directory from the root where all software-related files are placed. \SOFTWARE\BLOCKS The main directory where all ‘Building Blocks’ are located. With μC/OS-II, I included a ‘building block’ that handles DOS-type compatible functions that are used by the example code. \SOFTWARE\BLOCKS\TO This directory contains the files for the TO utility (see Appendix E, TO). The source file is TO.C and is found in the \SOFTWARE\TO\SOURCE directory. The DOS executable file (TO.EXE) is found in the \SOFTWARE\TO\EXE directory. Note that TO requires a file called TO.TBL which must reside on your root directory. An example of TO.TBL is also found in the \SOFTWARE\TO\EXE directory. You will need to move TO.TBL to the root directory if you are to use TO.EXE. \SOFTWARE\uCOS-II The main directory where all μC/OS-II files are located. \SOFTWARE\uCOS-II\EX1_x86L This directory contains the source code for EXAMPLE #1 (see section 1.07, Example #1) which is intended to run under DOS (or a DOS window under Windows 95). \SOFTWARE\uCOS-II\EX2_x86L This directory contains the source code for EXAMPLE #2 (see section 1.08, Example #2) which is intended to run under DOS (or a DOS window under Windows 95). \SOFTWARE\uCOS-II\EX3_x86L This directory contains the source code for EXAMPLE #3 (see section 1.09, Example #3) which is intended to run under DOS (or a DOS window under Windows 95). \SOFTWARE\uCOS-II\Ix86L This directory contains the source code for the processor dependent code (a.k.a. the port) of μC/OS-II for an 80x86 Real-Mode, Large Model processor. \SOFTWARE\uCOS-II\SOURCE This directory contains the source code for processor independent portion of μC/OS-II. This code is fully portable to other processor architectures.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值