1 /******************************************************************************
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
15 * - Support for a transacted mode
17 * Copyright 1999 Thuy Nguyen
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.
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.
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
42 #define NONAMELESSUNION
43 #define NONAMELESSSTRUCT
52 #include "storage32.h"
54 #include "wine/debug.h"
56 WINE_DEFAULT_DEBUG_CHANNEL(storage);
58 /***********************************************************
59 * Data structures used internally by the BigBlockFile
63 /* We map in PAGE_SIZE-sized chunks. Must be a multiple of 4096. */
64 #define PAGE_SIZE 131072
66 #define BLOCKS_PER_PAGE (PAGE_SIZE / BIG_BLOCK_SIZE)
68 /* We keep a list of recently-discarded pages. This controls the
69 * size of that list. */
70 #define MAX_VICTIM_PAGES 16
72 /* This structure provides one bit for each block in a page.
73 * Use BIGBLOCKFILE_{Test,Set,Clear}Bit to manipulate it. */
76 unsigned int bits[BLOCKS_PER_PAGE / (CHAR_BIT * sizeof(unsigned int))];
80 * This structure identifies the paged that are mapped
81 * from the file and their position in memory. It is
82 * also used to hold a reference count to those pages.
84 * page_index identifies which PAGE_SIZE chunk from the
85 * file this mapping represents. (The mappings are always
97 BlockBits readable_blocks;
98 BlockBits writable_blocks;
101 /***********************************************************
102 * Prototypes for private methods
104 static void* BIGBLOCKFILE_GetMappedView(LPBIGBLOCKFILE This,
106 static void BIGBLOCKFILE_ReleaseMappedPage(LPBIGBLOCKFILE This,
108 static void BIGBLOCKFILE_FreeAllMappedPages(LPBIGBLOCKFILE This);
109 static void BIGBLOCKFILE_UnmapAllMappedPages(LPBIGBLOCKFILE This);
110 static void BIGBLOCKFILE_RemapAllMappedPages(LPBIGBLOCKFILE This);
111 static void* BIGBLOCKFILE_GetBigBlockPointer(LPBIGBLOCKFILE This,
113 DWORD desired_access);
114 static MappedPage* BIGBLOCKFILE_GetPageFromPointer(LPBIGBLOCKFILE This,
116 static MappedPage* BIGBLOCKFILE_CreatePage(LPBIGBLOCKFILE This,
118 static DWORD BIGBLOCKFILE_GetProtectMode(DWORD openFlags);
119 static BOOL BIGBLOCKFILE_FileInit(LPBIGBLOCKFILE This, HANDLE hFile);
120 static BOOL BIGBLOCKFILE_MemInit(LPBIGBLOCKFILE This, ILockBytes* plkbyt);
122 /* Note that this evaluates a and b multiple times, so don't
123 * pass expressions with side effects. */
124 #define ROUND_UP(a, b) ((((a) + (b) - 1)/(b))*(b))
126 /***********************************************************
127 * Blockbits functions.
129 static inline BOOL BIGBLOCKFILE_TestBit(const BlockBits *bb,
132 unsigned int array_index = index / (CHAR_BIT * sizeof(unsigned int));
133 unsigned int bit_index = index % (CHAR_BIT * sizeof(unsigned int));
135 return bb->bits[array_index] & (1 << bit_index);
138 static inline void BIGBLOCKFILE_SetBit(BlockBits *bb, unsigned int index)
140 unsigned int array_index = index / (CHAR_BIT * sizeof(unsigned int));
141 unsigned int bit_index = index % (CHAR_BIT * sizeof(unsigned int));
143 bb->bits[array_index] |= (1 << bit_index);
146 static inline void BIGBLOCKFILE_ClearBit(BlockBits *bb, unsigned int index)
148 unsigned int array_index = index / (CHAR_BIT * sizeof(unsigned int));
149 unsigned int bit_index = index % (CHAR_BIT * sizeof(unsigned int));
151 bb->bits[array_index] &= ~(1 << bit_index);
154 static inline void BIGBLOCKFILE_Zero(BlockBits *bb)
156 memset(bb->bits, 0, sizeof(bb->bits));
159 /******************************************************************************
160 * BIGBLOCKFILE_Construct
162 * Construct a big block file. Create the file mapping object.
163 * Create the read only mapped pages list, the writable mapped page list
164 * and the blocks in use list.
166 BigBlockFile * BIGBLOCKFILE_Construct(
175 This = HeapAlloc(GetProcessHeap(), 0, sizeof(BigBlockFile));
180 This->fileBased = fileBased;
182 This->flProtect = BIGBLOCKFILE_GetProtectMode(openFlags);
184 This->blocksize = blocksize;
186 This->maplist = NULL;
187 This->victimhead = NULL;
188 This->victimtail = NULL;
189 This->num_victim_pages = 0;
193 if (!BIGBLOCKFILE_FileInit(This, hFile))
195 HeapFree(GetProcessHeap(), 0, This);
201 if (!BIGBLOCKFILE_MemInit(This, pLkByt))
203 HeapFree(GetProcessHeap(), 0, This);
211 /******************************************************************************
212 * BIGBLOCKFILE_FileInit
214 * Initialize a big block object supported by a file.
216 static BOOL BIGBLOCKFILE_FileInit(LPBIGBLOCKFILE This, HANDLE hFile)
219 This->hbytearray = 0;
220 This->pbytearray = NULL;
224 if (This->hfile == INVALID_HANDLE_VALUE)
227 This->filesize.u.LowPart = GetFileSize(This->hfile,
228 &This->filesize.u.HighPart);
230 if( This->filesize.u.LowPart || This->filesize.u.HighPart )
232 /* create the file mapping object
234 This->hfilemap = CreateFileMappingA(This->hfile,
242 CloseHandle(This->hfile);
247 This->hfilemap = NULL;
249 This->maplist = NULL;
251 TRACE("file len %lu\n", This->filesize.u.LowPart);
256 /******************************************************************************
257 * BIGBLOCKFILE_MemInit
259 * Initialize a big block object supported by an ILockBytes on HGLOABL.
261 static BOOL BIGBLOCKFILE_MemInit(LPBIGBLOCKFILE This, ILockBytes* plkbyt)
267 * Retrieve the handle to the byte array from the LockByte object.
269 if (GetHGlobalFromILockBytes(plkbyt, &(This->hbytearray)) != S_OK)
271 FIXME("May not be an ILockBytes on HGLOBAL\n");
275 This->pLkbyt = plkbyt;
278 * Increment the reference count of the ILockByte object since
279 * we're keeping a reference to it.
281 ILockBytes_AddRef(This->pLkbyt);
283 This->filesize.u.LowPart = GlobalSize(This->hbytearray);
284 This->filesize.u.HighPart = 0;
286 This->pbytearray = GlobalLock(This->hbytearray);
288 TRACE("mem on %p len %lu\n", This->pbytearray, This->filesize.u.LowPart);
293 /******************************************************************************
294 * BIGBLOCKFILE_Destructor
296 * Destructor. Clean up, free memory.
298 void BIGBLOCKFILE_Destructor(
301 BIGBLOCKFILE_FreeAllMappedPages(This);
305 CloseHandle(This->hfilemap);
306 CloseHandle(This->hfile);
310 GlobalUnlock(This->hbytearray);
311 ILockBytes_Release(This->pLkbyt);
316 HeapFree(GetProcessHeap(), 0, This);
319 /******************************************************************************
320 * BIGBLOCKFILE_GetROBigBlock
322 * Returns the specified block in read only mode.
323 * Will return NULL if the block doesn't exists.
325 void* BIGBLOCKFILE_GetROBigBlock(
330 * block index starts at -1
331 * translate to zero based index
333 if (index == 0xffffffff)
339 * validate the block index
342 if (This->blocksize * (index + 1)
343 > ROUND_UP(This->filesize.u.LowPart, This->blocksize))
345 TRACE("out of range %lu vs %lu\n", This->blocksize * (index + 1),
346 This->filesize.u.LowPart);
350 return BIGBLOCKFILE_GetBigBlockPointer(This, index, FILE_MAP_READ);
353 /******************************************************************************
354 * BIGBLOCKFILE_GetBigBlock
356 * Returns the specified block.
357 * Will grow the file if necessary.
359 void* BIGBLOCKFILE_GetBigBlock(LPBIGBLOCKFILE This, ULONG index)
362 * block index starts at -1
363 * translate to zero based index
365 if (index == 0xffffffff)
371 * make sure that the block physically exists
373 if ((This->blocksize * (index + 1)) > This->filesize.u.LowPart)
375 ULARGE_INTEGER newSize;
377 newSize.u.HighPart = 0;
378 newSize.u.LowPart = This->blocksize * (index + 1);
380 BIGBLOCKFILE_SetSize(This, newSize);
383 return BIGBLOCKFILE_GetBigBlockPointer(This, index, FILE_MAP_WRITE);
386 /******************************************************************************
387 * BIGBLOCKFILE_ReleaseBigBlock
389 * Releases the specified block.
391 void BIGBLOCKFILE_ReleaseBigBlock(LPBIGBLOCKFILE This, void *pBlock)
398 page = BIGBLOCKFILE_GetPageFromPointer(This, pBlock);
403 BIGBLOCKFILE_ReleaseMappedPage(This, page);
406 /******************************************************************************
407 * BIGBLOCKFILE_SetSize
409 * Sets the size of the file.
412 void BIGBLOCKFILE_SetSize(LPBIGBLOCKFILE This, ULARGE_INTEGER newSize)
414 if (This->filesize.u.LowPart == newSize.u.LowPart)
417 TRACE("from %lu to %lu\n", This->filesize.u.LowPart, newSize.u.LowPart);
419 * unmap all views, must be done before call to SetEndFile
421 BIGBLOCKFILE_UnmapAllMappedPages(This);
429 * close file-mapping object, must be done before call to SetEndFile
432 CloseHandle(This->hfilemap);
437 * This fixes a bug when saving through smbfs.
438 * smbmount a Windows shared directory, save a structured storage file
439 * to that dir: crash.
441 * The problem is that the SetFilePointer-SetEndOfFile combo below
442 * doesn't always succeed. The file is not grown. It seems like the
443 * operation is cached. By doing the WriteFile, the file is actually
445 * This hack is only needed when saving to smbfs.
447 memset(buf, '0', 10);
448 SetFilePointer(This->hfile, newSize.u.LowPart, NULL, FILE_BEGIN);
449 WriteFile(This->hfile, buf, 10, &w, NULL);
455 * set the new end of file
457 SetFilePointer(This->hfile, newSize.u.LowPart, NULL, FILE_BEGIN);
458 SetEndOfFile(This->hfile);
461 * re-create the file mapping object
463 This->hfilemap = CreateFileMappingA(This->hfile,
471 GlobalUnlock(This->hbytearray);
474 * Resize the byte array object.
476 ILockBytes_SetSize(This->pLkbyt, newSize);
479 * Re-acquire the handle, it may have changed.
481 GetHGlobalFromILockBytes(This->pLkbyt, &This->hbytearray);
482 This->pbytearray = GlobalLock(This->hbytearray);
485 This->filesize.u.LowPart = newSize.u.LowPart;
486 This->filesize.u.HighPart = newSize.u.HighPart;
488 BIGBLOCKFILE_RemapAllMappedPages(This);
491 /******************************************************************************
492 * BIGBLOCKFILE_GetSize
494 * Returns the size of the file.
497 ULARGE_INTEGER BIGBLOCKFILE_GetSize(LPBIGBLOCKFILE This)
499 return This->filesize;
502 /******************************************************************************
503 * BIGBLOCKFILE_AccessCheck [PRIVATE]
505 * block_index is the index within the page.
507 static BOOL BIGBLOCKFILE_AccessCheck(MappedPage *page, ULONG block_index,
508 DWORD desired_access)
510 assert(block_index < BLOCKS_PER_PAGE);
512 if (desired_access == FILE_MAP_READ)
514 if (BIGBLOCKFILE_TestBit(&page->writable_blocks, block_index))
517 BIGBLOCKFILE_SetBit(&page->readable_blocks, block_index);
521 assert(desired_access == FILE_MAP_WRITE);
523 if (BIGBLOCKFILE_TestBit(&page->readable_blocks, block_index))
526 BIGBLOCKFILE_SetBit(&page->writable_blocks, block_index);
532 /******************************************************************************
533 * BIGBLOCKFILE_GetBigBlockPointer [PRIVATE]
535 * Returns a pointer to the specified block.
537 static void* BIGBLOCKFILE_GetBigBlockPointer(
540 DWORD desired_access)
542 DWORD page_index = block_index / BLOCKS_PER_PAGE;
543 DWORD block_on_page = block_index % BLOCKS_PER_PAGE;
545 MappedPage *page = BIGBLOCKFILE_GetMappedView(This, page_index);
546 if (!page || !page->lpBytes) return NULL;
548 if (!BIGBLOCKFILE_AccessCheck(page, block_on_page, desired_access))
550 BIGBLOCKFILE_ReleaseMappedPage(This, page);
554 return (LPBYTE)page->lpBytes + (block_on_page * This->blocksize);
557 /******************************************************************************
558 * BIGBLOCKFILE_GetMappedPageFromPointer [PRIVATE]
560 * pBlock is a pointer to a block on a page.
561 * The page has to be on the in-use list. (As oppsed to the victim list.)
563 * Does not increment the usage count.
565 static MappedPage *BIGBLOCKFILE_GetPageFromPointer(LPBIGBLOCKFILE This,
570 for (page = This->maplist; page != NULL; page = page->next)
572 if ((LPBYTE)pBlock >= (LPBYTE)page->lpBytes
573 && (LPBYTE)pBlock <= (LPBYTE)page->lpBytes + PAGE_SIZE)
581 /******************************************************************************
582 * BIGBLOCKFILE_FindPageInList [PRIVATE]
585 static MappedPage *BIGBLOCKFILE_FindPageInList(MappedPage *head,
588 for (; head != NULL; head = head->next)
590 if (head->page_index == page_index)
592 InterlockedIncrement(&head->refcnt);
601 static void BIGBLOCKFILE_UnlinkPage(MappedPage *page)
603 if (page->next) page->next->prev = page->prev;
604 if (page->prev) page->prev->next = page->next;
607 static void BIGBLOCKFILE_LinkHeadPage(MappedPage **head, MappedPage *page)
609 if (*head) (*head)->prev = page;
615 /******************************************************************************
616 * BIGBLOCKFILE_GetMappedView [PRIVATE]
618 * Gets the page requested if it is already mapped.
619 * If it's not already mapped, this method will map it
621 static void * BIGBLOCKFILE_GetMappedView(
627 page = BIGBLOCKFILE_FindPageInList(This->maplist, page_index);
630 page = BIGBLOCKFILE_FindPageInList(This->victimhead, page_index);
633 This->num_victim_pages--;
635 BIGBLOCKFILE_Zero(&page->readable_blocks);
636 BIGBLOCKFILE_Zero(&page->writable_blocks);
642 /* If the page is not already at the head of the list, move
643 * it there. (Also moves pages from victim to main list.) */
644 if (This->maplist != page)
646 if (This->victimhead == page) This->victimhead = page->next;
647 if (This->victimtail == page) This->victimtail = page->prev;
649 BIGBLOCKFILE_UnlinkPage(page);
651 BIGBLOCKFILE_LinkHeadPage(&This->maplist, page);
657 page = BIGBLOCKFILE_CreatePage(This, page_index);
658 if (!page) return NULL;
660 BIGBLOCKFILE_LinkHeadPage(&This->maplist, page);
665 static BOOL BIGBLOCKFILE_MapPage(LPBIGBLOCKFILE This, MappedPage *page)
667 DWORD lowoffset = PAGE_SIZE * page->page_index;
672 DWORD desired_access;
674 if( !This->hfilemap )
677 if (lowoffset + PAGE_SIZE > This->filesize.u.LowPart)
678 numBytesToMap = This->filesize.u.LowPart - lowoffset;
680 numBytesToMap = PAGE_SIZE;
682 if (This->flProtect == PAGE_READONLY)
683 desired_access = FILE_MAP_READ;
685 desired_access = FILE_MAP_WRITE;
687 page->lpBytes = MapViewOfFile(This->hfilemap, desired_access, 0,
688 lowoffset, numBytesToMap);
692 page->lpBytes = (LPBYTE)This->pbytearray + lowoffset;
695 TRACE("mapped page %lu to %p\n", page->page_index, page->lpBytes);
697 return page->lpBytes != NULL;
700 static MappedPage *BIGBLOCKFILE_CreatePage(LPBIGBLOCKFILE This,
705 page = HeapAlloc(GetProcessHeap(), 0, sizeof(MappedPage));
709 page->page_index = page_index;
715 BIGBLOCKFILE_MapPage(This, page);
717 BIGBLOCKFILE_Zero(&page->readable_blocks);
718 BIGBLOCKFILE_Zero(&page->writable_blocks);
723 static void BIGBLOCKFILE_UnmapPage(LPBIGBLOCKFILE This, MappedPage *page)
725 TRACE("%ld at %p\n", page->page_index, page->lpBytes);
726 if (page->refcnt > 0)
727 ERR("unmapping inuse page %p\n", page->lpBytes);
729 if (This->fileBased && page->lpBytes)
730 UnmapViewOfFile(page->lpBytes);
732 page->lpBytes = NULL;
735 static void BIGBLOCKFILE_DeletePage(LPBIGBLOCKFILE This, MappedPage *page)
737 BIGBLOCKFILE_UnmapPage(This, page);
739 HeapFree(GetProcessHeap(), 0, page);
742 /******************************************************************************
743 * BIGBLOCKFILE_ReleaseMappedPage [PRIVATE]
745 * Decrements the reference count of the mapped page.
747 static void BIGBLOCKFILE_ReleaseMappedPage(
751 assert(This != NULL);
752 assert(page != NULL);
754 /* If the page is no longer refenced, move it to the victim list.
755 * If the victim list is too long, kick somebody off. */
756 if (!InterlockedDecrement(&page->refcnt))
758 if (This->maplist == page) This->maplist = page->next;
760 BIGBLOCKFILE_UnlinkPage(page);
762 if (MAX_VICTIM_PAGES > 0)
764 if (This->num_victim_pages >= MAX_VICTIM_PAGES)
766 MappedPage *victim = This->victimtail;
769 This->victimtail = victim->prev;
770 if (This->victimhead == victim)
771 This->victimhead = victim->next;
773 BIGBLOCKFILE_UnlinkPage(victim);
774 BIGBLOCKFILE_DeletePage(This, victim);
777 else This->num_victim_pages++;
779 BIGBLOCKFILE_LinkHeadPage(&This->victimhead, page);
780 if (This->victimtail == NULL) This->victimtail = page;
783 BIGBLOCKFILE_DeletePage(This, page);
787 static void BIGBLOCKFILE_DeleteList(LPBIGBLOCKFILE This, MappedPage *list)
791 MappedPage *next = list->next;
793 BIGBLOCKFILE_DeletePage(This, list);
799 /******************************************************************************
800 * BIGBLOCKFILE_FreeAllMappedPages [PRIVATE]
802 * Unmap all currently mapped pages.
803 * Empty mapped pages list.
805 static void BIGBLOCKFILE_FreeAllMappedPages(
808 BIGBLOCKFILE_DeleteList(This, This->maplist);
809 BIGBLOCKFILE_DeleteList(This, This->victimhead);
811 This->maplist = NULL;
812 This->victimhead = NULL;
813 This->victimtail = NULL;
814 This->num_victim_pages = 0;
817 static void BIGBLOCKFILE_UnmapList(LPBIGBLOCKFILE This, MappedPage *list)
819 for (; list != NULL; list = list->next)
821 BIGBLOCKFILE_UnmapPage(This, list);
825 static void BIGBLOCKFILE_UnmapAllMappedPages(LPBIGBLOCKFILE This)
827 BIGBLOCKFILE_UnmapList(This, This->maplist);
828 BIGBLOCKFILE_UnmapList(This, This->victimhead);
831 static void BIGBLOCKFILE_RemapList(LPBIGBLOCKFILE This, MappedPage *list)
835 MappedPage *next = list->next;
837 if (list->page_index * PAGE_SIZE > This->filesize.u.LowPart)
839 TRACE("discarding %lu\n", list->page_index);
841 /* page is entirely outside of the file, delete it */
842 BIGBLOCKFILE_UnlinkPage(list);
843 BIGBLOCKFILE_DeletePage(This, list);
847 /* otherwise, remap it */
848 BIGBLOCKFILE_MapPage(This, list);
855 static void BIGBLOCKFILE_RemapAllMappedPages(LPBIGBLOCKFILE This)
857 BIGBLOCKFILE_RemapList(This, This->maplist);
858 BIGBLOCKFILE_RemapList(This, This->victimhead);
861 /****************************************************************************
862 * BIGBLOCKFILE_GetProtectMode
864 * This function will return a protection mode flag for a file-mapping object
865 * from the open flags of a file.
867 static DWORD BIGBLOCKFILE_GetProtectMode(DWORD openFlags)
869 switch(STGM_ACCESS_MODE(openFlags))
873 return PAGE_READWRITE;
875 return PAGE_READONLY;