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

📄 pc_memry.c

📁 ertfs文件系统里面既有完整ucos程序
💻 C
字号:
/*
* EBS - RTFS (Real Time File Manager)
* 
*
* Copyright EBS Inc. 1996
* All rights reserved.
* This code may not be redistributed in source or linkable object form
* without the consent of its author.
*/
/******************************************************************************
    PC_MEMRY - System specific memory management routines.
                        
Summary
    All global memory management occurs in this file including:
    
        .  Declaration of globals
        .  Initialization of systemwide lists and tables.
        .  Variable sized memory allocator functions for FATs.
        .  Optional de-allocation of systemwide lists and tables.

******************************************************************************/

#include <pcdisk.h>

/* BSS data. See BSS.C */
extern DDRIVE  KS_FAR *mem_drives_structures;
extern BLKBUFF KS_FAR *mem_block_pool;
extern PC_FILE KS_FAR *mem_file_pool;
extern DROBJ    KS_FAR *mem_drobj_pool;
extern FINODE  KS_FAR *mem_finode_pool;
extern FINODE  KS_FAR *mem_finode_freelist;
extern DROBJ    KS_FAR *mem_drobj_freelist;

extern DDRIVE  KS_FAR _mem_drives_structures[NDRIVES];
extern DDRIVE  KS_FAR *drno_to_dr_map[NDRIVES];

extern BLKBUFF  KS_FAR _mem_block_pool[NBLKBUFFS];
extern PC_FILE KS_FAR _mem_file_pool[NUSERFILES];
extern DROBJ    KS_FAR _mem_drobj_pool[NDROBJS];
extern FINODE  KS_FAR _mem_finode_pool[NFINODES];

/* BSS area used by the block buffer pool system */
extern dword  useindex;
extern DDRIVE *scratch_pdrive;


/* Note: There are also a few static declarations in devtable.c
        and the  device drivers (bios.c, ide_drv.c fl_drver.c, fl_host.c) */

/**************************************************************************
    PC_NUM_DRIVES -  Return total number of drives in the system

 Description
    This routine returns the number of drives in the system 

 Returns
    The number
*****************************************************************************/
int pc_num_drives(void)                                 /* __fn__ */
{
    return(NDRIVES);
}

/**************************************************************************
    PC_NUSERFILES -  Return total number of uses allowed in the system

 Description
    This routine returns the number of user in the system   

 Returns
    The number
*****************************************************************************/
int pc_num_users(void)                                  /* __fn__ */
{
    return(NUM_USERS);
}

/**************************************************************************
    PC_NUSERFILES -  Return total number of userfiles alloed in the system

 Description
    This routine returns the number of user files in the system 

 Returns
    The number
*****************************************************************************/

int pc_nuserfiles(void)                                 /* __fn__ */
{
    return(NUSERFILES);
}

/**************************************************************************
    PC_VALIDATE_DRIVENO -  Verify that a drive number is <= NDRIVES

 Description
    This routine is called when a routine is handed a drive number and
    needs to know if it is within the number of drives set during
    the congiguration.

 Returns
    TRUE if the drive number is valid or FALSE.
*****************************************************************************/

BOOLEAN pc_validate_driveno(int driveno)                            /* __fn__ */
{
    if ((driveno < 0) || (driveno >= NDRIVES))
        return(FALSE);
    else
        return(TRUE);
}

/**************************************************************************
    PC_MEMORY_INIT -  Initialize and allocate File system structures.

    THIS ROUTINE MUST BE CALLED BEFORE ANY FILE SYSTEM ROUTINES !!!!!!
    IT IS CALLED BY THE PC_ERTFS_INIT() Function

 Description
    This routine must be called before any file system routines. Its job
    is to allocate tables needed by the file system. We chose to implement
    memory management this way to provide maximum flexibility for embedded
    system developers. In the reference port we use malloc to allocate the
    various chunks of memory we need, but we could just have easily comiled
    the tables into the BSS section of the program. 

    Use whatever method makes sense in you system.

    Note the total number of bytes allocated by this routine is:
        (sizeof(DDRIVE) * NDRIVES) + (sizeof(PC_FILE)*NUSERFILES) +
        (sizeof(BLKBUFF)*NBLKBUFFS)+ (sizeof(DROBJ)*NDROBJS) +
        (sizeof(FINODE)*NFINODES)


 Returns
    TRUE on success or no ON Failure.
*****************************************************************************/
void null_pointers(void)
{
    inoroot = 0;
    mem_drives_structures = 0;
    mem_block_pool = 0;
    mem_file_pool = 0;
    mem_drobj_pool = 0;
    mem_drobj_freelist = 0;
    mem_finode_pool = 0;
    mem_finode_freelist = 0;
}
void bssdummy(void);

