os_mem.lst

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

LST
500
字号
 258              }
 259          #endif
 260              len = OS_StrLen(pname);                      /* Can we fit the string in the storage area?         */
 261              if (len > (OS_MEM_NAME_SIZE - 1)) {          /* No                                                 */
 262                  OS_EXIT_CRITICAL();
 263                  *err = OS_MEM_NAME_TOO_LONG;
 264                  return;
 265              }
 266              (void)OS_StrCopy(pmem->OSMemName, pname);    /* Yes, copy name to the memory partition header      */
 267              OS_EXIT_CRITICAL();
 268              *err = OS_NO_ERR;
 269          }
 270          #endif
 271          
 272          /*$PAGE*/
 273          /*
 274          *********************************************************************************************************
 275          *                                         RELEASE A MEMORY BLOCK
 276          *
 277          * Description : Returns a memory block to a partition
 278          *
 279          * Arguments   : pmem    is a pointer to the memory partition control block
 280          *
 281          *               pblk    is a pointer to the memory block being released.
 282          *
 283          * Returns     : OS_NO_ERR            if the memory block was inserted into the partition
 284          *               OS_MEM_FULL          if you are returning a memory block to an already FULL memory
 285          *                                    partition (You freed more blocks than you allocated!)
 286          *               OS_MEM_INVALID_PMEM  if you passed a NULL pointer for 'pmem'
 287          *               OS_MEM_INVALID_PBLK  if you passed a NULL pointer for the block to release.
 288          *********************************************************************************************************
 289          */
 290          
 291          INT8U  OSMemPut (OS_MEM *pmem, void *pblk)
 292          {
 293          #if OS_CRITICAL_METHOD == 3                      /* Allocate storage for CPU status register           */
                  OS_CPU_SR  cpu_sr = 0;
              #endif
 296          
 297          
 298          
 299          #if OS_ARG_CHK_EN > 0
 300              if (pmem == (OS_MEM *)0) {                   /* Must point to a valid memory partition             */
 301                  return (OS_MEM_INVALID_PMEM);
 302              }
 303              if (pblk == (void *)0) {                     /* Must release a valid block                         */
 304                  return (OS_MEM_INVALID_PBLK);
 305              }
 306          #endif
 307              OS_ENTER_CRITICAL();
C51 COMPILER V8.08   OS_MEM                                                                08/04/2008 21:49:52 PAGE 7   

 308              if (pmem->OSMemNFree >= pmem->OSMemNBlks) {  /* Make sure all blocks not already returned          */
 309                  OS_EXIT_CRITICAL();
 310                  return (OS_MEM_FULL);
 311              }
 312              *(void **)pblk      = pmem->OSMemFreeList;   /* Insert released block into free block list         */
 313              pmem->OSMemFreeList = pblk;
 314              pmem->OSMemNFree++;                          /* One more memory block in this partition            */
 315              OS_EXIT_CRITICAL();
 316              return (OS_NO_ERR);                          /* Notify caller that memory block was released       */
 317          }
 318          /*$PAGE*/
 319          /*
 320          *********************************************************************************************************
 321          *                                          QUERY MEMORY PARTITION
 322          *
 323          * Description : This function is used to determine the number of free memory blocks and the number of
 324          *               used memory blocks from a memory partition.
 325          *
 326          * Arguments   : pmem        is a pointer to the memory partition control block
 327          *
 328          *               p_mem_data  is a pointer to a structure that will contain information about the memory
 329          *                           partition.
 330          *
 331          * Returns     : OS_NO_ERR            If no errors were found.
 332          *               OS_MEM_INVALID_PMEM  if you passed a NULL pointer for 'pmem'
 333          *               OS_MEM_INVALID_PDATA if you passed a NULL pointer to the data recipient.
 334          *********************************************************************************************************
 335          */
 336          
 337          #if OS_MEM_QUERY_EN > 0
 338          INT8U  OSMemQuery (OS_MEM *pmem, OS_MEM_DATA *p_mem_data)
 339          {
 340          #if OS_CRITICAL_METHOD == 3                      /* Allocate storage for CPU status register           */
                  OS_CPU_SR  cpu_sr = 0;
              #endif
 343          
 344          
 345          
 346          #if OS_ARG_CHK_EN > 0
 347              if (pmem == (OS_MEM *)0) {                   /* Must point to a valid memory partition             */
 348                  return (OS_MEM_INVALID_PMEM);
 349              }
 350              if (p_mem_data == (OS_MEM_DATA *)0) {        /* Must release a valid storage area for the data     */
 351                  return (OS_MEM_INVALID_PDATA);
 352              }
 353          #endif
 354              OS_ENTER_CRITICAL();
 355              p_mem_data->OSAddr     = pmem->OSMemAddr;
 356              p_mem_data->OSFreeList = pmem->OSMemFreeList;
 357              p_mem_data->OSBlkSize  = pmem->OSMemBlkSize;
 358              p_mem_data->OSNBlks    = pmem->OSMemNBlks;
 359              p_mem_data->OSNFree    = pmem->OSMemNFree;
 360              OS_EXIT_CRITICAL();
 361              p_mem_data->OSNUsed    = p_mem_data->OSNBlks - p_mem_data->OSNFree;
 362              return (OS_NO_ERR);
 363          }
 364          #endif                                           /* OS_MEM_QUERY_EN                                    */
 365          /*$PAGE*/
 366          /*
 367          *********************************************************************************************************
 368          *                                    INITIALIZE MEMORY PARTITION MANAGER
 369          *
C51 COMPILER V8.08   OS_MEM                                                                08/04/2008 21:49:52 PAGE 8   

 370          * Description : This function is called by uC/OS-II to initialize the memory partition manager.  Your
 371          *               application MUST NOT call this function.
 372          *
 373          * Arguments   : none
 374          *
 375          * Returns     : none
 376          *
 377          * Note(s)    : This function is INTERNAL to uC/OS-II and your application should not call it.
 378          *********************************************************************************************************
 379          */
 380          
 381          void  OS_MemInit (void)
 382          {
 383          #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
 391          
 392          #if OS_MAX_MEM_PART >= 2
 393              OS_MEM  *pmem;
 394              INT16U   i;
 395          
 396          
 397              OS_MemClr((INT8U *)&OSMemTbl[0], sizeof(OSMemTbl));   /* Clear the memory partition table          */
 398              pmem = &OSMemTbl[0];                                  /* Point to memory control block (MCB)       */
 399              for (i = 0; i < (OS_MAX_MEM_PART - 1); i++) {         /* Init. list of free memory partitions      */
 400                  pmem->OSMemFreeList = (void *)&OSMemTbl[i+1];     /* Chain list of free partitions             */
 401          #if OS_MEM_NAME_SIZE > 1
 402                  pmem->OSMemName[0]  = '?';                        /* Unknown name                              */
 403                  pmem->OSMemName[1]  = OS_ASCII_NUL;
 404          #endif
 405                  pmem++;
 406              }
 407              pmem->OSMemFreeList = (void *)0;                      /* Initialize last node                      */
 408          #if OS_MEM_NAME_SIZE > 1
 409              pmem->OSMemName[0]  = '?';                            /* Unknown name                              */
 410              pmem->OSMemName[1]  = OS_ASCII_NUL;
 411          #endif
 412          
 413              OSMemFreeList       = &OSMemTbl[0];                   /* Point to beginning of free list           */
 414          #endif
 415          }
 416          #endif                                                    /* OS_MEM_EN                                 */

C51 COMPILATION COMPLETE.  0 WARNING(S),  57 ERROR(S)

⌨️ 快捷键说明

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