Skip to content

Commit 5857539

Browse files
committed
Add source tracking for cfiles
1 parent 1253575 commit 5857539

File tree

3 files changed

+55
-21
lines changed

3 files changed

+55
-21
lines changed

code/cfile/cfile.cpp

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,32 @@ const char *Cfile_cdrom_dir = NULL;
105105
// Function prototypes for internally-called functions
106106
//
107107
int cfget_cfile_block();
108-
CFILE *cf_open_fill_cfblock(FILE * fp, int type);
109-
CFILE *cf_open_packed_cfblock(FILE *fp, int type, int offset, int size);
108+
CFILE *cf_open_fill_cfblock(const char* source, int line, FILE * fp, int type);
109+
CFILE *cf_open_packed_cfblock(const char* source, int line, FILE *fp, int type, int offset, int size);
110110

111111
#if defined _WIN32
112-
CFILE *cf_open_mapped_fill_cfblock(HANDLE hFile, int type);
112+
CFILE *cf_open_mapped_fill_cfblock(const char* source, int line, HANDLE hFile, int type);
113113
#elif defined SCP_UNIX
114-
CFILE *cf_open_mapped_fill_cfblock(FILE *fp, int type);
114+
CFILE *cf_open_mapped_fill_cfblock(const char* source, int line, FILE *fp, int type);
115115
#endif
116116

117117
void cf_chksum_long_init();
118118

119+
static void dump_opened_files()
120+
{
121+
for (int i = 0; i < MAX_CFILE_BLOCKS; i++) {
122+
auto cb = &Cfile_block_list[i];
123+
if (cb->type != CFILE_BLOCK_UNUSED) {
124+
mprintf((" %s:%d\n", cb->source_file, cb->line_num));
125+
}
126+
}
127+
}
128+
119129
void cfile_close()
120130
{
131+
mprintf(("Still opened files:\n"));
132+
dump_opened_files();
133+
121134
cf_free_secondary_filelist();
122135
}
123136

@@ -631,7 +644,7 @@ extern int game_cd_changed();
631644
// error ==> NULL
632645
//
633646

