1
0
Fork 0
forked from len0rd/rockbox

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
This commit is contained in:
Hardeep Sidhu 2003-06-03 18:04:22 +00:00
parent a3216f88d6
commit fc9b28d0a9
2 changed files with 20 additions and 4 deletions

View file

@ -1218,6 +1218,7 @@ int fat_open(unsigned int startcluster,
file->firstcluster = startcluster; file->firstcluster = startcluster;
file->lastcluster = startcluster; file->lastcluster = startcluster;
file->lastsector = 0; file->lastsector = 0;
file->clusternum = 0;
file->sectornum = 0; file->sectornum = 0;
file->eof = false; file->eof = false;
@ -1243,6 +1244,7 @@ int fat_create_file(char* name,
file->firstcluster = 0; file->firstcluster = 0;
file->lastcluster = 0; file->lastcluster = 0;
file->lastsector = 0; file->lastsector = 0;
file->clusternum = 0;
file->sectornum = 0; file->sectornum = 0;
file->eof = false; file->eof = false;
} }
@ -1508,6 +1510,7 @@ int fat_readwrite( struct fat_file *file, int sectorcount,
{ {
int cluster = file->lastcluster; int cluster = file->lastcluster;
int sector = file->lastsector; int sector = file->lastsector;
int clusternum = file->clusternum;
int numsec = file->sectornum; int numsec = file->sectornum;
bool eof = file->eof; bool eof = file->eof;
int first=0, last=0; int first=0, last=0;
@ -1533,17 +1536,21 @@ int fat_readwrite( struct fat_file *file, int sectorcount,
cluster = get_next_cluster(cluster); cluster = get_next_cluster(cluster);
sector = cluster2sec(cluster); sector = cluster2sec(cluster);
} }
clusternum++;
numsec=1;
if (!cluster) { if (!cluster) {
eof = true; eof = true;
if ( write ) { if ( write ) {
/* remember last cluster, in case /* remember last cluster, in case
we want to append to the file */ we want to append to the file */
cluster = oldcluster; cluster = oldcluster;
clusternum--;
} }
} }
else else
eof = false; eof = false;
numsec=1;
} }
else { else {
if (sector) if (sector)
@ -1583,6 +1590,7 @@ int fat_readwrite( struct fat_file *file, int sectorcount,
file->lastcluster = cluster; file->lastcluster = cluster;
file->lastsector = sector; file->lastsector = sector;
file->clusternum = clusternum;
file->sectornum = numsec; file->sectornum = numsec;
file->eof = eof; 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 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 cluster = file->firstcluster;
int i; 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 /* we need to find the sector BEFORE the requested, since
the file struct stores the last accessed sector */ the file struct stores the last accessed sector */
seeksector--; seeksector--;
clusternum = seeksector / fat_bpb.bpb_secperclus; numclusters = clusternum = seeksector / fat_bpb.bpb_secperclus;
sectornum = seeksector % fat_bpb.bpb_secperclus; sectornum = seeksector % fat_bpb.bpb_secperclus;
for (i=0; i<clusternum; i++) { if (file->clusternum && clusternum >= file->clusternum)
{
cluster = file->lastcluster;
numclusters -= file->clusternum;
}
for (i=0; i<numclusters; i++) {
cluster = get_next_cluster(cluster); cluster = get_next_cluster(cluster);
if (!cluster) { if (!cluster) {
DEBUGF("Seeking beyond the end of the file! " DEBUGF("Seeking beyond the end of the file! "
@ -1627,6 +1641,7 @@ int fat_seek(struct fat_file *file, unsigned int seeksector )
file->lastcluster = cluster; file->lastcluster = cluster;
file->lastsector = sector; file->lastsector = sector;
file->clusternum = clusternum;
file->sectornum = sectornum + 1; file->sectornum = sectornum + 1;
return 0; return 0;
} }

View file

@ -51,6 +51,7 @@ struct fat_file
int firstcluster; /* first cluster in file */ int firstcluster; /* first cluster in file */
int lastcluster; /* cluster of last access */ int lastcluster; /* cluster of last access */
int lastsector; /* sector of last access */ int lastsector; /* sector of last access */
int clusternum; /* current clusternum */
int sectornum; /* sector number in this cluster */ int sectornum; /* sector number in this cluster */
unsigned int direntry; /* short dir entry index from start of dir */ unsigned int direntry; /* short dir entry index from start of dir */
unsigned int direntries; /* number of dir entries used by this file */ unsigned int direntries; /* number of dir entries used by this file */