BOOLEAN pc_memory_init(void)                                                /*__fn__*/
{
    int i;
    int j;
    int l;
    DROBJ *pobj;
    FINODE *pfi;

    /* mem_drives_structures will be set if we've already been here */
    if (mem_drives_structures)
        return(TRUE);
    /* Call bss dummy routine. This makes sure bss.obj get linked in
       by the microsoft compiler */
    bssdummy();

    /* Zero all pointers before we initialize */
    null_pointers();

    /* Zero the drive map that maps drive numbers to devices */ 
    pc_memfill(&drno_to_dr_map[0], sizeof(drno_to_dr_map), (byte) 0);

    /* Zero block buffer pool data */
    useindex = 0;
    scratch_pdrive = 0;
    
    /* Call the kernel specific initialization code */
    if (!ks_resource_init())
    {
        return(FALSE);
    }

/*
    =!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=
    We simply assign our pointers to the placeholders in
    the BSS
    =!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=
 */
    mem_drives_structures = (DDRIVE KS_FAR *) _mem_drives_structures;
    mem_block_pool  = (BLKBUFF KS_FAR *) _mem_block_pool;
    mem_file_pool   = (PC_FILE KS_FAR *) _mem_file_pool;
    mem_drobj_pool  = (DROBJ KS_FAR *)  _mem_drobj_pool;
    mem_finode_pool = (FINODE KS_FAR *)  _mem_finode_pool;

    /* Initialize the drive structures */
    l =sizeof(DDRIVE); l *= NDRIVES;
    pc_memfill(mem_drives_structures, (word)l, (byte) 0);

    /* Initialize the block buffer array */
    for (i = 0, j = 1; i < (NBLKBUFFS-1); i++, j++)
    {
        pc_memfill(&mem_block_pool[i], sizeof(BLKBUFF) , (byte) 0);
        mem_block_pool[i].pnext = mem_block_pool + j;
    }
    pc_memfill(&mem_block_pool[NBLKBUFFS-1], sizeof(BLKBUFF) , (byte) 0);
    mem_block_pool[NBLKBUFFS-1].pnext = 0;

    /* make a NULL terminated freelist of the DROBJ pool using
        pdrive as the link. This linked freelist structure is used by the
        DROBJ memory allocator routine. */
    mem_drobj_freelist = mem_drobj_pool;
    for (i = 0,j = 1; i < NDROBJS-1; i++, j++)  
    {
        pobj = mem_drobj_freelist + j;
        mem_drobj_freelist[i].pdrive = (DDRIVE *) pobj;
    }
    mem_drobj_freelist[NDROBJS-1].pdrive = 0;

    /* Make a NULL terminated FINODE freelist using
        pnext as the link. This linked freelist is used by the FINODE 
        memory allocator routine */
    pfi = mem_finode_freelist = mem_finode_pool;
    for (i = 0; i < NFINODES-1; i++)    
    {
        pfi++;
        mem_finode_freelist->pnext = pfi;
        mem_finode_freelist++;
        mem_finode_freelist->pnext = 0;
    }
    /* Mark all user files free */
    for (i = 0; i < NUSERFILES; i++)
        mem_file_pool[i].is_free = TRUE;

    mem_finode_freelist = mem_finode_pool;

    return(TRUE);

}





/**************************************************************************
    PC_MEMORY_DROBJ -  Allocate a DROBJ structure
 Description
    If called with a null pointer, allocates and zeroes the space needed to
    store a DROBJ structure. If called with a NON-NULL pointer the DROBJ
    structure is returned to the heap.

 Returns
    If an ALLOC returns a valid pointer or NULL if no more core. If a free
    the return value is the input.

*****************************************************************************/

DROBJ *pc_memory_drobj(DROBJ *pobj)                             /*__fn__*/
{
DROBJ *preturn;
    preturn = 0;
    if (pobj)
    {
        /* Free it by putting it at the head of the freelist 
            NOTE: pdrive is used to link the freelist */
        OS_CLAIM_FSCRITICAL()
        pobj->pdrive = (DDRIVE *) mem_drobj_freelist;
        mem_drobj_freelist = pobj;
        OS_RELEASE_FSCRITICAL()
    }
    else
    {
        /* Alloc: return the first structure from the freelist */
        OS_CLAIM_FSCRITICAL()
        preturn =  mem_drobj_freelist;
        if (preturn)
        {
            mem_drobj_freelist = (DROBJ *) preturn->pdrive;
            pc_memfill(preturn, sizeof(DROBJ) , (byte) 0);
            OS_RELEASE_FSCRITICAL()
        }
        else
        {
            OS_RELEASE_FSCRITICAL()
            pc_report_error(PCERR_DROBJALLOC);
        }
    }
    return(preturn);
}


/**************************************************************************
    PC_MEMORY_FINODE -  Allocate a FINODE structure
 Description
    If called with a null pointer, allocates and zeroes the space needed to
    store a FINODE structure. If called with a NON-NULL pointer the FINODE
    structure is returned to the heap.

 Returns
    If an ALLOC returns a valid pointer or NULL if no more core. If a free
    the return value is the input.

*****************************************************************************/

FINODE *pc_memory_finode(FINODE *pinode)                            /*__fn__*/
{
FINODE *preturn;
    if (pinode)
    {
        /* Free it by putting it at the head of the freelist */
        OS_CLAIM_FSCRITICAL()
        pinode->pnext = mem_finode_freelist;
        mem_finode_freelist = pinode;
        OS_RELEASE_FSCRITICAL()
        preturn = pinode;
    }
    else
    {
        /* Alloc: return the first structure from the freelist */
        OS_CLAIM_FSCRITICAL()
        preturn =  mem_finode_freelist;
        if (preturn)
        {
            mem_finode_freelist = preturn->pnext;
            /* Zero the structure */
            pc_memfill(preturn, sizeof(FINODE) , (byte) 0);
            OS_RELEASE_FSCRITICAL()
        }
        else
        {
            OS_RELEASE_FSCRITICAL()
            pc_report_error(PCERR_FINODEALLOC);
        }
    }
    return(preturn);
}


⌨️ 快捷键说明

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