source: trunk/src/ole32/stg_bigblockfile.c@ 7505

Last change on this file since 7505 was 7502, checked in by phaller, 24 years ago

Fixed out-of-scope FIXME,TRACE,WARN macros

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