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