OS_MEM.lst
上传用户:tzjinxin1
上传日期:2022-08-08
资源大小:272k
文件大小:19k
开发平台:

Visual C++

  1. C51 COMPILER V8.02   OS_MEM                                                                06/22/2006 11:44:38 PAGE 1   
  2. C51 COMPILER V8.02, COMPILATION OF MODULE OS_MEM
  3. OBJECT MODULE PLACED IN .objOS_MEM.obj
  4. COMPILER INVOKED BY: C:KeilC51BINC51.EXE uCosiiOS_MEM.C LARGE BROWSE DEBUG OBJECTEXTEND PRINT(.lstOS_MEM.lst) OBJ
  5.                     -ECT(.objOS_MEM.obj)
  6. line level    source
  7.    1          /*
  8.    2          *********************************************************************************************************
  9.    3          *                                                uC/OS-II
  10.    4          *                                          The Real-Time Kernel
  11.    5          *                                            MEMORY MANAGEMENT
  12.    6          *
  13.    7          *                          (c) Copyright 1992-2001, Jean J. Labrosse, Weston, FL
  14.    8          *                                           All Rights Reserved
  15.    9          *
  16.   10          * File : OS_MEM.C
  17.   11          * By   : Jean J. Labrosse
  18.   12          *********************************************************************************************************
  19.   13          */
  20.   14          
  21.   15          #ifndef  OS_MASTER_FILE
  22.   16          #include "sourceincludes.h"
  23.   17          #endif
  24.   18          
  25.   19          #if (OS_MEM_EN > 0) && (OS_MAX_MEM_PART > 0)
  26.               /*
  27.               *********************************************************************************************************
  28.               *                                        CREATE A MEMORY PARTITION
  29.               *
  30.               * Description : Create a fixed-sized memory partition that will be managed by uC/OS-II.
  31.               *
  32.               * Arguments   : addr     is the starting address of the memory partition
  33.               *
  34.               *               nblks    is the number of memory blocks to create from the partition.
  35.               *
  36.               *               blksize  is the size (in bytes) of each block in the memory partition.
  37.               *
  38.               *               err      is a pointer to a variable containing an error message which will be set by
  39.               *                        this function to either:
  40.               *
  41.               *                        OS_NO_ERR            if the memory partition has been created correctly.
  42.               *                        OS_MEM_INVALID_ADDR  you are specifying an invalid address for the memory 
  43.               *                                             storage of the partition.
  44.               *                        OS_MEM_INVALID_PART  no free partitions available
  45.               *                        OS_MEM_INVALID_BLKS  user specified an invalid number of blocks (must be >= 2)
  46.               *                        OS_MEM_INVALID_SIZE  user specified an invalid block size
  47.               *                                             (must be greater than the size of a pointer)
  48.               * Returns    : != (OS_MEM *)0  is the partition was created
  49.               *              == (OS_MEM *)0  if the partition was not created because of invalid arguments or, no
  50.               *                              free partition is available.
  51.               *********************************************************************************************************
  52.               */
  53.               
  54.               OS_MEM  *OSMemCreate (void *addr, INT32U nblks, INT32U blksize, INT8U *err) reentrant 
  55.               {
  56.               #if OS_CRITICAL_METHOD == 3                           /* Allocate storage for CPU status register      */
  57.                   OS_CPU_SR  cpu_sr;
  58.               #endif    
  59.                   OS_MEM    *pmem;
  60.                   INT8U     *pblk;
  61. C51 COMPILER V8.02   OS_MEM                                                                06/22/2006 11:44:38 PAGE 2   
  62.                   void     **plink;
  63.                   INT32U     i;
  64.               
  65.               
  66.               #if OS_ARG_CHK_EN > 0
  67.                   if (addr == (void *)0) {                          /* Must pass a valid address for the memory part. */
  68.                       *err = OS_MEM_INVALID_ADDR;
  69.                       return ((OS_MEM *)0);
  70.                   }
  71.                   if (nblks < 2) {                                  /* Must have at least 2 blocks per partition      */
  72.                       *err = OS_MEM_INVALID_BLKS;
  73.                       return ((OS_MEM *)0);
  74.                   }
  75.                   if (blksize < sizeof(void *)) {                   /* Must contain space for at least a pointer      */
  76.                       *err = OS_MEM_INVALID_SIZE;
  77.                       return ((OS_MEM *)0);
  78.                   }
  79.               #endif
  80.                   OS_ENTER_CRITICAL();
  81.                   pmem = OSMemFreeList;                             /* Get next free memory partition                */
  82.                   if (OSMemFreeList != (OS_MEM *)0) {               /* See if pool of free partitions was empty      */
  83.                       OSMemFreeList = (OS_MEM *)OSMemFreeList->OSMemFreeList;
  84.                   }
  85.                   OS_EXIT_CRITICAL();
  86.                   if (pmem == (OS_MEM *)0) {                        /* See if we have a memory partition             */
  87.                       *err = OS_MEM_INVALID_PART;
  88.                       return ((OS_MEM *)0);
  89.                   }
  90.                   plink = (void **)addr;                            /* Create linked list of free memory blocks      */
  91.                   pblk  = (INT8U *)addr + blksize;
  92.                   for (i = 0; i < (nblks - 1); i++) {
  93.                       *plink = (void *)pblk;
  94.                       plink  = (void **)pblk;
  95.                       pblk   = pblk + blksize;
  96.                   }
  97.                   *plink = (void *)0;                               /* Last memory block points to NULL              */
  98.                   OS_ENTER_CRITICAL();
  99.                   pmem->OSMemAddr     = addr;                       /* Store start address of memory partition       */
  100.                   pmem->OSMemFreeList = addr;                       /* Initialize pointer to pool of free blocks     */
  101.                   pmem->OSMemNFree    = nblks;                      /* Store number of free blocks in MCB            */
  102.                   pmem->OSMemNBlks    = nblks;
  103.                   pmem->OSMemBlkSize  = blksize;                    /* Store block size of each memory blocks        */
  104.                   OS_EXIT_CRITICAL();
  105.                   *err   = OS_NO_ERR;
  106.                   return (pmem);
  107.               }
  108.               /*$PAGE*/
  109.               /*
  110.               *********************************************************************************************************
  111.               *                                          GET A MEMORY BLOCK
  112.               *
  113.               * Description : Get a memory block from a partition
  114.               *
  115.               * Arguments   : pmem    is a pointer to the memory partition control block
  116.               *
  117.               *               err     is a pointer to a variable containing an error message which will be set by this
  118.               *                       function to either:
  119.               *
  120.               *                       OS_NO_ERR           if the memory partition has been created correctly.
  121.               *                       OS_MEM_NO_FREE_BLKS if there are no more free memory blocks to allocate to caller
  122.               *                       OS_MEM_INVALID_PMEM if you passed a NULL pointer for 'pmem'
  123.               *
  124. C51 COMPILER V8.02   OS_MEM                                                                06/22/2006 11:44:38 PAGE 3   
  125.               * Returns     : A pointer to a memory block if no error is detected
  126.               *               A pointer to NULL if an error is detected
  127.               *********************************************************************************************************
  128.               */
  129.               
  130.               void  *OSMemGet (OS_MEM *pmem, INT8U *err) reentrant
  131.               {
  132.               #if OS_CRITICAL_METHOD == 3                           /* Allocate storage for CPU status register      */
  133.                   OS_CPU_SR  cpu_sr;
  134.               #endif    
  135.                   void      *pblk;
  136.               
  137.               
  138.               #if OS_ARG_CHK_EN > 0
  139.                   if (pmem == (OS_MEM *)0) {                        /* Must point to a valid memory partition         */
  140.                       *err = OS_MEM_INVALID_PMEM;
  141.                       return ((OS_MEM *)0);
  142.                   }
  143.               #endif
  144.                   OS_ENTER_CRITICAL();
  145.                   if (pmem->OSMemNFree > 0) {                       /* See if there are any free memory blocks       */
  146.                       pblk                = pmem->OSMemFreeList;    /* Yes, point to next free memory block          */
  147.                       pmem->OSMemFreeList = *(void **)pblk;         /*      Adjust pointer to new free list          */
  148.                       pmem->OSMemNFree--;                           /*      One less memory block in this partition  */
  149.                       OS_EXIT_CRITICAL();
  150.                       *err = OS_NO_ERR;                             /*      No error                                 */
  151.                       return (pblk);                                /*      Return memory block to caller            */
  152.                   }
  153.                   OS_EXIT_CRITICAL();
  154.                   *err = OS_MEM_NO_FREE_BLKS;                       /* No,  Notify caller of empty memory partition  */
  155.                   return ((void *)0);                               /*      Return NULL pointer to caller            */
  156.               }
  157.               /*$PAGE*/
  158.               /*
  159.               *********************************************************************************************************
  160.               *                                         RELEASE A MEMORY BLOCK
  161.               *
  162.               * Description : Returns a memory block to a partition
  163.               *
  164.               * Arguments   : pmem    is a pointer to the memory partition control block
  165.               *
  166.               *               pblk    is a pointer to the memory block being released.
  167.               *
  168.               * Returns     : OS_NO_ERR            if the memory block was inserted into the partition
  169.               *               OS_MEM_FULL          if you are returning a memory block to an already FULL memory 
  170.               *                                    partition (You freed more blocks than you allocated!)
  171.               *               OS_MEM_INVALID_PMEM  if you passed a NULL pointer for 'pmem'
  172.               *               OS_MEM_INVALID_PBLK  if you passed a NULL pointer for the block to release.
  173.               *********************************************************************************************************
  174.               */
  175.               
  176.               INT8U  OSMemPut (OS_MEM  *pmem, void *pblk) reentrant
  177.               {
  178.               #if OS_CRITICAL_METHOD == 3                      /* Allocate storage for CPU status register           */
  179.                   OS_CPU_SR  cpu_sr;
  180.               #endif    
  181.                   
  182.                   
  183.               #if OS_ARG_CHK_EN > 0
  184.                   if (pmem == (OS_MEM *)0) {                   /* Must point to a valid memory partition             */
  185.                       return (OS_MEM_INVALID_PMEM);
  186.                   }
  187. C51 COMPILER V8.02   OS_MEM                                                                06/22/2006 11:44:38 PAGE 4   
  188.                   if (pblk == (void *)0) {                     /* Must release a valid block                         */
  189.                       return (OS_MEM_INVALID_PBLK);
  190.                   }
  191.               #endif
  192.                   OS_ENTER_CRITICAL();
  193.                   if (pmem->OSMemNFree >= pmem->OSMemNBlks) {  /* Make sure all blocks not already returned          */
  194.                       OS_EXIT_CRITICAL();
  195.                       return (OS_MEM_FULL);
  196.                   }
  197.                   *(void **)pblk      = pmem->OSMemFreeList;   /* Insert released block into free block list         */
  198.                   pmem->OSMemFreeList = pblk;
  199.                   pmem->OSMemNFree++;                          /* One more memory block in this partition            */
  200.                   OS_EXIT_CRITICAL();
  201.                   return (OS_NO_ERR);                          /* Notify caller that memory block was released       */
  202.               }
  203.               /*$PAGE*/
  204.               /*
  205.               *********************************************************************************************************
  206.               *                                          QUERY MEMORY PARTITION
  207.               *
  208.               * Description : This function is used to determine the number of free memory blocks and the number of
  209.               *               used memory blocks from a memory partition.
  210.               *
  211.               * Arguments   : pmem    is a pointer to the memory partition control block
  212.               *
  213.               *               ppdata   is a pointer to a structure that will contain information about the memory
  214.               *                       partition.
  215.               *
  216.               * Returns     : OS_NO_ERR            If no errors were found.
  217.               *               OS_MEM_INVALID_PMEM  if you passed a NULL pointer for 'pmem'
  218.               *               OS_MEM_INVALID_PDATA if you passed a NULL pointer for the block to release.
  219.               *********************************************************************************************************
  220.               */
  221.               
  222.               #if OS_MEM_QUERY_EN > 0
  223.               INT8U  OSMemQuery (OS_MEM *pmem, OS_MEM_DATA *ppdata) reentrant
  224.               {
  225.               #if OS_CRITICAL_METHOD == 3                      /* Allocate storage for CPU status register           */
  226.                   OS_CPU_SR  cpu_sr;
  227.               #endif    
  228.                   
  229.                   
  230.               #if OS_ARG_CHK_EN > 0
  231.                   if (pmem == (OS_MEM *)0) {                   /* Must point to a valid memory partition             */
  232.                       return (OS_MEM_INVALID_PMEM);
  233.                   }
  234.                   if (ppdata == (OS_MEM_DATA *)0) {             /* Must release a valid storage area for the data     */
  235.                       return (OS_MEM_INVALID_PDATA);
  236.                   }
  237.               #endif
  238.                   OS_ENTER_CRITICAL();
  239.                   ppdata->OSAddr     = pmem->OSMemAddr;
  240.                   ppdata->OSFreeList = pmem->OSMemFreeList;
  241.                   ppdata->OSBlkSize  = pmem->OSMemBlkSize;
  242.                   ppdata->OSNBlks    = pmem->OSMemNBlks;
  243.                   ppdata->OSNFree    = pmem->OSMemNFree;
  244.                   OS_EXIT_CRITICAL();
  245.                   ppdata->OSNUsed    = ppdata->OSNBlks - ppdata->OSNFree;
  246.                   return (OS_NO_ERR);
  247.               }
  248.               #endif                                           /* OS_MEM_QUERY_EN                                    */
  249.               /*$PAGE*/
  250. C51 COMPILER V8.02   OS_MEM                                                                06/22/2006 11:44:38 PAGE 5   
  251.               /*
  252.               *********************************************************************************************************
  253.               *                                    INITIALIZE MEMORY PARTITION MANAGER
  254.               *
  255.               * Description : This function is called by uC/OS-II to initialize the memory partition manager.  Your
  256.               *               application MUST NOT call this function.
  257.               *
  258.               * Arguments   : none
  259.               *
  260.               * Returns     : none
  261.               *
  262.               * Note(s)    : This function is INTERNAL to uC/OS-II and your application should not call it.
  263.               *********************************************************************************************************
  264.               */
  265.               
  266.               void  OS_MemInit (void) reentrant
  267.               {
  268.               #if OS_MAX_MEM_PART == 1
  269.                   OSMemFreeList                = (OS_MEM *)&OSMemTbl[0]; /* Point to beginning of free list          */
  270.                   OSMemFreeList->OSMemFreeList = (void *)0;              /* Initialize last node                     */
  271.                   OSMemFreeList->OSMemAddr     = (void *)0;              /* Store start address of memory partition  */
  272.                   OSMemFreeList->OSMemNFree    = 0;                      /* No free blocks                           */
  273.                   OSMemFreeList->OSMemNBlks    = 0;                      /* No blocks                                */
  274.                   OSMemFreeList->OSMemBlkSize  = 0;                      /* Zero size                                */
  275.               #endif
  276.               
  277.               #if OS_MAX_MEM_PART >= 2
  278.                   OS_MEM  *pmem;
  279.                   INT16U   i;
  280.               
  281.               
  282.                   pmem = (OS_MEM *)&OSMemTbl[0];                    /* Point to memory control block (MCB)           */
  283.                   for (i = 0; i < (OS_MAX_MEM_PART - 1); i++) {     /* Init. list of free memory partitions          */
  284.                       pmem->OSMemFreeList = (void *)&OSMemTbl[i+1]; /* Chain list of free partitions                 */
  285.                       pmem->OSMemAddr     = (void *)0;              /* Store start address of memory partition       */
  286.                       pmem->OSMemNFree    = 0;                      /* No free blocks                                */
  287.                       pmem->OSMemNBlks    = 0;                      /* No blocks                                     */
  288.                       pmem->OSMemBlkSize  = 0;                      /* Zero size                                     */
  289.                       pmem++;
  290.                   }
  291.                   pmem->OSMemFreeList = (void *)0;                  /* Initialize last node                          */
  292.                   pmem->OSMemAddr     = (void *)0;                  /* Store start address of memory partition       */
  293.                   pmem->OSMemNFree    = 0;                          /* No free blocks                                */
  294.                   pmem->OSMemNBlks    = 0;                          /* No blocks                                     */
  295.                   pmem->OSMemBlkSize  = 0;                          /* Zero size                                     */
  296.               
  297.                   OSMemFreeList       = (OS_MEM *)&OSMemTbl[0];     /* Point to beginning of free list               */
  298.               #endif
  299.               }
  300.               #endif                                           /* OS_MEM_EN                                          */
  301. MODULE INFORMATION:   STATIC OVERLAYABLE
  302.    CODE SIZE        =   ----    ----
  303.    CONSTANT SIZE    =   ----    ----
  304.    XDATA SIZE       =   ----    ----
  305.    PDATA SIZE       =   ----    ----
  306.    DATA SIZE        =   ----    ----
  307.    IDATA SIZE       =   ----    ----
  308.    BIT SIZE         =   ----    ----
  309. END OF MODULE INFORMATION.
  310. C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)