⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 emos_mem.c

📁 emos是一个新的类似于ucos的内核
💻 C
字号:
/****************************************************************************
 *
 * (c) Copyright 2001,2008, EMB system, All Rights Reserved.
 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF EMB SYSTEM, INC.
 * The copyright notice above does not evidence any actual or intended
 * publication of such source code. 
 *
 *  Subsystem:    EMOS 
 *  File:         emos_mem.c
 *  Author:       zenf zhao
 *  Description:  embedded software system mem managenent
 *
 ****************************************************************************/
#include "emos_inc.h"
#include "emos_cfg.h"
#include "emos_core.h"

/* memory maagement function definition*/
#if EMOS_MEM_EN && (EMOS_MAX_MEM_PART >= 2)
void          emosMemInit(void);
EMOS_MEM_T*   emosMemCreate(void *addr, uint32 nblks, uint32 blksize, uint8 *err);
void*         emosMemGet(EMOS_MEM_T  *pmem, uint8 *err);
uint8         emosMemPut(EMOS_MEM_T  *pmem, void *pblk);
uint8         emosMemQuery(EMOS_MEM_T  *pmem, EMOS_MEM_DATA_T *pdata);
#endif

#if EMOS_MEM_EN && (EMOS_MAX_MEM_PART >= 2)

 /* memory management global variables definition,
     Pointer to free list of memory partitions  */
static  EMOS_MEM_T* emosMemFreeList = 0;           
static  uint32      emosInited = 0;
/* Storage for memory partition manager  */
static  EMOS_MEM_T  emosMemTbl[EMOS_MAX_MEM_PART]={{0}};


void emosMemInit (void)
{
    EMOS_MEM_T* pmem = 0;
    uint32      i = 0;

    if(0 != emosInited)  
    {
    	return ;
    }
    
   /* Point to memory control block (MCB)     */
    pmem = (EMOS_MEM_T*)&emosMemTbl[0];                    
    for (i = 0; i < (EMOS_MAX_MEM_PART - 1); i++)
    {
    	/* Init list of free memory partitions
    	    Chain list of free partitions */
        pmem->osMemFreeList = (void *)&emosMemTbl[i+1]; 
        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++;
    }
    
    emosMemTbl[EMOS_MAX_MEM_PART - 1].osMemFreeList = (void *)0;
    emosMemFreeList = (EMOS_MEM_T*)&emosMemTbl[0];
    emosInited=1;
    return;
}

/*********************************************************************************************************
* CREATE A MEMORY PARTITION
* Description : Create a fixed-sized memory partition that will be managed by EMOS.
* 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, nofree partition is available.
*  
*  notes by modification from ucOS-II
*  Create Memblks = nblks*blksize = size(addr), addr must provide the capacity address >= nblks*blksize
*  further,create the list, the first 4B space of each blks stores the next blk address, that is the linkedlist
**********************************************************************************************************/
EMOS_MEM_T*emosMemCreate (void *addr, uint32 nblks, uint32 blksize, uint8 *err)
{
    EMOS_MEM_T* pmem=0;
    uint8*      pblk=0;
    void**       plink=0;
    uint32      i=0;

    if (nblks < 2) 
    {  
       /* Must have at least 2 blocks per partition      */
        *err = EMOS_MEM_INVALID_BLKS;
        return ((EMOS_MEM_T*)0);
    }
    
    if (blksize < sizeof(void *))
    {
    	/* Must contain space for at least a pointer      */
        *err = EMOS_MEM_INVALID_SIZE;
        return ((EMOS_MEM_T*)0);
    }
    
    EMOS_ENTER_CRITICAL();
    pmem = emosMemFreeList;                             /* Get next free memory partition                */
    if (emosMemFreeList != (EMOS_MEM_T*)0)
    {
   	    /* See if pool of free partitions was empty      */
         emosMemFreeList = (EMOS_MEM_T*)emosMemFreeList->osMemFreeList;
    }
    EMOS_EXIT_CRITICAL();
    
    if (pmem == (EMOS_MEM_T*)0)
    {
    	/* See if we have a memory partition             */
        *err = EMOS_MEM_INVALID_PART;
        return ((EMOS_MEM_T*)0);
    }
    
    /* Create linked list of free memory blocks      */
    plink = (void **)addr;                            
    pblk  = (uint8 *)addr + blksize;
    for (i = 0; i < (nblks - 1); i++) 
    {
         *plink = (void *)pblk;
          plink  = (void **)pblk;
          pblk   = pblk + blksize;
    }
        /* Last memory block points to NULL*/
        *plink = (void *)0;                               
    
    EMOS_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        */
    EMOS_EXIT_CRITICAL();
    
    *err   =EMOS_NO_ERR;
    return (pmem);
}

