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

📄 pc_memry.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                 
*                                                                       
*       PC_MEMRY.C                              FILE  2.3
*                                                                       
* COMPONENT                                                             
*                                                                       
*       Nucleus File                                                    
*                                                                       
* DESCRIPTION                                                           
*                                                                       
*       System specific memory management routines.                     
*                                                                       
* DATA STRUCTURES                                                       
*                                                                       
*       drive_locks                         Drive locks list.           
*       fat_locks                           FAT locks list.             
*       drive_io_locks                      Drive I/O locks list.       
*       finode_lock_handles                 FINODE lock list.           
*       handles_are_alloced                 Memory allocate flag        
*                                            (pc_memory_init use).      
*       user_heap                           User heap.                  
*       *inoroot                            Begining of inode pool.     
*       Task_Map                            Task map list.              
*       *nu_to_rtfs_task_map                File system user task map.  
*       *mem_block_pool                     Memory block pool list.     
*       *mem_file_pool                      Memory file pool list.      
*       *mem_drobj_pool                     Memory DROBJ pool list.     
*       *mem_drobj_freelist                 Memory DROBJ free list.     
*       *mem_finode_pool                    Memory FINODE pool list.    
*       *mem_finode_freelist                Memory FINODE free list.    
*       *NUF_Drive_Pointers                 File system Driver pointer. 
*       NUF_Fat_Type                        Drive FAT type list.        
*       NUFP_Events                         File system event group list.
*       NUF_FILE_SYSTEM_MUTEX               File system semaphore.      
*                                                                       
* FUNCTIONS                                                             
*                                                                       
*       NUF_Alloc                           Allocate memory.            
*       pc_memory_init                      This routine must be called 
*                                            before any file system     
*                                            routines.                  
*       pc_memory_close                     Free all memory used by     
*                                            the file system and make it
*                                            ready to runagain.         
*       pc_memory_drobj                     If called with a null       
*                                            pointer, allocates and     
*                                            zeroes the space needed to 
*                                            store a DROBJ structure.   
*       pc_memory_finode                    If called with a null       
*                                            pointer, allocates and     
*                                            zeroes the space needed to 
*                                            store a FINODE structure.  
*                                                                       
* DEPENDENCIES                                                          
*                                                                       
*       nucleus.h                           System definitions          
*       pcdisk.h                            File common definitions     
*                                                                       
*************************************************************************/

#include        "plus\nucleus.h"
#include        "file\pcdisk.h"


/* Things we need if using fine grained multitasking */
#if (LOCK_METHOD == 2)
/* Semaphore handles used for reentrancy control on fats drives and finodes */
LOCKOBJ                 drive_locks[NDRIVES];
LOCKOBJ                 fat_locks[NDRIVES];
LOCKOBJ                 drive_io_locks[NDRIVES];
WAIT_HANDLE_TYPE        finode_lock_handles[NFINODES];
INT                     handles_are_alloced = NO;
#endif 


/* List of users. see porting guide and pc_users.c. The only user 
   structure in single tasking environments  */
PFILE_SYSTEM_USER       user_heap = 0;
FINODE                  *inoroot = 0;             /* Begining of inode pool */
/* SPR214 07-21-01 Removed unused array */
/* DDRIVE                  *drv_array[NDRIVES];   Array of open drives */
INT16                   Task_Map[NUM_USERS];
INT16                   *nu_to_rtfs_task_map;
BLKBUFF                 *mem_block_pool = 0;
PC_FILE                 *mem_file_pool = 0;
DROBJ                   *mem_drobj_pool = 0;
DROBJ                   *mem_drobj_freelist = 0;
FINODE                  *mem_finode_pool = 0;
FINODE                  *mem_finode_freelist = 0;
UNSIGNED                *NUF_Drive_Pointers[NDRIVES];
INT                     NUF_Fat_Type[NDRIVES]; 

/*  The following is a definition which allows the Event IDs used
    by Nucleus PLUS.  */
NU_EVENT_GROUP      NUFP_Events[NUF_NUM_EVENTS];

/*  The following is the definition of the Semaphore used by Nucleus FILE. */
NU_SEMAPHORE            NUF_FILE_SYSTEM_MUTEX;


extern  NU_MEMORY_POOL  System_Memory;
extern  INT             NUF_Drive_Fat_Size[NDRIVES];


/************************************************************************
* FUNCTION                                                              
*                                                                       
*       NUF_Alloc                                                       
*                                                                       
* DESCRIPTION                                                           
*                                                                       
*       Nucleus - NU_Allocate_Memory call function.                     
*       See Nucleus PLUS manual.                                        
*                                                                       
* AUTHOR                                                                
*                                                                       
*       Takahiro Takahashi
*                                                                       
* INPUTS                                                                
*                                                                       
*       nbytes                              Allocate memory size(byte). 
*                                                                       
* OUTPUTS                                                               
*                                                                       
*       None.                                                           
*                                                                       
*************************************************************************/
VOID *NUF_Alloc(INT nbytes)
{
VOID        *return_ptr;
INT         alloc_status;


    alloc_status = NU_Allocate_Memory(&System_Memory, &return_ptr,
                      (UNSIGNED) nbytes,
                      NU_NO_SUSPEND);

    if (alloc_status != NU_SUCCESS)
    {
#ifdef DEBUG
        DEBUG_PRINT("Can not allocate memory\n");
#endif
        return_ptr = NU_NULL;
    }
    return((void *) return_ptr);

}


