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