Changeset 1165 for trunk/src/kmk/kmkbuiltin/kDepIDB.c
- Timestamp:
- Sep 30, 2007, 9:36:23 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kmk/kmkbuiltin/kDepIDB.c
r1163 r1165 38 38 # include <stdint.h> 39 39 #else 40 # define USE_WIN_MMAP 41 # include <io.h> 42 # include <Windows.h> 40 43 typedef unsigned char uint8_t; 41 44 typedef unsigned short uint16_t; … … 137 140 138 141 142 #ifdef USE_WIN_MMAP 143 /** Handle to the current file mapping object. */ 144 static HANDLE g_hMapObj = NULL; 145 #endif 146 139 147 140 148 /** 141 149 * Reads the file specified by the pInput file stream into memory. 142 150 * The size of the file is returned in *pcbFile if specified. 143 * The returned pointer should be freed by free().151 * The returned pointer should be freed by FreeFileMemory(). 144 152 */ 145 153 void *ReadFileIntoMemory(FILE *pInput, size_t *pcbFile) … … 152 160 * Figure out file size. 153 161 */ 162 #if defined(_MSC_VER) 163 cbFile = _filelength(fileno(pInput)); 164 if (cbFile < 0) 165 #else 154 166 if ( fseek(pInput, 0, SEEK_END) < 0 155 167 || (cbFile = ftell(pInput)) < 0 156 168 || fseek(pInput, 0, SEEK_SET)) 169 #endif 157 170 { 158 171 fprintf(stderr, "%s: error: Failed to determin file size.\n", argv0); … … 163 176 164 177 /* 178 * Try mmap first. 179 */ 180 #ifdef USE_WIN_MMAP 181 { 182 HANDLE hMapObj = CreateFileMapping((HANDLE)_get_osfhandle(fileno(pInput)), 183 NULL, PAGE_READONLY, 0, cbFile, NULL); 184 if (hMapObj != NULL) 185 { 186 pvFile = MapViewOfFile(hMapObj, FILE_MAP_READ, 0, 0, cbFile); 187 if (pvFile) 188 { 189 g_hMapObj = hMapObj; 190 return pvFile; 191 } 192 fprintf(stderr, "%s: warning: MapViewOfFile failed, %d.\n", argv0, GetLastError()); 193 CloseHandle(hMapObj); 194 } 195 else 196 fprintf(stderr, "%s: warning: CreateFileMapping failed, %d.\n", argv0, GetLastError()); 197 } 198 199 #endif 200 201 /* 165 202 * Allocate memory and read the file. 166 203 */ … … 168 205 if (pvFile) 169 206 { 207 #if 1 208 long cbRead = (long)read(fileno(pInput), pvFile, cbFile); 209 if (cbRead == cbFile) 210 #else 170 211 if (fread(pvFile, cbFile, 1, pInput)) 212 #endif 171 213 { 172 214 ((uint8_t *)pvFile)[cbFile] = '\0'; … … 181 223 } 182 224 225 226 static void FreeFileMemory(void *pvFile) 227 { 228 #if defined(USE_WIN_MMAP) 229 if (g_hMapObj) 230 { 231 UnmapViewOfFile(pvFile); 232 CloseHandle(g_hMapObj); 233 return; 234 } 235 #endif 236 free(pvFile); 237 } 183 238 184 239 … … 726 781 } 727 782 728 free(pbFile);783 FreeFileMemory(pbFile); 729 784 return rc; 730 785 } … … 925 980 } 926 981 982 depCleanup(); 927 983 return i; 928 984 }
Note:
See TracChangeset
for help on using the changeset viewer.