/************************************************************************
* FUNCTION                                                              
*                                                                       
*       pc_memory_init                                                  
*                                                                       
* 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.                                     
*                                                                       
* AUTHOR                                                                
*                                                                       
*       Takahiro Takahashi
*                                                                       
* INPUTS                                                                
*                                                                       
*       None.                                                           
*                                                                       
* OUTPUTS                                                               
*                                                                       
*       YES on success or no ON Failure.                                
*                                                                       
*************************************************************************/
INT pc_memory_init(VOID)
{
INT16       i;
INT16       j;
UNSIGNED    pool_size;
UINT16      event_id;
DROBJ       *pobj;
FINODE      *pfi;
OPTION      preempt_status;
#if (RAMDISK)
#if (RAMDISK_FROMPOOL)
            void *pointer;
#endif
#endif
    preempt_status = NU_Change_Preemption(NU_NO_PREEMPT);

    /* Check if already initialized. If so don't do it again */
    if (user_heap)
    {
        NU_Change_Preemption(preempt_status);
        return(YES);
    }

    /*  Initialize all of the Events.  We don't know how many events the
        user is going to define because he may or may not use the IDE,
        FLOPPY, or RAMDISK drivers. */
    for (event_id = 0; event_id < NUF_NUM_EVENTS; event_id++)
    {

        /*  Create the Event Group. */
        if (NU_Create_Event_Group(&NUFP_Events[event_id],
                                    "EVENT") != NU_SUCCESS)
            return(NO);

    }

    /*  Initialize all of the Semaphores.  */
    if (NU_Create_Semaphore(&NUF_FILE_SYSTEM_MUTEX, "SEM 0", 1,
                                    NU_FIFO) != NU_SUCCESS)
        return(NO);


/* NUCLEUS. Event handles are simple indeces under nucleus */

    mem_block_pool = NULL;
    mem_file_pool = NULL;
    mem_drobj_pool = NULL;
    mem_drobj_freelist = NULL;
    mem_finode_pool = NULL;
    mem_finode_freelist = NULL;
    user_heap = NULL;
    nu_to_rtfs_task_map = NULL;

/* NUCLEUS - The current user is a macro not a constant */

#if (RAMDISK)
#if (RAMDISK_FROMPOOL)
    /*  Create the RAMDISK Partition. */
#define POOL_SIZE \
    ((unsigned)(((unsigned)NUM_RAMDISK_PAGES) * \
                (((unsigned)NUF_RAMDISK_PARTITION_SIZE) + \
                 ((unsigned)PARTITION_SIZE))))

    if (NU_Allocate_Memory(&System_Memory, &pointer,
                           POOL_SIZE + ALLOC_SIZE,
                           NU_NO_SUSPEND) != NU_SUCCESS)
        return(-1);

    if (NU_Create_Partition_Pool(&NUF_RAMDISK_PARTITION, "RAMDISK",
                                pointer, POOL_SIZE,
                                NUF_RAMDISK_PARTITION_SIZE,
                                NU_FIFO) != NU_SUCCESS)
        return(-2);
#endif
#endif

    /* Allocate all event handles */
#if (LOCK_METHOD == 2)
    /* Allocate all message handles needed by RTFS. */
    /* If they were already alloced we don't allocate them here, we just
       check that they are still valid */
    for (i = 0; i < NDRIVES; i++)
    {
        NUF_Drive_Pointers[i] = (UNSIGNED *)0;
        NUF_Fat_Type[i] = 0;

        if (!handles_are_alloced)
            drive_locks[i].wait_handle = pc_alloc_lock();
        drive_locks[i].opencount = 0;
        drive_locks[i].exclusive = NO;
        if (!handles_are_alloced)
            fat_locks[i].wait_handle =  pc_alloc_lock();
        fat_locks[i].opencount = 0;
        fat_locks[i].exclusive = NO;
        if (!handles_are_alloced)
            drive_io_locks[i].wait_handle =  pc_alloc_lock();
        drive_io_locks[i].opencount = 0;
        drive_io_locks[i].exclusive = NO;
    }
    for (i = 0; i < NFINODES; i++)
    {
        if (!handles_are_alloced)
            finode_lock_handles[i] = pc_alloc_lock();
    }
    handles_are_alloced = YES;
#endif

    /* Initialize our user list - managed by code in pc_users.c */
      pool_size =sizeof(FILE_SYSTEM_USER);
      pool_size *= NUM_USERS;
      
    if (NU_Allocate_Memory(&System_Memory, 
                                (VOID **)&user_heap, 
                                pool_size,
                                NU_NO_SUSPEND) != NU_SUCCESS)

    {
        user_heap = NULL;
        goto meminit_failed;
    }
    pc_memfill(user_heap, (INT)pool_size, (UINT8) 0);
    /* Point fs_user at the heap. If NUM_USERS is one it will stay forever */
    /* NUCLEUS - The current user is a macro not a constant */

⌨️ 快捷键说明

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