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

📄 block.c

📁 NUcleus plus 支持的文件系统。 是学习文件系统的很好参考资料。
💻 C
📖 第 1 页 / 共 2 页
字号:
/************************************************************************
*                                                                       
*       Copyright (c) 2001 by Accelerated Technology, Inc.              
*                                                                       
*  PROPRIETARY RIGHTS of Accelerated Technology are  involved in        
*  the subject matter of this material.  All manufacturing,             
*  reproduction, use, and sales rights pertaining to this subject       
*  matter are  governed by the license agreement.  The recipient of     
*     this software implicitly accepts the terms of the license.        
*                                                                       
*                                                                       
*************************************************************************

*************************************************************************
* FILE NAME                                     VERSION                 
*                                                                       
*       BLOCK.C                                 FILE  2.3              
*                                                                       
* COMPONENT                                                             
*                                                                       
*       Nucleus File                                                    
*                                                                       
* DESCRIPTION                                                           
*                                                                       
*       Directory block buffering routines.                             
*                                                                       
* DATA STRUCTURES                                                       
*                                                                       
*       None.                                                           
*                                                                       
* FUNCTIONS                                                             
*                                                                       
*       pc_alloc_blk                        Internal to this file.      
*       pc_blkpool                          Internal to this file.      
*       pc_free_all_blk                     Release all buffers         
*                                            associated with a drive.   
*       pc_free_buf                         Release a single buffer for 
*                                            possible re-use.           
*       pc_read_blk                         Read data from disk into a  
*                                            buffer.                    
*       pc_write_blk                        Write data from the buffer  
*                                            to the disk.               
*                                                                       
* DEPENDENCIES                                                          
*                                                                       
*       pcdisk.h                            File common definitions     
*                                                                       
*************************************************************************/

#include        "file\pcdisk.h"

extern BLKBUFF          *mem_block_pool;
extern _PC_BDEVSW       pc_bdevsw[];


/************************************************************************
* FUNCTION                                                              
*                                                                       
*       pc_alloc_blk                                                    
*                                                                       
* DESCRIPTION                                                           
*                                                                       
*       Use pdrive and blockno to search for a buffer in the buffer     
*       pool. If not found create a new buffer entry by discarding the  
*       Least recently used buffer in the buffer pool. The buffer is    
*       locked in core. A pointer to the buffer is returned in ppblk.   
*       If all buffers are in use it returns NULL in *ppblk.            
*                                                                       
* AUTHOR                                                                
*                                                                       
*       Takahiro Takahashi                        
*                                                                       
* INPUTS                                                                
*                                                                       
*       pblk                                Read block buffer.          
*       pdriver                             Pointer to DDRIVE structure.
*       blockno                             Block number to read        
*                                                                       
* OUTPUTS                                                               
*                                                                       
*       1                                   Buffer was found in the pool.
*       0                                   New buffer was assigned.    
*       NUF_NO_BLOCK                        No block buffer available.  
*                                                                       
*************************************************************************/
INT pc_alloc_blk(BLKBUFF **ppblk, DDRIVE *pdrive, UINT32 blockno)
{
BLKBUFF     *pblk;
BLKBUFF     *oldest = NULL;
BLKBUFF     *freeblk = NULL;
UINT32      lru = (UINT32) ~0L;
static UINT32   useindex = 0L;


    /* Get or init the block pool */
    pblk = pc_blkpool(pdrive); 

#ifdef DEBUG3
            DEBUG_PRINT("Alloc block %d\n", blockno);
#endif

    /* Initialize ppblk */
    *ppblk = NULL;

    /* Increment last recently used index */
    useindex += 1;

    while (pblk)
    {
        if (!pblk->pdrive)
        {
            /* This buffer's free */
            freeblk = pblk;
        }
        else
        {
            if ( (pblk->pdrive == pdrive) && (pblk->blockno == blockno) )
            {
                /* Found it */
                *ppblk = pblk;
                /* Update the last recently used stuf */
                pblk->lru = useindex;
                pblk->use_count += 1;
                /* useindex wraps around */
                if (useindex == 0)
                {
                    /* Get or init the block pool */
                    pblk = pc_blkpool(pdrive); 
                    /* Reset last recently used stuf */
                    while (pblk)
                    {
                        pblk->lru = 0;
                        pblk = pblk->pnext;
                    }
                }
                return(1);
            }
            else
            {
                /* No match. see if its a candidate for swapping if we run out of
                   pointers */
                if (!pblk->use_count)
                {
                    if (pblk->lru < lru)
                    {
                        lru = pblk->lru;
                        oldest = pblk;
                    }
                }
            }
        }
        pblk = pblk->pnext;
    }

    /* If off the end of the list we have to bump somebody */
    if (freeblk)
        pblk = freeblk;
    else
        pblk = oldest;

    if (!pblk)
    {
        pc_report_error(PCERR_BLOCKCLAIM);
        /* Panic */
        *ppblk = NULL;
        return(NUF_NO_BLOCK);
    }

    pblk->lru = useindex;
    pblk->pdrive = pdrive;
    pblk->blockno = blockno;
    pblk->use_count = 1;
    pblk->io_pending = NO;

    *ppblk = pblk;

    /* Return NO since we didn't find it in the buffer pool */
    return(0);
}


/************************************************************************
* FUNCTION                                                              
*                                                                       
*       pc_blkpool                                                      
*                                                                       
* DESCRIPTION                                                           
*                                                                       
*       Return the beginning of the buffer pool for a drive. If the     
*       pool is uninitialized report it. The buffer pool should have    
*       been initialized in pc_memory_init.                             
*                                                                       
* AUTHOR                                                                
*                                                                       
*       Takahiro Takahashi                         
*                                                                       
* INPUTS                                                                
*                                                                       
*       pdrive                              Drive management structure. 
*                                                                       
* OUTPUTS                                                               
*                                                                       
*       BLKBUFF *                           Pointer to block pool       
*                                                                       
*************************************************************************/
BLKBUFF *pc_blkpool(DDRIVE *pdrive)
{

    pdrive = pdrive;                /* Make compilers happy */
    /* If not initialized somebody didn't call pc_meminit */
    if (!mem_block_pool)
        pc_report_error(PCERR_BLOCKALLOC);
    return(mem_block_pool);
}


/************************************************************************
* FUNCTION                                                              
*                                                                       
*       pc_free_all_blk                                                 
*                                                                       

⌨️ 快捷键说明

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