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