void* emosMemGet(EMOS_MEM_T  *pmem, uint8 *err)
{
    void *pblk=0;
    EMOS_ENTER_CRITICAL();
    if(pmem->osMemNFree > 0)
    {
      pblk = pmem->osMemFreeList;
      pmem->osMemFreeList = *(void **)pblk; 
      pmem->osMemNFree--;
      EMOS_EXIT_CRITICAL();
      *err=EMOS_NO_ERR;
      return (pblk);
    }
    
    else
    {
      EMOS_EXIT_CRITICAL();
      *err=EMOS_MEM_NO_FREE_BLKS;
      return ((void*)0);
    }       
}

uint8 emosMemPut(EMOS_MEM_T  *pmem, void *pblk)
{
    EMOS_ENTER_CRITICAL();
    if(pmem->osMemNFree >= pmem->osMemNBlks)
    {
       EMOS_EXIT_CRITICAL();
       return (EMOS_MEM_FULL);
    }
    *(void**)pblk=pmem->osMemFreeList;
    pmem->osMemFreeList = pblk;
    EMOS_EXIT_CRITICAL();
    return EMOS_NO_ERR;
}

uint8 emosMemQuery(EMOS_MEM_T  *pmem, EMOS_MEM_DATA_T *pdata)
{
    EMOS_ENTER_CRITICAL();
    pdata->osAddr=pmem->osMemAddr;
    pdata->osFreeList=pmem->osMemFreeList;
    pdata->osBlkSize=pmem->osMemBlkSize;
    pdata->osNBlks=pmem->osMemNBlks;
    pdata->osNFree=pmem->osMemNFree;
    EMOS_EXIT_CRITICAL();	
    pdata->osNUsed=pmem->osMemNBlks-pmem->osMemNFree;
    return EMOS_NO_ERR;
}

void emos_mem_view(EMOS_MEM_T*mem)
{
   if(0==mem)    printf("the mem partitions null\r\n"); 	
   printf("---------------------------------------------------------------\r\n");
   printf("mem partitions information view of the emos system\r\n");
   printf("version 1.0 " EMOS_AUTH_STRING "\r\n");
   printf("---------------------------------------------------------------\r\n");
   printf("memid    membase  memfree  blksize blknum blkfree   \r\n");
   printf("%08x ",(int)mem);
   printf("%08x ",(int)mem->osMemAddr);
   printf("%08x ",(int)mem->osMemFreeList);
   printf("%-7d ",(int)mem->osMemBlkSize);
   printf("%-6d ",(int)mem->osMemNBlks);
   printf("%-7d ",(int)mem->osMemNFree);
   printf("\r\n");

  return ;
}
int emos_mem_test()
{
  EMOS_MEM_T* memId;
  void*                 pmem;
  uint8                 err;
  int                     i;

  if(0 == emosInited )
  {
  	 emosMemInit ();
  }
  
  EMOS_Printf("the mem address\r\n"); 
  for(i=0; i < EMOS_MAX_MEM_PART; i++)
  {
    printf("%08x ",(int)&emosMemTbl[i]);
    if(7 == i%8) printf("\r\n"); 
  }
  printf("\r\n"); 
  
  memId=emosMemFreeList;
  i=0;
  while(NULL != memId)
  {
    printf("%08x ",(int)&emosMemTbl[i]);
    if(7==(i++)%8) printf("\r\n"); 
    memId=(EMOS_MEM_T*)memId->osMemFreeList;
  }
    printf("\r\n"); 
  
  pmem=(void*)EMOS_MALLOC(16*16+10);
  memId=emosMemCreate (pmem, 16,16, &err);
  
  if(err!=0) 
  {
     printf("the mem id created error! memid=%x\r\n", (int)memId);
  }
  else 
  {
  	 printf("the mem id created OK! memid=%x\r\n",   (int)memId);
  }
  
  emos_mem_view(memId);
  EMOS_FREE(pmem)
  return 0;
}
#endif


 /*
 * Please add "$" around "Id" and "Log".
 * $Id$
 * $Log$
 *
 */

 

⌨️ 快捷键说明

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