Skip to content

Commit 552f999

Browse files
remove support for memory mapped files (#6748)
FSO doesn't appear to use this any longer. And even when it was enabled the requirements made it largely useless anyway. Removing to clean up cfile a bit and so we don't have to keep maintaining it.
1 parent 9e9fc22 commit 552f999

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+81
-249
lines changed

code/anim/animplay.cpp

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -667,12 +667,10 @@ void anim_read_header(anim *ptr, CFILE *fp)
667667
*
668668
* @param real_filename Filename of animation
669669
* @param cf_dir_type
670-
* @param file_mapped Whether to use memory-mapped file or not.
671-
*
672-
* @details Memory-mapped files will page in the animation from disk as it is needed, but performance is not as good.
670+
*
673671
* @return Pointer to anim that is loaded if success, NULL if failure.
674672
*/
675-
anim *anim_load(const char *real_filename, int cf_dir_type, int file_mapped)
673+
anim *anim_load(const char *real_filename, int cf_dir_type)
676674
{
677675
anim *ptr;
678676
CFILE *fp;
@@ -697,7 +695,7 @@ anim *anim_load(const char *real_filename, int cf_dir_type, int file_mapped)
697695
}
698696

699697
if (!ptr) {
700-
fp = cfopen(name, "rb", CFILE_NORMAL, cf_dir_type);
698+
fp = cfopen(name, "rb", cf_dir_type);
701699
if ( !fp )
702700
return NULL;
703701

@@ -743,18 +741,9 @@ anim *anim_load(const char *real_filename, int cf_dir_type, int file_mapped)
743741

744742
ptr->cfile_ptr = NULL;
745743

746-
if ( file_mapped == PAGE_FROM_MEM) {
747-
// Try mapping the file to memory
748-
ptr->flags |= ANF_MEM_MAPPED;
749-
ptr->cfile_ptr = cfopen(name, "rb", CFILE_MEMORY_MAPPED, cf_dir_type);
750-
}
751-
752-
// couldn't memory-map file... must be in a packfile, so stream manually
753-
if ( file_mapped == PAGE_FROM_MEM && !ptr->cfile_ptr ) {
754-
ptr->flags &= ~ANF_MEM_MAPPED;
755-
ptr->flags |= ANF_STREAMED;
756-
ptr->cfile_ptr = cfopen(name, "rb", CFILE_NORMAL, cf_dir_type);
757-
}
744+
// NOTE: mapped files no longer supported!!
745+
ptr->flags |= ANF_STREAMED;
746+
ptr->cfile_ptr = cfopen(name, "rb", cf_dir_type);
758747

759748
ptr->cache = NULL;
760749

code/anim/animplay.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,6 @@ typedef struct {
3838
int ping_pong;
3939
} anim_play_struct;
4040

41-
enum
42-
{
43-
PAGE_FROM_DISK = 0,
44-
PAGE_FROM_MEM = 1
45-
};
46-
4741
extern int Anim_paused;
4842

4943
void anim_init();
@@ -57,7 +51,7 @@ void anim_ignore_next_frametime();
5751
int anim_show_next_frame(anim_instance *instance, float frametime);
5852
void anim_release_all_instances(int screen_id = 0);
5953
void anim_release_render_instance(anim_instance* instance);
60-
anim *anim_load(const char *name, int cf_dir_type = CF_TYPE_ANY, int file_mapped = PAGE_FROM_DISK);
54+
anim *anim_load(const char *name, int cf_dir_type = CF_TYPE_ANY);
6155
int anim_free(anim *ptr);
6256
void anim_read_header(anim *ptr, CFILE *fp);
6357
void anim_reverse_direction(anim_instance *ai); // called automatically for ping-ponging, and can also be called externally

code/cfile/cfile.cpp

Lines changed: 6 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,10 @@
2020
#include <io.h>
2121
#include <direct.h>
2222
#include <windows.h>
23-
#include <winbase.h> /* needed for memory mapping of file functions */
2423
#endif
2524

2625
#ifdef SCP_UNIX
2726
#include <glob.h>
28-
#include <sys/mman.h>
2927
#endif
3028

3129
#include "cfile/cfile.h"
@@ -108,12 +106,6 @@ static CFILE *cf_open_fill_cfblock(const char* source, int line, const char* ori
108106
static CFILE *cf_open_packed_cfblock(const char* source, int line, const char* original_filename, FILE *fp, int type, size_t offset, size_t size);
109107
static CFILE *cf_open_memory_fill_cfblock(const char* source, int line, const char* original_filename, const void* data, size_t size, int dir_type);
110108

111-
#if defined _WIN32
112-
static CFILE *cf_open_mapped_fill_cfblock(const char* source, int line, const char* original_filename, HANDLE hFile, int type);
113-
#elif defined SCP_UNIX
114-
static CFILE *cf_open_mapped_fill_cfblock(const char* source, int line, const char* original_filename, FILE *fp, int type);
115-
#endif
116-
117109
static void cf_chksum_long_init();
118110

119111
static void dump_opened_files()
@@ -632,18 +624,13 @@ void cf_create_directory(int dir_type, uint32_t location_flags)
632624
// parameters: *filepath ==> name of file to open (may be path+name)
633625
// *mode ==> specifies how file should be opened (eg "rb" for read binary)
634626
// passing NULL to mode triggers an assert and returns NULL
635-
// type ==> one of: CFILE_NORMAL
636-
// CFILE_MEMORY_MAPPED
637627
// dir_type => override extension check, value is one of CF_TYPE* #defines
638628
//
639-
// NOTE: type parameter is an optional parameter. The default value is CFILE_NORMAL
640-
//
641-
//
642629
// returns: success ==> address of CFILE structure
643630
// error ==> NULL
644631
//
645632

646-
CFILE* _cfopen(const char* source, int line, const char* file_path, const char* mode, int type, int dir_type,
633+
CFILE* _cfopen(const char* source, int line, const char* file_path, const char* mode, int dir_type,
647634
bool /* localize */, uint32_t location_flags)
648635
{
649636
/* Bobboau, what is this doing here? 31 is way too short... - Goober5000
@@ -659,12 +646,6 @@ CFILE* _cfopen(const char* source, int line, const char* file_path, const char*
659646
// Check that all the parameters make sense
660647
Assert(file_path && strlen(file_path));
661648
Assert( mode != NULL );
662-
663-
// Can only open read-only binary files in memory mapped mode.
664-
if ( (type & CFILE_MEMORY_MAPPED) && strcmp(mode,"rb") != 0 ) {
665-
Int3();
666-
return NULL;
667-
}
668649

669650
//===========================================================
670651
// If in write mode, just try to open the file straight off
@@ -689,7 +670,6 @@ CFILE* _cfopen(const char* source, int line, const char* file_path, const char*
689670

690671
cf_create_default_path_string(longname, dir_type, file_path, location_flags);
691672
}
692-
Assert( !(type & CFILE_MEMORY_MAPPED) );
693673

694674
// JOHN: TODO, you should create the path if it doesn't exist.
695675

@@ -740,35 +720,11 @@ CFILE* _cfopen(const char* source, int line, const char* file_path, const char*
740720
auto find_res = cf_find_file_location(file_path, dir_type, location_flags);
741721

742722
if ( find_res.found ) {
743-
744723
// Fount it, now create a cfile out of it
745724
nprintf(("CFileDebug", "Requested file %s found at: %s\n", file_path, find_res.full_name.c_str()));
746725

747-
if ( type & CFILE_MEMORY_MAPPED ) {
748-
749-
// Can't open memory mapped files out of pack or memory files
750-
if ( find_res.offset == 0 && find_res.data_ptr != nullptr ) {
751-
#if defined _WIN32
752-
HANDLE hFile;
753-
754-
hFile = CreateFile(find_res.full_name.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
755-
756-
if (hFile != INVALID_HANDLE_VALUE) {
757-
return cf_open_mapped_fill_cfblock(source, line, file_path, hFile, dir_type);
758-
}
759-
#elif defined SCP_UNIX
760-
FILE* fp = fopen(find_res.full_name.c_str(), "rb");
761-
if (fp) {
762-
return cf_open_mapped_fill_cfblock(source, line, file_path, fp, dir_type);
763-
}
764-
#endif
765-
}
766-
767-
} else {
768-
// since cfopen_special already has all the code to handle the opening we can just use that here
769-
return _cfopen_special(source, line, find_res, mode, dir_type);
770-
}
771-
726+
// since cfopen_special already has all the code to handle the opening we can just use that here
727+
return _cfopen_special(source, line, find_res, mode, dir_type);
772728
}
773729

774730
return NULL;
@@ -893,27 +849,7 @@ int cfclose( CFILE * cfile )
893849
Assert(cfile != NULL);
894850

895851
result = 0;
896-
if ( cfile->data && cfile->mem_mapped ) {
897-
// close memory mapped file
898-
#if defined _WIN32
899-
result = UnmapViewOfFile((void*)cfile->data);
900-
Assert(result);
901-
result = CloseHandle(cfile->hInFile);
902-
Assert(result); // Ensure file handle is closed properly
903-
result = CloseHandle(cfile->hMapFile);
904-
Assert(result); // Ensure file handle is closed properly
905-
result = 0;
906-
#elif defined SCP_UNIX
907-
// FIXME: result is wrong after munmap() but it is successful
908-
//result = munmap(cfile->data, cfile->data_length);
909-
//Assert(result);
910-
// This const_cast is safe since the pointer returned by mmap was also non-const
911-
munmap(const_cast<void*>(cfile->data), cfile->data_length);
912-
if ( cfile->fp != nullptr)
913-
result = fclose(cfile->fp);
914-
#endif
915-
916-
} else if ( cfile->fp != nullptr ) {
852+
if ( cfile->fp != nullptr ) {
917853
Assert(cfile->fp != nullptr);
918854
result = fclose(cfile->fp);
919855
} else {
@@ -958,7 +894,6 @@ static CFILE *cf_open_fill_cfblock(const char* source, int line, const char* ori
958894
} else {
959895
CFILE *cfp = &Cfile_block_list[cfile_block_index];
960896
cfp->data = nullptr;
961-
cfp->mem_mapped = false;
962897
cfp->fp = fp;
963898
cfp->dir_type = type;
964899
cfp->max_read_len = 0;
@@ -997,7 +932,6 @@ static CFILE *cf_open_packed_cfblock(const char* source, int line, const char* o
997932

998933
cfp->data = nullptr;
999934
cfp->fp = fp;
1000-
cfp->mem_mapped = false;
1001935
cfp->dir_type = type;
1002936
cfp->max_read_len = 0;
1003937

@@ -1012,70 +946,6 @@ static CFILE *cf_open_packed_cfblock(const char* source, int line, const char* o
1012946

1013947
}
1014948

1015-
1016-
1017-
// cf_open_mapped_fill_cfblock() will fill up a Cfile_block element in the Cfile_block_list[] array
1018-
// for the case of a file being opened by cf_open_mapped();
1019-
//
1020-
// returns: ptr CFILE structure.
1021-
//
1022-
#if defined _WIN32
1023-
static CFILE *cf_open_mapped_fill_cfblock(const char* source, int line, const char* original_filename, HANDLE hFile, int type)
1024-
#elif defined SCP_UNIX
1025-
static CFILE *cf_open_mapped_fill_cfblock(const char* source, int line, const char* original_filename, FILE *fp, int type)
1026-
#endif
1027-
{
1028-
int cfile_block_index;
1029-
1030-
cfile_block_index = cfget_cfile_block();
1031-
if ( cfile_block_index == -1 ) {
1032-
#ifdef SCP_UNIX
1033-
fclose(fp);
1034-
#endif
1035-
return NULL;
1036-
}
1037-
else {
1038-
CFILE *cfp = &Cfile_block_list[cfile_block_index];
1039-
1040-
cfp->max_read_len = 0;
1041-
cfp->fp = nullptr;
1042-
cfp->mem_mapped = true;
1043-
#if defined _WIN32
1044-
cfp->hInFile = hFile;
1045-
#endif
1046-
cfp->dir_type = type;
1047-
1048-
cfp->original_filename = original_filename;
1049-
cfp->source_file = source;
1050-
cfp->line_num = line;
1051-
1052-
cf_init_lowlevel_read_code(cfp, 0, 0, 0 );
1053-
1054-
#if defined _WIN32
1055-
cfp->hMapFile = CreateFileMapping(cfp->hInFile, NULL, PAGE_READONLY, 0, 0, NULL);
1056-
if (cfp->hMapFile == NULL) {
1057-
nprintf(("Error", "Could not create file-mapping object.\n"));
1058-
return NULL;
1059-
}
1060-
1061-
cfp->data = (ubyte*)MapViewOfFile(cfp->hMapFile, FILE_MAP_READ, 0, 0, 0);
1062-
Assert( cfp->data != NULL );
1063-
#elif defined SCP_UNIX
1064-
cfp->fp = fp;
1065-
cfp->data_length = filelength(fileno(fp));
1066-
cfp->data = mmap(nullptr, // start
1067-
cfp->data_length, // length
1068-
PROT_READ, // prot
1069-
MAP_SHARED, // flags
1070-
fileno(fp), // fd
1071-
0); // offset
1072-
Assert(cfp->data != nullptr);
1073-
#endif
1074-
1075-
return cfp;
1076-
}
1077-
}
1078-
1079949
static CFILE *cf_open_memory_fill_cfblock(const char* source, int line, const char* original_filename, const void* data, size_t size, int dir_type)
1080950
{
1081951
int cfile_block_index;
@@ -1089,7 +959,6 @@ static CFILE *cf_open_memory_fill_cfblock(const char* source, int line, const ch
1089959

1090960
cfp->max_read_len = 0;
1091961
cfp->fp = nullptr;
1092-
cfp->mem_mapped = false;
1093962
cfp->dir_type = dir_type;
1094963

1095964
cfp->original_filename = original_filename;
@@ -1333,9 +1202,6 @@ int cfwrite_string_len(const char *buf, CFILE *file)
13331202
int cfilelength(CFILE* cfile) {
13341203
Assert(cfile != NULL);
13351204

1336-
// TODO: return length of memory mapped file
1337-
Assert(!cfile->mem_mapped);
1338-
13391205
// cfile->size gets set at cfopen
13401206

13411207
// The rest of the code still uses ints, do an overflow check to detect cases where this fails
@@ -1728,7 +1594,7 @@ int cf_chksum_short(const char *filename, ushort *chksum, int max_size, int cf_t
17281594
*chksum = 0;
17291595

17301596
// attempt to open the file
1731-
cfile = cfopen(filename,"rt",CFILE_NORMAL,cf_type);
1597+
cfile = cfopen(filename,"rt",cf_type);
17321598
if(cfile == NULL){
17331599
return 0;
17341600
}
@@ -1778,7 +1644,7 @@ int cf_chksum_long(const char *filename, uint *chksum, int max_size, int cf_type
17781644
*chksum = 0;
17791645

17801646
// attempt to open the file
1781-
cfile = cfopen(filename,"rt",CFILE_NORMAL,cf_type);
1647+
cfile = cfopen(filename,"rt",cf_type);
17821648
if(cfile == NULL){
17831649
return 0;
17841650
}

code/cfile/cfile.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,6 @@ typedef struct {
8585
// TRUE if type is specified and valid
8686
#define CF_TYPE_SPECIFIED(path_type) (((path_type)>CF_TYPE_INVALID) && ((path_type)<CF_MAX_PATH_TYPES))
8787

88-
// #define's for the type parameter in cfopen.
89-
#define CFILE_NORMAL 0 // open file normally
90-
#define CFILE_MEMORY_MAPPED (1<<0) // open file as a memory-mapped file
91-
9288
#define CF_SORT_NONE 0
9389
#define CF_SORT_NAME 1
9490
#define CF_SORT_TIME 2
@@ -181,7 +177,7 @@ int cf_get_dir_type(const CFILE *cfile);
181177

182178
// Opens the file. If no path is given, use the extension to look into the
183179
// default path. If mode is NULL, delete the file.
184-
CFILE* _cfopen(const char* source_file, int line, const char* filename, const char* mode, int type = CFILE_NORMAL,
180+
CFILE* _cfopen(const char* source_file, int line, const char* filename, const char* mode,
185181
int dir_type = CF_TYPE_ANY, bool localize = false, uint32_t location_flags = CF_LOCATION_ALL);
186182
#define cfopen(...) _cfopen(LOCATION, __VA_ARGS__) // Pass source location to the function
187183

code/cfile/cfilearchive.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,6 @@ int cfseek( CFILE *cfile, int offset, int where )
149149
if (cfile->compression_info.header != 0)
150150
return comp_fseek(cfile, offset, where);
151151

152-
// TODO: seek to offset in memory mapped file
153-
Assert( !cfile->mem_mapped );
154-
155152
size_t goal_position;
156153

157154
switch( where ) {

code/cfile/cfilearchive.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,6 @@ struct CFILE {
3838
int dir_type; // directory location
3939
FILE* fp; // File pointer if opening an individual file
4040
const void* data; // Pointer for memory-mapped file access. NULL if not mem-mapped.
41-
bool mem_mapped; // Flag for memory mapped files (if data is not null and this is false it means that it's an embedded file)
42-
#ifdef _WIN32
43-
HANDLE hInFile; // Handle from CreateFile()
44-
HANDLE hMapFile; // Handle from CreateFileMapping()
45-
#else
46-
size_t data_length; // length of data for mmap
47-
#endif
4841
size_t lib_offset;
4942
size_t raw_position;
5043
size_t size; // for packed files

code/controlconfig/controlsconfig.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1326,7 +1326,6 @@ bool control_config_accept(bool API_Access)
13261326
// Check if a preset file with name already exists. If so, prompt the user
13271327
CFILE* fp = cfopen((str + ".json").c_str(),
13281328
"r",
1329-
CFILE_NORMAL,
13301329
CF_TYPE_PLAYER_BINDS,
13311330
false,
13321331
CF_LOCATION_ROOT_USER | CF_LOCATION_ROOT_GAME | CF_LOCATION_TYPE_ROOT);

code/controlconfig/controlsconfigcommon.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1956,7 +1956,7 @@ int control_config_common_write_tbl(bool overwrite = false, bool all = false) {
19561956
return 1;
19571957
}
19581958

1959-
CFILE* cfile = cfopen("controlconfigdefaults.tbl", "w", CFILE_NORMAL, CF_TYPE_TABLES);
1959+
CFILE* cfile = cfopen("controlconfigdefaults.tbl", "w", CF_TYPE_TABLES);
19601960
if (cfile == nullptr) {
19611961
// Could not open. Bail.
19621962
return 1;

0 commit comments

Comments
 (0)