| 1 | /****************************************************************************** | 
|---|
| 2 | * | 
|---|
| 3 | * BigBlockFile | 
|---|
| 4 | * | 
|---|
| 5 | * This is the implementation of a file that consists of blocks of | 
|---|
| 6 | * a predetermined size. | 
|---|
| 7 | * This class is used in the Compound File implementation of the | 
|---|
| 8 | * IStorage and IStream interfaces. It provides the functionality | 
|---|
| 9 | * to read and write any blocks in the file as well as setting and | 
|---|
| 10 | * obtaining the size of the file. | 
|---|
| 11 | * The blocks are indexed sequentially from the start of the file | 
|---|
| 12 | * starting with -1. | 
|---|
| 13 | * | 
|---|
| 14 | * TODO: | 
|---|
| 15 | * - Support for a transacted mode | 
|---|
| 16 | * | 
|---|
| 17 | * Copyright 1999 Thuy Nguyen | 
|---|
| 18 | * | 
|---|
| 19 | */ | 
|---|
| 20 |  | 
|---|
| 21 | #ifdef __WIN32OS2__ | 
|---|
| 22 | #define inline | 
|---|
| 23 |  | 
|---|
| 24 | #include <odin.h> | 
|---|
| 25 | #include "ole32.h" | 
|---|
| 26 | #include "heapstring.h" | 
|---|
| 27 |  | 
|---|
| 28 | #endif | 
|---|
| 29 |  | 
|---|
| 30 | #include <assert.h> | 
|---|
| 31 | #include <stdlib.h> | 
|---|
| 32 | #include <stdio.h> | 
|---|
| 33 | #include <string.h> | 
|---|
| 34 | #include <limits.h> | 
|---|
| 35 |  | 
|---|
| 36 | #include "winbase.h" | 
|---|
| 37 | #include "winerror.h" | 
|---|
| 38 | #include "wine/obj_base.h" | 
|---|
| 39 | #include "wine/obj_storage.h" | 
|---|
| 40 | #include "ole2.h" | 
|---|
| 41 |  | 
|---|
| 42 | #include "storage32.h" | 
|---|
| 43 |  | 
|---|
| 44 | #include "debugtools.h" | 
|---|
| 45 |  | 
|---|
| 46 | DEFAULT_DEBUG_CHANNEL(storage); | 
|---|
| 47 |  | 
|---|
| 48 | /*********************************************************** | 
|---|
| 49 | * Data structures used internally by the BigBlockFile | 
|---|
| 50 | * class. | 
|---|
| 51 | */ | 
|---|
| 52 |  | 
|---|
| 53 | /* We map in PAGE_SIZE-sized chunks. Must be a multiple of 4096. */ | 
|---|
| 54 | #define PAGE_SIZE       131072 | 
|---|
| 55 |  | 
|---|
| 56 | #define BLOCKS_PER_PAGE (PAGE_SIZE / BIG_BLOCK_SIZE) | 
|---|
| 57 |  | 
|---|
| 58 | /* We keep a list of recently-discarded pages. This controls the | 
|---|
| 59 | * size of that list. */ | 
|---|
| 60 | #define MAX_VICTIM_PAGES 16 | 
|---|
| 61 |  | 
|---|
| 62 | /* This structure provides one bit for each block in a page. | 
|---|
| 63 | * Use BIGBLOCKFILE_{Test,Set,Clear}Bit to manipulate it. */ | 
|---|
| 64 | typedef struct | 
|---|
| 65 | { | 
|---|
| 66 | unsigned int bits[BLOCKS_PER_PAGE / (CHAR_BIT * sizeof(unsigned int))]; | 
|---|
| 67 | } BlockBits; | 
|---|
| 68 |  | 
|---|
| 69 | /*** | 
|---|
| 70 | * This structure identifies the paged that are mapped | 
|---|
| 71 | * from the file and their position in memory. It is | 
|---|
| 72 | * also used to hold a reference count to those pages. | 
|---|
| 73 | * | 
|---|
| 74 | * page_index identifies which PAGE_SIZE chunk from the | 
|---|
| 75 | * file this mapping represents. (The mappings are always | 
|---|
| 76 | * PAGE_SIZE-aligned.) | 
|---|
| 77 | */ | 
|---|
| 78 | struct MappedPage | 
|---|
| 79 | { | 
|---|
| 80 | MappedPage *next; | 
|---|
| 81 | MappedPage *prev; | 
|---|
| 82 |  | 
|---|
| 83 | DWORD  page_index; | 
|---|
| 84 | LPVOID lpBytes; | 
|---|
| 85 | LONG   refcnt; | 
|---|
| 86 |  | 
|---|
| 87 | BlockBits readable_blocks; | 
|---|
| 88 | BlockBits writable_blocks; | 
|---|
| 89 | }; | 
|---|
| 90 |  | 
|---|
| 91 | /*********************************************************** | 
|---|
| 92 | * Prototypes for private methods | 
|---|
| 93 | */ | 
|---|
| 94 | static void*     BIGBLOCKFILE_GetMappedView(LPBIGBLOCKFILE This, | 
|---|
| 95 | DWORD          page_index); | 
|---|
| 96 | static void      BIGBLOCKFILE_ReleaseMappedPage(LPBIGBLOCKFILE This, | 
|---|
| 97 | MappedPage *page); | 
|---|
| 98 | static void      BIGBLOCKFILE_FreeAllMappedPages(LPBIGBLOCKFILE This); | 
|---|
| 99 | static void      BIGBLOCKFILE_UnmapAllMappedPages(LPBIGBLOCKFILE This); | 
|---|
| 100 | static void      BIGBLOCKFILE_RemapAllMappedPages(LPBIGBLOCKFILE This); | 
|---|
| 101 | static void*     BIGBLOCKFILE_GetBigBlockPointer(LPBIGBLOCKFILE This, | 
|---|
| 102 | ULONG          index, | 
|---|
| 103 | DWORD          desired_access); | 
|---|
| 104 | static MappedPage* BIGBLOCKFILE_GetPageFromPointer(LPBIGBLOCKFILE This, | 
|---|
| 105 | void*         pBlock); | 
|---|
| 106 | static MappedPage* BIGBLOCKFILE_CreatePage(LPBIGBLOCKFILE This, | 
|---|
| 107 | ULONG page_index); | 
|---|
| 108 | static DWORD     BIGBLOCKFILE_GetProtectMode(DWORD openFlags); | 
|---|
| 109 | static BOOL      BIGBLOCKFILE_FileInit(LPBIGBLOCKFILE This, HANDLE hFile); | 
|---|
| 110 | static BOOL      BIGBLOCKFILE_MemInit(LPBIGBLOCKFILE This, ILockBytes* plkbyt); | 
|---|
| 111 |  | 
|---|
| 112 | /* Note that this evaluates a and b multiple times, so don't | 
|---|
| 113 | * pass expressions with side effects. */ | 
|---|
| 114 | #define ROUND_UP(a, b) ((((a) + (b) - 1)/(b))*(b)) | 
|---|
| 115 |  | 
|---|
| 116 | /*********************************************************** | 
|---|
| 117 | * Blockbits functions. | 
|---|
| 118 | */ | 
|---|
| 119 | static inline BOOL BIGBLOCKFILE_TestBit(const BlockBits *bb, | 
|---|
| 120 | unsigned int index) | 
|---|
| 121 | { | 
|---|
| 122 | unsigned int array_index = index / (CHAR_BIT * sizeof(unsigned int)); | 
|---|
| 123 | unsigned int bit_index = index % (CHAR_BIT * sizeof(unsigned int)); | 
|---|
| 124 |  | 
|---|
| 125 | return bb->bits[array_index] & (1 << bit_index); | 
|---|
| 126 | } | 
|---|
| 127 |  | 
|---|
| 128 | static inline void BIGBLOCKFILE_SetBit(BlockBits *bb, unsigned int index) | 
|---|
| 129 | { | 
|---|
| 130 | unsigned int array_index = index / (CHAR_BIT * sizeof(unsigned int)); | 
|---|
| 131 | unsigned int bit_index = index % (CHAR_BIT * sizeof(unsigned int)); | 
|---|
| 132 |  | 
|---|
| 133 | bb->bits[array_index] |= (1 << bit_index); | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | static inline void BIGBLOCKFILE_ClearBit(BlockBits *bb, unsigned int index) | 
|---|
| 137 | { | 
|---|
| 138 | unsigned int array_index = index / (CHAR_BIT * sizeof(unsigned int)); | 
|---|
| 139 | unsigned int bit_index = index % (CHAR_BIT * sizeof(unsigned int)); | 
|---|
| 140 |  | 
|---|
| 141 | bb->bits[array_index] &= ~(1 << bit_index); | 
|---|
| 142 | } | 
|---|
| 143 |  | 
|---|
| 144 | static inline void BIGBLOCKFILE_Zero(BlockBits *bb) | 
|---|
| 145 | { | 
|---|
| 146 | memset(bb->bits, 0, sizeof(bb->bits)); | 
|---|
| 147 | } | 
|---|
| 148 |  | 
|---|
| 149 | /****************************************************************************** | 
|---|
| 150 | *      BIGBLOCKFILE_Construct | 
|---|
| 151 | * | 
|---|
| 152 | * Construct a big block file. Create the file mapping object. | 
|---|
| 153 | * Create the read only mapped pages list, the writable mapped page list | 
|---|
| 154 | * and the blocks in use list. | 
|---|
| 155 | */ | 
|---|
| 156 | BigBlockFile * BIGBLOCKFILE_Construct( | 
|---|
| 157 | HANDLE   hFile, | 
|---|
| 158 | ILockBytes* pLkByt, | 
|---|
| 159 | DWORD    openFlags, | 
|---|
| 160 | ULONG    blocksize, | 
|---|
| 161 | BOOL     fileBased) | 
|---|
| 162 | { | 
|---|
| 163 | LPBIGBLOCKFILE This; | 
|---|
| 164 |  | 
|---|
| 165 | This = (LPBIGBLOCKFILE)HeapAlloc(GetProcessHeap(), 0, sizeof(BigBlockFile)); | 
|---|
| 166 |  | 
|---|
| 167 | if (This == NULL) | 
|---|
| 168 | return NULL; | 
|---|
| 169 |  | 
|---|
| 170 | This->fileBased = fileBased; | 
|---|
| 171 |  | 
|---|
| 172 | This->flProtect = BIGBLOCKFILE_GetProtectMode(openFlags); | 
|---|
| 173 |  | 
|---|
| 174 | This->blocksize = blocksize; | 
|---|
| 175 |  | 
|---|
| 176 | This->maplist = NULL; | 
|---|
| 177 | This->victimhead = NULL; | 
|---|
| 178 | This->victimtail = NULL; | 
|---|
| 179 | This->num_victim_pages = 0; | 
|---|
| 180 |  | 
|---|
| 181 | if (This->fileBased) | 
|---|
| 182 | { | 
|---|
| 183 | if (!BIGBLOCKFILE_FileInit(This, hFile)) | 
|---|
| 184 | { | 
|---|
| 185 | HeapFree(GetProcessHeap(), 0, This); | 
|---|
| 186 | return NULL; | 
|---|
| 187 | } | 
|---|
| 188 | } | 
|---|
| 189 | else | 
|---|
| 190 | { | 
|---|
| 191 | if (!BIGBLOCKFILE_MemInit(This, pLkByt)) | 
|---|
| 192 | { | 
|---|
| 193 | HeapFree(GetProcessHeap(), 0, This); | 
|---|
| 194 | return NULL; | 
|---|
| 195 | } | 
|---|
| 196 | } | 
|---|
| 197 |  | 
|---|
| 198 | return This; | 
|---|
| 199 | } | 
|---|
| 200 |  | 
|---|
| 201 | /****************************************************************************** | 
|---|
| 202 | *      BIGBLOCKFILE_FileInit | 
|---|
| 203 | * | 
|---|
| 204 | * Initialize a big block object supported by a file. | 
|---|
| 205 | */ | 
|---|
| 206 | static BOOL BIGBLOCKFILE_FileInit(LPBIGBLOCKFILE This, HANDLE hFile) | 
|---|
| 207 | { | 
|---|
| 208 | This->pLkbyt     = NULL; | 
|---|
| 209 | This->hbytearray = 0; | 
|---|
| 210 | This->pbytearray = NULL; | 
|---|
| 211 |  | 
|---|
| 212 | This->hfile = hFile; | 
|---|
| 213 |  | 
|---|
| 214 | if (This->hfile == INVALID_HANDLE_VALUE) | 
|---|
| 215 | return FALSE; | 
|---|
| 216 |  | 
|---|
| 217 | /* create the file mapping object | 
|---|
| 218 | */ | 
|---|
| 219 | This->hfilemap = CreateFileMappingA(This->hfile, | 
|---|
| 220 | NULL, | 
|---|
| 221 | This->flProtect, | 
|---|
| 222 | 0, 0, | 
|---|
| 223 | NULL); | 
|---|
| 224 |  | 
|---|
| 225 | if (!This->hfilemap) | 
|---|
| 226 | { | 
|---|
| 227 | CloseHandle(This->hfile); | 
|---|
| 228 | return FALSE; | 
|---|
| 229 | } | 
|---|
| 230 |  | 
|---|
| 231 | This->filesize.s.LowPart = GetFileSize(This->hfile, | 
|---|
| 232 | &This->filesize.s.HighPart); | 
|---|
| 233 |  | 
|---|
| 234 | This->maplist = NULL; | 
|---|
| 235 |  | 
|---|
| 236 | TRACE("file len %lu\n", This->filesize.s.LowPart); | 
|---|
| 237 |  | 
|---|
| 238 | return TRUE; | 
|---|
| 239 | } | 
|---|
| 240 |  | 
|---|
| 241 | /****************************************************************************** | 
|---|
| 242 | *      BIGBLOCKFILE_MemInit | 
|---|
| 243 | * | 
|---|
| 244 | * Initialize a big block object supported by an ILockBytes on HGLOABL. | 
|---|
| 245 | */ | 
|---|
| 246 | static BOOL BIGBLOCKFILE_MemInit(LPBIGBLOCKFILE This, ILockBytes* plkbyt) | 
|---|
| 247 | { | 
|---|
| 248 | This->hfile       = 0; | 
|---|
| 249 | This->hfilemap    = 0; | 
|---|
| 250 |  | 
|---|
| 251 | /* | 
|---|
| 252 | * Retrieve the handle to the byte array from the LockByte object. | 
|---|
| 253 | */ | 
|---|
| 254 | if (GetHGlobalFromILockBytes(plkbyt, &(This->hbytearray)) != S_OK) | 
|---|
| 255 | { | 
|---|
| 256 | FIXME("May not be an ILockBytes on HGLOBAL\n"); | 
|---|
| 257 | return FALSE; | 
|---|
| 258 | } | 
|---|
| 259 |  | 
|---|
| 260 | This->pLkbyt = plkbyt; | 
|---|
| 261 |  | 
|---|
| 262 | /* | 
|---|
| 263 | * Increment the reference count of the ILockByte object since | 
|---|
| 264 | * we're keeping a reference to it. | 
|---|
| 265 | */ | 
|---|
| 266 | ILockBytes_AddRef(This->pLkbyt); | 
|---|
| 267 |  | 
|---|
| 268 | This->filesize.s.LowPart = GlobalSize(This->hbytearray); | 
|---|
| 269 | This->filesize.s.HighPart = 0; | 
|---|
| 270 |  | 
|---|
| 271 | This->pbytearray = GlobalLock(This->hbytearray); | 
|---|
| 272 |  | 
|---|
| 273 | TRACE("mem on %p len %lu\n", This->pbytearray, This->filesize.s.LowPart); | 
|---|
| 274 |  | 
|---|
| 275 | return TRUE; | 
|---|
| 276 | } | 
|---|
| 277 |  | 
|---|
| 278 | /****************************************************************************** | 
|---|
| 279 | *      BIGBLOCKFILE_Destructor | 
|---|
| 280 | * | 
|---|
| 281 | * Destructor. Clean up, free memory. | 
|---|
| 282 | */ | 
|---|
| 283 | void BIGBLOCKFILE_Destructor( | 
|---|
| 284 | LPBIGBLOCKFILE This) | 
|---|
| 285 | { | 
|---|
| 286 | BIGBLOCKFILE_FreeAllMappedPages(This); | 
|---|
| 287 |  | 
|---|
| 288 | if (This->fileBased) | 
|---|
| 289 | { | 
|---|
| 290 | CloseHandle(This->hfilemap); | 
|---|
| 291 | CloseHandle(This->hfile); | 
|---|
| 292 | } | 
|---|
| 293 | else | 
|---|
| 294 | { | 
|---|
| 295 | GlobalUnlock(This->hbytearray); | 
|---|
| 296 | ILockBytes_Release(This->pLkbyt); | 
|---|
| 297 | } | 
|---|
| 298 |  | 
|---|
| 299 | /* destroy this | 
|---|
| 300 | */ | 
|---|
| 301 | HeapFree(GetProcessHeap(), 0, This); | 
|---|
| 302 | } | 
|---|
| 303 |  | 
|---|
| 304 | /****************************************************************************** | 
|---|
| 305 | *      BIGBLOCKFILE_GetROBigBlock | 
|---|
| 306 | * | 
|---|
| 307 | * Returns the specified block in read only mode. | 
|---|
| 308 | * Will return NULL if the block doesn't exists. | 
|---|
| 309 | */ | 
|---|
| 310 | void* BIGBLOCKFILE_GetROBigBlock( | 
|---|
| 311 | LPBIGBLOCKFILE This, | 
|---|
| 312 | ULONG          index) | 
|---|
| 313 | { | 
|---|
| 314 | /* | 
|---|
| 315 | * block index starts at -1 | 
|---|
| 316 | * translate to zero based index | 
|---|
| 317 | */ | 
|---|
| 318 | if (index == 0xffffffff) | 
|---|
| 319 | index = 0; | 
|---|
| 320 | else | 
|---|
| 321 | index++; | 
|---|
| 322 |  | 
|---|
| 323 | /* | 
|---|
| 324 | * validate the block index | 
|---|
| 325 | * | 
|---|
| 326 | */ | 
|---|
| 327 | if (This->blocksize * (index + 1) | 
|---|
| 328 | > ROUND_UP(This->filesize.s.LowPart, This->blocksize)) | 
|---|
| 329 | { | 
|---|
| 330 | TRACE("out of range %lu vs %lu\n", This->blocksize * (index + 1), | 
|---|
| 331 | This->filesize.s.LowPart); | 
|---|
| 332 | return NULL; | 
|---|
| 333 | } | 
|---|
| 334 |  | 
|---|
| 335 | return BIGBLOCKFILE_GetBigBlockPointer(This, index, FILE_MAP_READ); | 
|---|
| 336 | } | 
|---|
| 337 |  | 
|---|
| 338 | /****************************************************************************** | 
|---|
| 339 | *      BIGBLOCKFILE_GetBigBlock | 
|---|
| 340 | * | 
|---|
| 341 | * Returns the specified block. | 
|---|
| 342 | * Will grow the file if necessary. | 
|---|
| 343 | */ | 
|---|
| 344 | void* BIGBLOCKFILE_GetBigBlock(LPBIGBLOCKFILE This, ULONG index) | 
|---|
| 345 | { | 
|---|
| 346 | /* | 
|---|
| 347 | * block index starts at -1 | 
|---|
| 348 | * translate to zero based index | 
|---|
| 349 | */ | 
|---|
| 350 | if (index == 0xffffffff) | 
|---|
| 351 | index = 0; | 
|---|
| 352 | else | 
|---|
| 353 | index++; | 
|---|
| 354 |  | 
|---|
| 355 | /* | 
|---|
| 356 | * make sure that the block physically exists | 
|---|
| 357 | */ | 
|---|
| 358 | if ((This->blocksize * (index + 1)) > This->filesize.s.LowPart) | 
|---|
| 359 | { | 
|---|
| 360 | ULARGE_INTEGER newSize; | 
|---|
| 361 |  | 
|---|
| 362 | newSize.s.HighPart = 0; | 
|---|
| 363 | newSize.s.LowPart = This->blocksize * (index + 1); | 
|---|
| 364 |  | 
|---|
| 365 | BIGBLOCKFILE_SetSize(This, newSize); | 
|---|
| 366 | } | 
|---|
| 367 |  | 
|---|
| 368 | return BIGBLOCKFILE_GetBigBlockPointer(This, index, FILE_MAP_WRITE); | 
|---|
| 369 | } | 
|---|
| 370 |  | 
|---|
| 371 | /****************************************************************************** | 
|---|
| 372 | *      BIGBLOCKFILE_ReleaseBigBlock | 
|---|
| 373 | * | 
|---|
| 374 | * Releases the specified block. | 
|---|
| 375 | */ | 
|---|
| 376 | void BIGBLOCKFILE_ReleaseBigBlock(LPBIGBLOCKFILE This, void *pBlock) | 
|---|
| 377 | { | 
|---|
| 378 | MappedPage *page; | 
|---|
| 379 |  | 
|---|
| 380 | if (pBlock == NULL) | 
|---|
| 381 | return; | 
|---|
| 382 |  | 
|---|
| 383 | page = BIGBLOCKFILE_GetPageFromPointer(This, pBlock); | 
|---|
| 384 |  | 
|---|
| 385 | if (page == NULL) | 
|---|
| 386 | return; | 
|---|
| 387 |  | 
|---|
| 388 | BIGBLOCKFILE_ReleaseMappedPage(This, page); | 
|---|
| 389 | } | 
|---|
| 390 |  | 
|---|
| 391 | /****************************************************************************** | 
|---|
| 392 | *      BIGBLOCKFILE_SetSize | 
|---|
| 393 | * | 
|---|
| 394 | * Sets the size of the file. | 
|---|
| 395 | * | 
|---|
| 396 | */ | 
|---|
| 397 | void BIGBLOCKFILE_SetSize(LPBIGBLOCKFILE This, ULARGE_INTEGER newSize) | 
|---|
| 398 | { | 
|---|
| 399 | if (This->filesize.s.LowPart == newSize.s.LowPart) | 
|---|
| 400 | return; | 
|---|
| 401 |  | 
|---|
| 402 | TRACE("from %lu to %lu\n", This->filesize.s.LowPart, newSize.s.LowPart); | 
|---|
| 403 | /* | 
|---|
| 404 | * unmap all views, must be done before call to SetEndFile | 
|---|
| 405 | */ | 
|---|
| 406 | BIGBLOCKFILE_UnmapAllMappedPages(This); | 
|---|
| 407 |  | 
|---|
| 408 | if (This->fileBased) | 
|---|
| 409 | { | 
|---|
| 410 | char buf[10]; | 
|---|
| 411 |  | 
|---|
| 412 | /* | 
|---|
| 413 | * close file-mapping object, must be done before call to SetEndFile | 
|---|
| 414 | */ | 
|---|
| 415 | CloseHandle(This->hfilemap); | 
|---|
| 416 | This->hfilemap = 0; | 
|---|
| 417 |  | 
|---|
| 418 | /* | 
|---|
| 419 | * BEGIN HACK | 
|---|
| 420 | * This fixes a bug when saving through smbfs. | 
|---|
| 421 | * smbmount a Windows shared directory, save a structured storage file | 
|---|
| 422 | * to that dir: crash. | 
|---|
| 423 | * | 
|---|
| 424 | * The problem is that the SetFilePointer-SetEndOfFile combo below | 
|---|
| 425 | * doesn't always succeed. The file is not grown. It seems like the | 
|---|
| 426 | * operation is cached. By doing the WriteFile, the file is actually | 
|---|
| 427 | * grown on disk. | 
|---|
| 428 | * This hack is only needed when saving to smbfs. | 
|---|
| 429 | */ | 
|---|
| 430 | memset(buf, '0', 10); | 
|---|
| 431 | SetFilePointer(This->hfile, newSize.s.LowPart, NULL, FILE_BEGIN); | 
|---|
| 432 | WriteFile(This->hfile, buf, 10, NULL, NULL); | 
|---|
| 433 | /* | 
|---|
| 434 | * END HACK | 
|---|
| 435 | */ | 
|---|
| 436 |  | 
|---|
| 437 | /* | 
|---|
| 438 | * set the new end of file | 
|---|
| 439 | */ | 
|---|
| 440 | SetFilePointer(This->hfile, newSize.s.LowPart, NULL, FILE_BEGIN); | 
|---|
| 441 | SetEndOfFile(This->hfile); | 
|---|
| 442 |  | 
|---|
| 443 | /* | 
|---|
| 444 | * re-create the file mapping object | 
|---|
| 445 | */ | 
|---|
| 446 | This->hfilemap = CreateFileMappingA(This->hfile, | 
|---|
| 447 | NULL, | 
|---|
| 448 | This->flProtect, | 
|---|
| 449 | 0, 0, | 
|---|
| 450 | NULL); | 
|---|
| 451 | } | 
|---|
| 452 | else | 
|---|
| 453 | { | 
|---|
| 454 | GlobalUnlock(This->hbytearray); | 
|---|
| 455 |  | 
|---|
| 456 | /* | 
|---|
| 457 | * Resize the byte array object. | 
|---|
| 458 | */ | 
|---|
| 459 | ILockBytes_SetSize(This->pLkbyt, newSize); | 
|---|
| 460 |  | 
|---|
| 461 | /* | 
|---|
| 462 | * Re-acquire the handle, it may have changed. | 
|---|
| 463 | */ | 
|---|
| 464 | GetHGlobalFromILockBytes(This->pLkbyt, &This->hbytearray); | 
|---|
| 465 | This->pbytearray = GlobalLock(This->hbytearray); | 
|---|
| 466 | } | 
|---|
| 467 |  | 
|---|
| 468 | This->filesize.s.LowPart = newSize.s.LowPart; | 
|---|
| 469 | This->filesize.s.HighPart = newSize.s.HighPart; | 
|---|
| 470 |  | 
|---|
| 471 | BIGBLOCKFILE_RemapAllMappedPages(This); | 
|---|
| 472 | } | 
|---|
| 473 |  | 
|---|
| 474 | /****************************************************************************** | 
|---|
| 475 | *      BIGBLOCKFILE_GetSize | 
|---|
| 476 | * | 
|---|
| 477 | * Returns the size of the file. | 
|---|
| 478 | * | 
|---|
| 479 | */ | 
|---|
| 480 | ULARGE_INTEGER BIGBLOCKFILE_GetSize(LPBIGBLOCKFILE This) | 
|---|
| 481 | { | 
|---|
| 482 | return This->filesize; | 
|---|
| 483 | } | 
|---|
| 484 |  | 
|---|
| 485 | /****************************************************************************** | 
|---|
| 486 | *      BIGBLOCKFILE_AccessCheck     [PRIVATE] | 
|---|
| 487 | * | 
|---|
| 488 | * block_index is the index within the page. | 
|---|
| 489 | */ | 
|---|
| 490 | static BOOL BIGBLOCKFILE_AccessCheck(MappedPage *page, ULONG block_index, | 
|---|
| 491 | DWORD desired_access) | 
|---|
| 492 | { | 
|---|
| 493 | assert(block_index < BLOCKS_PER_PAGE); | 
|---|
| 494 |  | 
|---|
| 495 | if (desired_access == FILE_MAP_READ) | 
|---|
| 496 | { | 
|---|
| 497 | if (BIGBLOCKFILE_TestBit(&page->writable_blocks, block_index)) | 
|---|
| 498 | return FALSE; | 
|---|
| 499 |  | 
|---|
| 500 | BIGBLOCKFILE_SetBit(&page->readable_blocks, block_index); | 
|---|
| 501 | } | 
|---|
| 502 | else | 
|---|
| 503 | { | 
|---|
| 504 | assert(desired_access == FILE_MAP_WRITE); | 
|---|
| 505 |  | 
|---|
| 506 | if (BIGBLOCKFILE_TestBit(&page->readable_blocks, block_index)) | 
|---|
| 507 | return FALSE; | 
|---|
| 508 |  | 
|---|
| 509 | BIGBLOCKFILE_SetBit(&page->writable_blocks, block_index); | 
|---|
| 510 | } | 
|---|
| 511 |  | 
|---|
| 512 | return TRUE; | 
|---|
| 513 | } | 
|---|
| 514 |  | 
|---|
| 515 | /****************************************************************************** | 
|---|
| 516 | *      BIGBLOCKFILE_GetBigBlockPointer     [PRIVATE] | 
|---|
| 517 | * | 
|---|
| 518 | * Returns a pointer to the specified block. | 
|---|
| 519 | */ | 
|---|
| 520 | static void* BIGBLOCKFILE_GetBigBlockPointer( | 
|---|
| 521 | LPBIGBLOCKFILE This, | 
|---|
| 522 | ULONG          block_index, | 
|---|
| 523 | DWORD          desired_access) | 
|---|
| 524 | { | 
|---|
| 525 | DWORD page_index = block_index / BLOCKS_PER_PAGE; | 
|---|
| 526 | DWORD block_on_page = block_index % BLOCKS_PER_PAGE; | 
|---|
| 527 |  | 
|---|
| 528 | MappedPage *page = BIGBLOCKFILE_GetMappedView(This, page_index); | 
|---|
| 529 | if (!page || !page->lpBytes) return NULL; | 
|---|
| 530 |  | 
|---|
| 531 | if (!BIGBLOCKFILE_AccessCheck(page, block_on_page, desired_access)) | 
|---|
| 532 | { | 
|---|
| 533 | BIGBLOCKFILE_ReleaseMappedPage(This, page); | 
|---|
| 534 | return NULL; | 
|---|
| 535 | } | 
|---|
| 536 |  | 
|---|
| 537 | return (LPBYTE)page->lpBytes + (block_on_page * This->blocksize); | 
|---|
| 538 | } | 
|---|
| 539 |  | 
|---|
| 540 | /****************************************************************************** | 
|---|
| 541 | *      BIGBLOCKFILE_GetMappedPageFromPointer     [PRIVATE] | 
|---|
| 542 | * | 
|---|
| 543 | * pBlock is a pointer to a block on a page. | 
|---|
| 544 | * The page has to be on the in-use list. (As oppsed to the victim list.) | 
|---|
| 545 | * | 
|---|
| 546 | * Does not increment the usage count. | 
|---|
| 547 | */ | 
|---|
| 548 | static MappedPage *BIGBLOCKFILE_GetPageFromPointer(LPBIGBLOCKFILE This, | 
|---|
| 549 | void *pBlock) | 
|---|
| 550 | { | 
|---|
| 551 | MappedPage *page; | 
|---|
| 552 |  | 
|---|
| 553 | for (page = This->maplist; page != NULL; page = page->next) | 
|---|
| 554 | { | 
|---|
| 555 | if ((LPBYTE)pBlock >= (LPBYTE)page->lpBytes | 
|---|
| 556 | && (LPBYTE)pBlock <= (LPBYTE)page->lpBytes + PAGE_SIZE) | 
|---|
| 557 | break; | 
|---|
| 558 |  | 
|---|
| 559 | } | 
|---|
| 560 |  | 
|---|
| 561 | return page; | 
|---|
| 562 | } | 
|---|
| 563 |  | 
|---|
| 564 | /****************************************************************************** | 
|---|
| 565 | *      BIGBLOCKFILE_FindPageInList      [PRIVATE] | 
|---|
| 566 | * | 
|---|
| 567 | */ | 
|---|
| 568 | static MappedPage *BIGBLOCKFILE_FindPageInList(MappedPage *head, | 
|---|
| 569 | ULONG page_index) | 
|---|
| 570 | { | 
|---|
| 571 | for (; head != NULL; head = head->next) | 
|---|
| 572 | { | 
|---|
| 573 | if (head->page_index == page_index) | 
|---|
| 574 | { | 
|---|
| 575 | InterlockedIncrement(&head->refcnt); | 
|---|
| 576 | break; | 
|---|
| 577 | } | 
|---|
| 578 | } | 
|---|
| 579 |  | 
|---|
| 580 | return head; | 
|---|
| 581 |  | 
|---|
| 582 | } | 
|---|
| 583 |  | 
|---|
| 584 | static void BIGBLOCKFILE_UnlinkPage(MappedPage *page) | 
|---|
| 585 | { | 
|---|
| 586 | if (page->next) page->next->prev = page->prev; | 
|---|
| 587 | if (page->prev) page->prev->next = page->next; | 
|---|
| 588 | } | 
|---|
| 589 |  | 
|---|
| 590 | static void BIGBLOCKFILE_LinkHeadPage(MappedPage **head, MappedPage *page) | 
|---|
| 591 | { | 
|---|
| 592 | if (*head) (*head)->prev = page; | 
|---|
| 593 | page->next = *head; | 
|---|
| 594 | page->prev = NULL; | 
|---|
| 595 | *head = page; | 
|---|
| 596 | } | 
|---|
| 597 |  | 
|---|
| 598 | /****************************************************************************** | 
|---|
| 599 | *      BIGBLOCKFILE_GetMappedView      [PRIVATE] | 
|---|
| 600 | * | 
|---|
| 601 | * Gets the page requested if it is already mapped. | 
|---|
| 602 | * If it's not already mapped, this method will map it | 
|---|
| 603 | */ | 
|---|
| 604 | static void * BIGBLOCKFILE_GetMappedView( | 
|---|
| 605 | LPBIGBLOCKFILE This, | 
|---|
| 606 | DWORD          page_index) | 
|---|
| 607 | { | 
|---|
| 608 | MappedPage *page; | 
|---|
| 609 |  | 
|---|
| 610 | page = BIGBLOCKFILE_FindPageInList(This->maplist, page_index); | 
|---|
| 611 | if (!page) | 
|---|
| 612 | { | 
|---|
| 613 | page = BIGBLOCKFILE_FindPageInList(This->victimhead, page_index); | 
|---|
| 614 | if (page) | 
|---|
| 615 | { | 
|---|
| 616 | This->num_victim_pages--; | 
|---|
| 617 |  | 
|---|
| 618 | BIGBLOCKFILE_Zero(&page->readable_blocks); | 
|---|
| 619 | BIGBLOCKFILE_Zero(&page->writable_blocks); | 
|---|
| 620 | } | 
|---|
| 621 | } | 
|---|
| 622 |  | 
|---|
| 623 | if (page) | 
|---|
| 624 | { | 
|---|
| 625 | /* If the page is not already at the head of the list, move | 
|---|
| 626 | * it there. (Also moves pages from victim to main list.) */ | 
|---|
| 627 | if (This->maplist != page) | 
|---|
| 628 | { | 
|---|
| 629 | if (This->victimhead == page) This->victimhead = page->next; | 
|---|
| 630 | if (This->victimtail == page) This->victimtail = page->prev; | 
|---|
| 631 |  | 
|---|
| 632 | BIGBLOCKFILE_UnlinkPage(page); | 
|---|
| 633 |  | 
|---|
| 634 | BIGBLOCKFILE_LinkHeadPage(&This->maplist, page); | 
|---|
| 635 | } | 
|---|
| 636 |  | 
|---|
| 637 | return page; | 
|---|
| 638 | } | 
|---|
| 639 |  | 
|---|
| 640 | page = BIGBLOCKFILE_CreatePage(This, page_index); | 
|---|
| 641 | if (!page) return NULL; | 
|---|
| 642 |  | 
|---|
| 643 | BIGBLOCKFILE_LinkHeadPage(&This->maplist, page); | 
|---|
| 644 |  | 
|---|
| 645 | return page; | 
|---|
| 646 | } | 
|---|
| 647 |  | 
|---|
| 648 | static BOOL BIGBLOCKFILE_MapPage(LPBIGBLOCKFILE This, MappedPage *page) | 
|---|
| 649 | { | 
|---|
| 650 | DWORD lowoffset = PAGE_SIZE * page->page_index; | 
|---|
| 651 |  | 
|---|
| 652 | if (This->fileBased) | 
|---|
| 653 | { | 
|---|
| 654 | DWORD numBytesToMap; | 
|---|
| 655 | DWORD desired_access; | 
|---|
| 656 |  | 
|---|
| 657 | if (lowoffset + PAGE_SIZE > This->filesize.s.LowPart) | 
|---|
| 658 | numBytesToMap = This->filesize.s.LowPart - lowoffset; | 
|---|
| 659 | else | 
|---|
| 660 | numBytesToMap = PAGE_SIZE; | 
|---|
| 661 |  | 
|---|
| 662 | if (This->flProtect == PAGE_READONLY) | 
|---|
| 663 | desired_access = FILE_MAP_READ; | 
|---|
| 664 | else | 
|---|
| 665 | desired_access = FILE_MAP_WRITE; | 
|---|
| 666 |  | 
|---|
| 667 | page->lpBytes = MapViewOfFile(This->hfilemap, desired_access, 0, | 
|---|
| 668 | lowoffset, numBytesToMap); | 
|---|
| 669 | } | 
|---|
| 670 | else | 
|---|
| 671 | { | 
|---|
| 672 | page->lpBytes = (LPBYTE)This->pbytearray + lowoffset; | 
|---|
| 673 | } | 
|---|
| 674 |  | 
|---|
| 675 | TRACE("mapped page %lu to %p\n", page->page_index, page->lpBytes); | 
|---|
| 676 |  | 
|---|
| 677 | return page->lpBytes != NULL; | 
|---|
| 678 | } | 
|---|
| 679 |  | 
|---|
| 680 | static MappedPage *BIGBLOCKFILE_CreatePage(LPBIGBLOCKFILE This, | 
|---|
| 681 | ULONG page_index) | 
|---|
| 682 | { | 
|---|
| 683 | MappedPage *page; | 
|---|
| 684 |  | 
|---|
| 685 | page = HeapAlloc(GetProcessHeap(), 0, sizeof(MappedPage)); | 
|---|
| 686 | if (page == NULL) | 
|---|
| 687 | return NULL; | 
|---|
| 688 |  | 
|---|
| 689 | page->page_index = page_index; | 
|---|
| 690 | page->refcnt = 1; | 
|---|
| 691 |  | 
|---|
| 692 | page->next = NULL; | 
|---|
| 693 | page->prev = NULL; | 
|---|
| 694 |  | 
|---|
| 695 | BIGBLOCKFILE_MapPage(This, page); | 
|---|
| 696 |  | 
|---|
| 697 | BIGBLOCKFILE_Zero(&page->readable_blocks); | 
|---|
| 698 | BIGBLOCKFILE_Zero(&page->writable_blocks); | 
|---|
| 699 |  | 
|---|
| 700 | return page; | 
|---|
| 701 | } | 
|---|
| 702 |  | 
|---|
| 703 | static void BIGBLOCKFILE_UnmapPage(LPBIGBLOCKFILE This, MappedPage *page) | 
|---|
| 704 | { | 
|---|
| 705 | TRACE("%ld at %p\n", page->page_index, page->lpBytes); | 
|---|
| 706 | if (page->refcnt > 0) | 
|---|
| 707 | ERR("unmapping inuse page %p\n", page->lpBytes); | 
|---|
| 708 |  | 
|---|
| 709 | if (This->fileBased && page->lpBytes) | 
|---|
| 710 | UnmapViewOfFile(page->lpBytes); | 
|---|
| 711 |  | 
|---|
| 712 | page->lpBytes = NULL; | 
|---|
| 713 | } | 
|---|
| 714 |  | 
|---|
| 715 | static void BIGBLOCKFILE_DeletePage(LPBIGBLOCKFILE This, MappedPage *page) | 
|---|
| 716 | { | 
|---|
| 717 | BIGBLOCKFILE_UnmapPage(This, page); | 
|---|
| 718 |  | 
|---|
| 719 | HeapFree(GetProcessHeap(), 0, page); | 
|---|
| 720 | } | 
|---|
| 721 |  | 
|---|
| 722 | /****************************************************************************** | 
|---|
| 723 | *      BIGBLOCKFILE_ReleaseMappedPage      [PRIVATE] | 
|---|
| 724 | * | 
|---|
| 725 | * Decrements the reference count of the mapped page. | 
|---|
| 726 | */ | 
|---|
| 727 | static void BIGBLOCKFILE_ReleaseMappedPage( | 
|---|
| 728 | LPBIGBLOCKFILE This, | 
|---|
| 729 | MappedPage    *page) | 
|---|
| 730 | { | 
|---|
| 731 | assert(This != NULL); | 
|---|
| 732 | assert(page != NULL); | 
|---|
| 733 |  | 
|---|
| 734 | /* If the page is no longer refenced, move it to the victim list. | 
|---|
| 735 | * If the victim list is too long, kick somebody off. */ | 
|---|
| 736 | if (!InterlockedDecrement(&page->refcnt)) | 
|---|
| 737 | { | 
|---|
| 738 | if (This->maplist == page) This->maplist = page->next; | 
|---|
| 739 |  | 
|---|
| 740 | BIGBLOCKFILE_UnlinkPage(page); | 
|---|
| 741 |  | 
|---|
| 742 | if (MAX_VICTIM_PAGES > 0) | 
|---|
| 743 | { | 
|---|
| 744 | if (This->num_victim_pages >= MAX_VICTIM_PAGES) | 
|---|
| 745 | { | 
|---|
| 746 | MappedPage *victim = This->victimtail; | 
|---|
| 747 | if (victim) | 
|---|
| 748 | { | 
|---|
| 749 | This->victimtail = victim->prev; | 
|---|
| 750 | if (This->victimhead == victim) | 
|---|
| 751 | This->victimhead = victim->next; | 
|---|
| 752 |  | 
|---|
| 753 | BIGBLOCKFILE_UnlinkPage(victim); | 
|---|
| 754 | BIGBLOCKFILE_DeletePage(This, victim); | 
|---|
| 755 | } | 
|---|
| 756 | } | 
|---|
| 757 | else This->num_victim_pages++; | 
|---|
| 758 |  | 
|---|
| 759 | BIGBLOCKFILE_LinkHeadPage(&This->victimhead, page); | 
|---|
| 760 | if (This->victimtail == NULL) This->victimtail = page; | 
|---|
| 761 | } | 
|---|
| 762 | else | 
|---|
| 763 | BIGBLOCKFILE_DeletePage(This, page); | 
|---|
| 764 | } | 
|---|
| 765 | } | 
|---|
| 766 |  | 
|---|
| 767 | static void BIGBLOCKFILE_DeleteList(LPBIGBLOCKFILE This, MappedPage *list) | 
|---|
| 768 | { | 
|---|
| 769 | while (list != NULL) | 
|---|
| 770 | { | 
|---|
| 771 | MappedPage *next = list->next; | 
|---|
| 772 |  | 
|---|
| 773 | BIGBLOCKFILE_DeletePage(This, list); | 
|---|
| 774 |  | 
|---|
| 775 | list = next; | 
|---|
| 776 | } | 
|---|
| 777 | } | 
|---|
| 778 |  | 
|---|
| 779 | /****************************************************************************** | 
|---|
| 780 | *      BIGBLOCKFILE_FreeAllMappedPages     [PRIVATE] | 
|---|
| 781 | * | 
|---|
| 782 | * Unmap all currently mapped pages. | 
|---|
| 783 | * Empty mapped pages list. | 
|---|
| 784 | */ | 
|---|
| 785 | static void BIGBLOCKFILE_FreeAllMappedPages( | 
|---|
| 786 | LPBIGBLOCKFILE This) | 
|---|
| 787 | { | 
|---|
| 788 | BIGBLOCKFILE_DeleteList(This, This->maplist); | 
|---|
| 789 | BIGBLOCKFILE_DeleteList(This, This->victimhead); | 
|---|
| 790 |  | 
|---|
| 791 | This->maplist = NULL; | 
|---|
| 792 | This->victimhead = NULL; | 
|---|
| 793 | This->victimtail = NULL; | 
|---|
| 794 | This->num_victim_pages = 0; | 
|---|
| 795 | } | 
|---|
| 796 |  | 
|---|
| 797 | static void BIGBLOCKFILE_UnmapList(LPBIGBLOCKFILE This, MappedPage *list) | 
|---|
| 798 | { | 
|---|
| 799 | for (; list != NULL; list = list->next) | 
|---|
| 800 | { | 
|---|
| 801 | BIGBLOCKFILE_UnmapPage(This, list); | 
|---|
| 802 | } | 
|---|
| 803 | } | 
|---|
| 804 |  | 
|---|
| 805 | static void BIGBLOCKFILE_UnmapAllMappedPages(LPBIGBLOCKFILE This) | 
|---|
| 806 | { | 
|---|
| 807 | BIGBLOCKFILE_UnmapList(This, This->maplist); | 
|---|
| 808 | BIGBLOCKFILE_UnmapList(This, This->victimhead); | 
|---|
| 809 | } | 
|---|
| 810 |  | 
|---|
| 811 | static void BIGBLOCKFILE_RemapList(LPBIGBLOCKFILE This, MappedPage *list) | 
|---|
| 812 | { | 
|---|
| 813 | while (list != NULL) | 
|---|
| 814 | { | 
|---|
| 815 | MappedPage *next = list->next; | 
|---|
| 816 |  | 
|---|
| 817 | if (list->page_index * PAGE_SIZE > This->filesize.s.LowPart) | 
|---|
| 818 | { | 
|---|
| 819 | TRACE("discarding %lu\n", list->page_index); | 
|---|
| 820 |  | 
|---|
| 821 | /* page is entirely outside of the file, delete it */ | 
|---|
| 822 | BIGBLOCKFILE_UnlinkPage(list); | 
|---|
| 823 | BIGBLOCKFILE_DeletePage(This, list); | 
|---|
| 824 | } | 
|---|
| 825 | else | 
|---|
| 826 | { | 
|---|
| 827 | /* otherwise, remap it */ | 
|---|
| 828 | BIGBLOCKFILE_MapPage(This, list); | 
|---|
| 829 | } | 
|---|
| 830 |  | 
|---|
| 831 | list = next; | 
|---|
| 832 | } | 
|---|
| 833 | } | 
|---|
| 834 |  | 
|---|
| 835 | static void BIGBLOCKFILE_RemapAllMappedPages(LPBIGBLOCKFILE This) | 
|---|
| 836 | { | 
|---|
| 837 | BIGBLOCKFILE_RemapList(This, This->maplist); | 
|---|
| 838 | BIGBLOCKFILE_RemapList(This, This->victimhead); | 
|---|
| 839 | } | 
|---|
| 840 |  | 
|---|
| 841 | /**************************************************************************** | 
|---|
| 842 | *      BIGBLOCKFILE_GetProtectMode | 
|---|
| 843 | * | 
|---|
| 844 | * This function will return a protection mode flag for a file-mapping object | 
|---|
| 845 | * from the open flags of a file. | 
|---|
| 846 | */ | 
|---|
| 847 | static DWORD BIGBLOCKFILE_GetProtectMode(DWORD openFlags) | 
|---|
| 848 | { | 
|---|
| 849 | if (openFlags & (STGM_WRITE | STGM_READWRITE)) | 
|---|
| 850 | return PAGE_READWRITE; | 
|---|
| 851 | else | 
|---|
| 852 | return PAGE_READONLY; | 
|---|
| 853 | } | 
|---|