634-
CFILE *cfopen(const char *file_path, const char *mode, int type, int dir_type, bool localize)
647+
CFILE *_cfopen(const char* source, int line, const char *file_path, const char *mode, int type, int dir_type, bool localize)
635648
{
636649
/* Bobboau, what is this doing here? 31 is way too short... - Goober5000
637650
if( strlen(file_path) > 31 )
@@ -716,7 +729,7 @@ CFILE *cfopen(const char *file_path, const char *mode, int type, int dir_type, b
716729

717730
FILE *fp = fopen(longname, happy_mode);
718731
if (fp) {
719-
return cf_open_fill_cfblock(fp, dir_type);
732+
return cf_open_fill_cfblock(source, line, fp, dir_type);
720733
}
721734
return NULL;
722735
}
@@ -745,12 +758,12 @@ CFILE *cfopen(const char *file_path, const char *mode, int type, int dir_type, b
745758
hFile = CreateFile(longname, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
746759

747760
if (hFile != INVALID_HANDLE_VALUE) {
748-
return cf_open_mapped_fill_cfblock(hFile, dir_type);
761+
return cf_open_mapped_fill_cfblock(source, line, hFile, dir_type);
749762
}
750763
#elif defined SCP_UNIX
751764
FILE *fp = fopen( longname, "rb" );
752765
if (fp) {
753-
return cf_open_mapped_fill_cfblock(fp, dir_type);
766+
return cf_open_mapped_fill_cfblock(source, line, fp, dir_type);
754767
}
755768
#endif
756769
}
@@ -762,10 +775,10 @@ CFILE *cfopen(const char *file_path, const char *mode, int type, int dir_type, b
762775
if ( fp ) {
763776
if ( offset ) {
764777
// Found it in a pack file
765-
return cf_open_packed_cfblock(fp, dir_type, offset, size );
778+
return cf_open_packed_cfblock(source, line, fp, dir_type, offset, size );
766779
} else {
767780
// Found it in a normal file
768-
return cf_open_fill_cfblock(fp, dir_type);
781+
return cf_open_fill_cfblock(source, line, fp, dir_type);
769782
}
770783
}
771784
}
@@ -786,7 +799,7 @@ CFILE *cfopen(const char *file_path, const char *mode, int type, int dir_type, b
786799
// returns: success ==> address of CFILE structure
787800
// error ==> NULL
788801
//
789-
CFILE *cfopen_special(const char *file_path, const char *mode, const int size, const int offset, int dir_type)
802+
CFILE *_cfopen_special(const char* source, int line, const char *file_path, const char *mode, const int size, const int offset, int dir_type)
790803
{
791804
if ( !cfile_inited) {
792805
Int3();
@@ -811,10 +824,10 @@ CFILE *cfopen_special(const char *file_path, const char *mode, const int size, c
811824

812825
if ( offset ) {
813826
// it's in a pack file
814-
return cf_open_packed_cfblock(fp, dir_type, offset, size);
827+
return cf_open_packed_cfblock(source, line, fp, dir_type, offset, size);
815828
} else {
816829
// it's a normal file
817-
return cf_open_fill_cfblock(fp, dir_type);
830+
return cf_open_fill_cfblock(source, line, fp, dir_type);
818831
}
819832
}
820833

@@ -833,7 +846,7 @@ CFILE *ctmpfile()
833846
FILE *fp;
834847
fp = tmpfile();
835848
if ( fp )
836-
return cf_open_fill_cfblock(fp, 0);
849+
return cf_open_fill_cfblock(LOCATION, fp, 0);
837850
else
838851
return NULL;
839852
}
@@ -863,7 +876,12 @@ int cfget_cfile_block()
863876

864877
// If we've reached this point, a free Cfile_block could not be found
865878
nprintf(("Warning","A free Cfile_block could not be found.\n"));
866-
Assert(0); // out of free cfile blocks
879+
880+
// Dump a list of all opened files
881+
mprintf(("Out of cfile blocks! Currently opened files:\n"));
882+
883+
Assertion(false, "There are no more free cfile blocks. This means that there are too many files opened by FSO.\n"
884+
"This is probably caused by a programming or scripting error where a file does not get closed."); // out of free cfile blocks
867885
return -1;
868886
}
869887

@@ -941,7 +959,7 @@ int cf_is_valid(CFILE *cfile)
941959
// returns: success ==> ptr to CFILE structure.
942960
// error ==> NULL
943961
//
944-
CFILE *cf_open_fill_cfblock(FILE *fp, int type)
962+
CFILE *cf_open_fill_cfblock(const char* source, int line, FILE *fp, int type)
945963
{
946964
int cfile_block_index;
947965

@@ -960,6 +978,9 @@ CFILE *cf_open_fill_cfblock(FILE *fp, int type)
960978
cfbp->fp = fp;
961979
cfbp->dir_type = type;
962980
cfbp->max_read_len = 0;
981+
982+
cfbp->source_file = source;
983+
cfbp->line_num = line;
963984

964985
int pos = ftell(fp);
965986
if(pos == -1L)
@@ -977,7 +998,7 @@ CFILE *cf_open_fill_cfblock(FILE *fp, int type)
977998
// returns: success ==> ptr to CFILE structure.
978999
// error ==> NULL
9791000
//
980-
CFILE *cf_open_packed_cfblock(FILE *fp, int type, int offset, int size)
1001+
CFILE *cf_open_packed_cfblock(const char* source, int line, FILE *fp, int type, int offset, int size)
9811002
{
9821003
// Found it in a pack file
9831004
int cfile_block_index;
@@ -999,6 +1020,9 @@ CFILE *cf_open_packed_cfblock(FILE *fp, int type, int offset, int size)
9991020
cfbp->dir_type = type;
10001021
cfbp->max_read_len = 0;
10011022

1023+
cfbp->source_file = source;
1024+
cfbp->line_num = line;
1025+
10021026
cf_init_lowlevel_read_code(cfp,offset, size, 0 );
10031027

10041028
return cfp;
@@ -1014,9 +1038,9 @@ CFILE *cf_open_packed_cfblock(FILE *fp, int type, int offset, int size)
10141038
// returns: ptr CFILE structure.
10151039
//
10161040
#if defined _WIN32
1017-
CFILE *cf_open_mapped_fill_cfblock(HANDLE hFile, int type)
1041+
CFILE *cf_open_mapped_fill_cfblock(const char* source, int line, HANDLE hFile, int type)
10181042
#elif defined SCP_UNIX
1019-
CFILE *cf_open_mapped_fill_cfblock(FILE *fp, int type)
1043+
CFILE *cf_open_mapped_fill_cfblock(const char* source, int line, FILE *fp, int type)
10201044
#endif
10211045
{
10221046
int cfile_block_index;
@@ -1043,6 +1067,9 @@ CFILE *cf_open_mapped_fill_cfblock(FILE *fp, int type)
10431067
#endif
10441068
cfbp->dir_type = type;
10451069

1070+
cfbp->source_file = source;
1071+
cfbp->line_num = line;
1072+
10461073
cf_init_lowlevel_read_code(cfp, 0, 0, 0 );
10471074
#if defined _WIN32
10481075
cfbp->hMapFile = CreateFileMapping(cfbp->hInFile, NULL, PAGE_READONLY, 0, 0, NULL);

code/cfile/cfile.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,15 @@ int cf_get_dir_type(CFILE *cfile);
129129

130130
// Opens the file. If no path is given, use the extension to look into the
131131
// default path. If mode is NULL, delete the file.
132-
CFILE *cfopen(const char *filename, const char *mode, int type = CFILE_NORMAL, int dir_type = CF_TYPE_ANY, bool localize = false);
132+
CFILE *_cfopen(const char* source_file, int line, const char *filename, const char *mode,
133+
int type = CFILE_NORMAL, int dir_type = CF_TYPE_ANY, bool localize = false);
134+
#define cfopen(...) _cfopen(LOCATION, __VA_ARGS__) // Pass source location to the function
133135

134136
// like cfopen(), but it accepts a fully qualified path only (ie, the result of a cf_find_file_location() call)
135137
// NOTE: only supports reading files!!
136-
CFILE *cfopen_special(const char *file_path, const char *mode, const int size, const int offset, int dir_type = CF_TYPE_ANY);
138+
CFILE *_cfopen_special(const char* source_file, int line, const char *file_path, const char *mode,
139+
const int size, const int offset, int dir_type = CF_TYPE_ANY);
140+
#define cfopen_special(...) _cfopen_special(LOCATION, __VA_ARGS__) // Pass source location to the function
137141

138142
// Flush the open file buffer
139143
int cflush(CFILE *cfile);

code/cfile/cfilearchive.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ typedef struct Cfile_block {
3939
int size; // for packed files
4040

4141
size_t max_read_len; // max read offset, for special error handling
42+
43+
const char* source_file;
44+
int line_num;
4245
} Cfile_block;
4346

4447
#define MAX_CFILE_BLOCKS 64

0 commit comments

Comments
 (0)