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

📄 ss_mem.c

📁 中国石油二期加油站IC系统后台通讯软件
💻 C
字号:


/********************************************************************20**

     Name:     System Services -- Memory management interface

     Type:     C source file

     Desc:     Implementation of the memory management interface.

     File:     ss_mem.c

     Sid:      ss_mem.c 1.2  -  08/11/98 10:47:01

     Prg:      kp

*********************************************************************21*/


/* header include files (.h) */

#include "envopt.h"        /* environment options */
#include "envdep.h"        /* environment dependent */
#include "envind.h"        /* environment independent */

#include "gen.h"           /* general layer */
#include "ssi.h"           /* system services */

#include "ss_err.h"        /* errors */
#include "ss_dep.h"        /* implementation-specific */
#include "ss_queue.h"      /* queues */
#include "ss_msg.h"        /* messaging */
#include "ss_mem.h"        /* memory management interface */
#include "ss_gen.h"        /* general */



/* header/extern include files (.x) */

#include "gen.x"           /* general layer */
#include "ssi.x"           /* system services */

#include "ss_dep.x"        /* implementation-specific */
#include "ss_queue.x"      /* queues */
#include "ss_task.x"       /* tasking */
#include "ss_timer.x"      /* timers */
#include "ss_strm.x"       /* STREAMS */
#include "ss_msg.x"        /* messaging */
#include "ss_mem.x"        /* memory management interface */
#include "ss_drvr.x"       /* driver tasks */
#include "ss_gen.x"        /* general */



/*
*     Interface functions
*/


/*
*
*       Fun:   SRegRegion
*
*       Desc:  Registers a memory region.
*
*       Ret:   ROK      - ok
*              RFAILED  - failed, general (optional)
*
*       Notes:
*
*       File:  ss_mem.c
*
*/
#ifdef ANSI
PUBLIC S16 SRegRegion
(
Region region,                  /* region ID */
SRegInfo *regInfo               /* information about the region */
)
#else
PUBLIC S16 SRegRegion(region, regInfo)
Region region;                  /* region ID */
SRegInfo *regInfo;              /* information about the region */
#endif
{
    S16 ret;


    TRC1(SRegRegion);


#if (ERRCLASS & ERRCLS_INT_PAR)

    /* validate region ID */
    if (region >= SS_MAX_REGS)
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS203, region, "Invalid region");
        RETVALUE(RFAILED);
    }

    /* validate region info pointer */
    if (regInfo == NULLP)
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS204, ERRZERO, "Null pointer");
        RETVALUE(RFAILED);
    }
#endif


    /* lock the region table */
    SS_ACQUIRE_SEMA(&osCp.regionTblSem, ret);
    if (ret != ROK)
    {

#if (ERRCLASS & ERRCLS_DEBUG)
        SSLOGERROR(ERRCLS_DEBUG, ESS205, (ErrVal) ret,
                   "Could not lock region table");
#endif

        RETVALUE(RFAILED);
    }


#if (ERRCLASS & ERRCLS_INT_PAR)
    /* verify that this region is not registered */
    if (osCp.regionTbl[region].used == TRUE)
    {
        SS_RELEASE_SEMA(&osCp.regionTblSem);

        SSLOGERROR(ERRCLS_INT_PAR, ESS206, ERRZERO, "Region ID used");
        RETVALUE(RFAILED);
    }
#endif


    /* fill in the region information */
    osCp.regionTbl[region].used = TRUE;
    osCp.regionTbl[region].regCb = regInfo->regCb;
    osCp.regionTbl[region].flags = regInfo->flags;
    osCp.regionTbl[region].start = regInfo->start;
    osCp.regionTbl[region].size = regInfo->size;
    osCp.regionTbl[region].alloc = regInfo->alloc;
    osCp.regionTbl[region].free = regInfo->free;
    osCp.regionTbl[region].ctl = regInfo->ctl;

    osCp.numRegions++;


    /* unlock the region table */
    SS_RELEASE_SEMA(&osCp.regionTblSem);


    RETVALUE(ROK);
}


/*
*
*       Fun:   SDeregRegion
*
*       Desc:  Deregisters a memory region.
*
*       Ret:   ROK      - ok
*              RFAILED  - failed, general (optional)
*
*       Notes:
*
*       File:  ss_mem.c
*
*/
#ifdef ANSI
PUBLIC S16 SDeregRegion
(
Region region                   /* region ID */
)
#else
PUBLIC S16 SDeregRegion(region)
Region region;                  /* region ID */
#endif
{
    S16 ret;


    TRC1(SDeregRegion);


#if (ERRCLASS & ERRCLS_INT_PAR)

    /* validate region ID */
    if (region >= SS_MAX_REGS)
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS207, region, "Invalid region");
        RETVALUE(RFAILED);
    }
#endif


    /* lock the region table */
    SS_ACQUIRE_SEMA(&osCp.regionTblSem, ret);
    if (ret != ROK)
    {

#if (ERRCLASS & ERRCLS_DEBUG)
        SSLOGERROR(ERRCLS_DEBUG, ESS208, (ErrVal) ret,
                   "Could not lock region table");
#endif

        RETVALUE(RFAILED);
    }


#if (ERRCLASS & ERRCLS_INT_PAR)
    /* check if this region is registered */
    if (osCp.regionTbl[region].used == FALSE)
    {
        SS_RELEASE_SEMA(&osCp.regionTblSem);
        SSLOGERROR(ERRCLS_INT_PAR, ESS209, region, "Region not registered");
        RETVALUE(RFAILED);
    }
