Make our ascii strings static const.
[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   This->filesize.u.LowPart = GetFileSize(This->hfile,
226                                          &This->filesize.u.HighPart);
227
228   if( This->filesize.u.LowPart || This->filesize.u.HighPart )
229   {
230     /* create the file mapping object
231      */
232     This->hfilemap = CreateFileMappingA(This->hfile,
233                                         NULL,
234                                         This->flProtect,
235                                         0, 0,
236                                         NULL);
237
238     if (!This->hfilemap)
239     {
240       CloseHandle(This->hfile);
241       return FALSE;
242     }
243   }
244   else
245     This->hfilemap = NULL;
246
247   This->maplist = NULL;
248
249   TRACE("file len %lu\n", This->filesize.u.LowPart);
250
251   return TRUE;
252 }
253
254 /******************************************************************************
255  *      BIGBLOCKFILE_MemInit
256  *
257  * Initialize a big block object supported by an ILockBytes on HGLOABL.
258  */
259 static BOOL BIGBLOCKFILE_MemInit(LPBIGBLOCKFILE This, ILockBytes* plkbyt)
260 {
261   This->hfile       = 0;
262   This->hfilemap    = 0;
263
264   /*
265    * Retrieve the handle to the byte array from the LockByte object.
266    */
267   if (GetHGlobalFromILockBytes(plkbyt, &(This->hbytearray)) != S_OK)
268   {
269     FIXME("May not be an ILockBytes on HGLOBAL\n");
270     return FALSE;
271   }
272
273   This->pLkbyt = plkbyt;
274
275   /*
276    * Increment the reference count of the ILockByte object since
277    * we're keeping a reference to it.
278    */
279   ILockBytes_AddRef(This->pLkbyt);
280
281   This->filesize.u.LowPart = GlobalSize(This->hbytearray);
282   This->filesize.u.HighPart = 0;
283
284   This->pbytearray = GlobalLock(This->hbytearray);
285
286   TRACE("mem on %p len %lu\n", This->pbytearray, This->filesize.u.LowPart);
287
288   return TRUE;
289 }
290
291 /******************************************************************************
292  *      BIGBLOCKFILE_Destructor
293  *
294  * Destructor. Clean up, free memory.
295  */
296 void BIGBLOCKFILE_Destructor(
297   LPBIGBLOCKFILE This)
298 {
299   BIGBLOCKFILE_FreeAllMappedPages(This);
300
301   if (This->fileBased)
302   {
303     CloseHandle(This->hfilemap);
304     CloseHandle(This->hfile);
305   }
306   else
307   {
308     GlobalUnlock(This->hbytearray);
309     ILockBytes_Release(This->pLkbyt);
310   }
311
312   /* destroy this
313    */
314   HeapFree(GetProcessHeap(), 0, This);
315 }
316
317 /******************************************************************************
318  *      BIGBLOCKFILE_GetROBigBlock
319  *
320  * Returns the specified block in read only mode.
321  * Will return NULL if the block doesn't exists.
322  */
323 void* BIGBLOCKFILE_GetROBigBlock(
324   LPBIGBLOCKFILE This,
325   ULONG          index)
326 {
327   /*
328    * block index starts at -1
329    * translate to zero based index
330    */
331   if (index == 0xffffffff)
332     index = 0;
333   else
334     index++;
335
336   /*
337    * validate the block index
338    *
339    */
340   if (This->blocksize * (index + 1)
341       > ROUND_UP(This->filesize.u.LowPart, This->blocksize))
342   {
343     TRACE("out of range %lu vs %lu\n", This->blocksize * (index + 1),
344           This->filesize.u.LowPart);
345     return NULL;
346   }
347
348   return BIGBLOCKFILE_GetBigBlockPointer(This, index, FILE_MAP_READ);
349 }
350
351 /******************************************************************************
352  *      BIGBLOCKFILE_GetBigBlock
353  *
354  * Returns the specified block.
355  * Will grow the file if necessary.
356  */
357 void* BIGBLOCKFILE_GetBigBlock(LPBIGBLOCKFILE This, ULONG index)
358 {
359   /*
360    * block index starts at -1
361    * translate to zero based index
362    */
363   if (index == 0xffffffff)
364     index = 0;
365   else
366     index++;
367
368   /*
369    * make sure that the block physically exists
370    */
371   if ((This->blocksize * (index + 1)) > This->filesize.u.LowPart)
372   {
373     ULARGE_INTEGER newSize;
374
375     newSize.u.HighPart = 0;
376     newSize.u.LowPart = This->blocksize * (index + 1);
377
378     BIGBLOCKFILE_SetSize(This, newSize);
379   }
380
381   return BIGBLOCKFILE_GetBigBlockPointer(This, index, FILE_MAP_WRITE);
382 }
383
384 /******************************************************************************
385  *      BIGBLOCKFILE_ReleaseBigBlock
386  *
387  * Releases the specified block.
388  */
389 void BIGBLOCKFILE_ReleaseBigBlock(LPBIGBLOCKFILE This, void *pBlock)
390 {
391     MappedPage *page;
392
393     if (pBlock == NULL)
394         return;
395
396     page = BIGBLOCKFILE_GetPageFromPointer(This, pBlock);
397
398     if (page == NULL)
399         return;
400
401     BIGBLOCKFILE_ReleaseMappedPage(This, page);
402 }
403
404 /******************************************************************************
405  *      BIGBLOCKFILE_SetSize
406  *
407  * Sets the size of the file.
408  *
409  */
410 void BIGBLOCKFILE_SetSize(LPBIGBLOCKFILE This, ULARGE_INTEGER newSize)
411 {
412   if (This->filesize.u.LowPart == newSize.u.LowPart)
413     return;
414
415   TRACE("from %lu to %lu\n", This->filesize.u.LowPart, newSize.u.LowPart);
416   /*
417    * unmap all views, must be done before call to SetEndFile
418    */
419   BIGBLOCKFILE_UnmapAllMappedPages(This);
420
421   if (This->fileBased)
422   {
423     char buf[10];
424
425     /*
426      * close file-mapping object, must be done before call to SetEndFile
427      */
428     if( This->hfilemap )
429       CloseHandle(This->hfilemap);
430     This->hfilemap = 0;
431
432     /*
433      * BEGIN HACK
434      * This fixes a bug when saving through smbfs.
435      * smbmount a Windows shared directory, save a structured storage file
436      * to that dir: crash.
437      *
438      * The problem is that the SetFilePointer-SetEndOfFile combo below
439      * doesn't always succeed. The file is not grown. It seems like the
440      * operation is cached. By doing the WriteFile, the file is actually
441      * grown on disk.
442      * This hack is only needed when saving to smbfs.
443      */
444     memset(buf, '0', 10);
445     SetFilePointer(This->hfile, newSize.u.LowPart, NULL, FILE_BEGIN);
446     WriteFile(This->hfile, buf, 10, NULL, NULL);
447     /*
448      * END HACK
449      */
450
451     /*
452      * set the new end of file
453      */
454     SetFilePointer(This->hfile, newSize.u.LowPart, NULL, FILE_BEGIN);
455     SetEndOfFile(This->hfile);
456
457     /*
458      * re-create the file mapping object
459      */
460     This->hfilemap = CreateFileMappingA(This->hfile,
461                                         NULL,
462                                         This->flProtect,
463                                         0, 0,
464                                         NULL);
465   }
466   else
467   {
468     GlobalUnlock(This->hbytearray);
469
470     /*
471      * Resize the byte array object.
472      */
473     ILockBytes_SetSize(This->pLkbyt, newSize);
474
475     /*
476      * Re-acquire the handle, it may have changed.
477      */
478     GetHGlobalFromILockBytes(This->pLkbyt, &This->hbytearray);
479     This->pbytearray = GlobalLock(This->hbytearray);
480   }
481
482   This->filesize.u.LowPart = newSize.u.LowPart;
483   This->filesize.u.HighPart = newSize.u.HighPart;
484
485   BIGBLOCKFILE_RemapAllMappedPages(This);
486 }
487
488 /******************************************************************************
489  *      BIGBLOCKFILE_GetSize
490  *
491  * Returns the size of the file.
492  *
493  */
494 ULARGE_INTEGER BIGBLOCKFILE_GetSize(LPBIGBLOCKFILE This)
495 {
496   return This->filesize;
497 }
498
499 /******************************************************************************
500  *      BIGBLOCKFILE_AccessCheck     [PRIVATE]
501  *
502  * block_index is the index within the page.
503  */
504 static BOOL BIGBLOCKFILE_AccessCheck(MappedPage *page, ULONG block_index,
505                                      DWORD desired_access)
506 {
507     assert(block_index < BLOCKS_PER_PAGE);
508
509     if (desired_access == FILE_MAP_READ)
510     {
511         if (BIGBLOCKFILE_TestBit(&page->writable_blocks, block_index))
512             return FALSE;
513
514         BIGBLOCKFILE_SetBit(&page->readable_blocks, block_index);
515     }
516     else
517     {
518         assert(desired_access == FILE_MAP_WRITE);
519
520         if (BIGBLOCKFILE_TestBit(&page->readable_blocks, block_index))
521             return FALSE;
522
523         BIGBLOCKFILE_SetBit(&page->writable_blocks, block_index);
524     }
525
526     return TRUE;
527 }
528
529 /******************************************************************************
530  *      BIGBLOCKFILE_GetBigBlockPointer     [PRIVATE]
531  *
532  * Returns a pointer to the specified block.
533  */
534 static void* BIGBLOCKFILE_GetBigBlockPointer(
535   LPBIGBLOCKFILE This,
536   ULONG          block_index,
537   DWORD          desired_access)
538 {
539     DWORD page_index = block_index / BLOCKS_PER_PAGE;
540     DWORD block_on_page = block_index % BLOCKS_PER_PAGE;
541
542     MappedPage *page = BIGBLOCKFILE_GetMappedView(This, page_index);
543     if (!page || !page->lpBytes) return NULL;
544
545     if (!BIGBLOCKFILE_AccessCheck(page, block_on_page, desired_access))
546     {
547         BIGBLOCKFILE_ReleaseMappedPage(This, page);
548         return NULL;
549     }
550
551     return (LPBYTE)page->lpBytes + (block_on_page * This->blocksize);
552 }
553
554 /******************************************************************************
555  *      BIGBLOCKFILE_GetMappedPageFromPointer     [PRIVATE]
556  *
557  * pBlock is a pointer to a block on a page.
558  * The page has to be on the in-use list. (As oppsed to the victim list.)
559  *
560  * Does not increment the usage count.
561  */
562 static MappedPage *BIGBLOCKFILE_GetPageFromPointer(LPBIGBLOCKFILE This,
563                                                    void *pBlock)
564 {
565     MappedPage *page;
566
567     for (page = This->maplist; page != NULL; page = page->next)
568     {
569         if ((LPBYTE)pBlock >= (LPBYTE)page->lpBytes
570             && (LPBYTE)pBlock <= (LPBYTE)page->lpBytes + PAGE_SIZE)
571             break;
572
573     }
574
575     return page;
576 }
577
578 /******************************************************************************
579  *      BIGBLOCKFILE_FindPageInList      [PRIVATE]
580  *
581  */
582 static MappedPage *BIGBLOCKFILE_FindPageInList(MappedPage *head,
583                                                ULONG page_index)
584 {
585     for (; head != NULL; head = head->next)
586     {
587         if (head->page_index == page_index)
588         {
589             InterlockedIncrement(&head->refcnt);
590             break;
591         }
592     }
593
594     return head;
595
596 }
597
598 static void BIGBLOCKFILE_UnlinkPage(MappedPage *page)
599 {
600     if (page->next) page->next->prev = page->prev;
601     if (page->prev) page->prev->next = page->next;
602 }
603
604 static void BIGBLOCKFILE_LinkHeadPage(MappedPage **head, MappedPage *page)
605 {
606     if (*head) (*head)->prev = page;
607     page->next = *head;
608     page->prev = NULL;
609     *head = page;
610 }
611
612 /******************************************************************************
613  *      BIGBLOCKFILE_GetMappedView      [PRIVATE]
614  *
615  * Gets the page requested if it is already mapped.
616  * If it's not already mapped, this method will map it
617  */
618 static void * BIGBLOCKFILE_GetMappedView(
619   LPBIGBLOCKFILE This,
620   DWORD          page_index)
621 {
622     MappedPage *page;
623
624     page = BIGBLOCKFILE_FindPageInList(This->maplist, page_index);
625     if (!page)
626     {
627         page = BIGBLOCKFILE_FindPageInList(This->victimhead, page_index);
628         if (page)
629         {
630             This->num_victim_pages--;
631
632             BIGBLOCKFILE_Zero(&page->readable_blocks);
633             BIGBLOCKFILE_Zero(&page->writable_blocks);
634         }
635     }
636
637     if (page)
638     {
639         /* If the page is not already at the head of the list, move
640          * it there. (Also moves pages from victim to main list.) */
641         if (This->maplist != page)
642         {
643             if (This->victimhead == page) This->victimhead = page->next;
644             if (This->victimtail == page) This->victimtail = page->prev;
645
646             BIGBLOCKFILE_UnlinkPage(page);
647
648             BIGBLOCKFILE_LinkHeadPage(&This->maplist, page);
649         }
650
651         return page;
652     }
653
654     page = BIGBLOCKFILE_CreatePage(This, page_index);
655     if (!page) return NULL;
656
657     BIGBLOCKFILE_LinkHeadPage(&This->maplist, page);
658
659     return page;
660 }
661
662 static BOOL BIGBLOCKFILE_MapPage(LPBIGBLOCKFILE This, MappedPage *page)
663 {
664     DWORD lowoffset = PAGE_SIZE * page->page_index;
665
666     if (This->fileBased)
667     {
668         DWORD numBytesToMap;
669         DWORD desired_access;
670
671         if( !This->hfilemap )
672             return FALSE;
673
674         if (lowoffset + PAGE_SIZE > This->filesize.u.LowPart)
675             numBytesToMap = This->filesize.u.LowPart - lowoffset;
676         else
677             numBytesToMap = PAGE_SIZE;
678
679         if (This->flProtect == PAGE_READONLY)
680             desired_access = FILE_MAP_READ;
681         else
682             desired_access = FILE_MAP_WRITE;
683
684         page->lpBytes = MapViewOfFile(This->hfilemap, desired_access, 0,
685                                       lowoffset, numBytesToMap);
686     }
687     else
688     {
689         page->lpBytes = (LPBYTE)This->pbytearray + lowoffset;
690     }
691
692     TRACE("mapped page %lu to %p\n", page->page_index, page->lpBytes);
693
694     return page->lpBytes != NULL;
695 }
696
697 static MappedPage *BIGBLOCKFILE_CreatePage(LPBIGBLOCKFILE This,
698                                            ULONG page_index)
699 {
700     MappedPage *page;
701
702     page = HeapAlloc(GetProcessHeap(), 0, sizeof(MappedPage));
703     if (page == NULL)
704       return NULL;
705
706     page->page_index = page_index;
707     page->refcnt = 1;
708
709     page->next = NULL;
710     page->prev = NULL;
711
712     BIGBLOCKFILE_MapPage(This, page);
713
714     BIGBLOCKFILE_Zero(&page->readable_blocks);
715     BIGBLOCKFILE_Zero(&page->writable_blocks);
716
717     return page;
718 }
719
720 static void BIGBLOCKFILE_UnmapPage(LPBIGBLOCKFILE This, MappedPage *page)
721 {
722     TRACE("%ld at %p\n", page->page_index, page->lpBytes);
723     if (page->refcnt > 0)
724         ERR("unmapping inuse page %p\n", page->lpBytes);
725
726     if (This->fileBased && page->lpBytes)
727         UnmapViewOfFile(page->lpBytes);
728
729     page->lpBytes = NULL;
730 }
731
732 static void BIGBLOCKFILE_DeletePage(LPBIGBLOCKFILE This, MappedPage *page)
733 {
734     BIGBLOCKFILE_UnmapPage(This, page);
735
736     HeapFree(GetProcessHeap(), 0, page);
737 }
738
739 /******************************************************************************
740  *      BIGBLOCKFILE_ReleaseMappedPage      [PRIVATE]
741  *
742  * Decrements the reference count of the mapped page.
743  */
744 static void BIGBLOCKFILE_ReleaseMappedPage(
745   LPBIGBLOCKFILE This,
746   MappedPage    *page)
747 {
748     assert(This != NULL);
749     assert(page != NULL);
750
751     /* If the page is no longer refenced, move it to the victim list.
752      * If the victim list is too long, kick somebody off. */
753     if (!InterlockedDecrement(&page->refcnt))
754     {
755         if (This->maplist == page) This->maplist = page->next;
756
757         BIGBLOCKFILE_UnlinkPage(page);
758
759         if (MAX_VICTIM_PAGES > 0)
760         {
761             if (This->num_victim_pages >= MAX_VICTIM_PAGES)
762             {
763                 MappedPage *victim = This->victimtail;
764                 if (victim)
765                 {
766                     This->victimtail = victim->prev;
767                     if (This->victimhead == victim)
768                         This->victimhead = victim->next;
769
770                     BIGBLOCKFILE_UnlinkPage(victim);
771                     BIGBLOCKFILE_DeletePage(This, victim);
772                 }
773             }
774             else This->num_victim_pages++;
775
776             BIGBLOCKFILE_LinkHeadPage(&This->victimhead, page);
777             if (This->victimtail == NULL) This->victimtail = page;
778         }
779         else
780             BIGBLOCKFILE_DeletePage(This, page);
781     }
782 }
783
784 static void BIGBLOCKFILE_DeleteList(LPBIGBLOCKFILE This, MappedPage *list)
785 {
786     while (list != NULL)
787     {
788         MappedPage *next = list->next;
789
790         BIGBLOCKFILE_DeletePage(This, list);
791
792         list = next;
793     }
794 }
795
796 /******************************************************************************
797  *      BIGBLOCKFILE_FreeAllMappedPages     [PRIVATE]
798  *
799  * Unmap all currently mapped pages.
800  * Empty mapped pages list.
801  */
802 static void BIGBLOCKFILE_FreeAllMappedPages(
803   LPBIGBLOCKFILE This)
804 {
805     BIGBLOCKFILE_DeleteList(This, This->maplist);
806     BIGBLOCKFILE_DeleteList(This, This->victimhead);
807
808     This->maplist = NULL;
809     This->victimhead = NULL;
810     This->victimtail = NULL;
811     This->num_victim_pages = 0;
812 }
813
814 static void BIGBLOCKFILE_UnmapList(LPBIGBLOCKFILE This, MappedPage *list)
815 {
816     for (; list != NULL; list = list->next)
817     {
818         BIGBLOCKFILE_UnmapPage(This, list);
819     }
820 }
821
822 static void BIGBLOCKFILE_UnmapAllMappedPages(LPBIGBLOCKFILE This)
823 {
824     BIGBLOCKFILE_UnmapList(This, This->maplist);
825     BIGBLOCKFILE_UnmapList(This, This->victimhead);
826 }
827
828 static void BIGBLOCKFILE_RemapList(LPBIGBLOCKFILE This, MappedPage *list)
829 {
830     while (list != NULL)
831     {
832         MappedPage *next = list->next;
833
834         if (list->page_index * PAGE_SIZE > This->filesize.u.LowPart)
835         {
836             TRACE("discarding %lu\n", list->page_index);
837
838             /* page is entirely outside of the file, delete it */
839             BIGBLOCKFILE_UnlinkPage(list);
840             BIGBLOCKFILE_DeletePage(This, list);
841         }
842         else
843         {
844             /* otherwise, remap it */
845             BIGBLOCKFILE_MapPage(This, list);
846         }
847
848         list = next;
849     }
850 }
851
852 static void BIGBLOCKFILE_RemapAllMappedPages(LPBIGBLOCKFILE This)
853 {
854     BIGBLOCKFILE_RemapList(This, This->maplist);
855     BIGBLOCKFILE_RemapList(This, This->victimhead);
856 }
857
858 /****************************************************************************
859  *      BIGBLOCKFILE_GetProtectMode
860  *
861  * This function will return a protection mode flag for a file-mapping object
862  * from the open flags of a file.
863  */
864 static DWORD BIGBLOCKFILE_GetProtectMode(DWORD openFlags)
865 {
866     if (openFlags & (STGM_WRITE | STGM_READWRITE))
867         return PAGE_READWRITE;
868     else
869         return PAGE_READONLY;
870 }