| 1 |
/* Emacs style mode select -*- C -*- |
| 2 |
*----------------------------------------------------------------------------- |
| 3 |
* |
| 4 |
* |
| 5 |
* PrBoom: a Doom port merged with LxDoom and LSDLDoom |
| 6 |
* based on BOOM, a modified and improved DOOM engine |
| 7 |
* Copyright (C) 2001 by |
| 8 |
* Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze |
| 9 |
* Copyright 2005, 2006 by |
| 10 |
* Florian Schulze, Colin Phipps, Neil Stevens, Andrey Budko |
| 11 |
* |
| 12 |
* This program is free software; you can redistribute it and/or |
| 13 |
* modify it under the terms of the GNU General Public License |
| 14 |
* as published by the Free Software Foundation; either version 2 |
| 15 |
* of the License, or (at your option) any later version. |
| 16 |
* |
| 17 |
* This program is distributed in the hope that it will be useful, |
| 18 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 |
* GNU General Public License for more details. |
| 21 |
* |
| 22 |
* You should have received a copy of the GNU General Public License |
| 23 |
* along with this program; if not, write to the Free Software |
| 24 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 25 |
* 02111-1307, USA. |
| 26 |
* |
| 27 |
* DESCRIPTION: |
| 28 |
* Transparent access to data in WADs using mmap |
| 29 |
* |
| 30 |
*----------------------------------------------------------------------------- |
| 31 |
*/ |
| 32 |
|
| 33 |
#ifdef HAVE_CONFIG_H |
| 34 |
#include "config.h" |
| 35 |
#endif |
| 36 |
|
| 37 |
#ifdef HAVE_UNISTD_H |
| 38 |
#include <unistd.h> |
| 39 |
#endif |
| 40 |
#ifdef _WIN32 |
| 41 |
#define WIN32_LEAN_AND_MEAN |
| 42 |
#include <windows.h> |
| 43 |
#else |
| 44 |
#include <sys/mman.h> |
| 45 |
#endif |
| 46 |
|
| 47 |
#include "doomstat.h" |
| 48 |
#include "doomtype.h" |
| 49 |
|
| 50 |
#include "w_wad.h" |
| 51 |
#include "z_zone.h" |
| 52 |
#include "lprintf.h" |
| 53 |
#include "i_system.h" |
| 54 |
#include "m_file.h" |
| 55 |
|
| 56 |
#include "e6y.h"//e6y |
| 57 |
|
| 58 |
static void **lump_data; |
| 59 |
|
| 60 |
#ifdef _WIN32 |
| 61 |
typedef struct { |
| 62 |
HANDLE hnd; |
| 63 |
OFSTRUCT fileinfo; |
| 64 |
HANDLE hnd_map; |
| 65 |
void *data; |
| 66 |
} mmap_info_t; |
| 67 |
|
| 68 |
mmap_info_t *mapped_wad; |
| 69 |
|
| 70 |
void W_DoneCache(void) |
| 71 |
{ |
| 72 |
size_t i; |
| 73 |
|
| 74 |
if (lump_data) { |
| 75 |
Z_Free(lump_data); |
| 76 |
lump_data = NULL; |
| 77 |
} |
| 78 |
|
| 79 |
if (!mapped_wad) |
| 80 |
return; |
| 81 |
for (i=0; i<numwadfiles; i++) |
| 82 |
{ |
| 83 |
if (mapped_wad[i].data) |
| 84 |
{ |
| 85 |
UnmapViewOfFile(mapped_wad[i].data); |
| 86 |
mapped_wad[i].data=NULL; |
| 87 |
} |
| 88 |
if (mapped_wad[i].hnd_map) |
| 89 |
{ |
| 90 |
CloseHandle(mapped_wad[i].hnd_map); |
| 91 |
mapped_wad[i].hnd_map=NULL; |
| 92 |
} |
| 93 |
if (mapped_wad[i].hnd) |
| 94 |
{ |
| 95 |
CloseHandle(mapped_wad[i].hnd); |
| 96 |
mapped_wad[i].hnd=NULL; |
| 97 |
} |
| 98 |
} |
| 99 |
Z_Free(mapped_wad); |
| 100 |
mapped_wad = NULL; |
| 101 |
} |
| 102 |
|
| 103 |
void W_InitCache(void) |
| 104 |
{ |
| 105 |
// Wipe any existing cache |
| 106 |
W_DoneCache(); |
| 107 |
|
| 108 |
// set up caching |
| 109 |
lump_data = Z_Calloc(numlumps, sizeof *lump_data); |
| 110 |
if (!lump_data) |
| 111 |
I_Error ("W_Init: Couldn't allocate lump data"); |
| 112 |
|
| 113 |
mapped_wad = Z_Calloc(numwadfiles,sizeof(mmap_info_t)); |
| 114 |
memset(mapped_wad,0,sizeof(mmap_info_t)*numwadfiles); |
| 115 |
{ |
| 116 |
int i; |
| 117 |
for (i=0; i<numlumps; i++) |
| 118 |
{ |
| 119 |
int wad_index = (int)(lumpinfo[i].wadfile-wadfiles); |
| 120 |
|
| 121 |
if (!lumpinfo[i].wadfile) |
| 122 |
continue; |
| 123 |
#ifdef RANGECHECK |
| 124 |
if ((wad_index<0)||((size_t)wad_index>=numwadfiles)) |
| 125 |
I_Error("W_InitCache: wad_index out of range"); |
| 126 |
#endif |
| 127 |
if (!mapped_wad[wad_index].data) |
| 128 |
{ |
| 129 |
wchar_t *wname = ConvertUtf8ToWide(wadfiles[wad_index].name); |
| 130 |
mapped_wad[wad_index].hnd = CreateFileW(wname, |
| 131 |
GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, |
| 132 |
NULL, OPEN_EXISTING, 0, NULL); |
| 133 |
Z_Free(wname); |
| 134 |
if (mapped_wad[wad_index].hnd==INVALID_HANDLE_VALUE) |
| 135 |
I_Error("W_InitCache: CreateFile for memory mapping failed (LastError %li)",GetLastError()); |
| 136 |
mapped_wad[wad_index].hnd_map = |
| 137 |
CreateFileMapping( |
| 138 |
mapped_wad[wad_index].hnd, |
| 139 |
NULL, |
| 140 |
PAGE_READONLY, |
| 141 |
0, |
| 142 |
0, |
| 143 |
NULL |
| 144 |
); |
| 145 |
if (mapped_wad[wad_index].hnd_map==NULL) |
| 146 |
I_Error("W_InitCache: CreateFileMapping for memory mapping failed (LastError %li)",GetLastError()); |
| 147 |
mapped_wad[wad_index].data = |
| 148 |
MapViewOfFile( |
| 149 |
mapped_wad[wad_index].hnd_map, |
| 150 |
FILE_MAP_READ, |
| 151 |
0, |
| 152 |
0, |
| 153 |
0 |
| 154 |
); |
| 155 |
if (mapped_wad[wad_index].data==NULL) |
| 156 |
I_Error("W_InitCache: MapViewOfFile for memory mapping failed (LastError %li)",GetLastError()); |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
const void* W_LumpByNum(int lump) |
| 163 |
{ |
| 164 |
int wad_index = (int)(lumpinfo[lump].wadfile-wadfiles); |
| 165 |
#ifdef RANGECHECK |
| 166 |
if ((wad_index<0)||((size_t)wad_index>=numwadfiles)) |
| 167 |
I_Error("W_LumpByNum: wad_index out of range"); |
| 168 |
if ((unsigned)lump >= (unsigned)numlumps) |
| 169 |
I_Error ("W_LumpByNum: %i >= numlumps",lump); |
| 170 |
#endif |
| 171 |
if (!lumpinfo[lump].wadfile) |
| 172 |
return NULL; |
| 173 |
return (void*)((unsigned char *)mapped_wad[wad_index].data+lumpinfo[lump].position); |
| 174 |
} |
| 175 |
|
| 176 |
#else |
| 177 |
|
| 178 |
void ** mapped_wad; |
| 179 |
|
| 180 |
void W_InitCache(void) |
| 181 |
{ |
| 182 |
int maxfd = 0; |
| 183 |
// set up caching |
| 184 |
lump_data = Z_Calloc(numlumps, sizeof *lump_data); |
| 185 |
if (!lump_data) |
| 186 |
I_Error ("W_Init: Couldn't allocate lump data"); |
| 187 |
|
| 188 |
{ |
| 189 |
int i; |
| 190 |
for (i=0; i<numlumps; i++) |
| 191 |
if (lumpinfo[i].wadfile) |
| 192 |
if (lumpinfo[i].wadfile->handle > maxfd) maxfd = lumpinfo[i].wadfile->handle; |
| 193 |
} |
| 194 |
mapped_wad = Z_Calloc(maxfd+1,sizeof *mapped_wad); |
| 195 |
{ |
| 196 |
int i; |
| 197 |
for (i=0; i<numlumps; i++) { |
| 198 |
if (lumpinfo[i].wadfile) { |
| 199 |
int fd = lumpinfo[i].wadfile->handle; |
| 200 |
if (!mapped_wad[fd]) |
| 201 |
if ((mapped_wad[fd] = mmap(NULL,I_Filelength(fd),PROT_READ,MAP_SHARED,fd,0)) == MAP_FAILED) |
| 202 |
I_Error("W_InitCache: failed to mmap"); |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
void W_DoneCache(void) |
| 209 |
{ |
| 210 |
{ |
| 211 |
int i; |
| 212 |
for (i=0; i<numlumps; i++) |
| 213 |
if (lumpinfo[i].wadfile) { |
| 214 |
int fd = lumpinfo[i].wadfile->handle; |
| 215 |
if (fd > 0 && mapped_wad[fd]) { |
| 216 |
if (munmap(mapped_wad[fd],I_Filelength(fd))) |
| 217 |
I_Error("W_DoneCache: failed to munmap"); |
| 218 |
mapped_wad[fd] = NULL; |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
Z_Free(mapped_wad); |
| 223 |
mapped_wad = NULL; |
| 224 |
} |
| 225 |
|
| 226 |
const void* W_LumpByNum(int lump) |
| 227 |
{ |
| 228 |
#ifdef RANGECHECK |
| 229 |
if ((unsigned)lump >= (unsigned)numlumps) |
| 230 |
I_Error ("W_LumpByNum: %i >= numlumps",lump); |
| 231 |
#endif |
| 232 |
if (!lumpinfo[lump].wadfile) |
| 233 |
return NULL; |
| 234 |
|
| 235 |
return |
| 236 |
(const void *) ( |
| 237 |
((const byte *) (mapped_wad[lumpinfo[lump].wadfile->handle])) |
| 238 |
+ lumpinfo[lump].position |
| 239 |
); |
| 240 |
} |
| 241 |
#endif |
| 242 |
|
| 243 |
/* |
| 244 |
* W_LockLumpNum |
| 245 |
* |
| 246 |
* This copies the lump into a malloced memory region and returns its address |
| 247 |
* instead of returning a pointer into the memory mapped area |
| 248 |
* |
| 249 |
*/ |
| 250 |
const void* W_LockLumpNum(int lump) |
| 251 |
{ |
| 252 |
size_t len = W_LumpLength(lump); |
| 253 |
const void *data = W_LumpByNum(lump); |
| 254 |
|
| 255 |
// read the lump in |
| 256 |
if (!lump_data[lump]) { |
| 257 |
lump_data[lump] = Z_Malloc(len); |
| 258 |
memcpy(lump_data[lump], data, len); |
| 259 |
} |
| 260 |
|
| 261 |
return lump_data[lump]; |
| 262 |
} |
| 263 |
|
| 264 |
void *W_GetModifiableLumpData(int lump) |
| 265 |
{ |
| 266 |
return lump_data[lump]; |
| 267 |
} |