FILECACH.CPP 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. ** Command & Conquer Red Alert(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***************************************************************************
  19. ** C O N F I D E N T I A L --- W E S T W O O D A S S O C I A T E S **
  20. ***************************************************************************
  21. * *
  22. * Project Name : Library - File Caching routines *
  23. * *
  24. * File Name : FILECACH.CPP *
  25. * *
  26. * Programmer : Scott K. Bowen *
  27. * *
  28. * Start Date : September 13, 1993 *
  29. * *
  30. * Last Update : April 18, 1994 [SKB] *
  31. * *
  32. *-------------------------------------------------------------------------*
  33. * Functions: *
  34. * Make_File_Resident -- Makes a file resident even if not flaged so. *
  35. * Flush_Unused_File_Cache -- Flushes the file cache of any non opened fi*
  36. * Free_Resident_File -- Free the given file if it is resident. *
  37. * Unfragment_File_Cache -- Does a garbage collection on the file heap. *
  38. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  39. #ifndef WWSTD_H
  40. #include <wwstd.h>
  41. #endif
  42. #ifndef _FILE_H
  43. #include "_file.h"
  44. #endif
  45. #ifndef WWMEM_H
  46. #include <wwmem.h>
  47. #endif
  48. #include <string.h>
  49. /*=========================================================================*/
  50. /* The following PRIVATE functions are in this file: */
  51. /*=========================================================================*/
  52. /*= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =*/
  53. /***************************************************************************
  54. * UNFRAGMENT_FILE_CACHE -- Does a garbage collection on the file heap. *
  55. * *
  56. * INPUT: NONE. *
  57. * *
  58. * OUTPUT: NONE. *
  59. * *
  60. * WARNINGS: Can be a lengthy process. *
  61. * *
  62. * HISTORY: *
  63. * 04/18/1994 SKB : Created. *
  64. *=========================================================================*/
  65. VOID Unfragment_File_Cache(VOID)
  66. {
  67. FileDataType *filedata;
  68. FileDataType *parent;
  69. UWORD idx;
  70. // Let the memory system clean up the file heap.
  71. Mem_Cleanup(FileCacheHeap);
  72. // Now get our pointers back.
  73. // Start after the parent PAK files since we will need to check our pointers
  74. // with them.
  75. filedata = &FileDataPtr[NumPAKFiles];
  76. for (idx = NumPAKFiles; idx < NumPAKFiles; idx++, filedata++) {
  77. while (filedata->Name) {
  78. // Only process files that are in the file cache.
  79. if (filedata->Ptr) {
  80. // Is a inner PAK file?
  81. if (filedata->Flag & FILEF_PACKED) {
  82. parent = &FileDataPtr[filedata->Disk];
  83. // Is it just a copied pointer of the parent?
  84. if (parent->Ptr == filedata->Ptr) {
  85. filedata->Ptr = Mem_Find(FileCacheHeap, filedata->Disk);
  86. }
  87. else
  88. filedata->Ptr = Mem_Find(FileCacheHeap, idx);
  89. }
  90. }
  91. else {
  92. filedata->Ptr = Mem_Find(FileCacheHeap, idx);
  93. }
  94. }
  95. }
  96. // Now that the children have been taken care of, let us do the parents.
  97. for (filedata = FileDataPtr, idx = 0; idx < NumPAKFiles; idx++, filedata++) {
  98. // Only process files that are in the file cache.
  99. if (filedata->Ptr) {
  100. filedata->Ptr = Mem_Find(FileCacheHeap, idx);
  101. }
  102. }
  103. }
  104. /***************************************************************************
  105. * MAKE_FILE_RESIDENT -- Makes a file resident even if not flaged so. *
  106. * *
  107. * INPUT: BYTE *filename - name of file to be made resident. *
  108. * *
  109. * OUTPUT: BOOL if successful. could fail in not enouph RAM or not found. *
  110. * *
  111. * WARNINGS: File must be in FileData table. *
  112. * *
  113. * HISTORY: *
  114. * 09/13/1993 SKB : Created. *
  115. *=========================================================================*/
  116. BOOL Make_File_Resident(BYTE const *filename)
  117. {
  118. FileDataType *filedata; // Pointer to the current FileData.
  119. FileDataType hold; // Hold buffer for record (DO NOT ACCESS DIRECTLY)
  120. WORD fileindex;
  121. WORD oldflag;
  122. WORD handle;
  123. fileindex = Find_File_Index(filename);
  124. // if the file is not in the table, we can't make it resident
  125. if (fileindex == ERROR) return(FALSE);
  126. // Get a pointer for quicker pointer action.
  127. filedata = &FileDataPtr[fileindex];
  128. // Change the flags for a moment.
  129. oldflag = filedata->Flag;
  130. filedata->Flag |= FILEF_RESIDENT;
  131. filedata->Flag &= ~FILEF_FLUSH;
  132. // Make the file resident.
  133. handle = Open_File(filename, READ);
  134. Close_File(handle);
  135. // Set flags back to normal.
  136. filedata->Flag = oldflag;
  137. return(TRUE);
  138. }
  139. /***************************************************************************
  140. * Flush_Unused_File_Cache -- Flushes the file cache of any non opened files. *
  141. * *
  142. * INPUT: WORD flush_keep - TRUE to flush even files marked FILEF_KEEP.*
  143. * *
  144. * OUTPUT: WORD Number of file flushed. *
  145. * *
  146. * WARNINGS: *
  147. * *
  148. * HISTORY: *
  149. * 02/23/1993 SB : Created. *
  150. *=========================================================================*/
  151. WORD Flush_Unused_File_Cache(WORD flush_keeps)
  152. {
  153. WORD index;
  154. WORD freed = 0;
  155. FileDataType *filedata = NULL;
  156. FileDataType hold; // Hold buffer for record (DO NOT ACCESS DIRECTLY)
  157. // Loop throuph the file table looking for files that could be freed.
  158. index = 0;
  159. filedata = &FileDataPtr[index];;
  160. while (filedata->Name && strlen(filedata->Name)) {
  161. if (filedata->Ptr && !filedata->OpenCount &&
  162. (flush_keeps || !(filedata->Flag & FILEF_KEEP)) ) {
  163. Mem_Free(FileCacheHeap, filedata->Ptr);
  164. filedata->Ptr = NULL;
  165. freed++;
  166. }
  167. index++;
  168. filedata = &FileDataPtr[index];;
  169. }
  170. return (freed);
  171. }
  172. /***************************************************************************
  173. * FREE_RESIDENT_FILE -- Free the given file if it is resident. *
  174. * *
  175. * INPUT: BYTE *file to free *
  176. * *
  177. * OUTPUT: TRUE if file was free'd, FALSE otherwise *
  178. * *
  179. * WARNINGS: none *
  180. * *
  181. * HISTORY: *
  182. * 07/22/1992 CY : Created. *
  183. *=========================================================================*/
  184. BOOL cdecl Free_Resident_File(BYTE const *file)
  185. {
  186. WORD fileindex;
  187. BOOL oldflag; // Previous file flag.
  188. FileDataType *filedata; // Pointer to the current FileData.
  189. FileDataType hold; // Hold buffer for record (DO NOT ACCESS DIRECTLY)
  190. // if the file is not in the table, we can't free it
  191. if ((fileindex = Find_File_Index(file)) == ERROR) {
  192. return(FALSE);
  193. }
  194. // get a pointer for quicker calculations.
  195. filedata = &FileDataPtr[fileindex];
  196. // If it isn't resident, don't try to Free it
  197. if (filedata->Ptr == NULL) {
  198. return(TRUE);
  199. }
  200. // Change the flags for a moment.
  201. oldflag = filedata->Flag;
  202. filedata->Flag &= ~(FILEF_RESIDENT|FILEF_KEEP);
  203. filedata->Flag |= FILEF_FLUSH;
  204. // Get the file out of Memory if it was there.
  205. Close_File(Open_File(file, READ));
  206. // Set flags back to original.
  207. filedata->Flag = oldflag;
  208. return(TRUE);
  209. }