os_mem.lst

来自「在51上运行的小的OS系统」· LST 代码 · 共 500 行 · 第 1/3 页

LST
500
字号
  98              pmem->OSMemNFree    = nblks;                      /* Store number of free blocks in MCB            */
  99              pmem->OSMemNBlks    = nblks;
 100              pmem->OSMemBlkSize  = blksize;                    /* Store block size of each memory blocks        */
 101              *err                = OS_NO_ERR;
 102              return (pmem);
 103          }
 104          /*$PAGE*/
 105          /*
 106          *********************************************************************************************************
 107          *                                          GET A MEMORY BLOCK
 108          *
 109          * Description : Get a memory block from a partition
 110          *
 111          * Arguments   : pmem    is a pointer to the memory partition control block
 112          *
 113          *               err     is a pointer to a variable containing an error message which will be set by this
 114          *                       function to either:
 115          *
 116          *                       OS_NO_ERR           if the memory partition has been created correctly.
 117          *                       OS_MEM_NO_FREE_BLKS if there are no more free memory blocks to allocate to caller
 118          *                       OS_MEM_INVALID_PMEM if you passed a NULL pointer for 'pmem'
 119          *
 120          * Returns     : A pointer to a memory block if no error is detected
 121          *               A pointer to NULL if an error is detected
 122          *********************************************************************************************************
C51 COMPILER V8.08   OS_MEM                                                                08/04/2008 21:49:52 PAGE 4   

 123          */
 124          
 125          void  *OSMemGet (OS_MEM *pmem, INT8U *err)
 126          {
 127              void      *pblk;
 128          #if OS_CRITICAL_METHOD == 3                           /* Allocate storage for CPU status register      */
                  OS_CPU_SR  cpu_sr = 0;
              #endif
 131          
 132          
 133          
 134          #if OS_ARG_CHK_EN > 0
 135              if (err == (INT8U *)0) {                          /* Validate 'err'                                */
 136                  return ((void *)0);
 137              }
 138              if (pmem == (OS_MEM *)0) {                        /* Must point to a valid memory partition        */
 139                  *err = OS_MEM_INVALID_PMEM;
 140                  return ((void *)0);
 141              }
 142          #endif
 143              OS_ENTER_CRITICAL();
 144              if (pmem->OSMemNFree > 0) {                       /* See if there are any free memory blocks       */
 145                  pblk                = pmem->OSMemFreeList;    /* Yes, point to next free memory block          */
 146                  pmem->OSMemFreeList = *(void **)pblk;         /*      Adjust pointer to new free list          */
 147                  pmem->OSMemNFree--;                           /*      One less memory block in this partition  */
 148                  OS_EXIT_CRITICAL();
 149                  *err = OS_NO_ERR;                             /*      No error                                 */
 150                  return (pblk);                                /*      Return memory block to caller            */
 151              }
 152              OS_EXIT_CRITICAL();
 153              *err = OS_MEM_NO_FREE_BLKS;                       /* No,  Notify caller of empty memory partition  */
 154              return ((void *)0);                               /*      Return NULL pointer to caller            */
 155          }
 156          /*$PAGE*/
 157          /*
 158          *********************************************************************************************************
 159          *                                   GET THE NAME OF A MEMORY PARTITION
 160          *
 161          * Description: This function is used to obtain the name assigned to a memory partition.
 162          *
 163          * Arguments  : pmem      is a pointer to the memory partition
 164          *
 165          *              pname     is a pointer to an ASCII string that will receive the name of the memory partitio
             -n.
 166          *
 167          *              err       is a pointer to an error code that can contain one of the following values:
 168          *
 169          *                        OS_NO_ERR                  if the name was copied to 'pname'
 170          *                        OS_MEM_INVALID_PMEM        if you passed a NULL pointer for 'pmem'
 171          *                        OS_ERR_PNAME_NULL          You passed a NULL pointer for 'pname'
 172          *
 173          * Returns    : The length of the string or 0 if 'pmem' is a NULL pointer.
 174          *********************************************************************************************************
 175          */
 176          
 177          #if OS_MEM_NAME_SIZE > 1
 178          INT8U  OSMemNameGet (OS_MEM *pmem, INT8U *pname, INT8U *err)
 179          {
 180              INT8U      len;
 181          #if OS_CRITICAL_METHOD == 3                      /* Allocate storage for CPU status register           */
                  OS_CPU_SR  cpu_sr = 0;
              #endif
C51 COMPILER V8.08   OS_MEM                                                                08/04/2008 21:49:52 PAGE 5   

 184          
 185          
 186          
 187              OS_ENTER_CRITICAL();
 188          #if OS_ARG_CHK_EN > 0
 189              if (err == (INT8U *)0) {                     /* Validate 'err'                                     */
 190                  OS_EXIT_CRITICAL();
 191                  return (0);
 192              }
 193              if (pmem == (OS_MEM *)0) {                   /* Is 'pmem' a NULL pointer?                          */
 194                  OS_EXIT_CRITICAL();                      /* Yes                                                */
 195                  *err = OS_MEM_INVALID_PMEM;
 196                  return (0);
 197              }
 198              if (pname == (INT8U *)0) {                    /* Is 'pname' a NULL pointer?                         */
 199                  OS_EXIT_CRITICAL();                      /* Yes                                                */
 200                  *err = OS_ERR_PNAME_NULL;
 201                  return (0);
 202              }
 203          #endif
 204              len  = OS_StrCopy(pname, pmem->OSMemName);   /* Copy name from OS_MEM                              */
 205              OS_EXIT_CRITICAL();
 206              *err = OS_NO_ERR;
 207              return (len);
 208          }
 209          #endif
 210          
 211          /*$PAGE*/
 212          /*
 213          *********************************************************************************************************
 214          *                                 ASSIGN A NAME TO A MEMORY PARTITION
 215          *
 216          * Description: This function assigns a name to a memory partition.
 217          *
 218          * Arguments  : pmem      is a pointer to the memory partition
 219          *
 220          *              pname     is a pointer to an ASCII string that contains the name of the memory partition.
 221          *
 222          *              err       is a pointer to an error code that can contain one of the following values:
 223          *
 224          *                        OS_NO_ERR                  if the name was copied to 'pname'
 225          *                        OS_MEM_INVALID_PMEM        if you passed a NULL pointer for 'pmem'
 226          *                        OS_ERR_PNAME_NULL          You passed a NULL pointer for 'pname'
 227          *                         OS_MEM_NAME_TOO_LONG       if the name doesn't fit in the storage area
 228          *
 229          * Returns    : None
 230          *********************************************************************************************************
 231          */
 232          
 233          #if OS_MEM_NAME_SIZE > 1
 234          void  OSMemNameSet (OS_MEM *pmem, INT8U *pname, INT8U *err)
 235          {
 236              INT8U      len;
 237          #if OS_CRITICAL_METHOD == 3                      /* Allocate storage for CPU status register           */
                  OS_CPU_SR  cpu_sr = 0;
              #endif
 240          
 241          
 242          
 243              OS_ENTER_CRITICAL();
 244          #if OS_ARG_CHK_EN > 0
 245              if (err == (INT8U *)0) {                     /* Validate 'err'                                     */
C51 COMPILER V8.08   OS_MEM                                                                08/04/2008 21:49:52 PAGE 6   

 246                  OS_EXIT_CRITICAL();
 247                  return;
 248              }
 249              if (pmem == (OS_MEM *)0) {                   /* Is 'pmem' a NULL pointer?                          */
 250                  OS_EXIT_CRITICAL();                      /* Yes                                                */
 251                  *err = OS_MEM_INVALID_PMEM;
 252                  return;
 253              }
 254              if (pname == (INT8U *)0) {                    /* Is 'pname' a NULL pointer?                         */
 255                  OS_EXIT_CRITICAL();                      /* Yes                                                */
 256                  *err = OS_ERR_PNAME_NULL;
 257                  return;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?