#endif


    /* empty the information about the region */
    osCp.regionTbl[region].used = FALSE;
    osCp.regionTbl[region].start = NULLP;
    osCp.regionTbl[region].size = 0;
    osCp.regionTbl[region].regCb = NULLP;
    osCp.regionTbl[region].flags = 0;
    osCp.regionTbl[region].alloc = NULLP;
    osCp.regionTbl[region].free = NULLP;
    osCp.regionTbl[region].ctl = NULLP;

    osCp.numRegions--;


    /* unlock the region table */
    SS_RELEASE_SEMA(&osCp.regionTblSem);


    RETVALUE(ROK);
}


/*
*
*       Fun:   SAlloc
*
*       Desc:  Allocates a block of memory of at least the specified
*              size.
*
*       Ret:   ROK      - ok
*              RFAILED  - failed, general (optional)
*
*       Notes:
*
*       File:  ss_mem.c
*
*/
#ifdef ANSI
PUBLIC S16 SAlloc
(
Region region,                  /* region ID */
Size *size,                     /* size of block required/allocated */
U32 flags,                      /* allocation flags */
Data **ptr                      /* filled with pointer to block */
)
#else
PUBLIC S16 SAlloc(region, size, flags, ptr)
Region region;                  /* region ID */
Size *size;                     /* size of block required/allocated */
U32 flags;                      /* allocation flags */
Data **ptr;                     /* filled with pointer to block */
#endif
{
    S16 ret;


    TRC1(SAlloc);


#if (ERRCLASS & ERRCLS_INT_PAR)

    /* validate region ID */
    if (region >= SS_MAX_REGS)
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS210, region, "Invalid region");
        RETVALUE(RFAILED);
    }
#endif


    /* acquire one semaphore, to protect against deregistration */
    SS_ACQUIRE_SEMA(&osCp.regionTblSem, ret);
    if (ret != ROK)
    {

#if (ERRCLASS & ERRCLS_DEBUG)
        SSLOGERROR(ERRCLS_DEBUG, ESS211, (ErrVal) ret,
                   "Could not lock region table");
#endif

        RETVALUE(RFAILED);
    }


#if (ERRCLASS & ERRCLS_INT_PAR)
    /* verify that this region is present */
    if (osCp.regionTbl[region].used == FALSE)
    {
        SS_RELEASE_SEMA(&osCp.regionTblSem);

        SSLOGERROR(ERRCLS_INT_PAR, ESS212, region, "Region not registered");
        RETVALUE(RFAILED);
    }
#endif


    /* call the memory manager, to allocate this memory */
    ret = (osCp.regionTbl[region].alloc)
          (osCp.regionTbl[region].regCb, size, flags, ptr);


    /* release the semaphore we took */
    SS_RELEASE_SEMA(&osCp.regionTblSem);
    if(ret != ROK)
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS212, osCp.regionTbl[region].regCb, "failed in alloc");
    }


    RETVALUE(ret);
}


/*
*
*       Fun:   SFree
*
*       Desc:  Frees a block of memory previously allocated by SAlloc().
*
*       Ret:   ROK      - ok
*              RFAILED  - failed, general (optional)
*
*       Notes:
*
*       File:  ss_mem.c
*
*/
#ifdef ANSI
PUBLIC S16 SFree
(
Region region,                  /* region ID */
Data *ptr,                      /* pointer to the allocated block */
Size size                       /* size of block */
)
#else
PUBLIC S16 SFree(region, ptr, size)
Region region;                  /* region ID */
Data *ptr;                      /* pointer to the allocated block */
Size size;                      /* size of block */
#endif
{
    S16 ret;


    TRC1(SFree);


#if (ERRCLASS & ERRCLS_INT_PAR)

    /* validate region ID */
    if (region >= SS_MAX_REGS)
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS213, region, "Invalid region");
        RETVALUE(RFAILED);
    }
#endif


    /* acquire one semaphore, to protect against deregistration */
    SS_ACQUIRE_SEMA(&osCp.regionTblSem, ret);
    if (ret != ROK)
    {

#if (ERRCLASS & ERRCLS_DEBUG)
        SSLOGERROR(ERRCLS_DEBUG, ESS214, (ErrVal) ret,
                   "Could not lock region table");
#endif

        RETVALUE(RFAILED);
    }


#if (ERRCLASS & ERRCLS_INT_PAR)
    /* verify that this region is around */
    if (osCp.regionTbl[region].used == FALSE)
    {
        SS_RELEASE_SEMA(&osCp.regionTblSem);

        SSLOGERROR(ERRCLS_INT_PAR, ESS215, region, "Region not registered");
        RETVALUE(RFAILED);
    }
#endif


    /* call the memory manager to free this memory */
    ret = (osCp.regionTbl[region].free)(osCp.regionTbl[region].regCb, ptr, size);


    /* release the semaphore we took */
    SS_RELEASE_SEMA(&osCp.regionTblSem);


    RETVALUE(ret);
}



/********************************************************************30**
 
         End of file: ss_mem.c 1.2  -  08/11/98 10:47:01
 
*********************************************************************31*/


/********************************************************************40**
 
        Notes:
 
*********************************************************************41*/

/********************************************************************50**
 
*********************************************************************51*/


/********************************************************************60**
 
        Revision history:
 
*********************************************************************61*/

/********************************************************************90**
 
     ver       pat    init                  description
------------ -------- ---- ----------------------------------------------
1.1          ---      kp   1. initial release

1.2          ---      kp   1. cosmetic changes, error codes regenerated

*********************************************************************91*/

⌨️ 快捷键说明

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