Skip to content

Commit 33f2259

Browse files
committed
adf_file: write mode improved - writing the current data block only if changed (optimized adfFileSeek and adfFileWrite).
1 parent fccb8e5 commit 33f2259

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/adf_file.c

+13-3
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,10 @@ RETCODE adfFileSeek ( struct AdfFile * const file,
281281
return RC_OK;
282282
}
283283

284-
if ( file->writeMode )
284+
if ( file->writeMode && file->currentDataBlockChanged ) {
285285
adfFileFlush ( file );
286+
file->currentDataBlockChanged = FALSE;
287+
}
286288

287289
if ( pos == 0 )
288290
return adfFileSeekStart ( file );
@@ -422,6 +424,7 @@ struct AdfFile * adfFileOpen ( struct AdfVolume * const vol,
422424
file->currentExt = NULL;
423425
file->nDataBlock = 0;
424426
file->curDataPtr = 0;
427+
file->currentDataBlockChanged = FALSE;
425428

426429
if ( mode_read ) {
427430
memcpy ( file->fileHdr, &entry, sizeof ( struct bFileHeaderBlock ) );
@@ -527,6 +530,7 @@ uint32_t adfFileRead ( struct AdfFile * const file,
527530
return bytesRead;
528531
}
529532
file->posInDataBlk = 0;
533+
file->currentDataBlockChanged = FALSE;
530534
}
531535

532536
unsigned size = min ( n - bytesRead, blockSize - file->posInDataBlk );
@@ -656,7 +660,9 @@ uint32_t adfFileWrite ( struct AdfFile * const file,
656660

657661
if ( file->pos == file->fileHdr->byteSize ) { // at EOF ?
658662
// ... create a new block
659-
if ( adfFileCreateNextBlock ( file ) == -1 ) {
663+
RETCODE rc = adfFileCreateNextBlock ( file );
664+
file->currentDataBlockChanged = FALSE;
665+
if ( rc == -1 ) {
660666
/* bug found by Rikard */
661667
adfEnv.wFct ( "adfWritefile : no more free sectors available" );
662668
//file->curDataPtr = 0; // invalidate data ptr
@@ -667,7 +673,10 @@ uint32_t adfFileWrite ( struct AdfFile * const file,
667673
// inside the existing data (at the end of a data block )
668674

669675
// write the block stored currently in the memory
670-
adfFileFlush ( file ); // to optimize (?)
676+
if ( file->currentDataBlockChanged ) {
677+
adfFileFlush ( file ); // to optimize (?)
678+
file->currentDataBlockChanged = FALSE;
679+
}
671680

672681
// - and read the next block
673682
RETCODE rc = adfFileReadNextBlock ( file );
@@ -689,6 +698,7 @@ uint32_t adfFileWrite ( struct AdfFile * const file,
689698
file->pos += size;
690699
bytesWritten += size;
691700
file->posInDataBlk += size;
701+
file->currentDataBlockChanged = TRUE;
692702

693703
// update file size in the header
694704
file->fileHdr->byteSize = max ( file->fileHdr->byteSize,

src/adf_file.h

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ struct AdfFile {
4848
unsigned posInDataBlk;
4949
unsigned posInExtBlk;
5050
BOOL writeMode;
51+
BOOL currentDataBlockChanged;
5152
};
5253

5354

0 commit comments

Comments
 (0)