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

📄 fatfs_tcache.c

📁 该工程是从ecos嵌入式系统下移植过来的一个小型的fat16文件系统
💻 C
字号:
//==========================================================================////      fatfs_tcache.c////      FAT file system FAT table cache functions////==========================================================================//####ECOSGPLCOPYRIGHTBEGIN####// -------------------------------------------// This file is part of eCos, the Embedded Configurable Operating System.// Copyright (C) 2003 Savin Zlobec //// eCos is free software; you can redistribute it and/or modify it under// the terms of the GNU General Public License as published by the Free// Software Foundation; either version 2 or (at your option) any later version.//// eCos is distributed in the hope that it will be useful, but WITHOUT ANY// WARRANTY; without even the implied warranty of MERCHANTABILITY or// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License// for more details.//// You should have received a copy of the GNU General Public License along// with eCos; if not, write to the Free Software Foundation, Inc.,// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.//// As a special exception, if other files instantiate templates or use macros// or inline functions from this file, or you compile this file and link it// with other works to produce a work based on this file, this file does not// by itself cause the resulting work to be covered by the GNU General Public// License. However the source code for this file must still be made available// in accordance with section (3) of the GNU General Public License.//// This exception does not invalidate any other reasons why a work based on// this file might be covered by the GNU General Public License.//// -------------------------------------------//####ECOSGPLCOPYRIGHTEND####//==========================================================================//#####DESCRIPTIONBEGIN####//// Author(s):           savin // Date:                2003-06-27////####DESCRIPTIONEND####////==========================================================================#include "cyg_type.h"#include "cyg_ass.h"#include "types.h"#include "fatfs.h"#include "stdio.h"#include "malloc.h"//==========================================================================// Defines & macros #define TC_INC FATFS_FAT_TABLE_CACHE_INCREMENT#ifdef CYGDBG_USE_ASSERTS# define USE_ASSERTS 1#endif#ifdef FATFS_TRACE_FAT_TABLE_CACHE# define TTC 1#else# define TTC 0#endif//==========================================================================// Private functions //--------------------------------------------------------------------------// tcache_increment()// Allocates or reallocates memory for FAT table cache.// Returns true is allocation succeded. static bool tcache_increment(fatfs_disk_t *disk, fatfs_tcache_t *tcache){    printf("max_size=%d size=%d", tcache->max_size, tcache->size);    if (NULL == tcache->clusters)    {        tcache->clusters =             (cyg_uint32 *)malloc( TC_INC*sizeof(cyg_uint32));        if (NULL == tcache->clusters)            return false;        tcache->max_size = TC_INC;    }    else    {        cyg_uint32 *newc;        newc = (cyg_uint32 *)malloc((tcache->max_size+TC_INC)*sizeof(cyg_uint32));        if (NULL == newc)            return false;        memcpy(newc, tcache->clusters, (tcache->max_size*sizeof(cyg_uint32)));        free(tcache->clusters);                tcache->clusters = newc;        tcache->max_size += TC_INC;    }    printf( "max_size=%d size=%d", tcache->max_size, tcache->size);    return true;}//==========================================================================// Exported functions //--------------------------------------------------------------------------// fatfs_tcache_create()// Creates FAT table caches memory pool.int  fatfs_tcache_create(fatfs_disk_t *disk, cyg_uint32 mem_size){    return ENOERR;}//--------------------------------------------------------------------------// fatfs_tcache_delete()// Deletes FAT table caches memory pool.void fatfs_tcache_delete(fatfs_disk_t *disk){	return;}//--------------------------------------------------------------------------// fatfs_tcache_init()// Initializes FAT table cache structure.voidfatfs_tcache_init(fatfs_disk_t *disk, fatfs_tcache_t *tcache){    CYG_CHECK_DATA_PTRC(tcache);    tcache->clusters = NULL;    tcache->size     = 0;    tcache->max_size = 0;}//--------------------------------------------------------------------------// fatfs_tcache_flush()// Frees given tcache.voidfatfs_tcache_flush(fatfs_disk_t *disk, fatfs_tcache_t *tcache){    CYG_CHECK_DATA_PTRC(tcache);    if (tcache->clusters != NULL)    {        free(tcache->clusters);        tcache->clusters = NULL;            tcache->size     = 0;        tcache->max_size = 0;    }}//--------------------------------------------------------------------------// fatfs_tcache_get()// Gets the cluster from cache at given position.// Returns true if cluster in cache. boolfatfs_tcache_get(fatfs_disk_t   *disk,                 fatfs_tcache_t *tcache,                  cyg_uint32      num,                  cyg_uint32     *cluster){    CYG_CHECK_DATA_PTRC(tcache);    printf( "size=%d max_size=%d", tcache->size, tcache->max_size);        // Check if requested cluster is cached    if (num >= tcache->size)    {        printf( "cluster num=%d not in tcache", num);        return false;    }        *cluster = tcache->clusters[num];      printf( "got cluster=%d num=%d from tcache", *cluster, num);    return true;}//--------------------------------------------------------------------------// fatfs_tcache_get()// Gets last cluster in cache.// Returns false if cache is empty.boolfatfs_tcache_get_last(fatfs_disk_t   *disk,                      fatfs_tcache_t *tcache,                       cyg_uint32     *num,                       cyg_uint32     *cluster){    CYG_CHECK_DATA_PTRC(tcache);    printf( "size=%d max_size=%d", tcache->size, tcache->max_size);        // Check if we have something in cache    if (tcache->size == 0)        return false;       *num = tcache->size - 1;     *cluster = tcache->clusters[*num];      printf( "got last cluster=%d num=%d from tcache", *cluster, *num);    return true;}//--------------------------------------------------------------------------// fatfs_tcache_set()// Sets given cluster in cache at given position.// There can't be any holes in cache (ie in order to set cluster// at position N all clusters from 0 to N-1 must be set before)// Returns true if set succeded.boolfatfs_tcache_set(fatfs_disk_t   *disk,                 fatfs_tcache_t *tcache,                  cyg_uint32      num,                  cyg_uint32      cluster){    CYG_CHECK_DATA_PTRC(tcache);    printf( "num=%d cluster=%d tcache size=%d max_size=%d",         num, cluster, tcache->size, tcache->max_size);    if (num > tcache->size)    {        printf( "won't make a hole in tcache");        return false;    }        if (num < tcache->size)    {        printf( "set cluster=%d num=%d in tcache", cluster, num);        tcache->clusters[num] = cluster;        return true;    }    // Here num is equal to size - check if we need to increment cache     if (tcache->size == tcache->max_size)    {        // If we can't get memory to increment the cache        // try to flush dead nodes fat table cache        if (!tcache_increment(disk, tcache))        {            fatfs_node_flush_dead_tcache(disk);             if (!tcache_increment(disk, tcache))                return false;        }    }        tcache->size++;    tcache->clusters[num] = cluster;    printf( "added cluster=%d num=%d to tcache", cluster, num);    return true;}// -------------------------------------------------------------------------// EOF fatfs_tcache.c

⌨️ 快捷键说明

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