From 3ad66b95fb2623e9d38cd7454ef7e7b492b6d6e9 Mon Sep 17 00:00:00 2001 From: Björn Stenberg Date: Wed, 29 May 2002 11:56:42 +0000 Subject: FAT cache now uses static memory git-svn-id: svn://svn.rockbox.org/rockbox/trunk@789 a1c6a512-1295-4272-9138-f99709370657 --- firmware/drivers/fat.c | 53 +++++++++++++++++--------------------------------- 1 file changed, 18 insertions(+), 35 deletions(-) (limited to 'firmware/drivers/fat.c') diff --git a/firmware/drivers/fat.c b/firmware/drivers/fat.c index 9a81107725..88d13c3eab 100644 --- a/firmware/drivers/fat.c +++ b/firmware/drivers/fat.c @@ -24,7 +24,7 @@ #include #include #include - +#include #include "fat.h" #include "ata.h" #include "debug.h" @@ -132,12 +132,13 @@ struct bpb fat_bpb; struct fat_cache_entry { - unsigned char *ptr; int secnum; - int dirty; + bool inuse; + bool dirty; }; -struct fat_cache_entry fat_cache[FAT_CACHE_SIZE]; +static char fat_cache_sectors[FAT_CACHE_SIZE][SECTOR_SIZE]; +static struct fat_cache_entry fat_cache[FAT_CACHE_SIZE]; /* sectors cache for longname use */ static unsigned char lastsector[SECTOR_SIZE]; @@ -182,16 +183,11 @@ int fat_mount(int startsector) int countofclusters; int i; - /* Clear the cache. Be aware! The bss section MUST have been cleared - at boot. Otherwise we will free() garbage pointers here */ for(i = 0;i < FAT_CACHE_SIZE;i++) { - if(fat_cache[i].ptr) - { - free(fat_cache[i].ptr); - } fat_cache[i].secnum = 8; /* We use a "safe" sector just in case */ - fat_cache[i].dirty = 0; + fat_cache[i].inuse = false; + fat_cache[i].dirty = false; } /* Read the sector */ @@ -329,12 +325,10 @@ static int bpb_is_sane(void) static void *cache_fat_sector(int secnum) { int cache_index = secnum & FAT_CACHE_MASK; - unsigned char *sec; - - sec = fat_cache[cache_index].ptr; /* Delete the cache entry if it isn't the sector we want */ - if(sec && fat_cache[cache_index].secnum != secnum) + if(fat_cache[cache_index].inuse && + fat_cache[cache_index].secnum != secnum) { #ifdef WRITE /* Write back if it is dirty */ @@ -347,36 +341,25 @@ static void *cache_fat_sector(int secnum) secnum); } } -#endif - free(sec); - - fat_cache[cache_index].ptr = NULL; fat_cache[cache_index].secnum = 8; /* Normally an unused sector */ - fat_cache[cache_index].dirty = 0; - sec = NULL; + fat_cache[cache_index].dirty = false; +#endif + fat_cache[cache_index].inuse = false; } /* Load the sector if it is not cached */ - if(!sec) + if(!fat_cache[cache_index].inuse) { - sec = malloc(fat_bpb.bpb_bytspersec); - if(!sec) - { - DEBUGF( "cache_fat_sector() - Out of memory\n"); - return NULL; - } - if(ata_read_sectors(secnum + fat_bpb.startsector,1,sec)) + if(ata_read_sectors(secnum + fat_bpb.startsector,1, + fat_cache_sectors[cache_index])) { - DEBUGF( "cache_fat_sector() - Could" - " not read sector %d\n", - secnum); - free(sec); + DEBUGF( "cache_fat_sector() - Could not read sector %d\n", secnum); return NULL; } - fat_cache[cache_index].ptr = sec; + fat_cache[cache_index].inuse = true; fat_cache[cache_index].secnum = secnum; } - return sec; + return fat_cache_sectors[cache_index]; } #ifdef DISK_WRITE -- cgit v1.2.3