From fc9b28d0a943033e7224c92c5a387e422dd469af Mon Sep 17 00:00:00 2001 From: Hardeep Sidhu Date: Tue, 3 Jun 2003 18:04:22 +0000 Subject: Small seek optimization -- begin seek at last read cluster if possible git-svn-id: svn://svn.rockbox.org/rockbox/trunk@3720 a1c6a512-1295-4272-9138-f99709370657 --- firmware/drivers/fat.c | 23 +++++++++++++++++++---- firmware/export/fat.h | 1 + 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/firmware/drivers/fat.c b/firmware/drivers/fat.c index 8f4e3e0632..ac6d240d8e 100644 --- a/firmware/drivers/fat.c +++ b/firmware/drivers/fat.c @@ -1218,6 +1218,7 @@ int fat_open(unsigned int startcluster, file->firstcluster = startcluster; file->lastcluster = startcluster; file->lastsector = 0; + file->clusternum = 0; file->sectornum = 0; file->eof = false; @@ -1243,6 +1244,7 @@ int fat_create_file(char* name, file->firstcluster = 0; file->lastcluster = 0; file->lastsector = 0; + file->clusternum = 0; file->sectornum = 0; file->eof = false; } @@ -1508,6 +1510,7 @@ int fat_readwrite( struct fat_file *file, int sectorcount, { int cluster = file->lastcluster; int sector = file->lastsector; + int clusternum = file->clusternum; int numsec = file->sectornum; bool eof = file->eof; int first=0, last=0; @@ -1533,17 +1536,21 @@ int fat_readwrite( struct fat_file *file, int sectorcount, cluster = get_next_cluster(cluster); sector = cluster2sec(cluster); } + + clusternum++; + numsec=1; + if (!cluster) { eof = true; if ( write ) { /* remember last cluster, in case we want to append to the file */ cluster = oldcluster; + clusternum--; } } else eof = false; - numsec=1; } else { if (sector) @@ -1583,6 +1590,7 @@ int fat_readwrite( struct fat_file *file, int sectorcount, file->lastcluster = cluster; file->lastsector = sector; + file->clusternum = clusternum; file->sectornum = numsec; file->eof = eof; @@ -1595,7 +1603,7 @@ int fat_readwrite( struct fat_file *file, int sectorcount, int fat_seek(struct fat_file *file, unsigned int seeksector ) { - int clusternum=0, sectornum=0, sector=0; + int clusternum=0, numclusters=0, sectornum=0, sector=0; int cluster = file->firstcluster; int i; @@ -1604,10 +1612,16 @@ int fat_seek(struct fat_file *file, unsigned int seeksector ) /* we need to find the sector BEFORE the requested, since the file struct stores the last accessed sector */ seeksector--; - clusternum = seeksector / fat_bpb.bpb_secperclus; + numclusters = clusternum = seeksector / fat_bpb.bpb_secperclus; sectornum = seeksector % fat_bpb.bpb_secperclus; - for (i=0; iclusternum && clusternum >= file->clusternum) + { + cluster = file->lastcluster; + numclusters -= file->clusternum; + } + + for (i=0; ilastcluster = cluster; file->lastsector = sector; + file->clusternum = clusternum; file->sectornum = sectornum + 1; return 0; } diff --git a/firmware/export/fat.h b/firmware/export/fat.h index ec71fceb05..921f04eb8d 100644 --- a/firmware/export/fat.h +++ b/firmware/export/fat.h @@ -51,6 +51,7 @@ struct fat_file int firstcluster; /* first cluster in file */ int lastcluster; /* cluster of last access */ int lastsector; /* sector of last access */ + int clusternum; /* current clusternum */ int sectornum; /* sector number in this cluster */ unsigned int direntry; /* short dir entry index from start of dir */ unsigned int direntries; /* number of dir entries used by this file */ -- cgit v1.2.3