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
40 #define NONAMELESSUNION
41 #define NONAMELESSSTRUCT
47 #include "storage32.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(storage);
53 /***********************************************************
54 * Data structures used internally by the BigBlockFile
58 /* We map in PAGE_SIZE-sized chunks. Must be a multiple of 4096. */
59 #define PAGE_SIZE 131072
61 #define BLOCKS_PER_PAGE (PAGE_SIZE / BIG_BLOCK_SIZE)
63 /* We keep a list of recently-discarded pages. This controls the
64 * size of that list. */
65 #define MAX_VICTIM_PAGES 16
67 /* This structure provides one bit for each block in a page.
68 * Use BIGBLOCKFILE_{Test,Set,Clear}Bit to manipulate it. */
71 unsigned int bits[BLOCKS_PER_PAGE / (CHAR_BIT * sizeof(unsigned int))];
75 * This structure identifies the paged that are mapped
76 * from the file and their position in memory. It is
77 * also used to hold a reference count to those pages.
79 * page_index identifies which PAGE_SIZE chunk from the
80 * file this mapping represents. (The mappings are always
92 BlockBits readable_blocks;
93 BlockBits writable_blocks;
96 /***********************************************************
97 * Prototypes for private methods
99 static void* BIGBLOCKFILE_GetMappedView(LPBIGBLOCKFILE This,
101 static void BIGBLOCKFILE_ReleaseMappedPage(LPBIGBLOCKFILE This,
103 static void BIGBLOCKFILE_FreeAllMappedPages(LPBIGBLOCKFILE This);
104 static void BIGBLOCKFILE_UnmapAllMappedPages(LPBIGBLOCKFILE This);
105 static void BIGBLOCKFILE_RemapAllMappedPages(LPBIGBLOCKFILE This);
106 static void* BIGBLOCKFILE_GetBigBlockPointer(LPBIGBLOCKFILE This,
108 DWORD desired_access);
109 static MappedPage* BIGBLOCKFILE_GetPageFromPointer(LPBIGBLOCKFILE This,
111 static MappedPage* BIGBLOCKFILE_CreatePage(LPBIGBLOCKFILE This,
113 static DWORD BIGBLOCKFILE_GetProtectMode(DWORD openFlags);
114 static BOOL BIGBLOCKFILE_FileInit(LPBIGBLOCKFILE This, HANDLE hFile);
115 static BOOL BIGBLOCKFILE_MemInit(LPBIGBLOCKFILE This, ILockBytes* plkbyt);
117 /* Note that this evaluates a and b multiple times, so don't
118 * pass expressions with side effects. */
119 #define ROUND_UP(a, b) ((((a) + (b) - 1)/(b))*(b))
121 /***********************************************************
122 * Blockbits functions.
124 static inline BOOL BIGBLOCKFILE_TestBit(const BlockBits *bb,
127 unsigned int array_index = index / (CHAR_BIT * sizeof(unsigned int));
128 unsigned int bit_index = index % (CHAR_BIT * sizeof(unsigned int));
130 return bb->bits[array_index] & (1 << bit_index);
133 static inline void BIGBLOCKFILE_SetBit(BlockBits *bb, unsigned int index)
135 unsigned int array_index = index / (CHAR_BIT * sizeof(unsigned int));
136 unsigned int bit_index = index % (CHAR_BIT * sizeof(unsigned int));
138 bb->bits[array_index] |= (1 << bit_index);
141 static inline void BIGBLOCKFILE_ClearBit(BlockBits *bb, unsigned int index)
143 unsigned int array_index = index / (CHAR_BIT * sizeof(unsigned int));
144 unsigned int bit_index = index % (CHAR_BIT * sizeof(unsigned int));
146 bb->bits[array_index] &= ~(1 << bit_index);
149 static inline void BIGBLOCKFILE_Zero(BlockBits *bb)
151 memset(bb->bits, 0, sizeof(bb->bits));
154 /******************************************************************************
155 * BIGBLOCKFILE_Construct
157 * Construct a big block file. Create the file mapping object.
158 * Create the read only mapped pages list, the writable mapped page list
159 * and the blocks in use list.
161 BigBlockFile * BIGBLOCKFILE_Construct(
170 This = (LPBIGBLOCKFILE)HeapAlloc(GetProcessHeap(), 0, sizeof(BigBlockFile));
175 This->fileBased = fileBased;
177 This->flProtect = BIGBLOCKFILE_GetProtectMode(openFlags);
179 This->blocksize = blocksize;
181 This->maplist = NULL;
182 This->victimhead = NULL;
183 This->victimtail = NULL;
184 This->num_victim_pages = 0;
188 if (!BIGBLOCKFILE_FileInit(This, hFile))
190 HeapFree(GetProcessHeap(), 0, This);
196 if (!BIGBLOCKFILE_MemInit(This, pLkByt))
198 HeapFree(GetProcessHeap(), 0, This);
206 /******************************************************************************
207 * BIGBLOCKFILE_FileInit
209 * Initialize a big block object supported by a file.
211 static BOOL BIGBLOCKFILE_FileInit(LPBIGBLOCKFILE This, HANDLE hFile)
214 This->hbytearray = 0;
215 This->pbytearray = NULL;
219 if (This->hfile == INVALID_HANDLE_VALUE)
222 /* create the file mapping object
224 This->hfilemap = CreateFileMappingA(This->hfile,
232 CloseHandle(This->hfile);
236 This->filesize.s.LowPart = GetFileSize(This->hfile,
237 &This->filesize.s.HighPart);
239 This->maplist = NULL;
241 TRACE("file len %lu\n", This->filesize.s.LowPart);
246 /******************************************************************************
247 * BIGBLOCKFILE_MemInit
249 * Initialize a big block object supported by an ILockBytes on HGLOABL.
251 static BOOL BIGBLOCKFILE_MemInit(LPBIGBLOCKFILE This, ILockBytes* plkbyt)
257 * Retrieve the handle to the byte array from the LockByte object.
259 if (GetHGlobalFromILockBytes(plkbyt, &(This->hbytearray)) != S_OK)
261 FIXME("May not be an ILockBytes on HGLOBAL\n");
265 This->pLkbyt = plkbyt;
268 * Increment the reference count of the ILockByte object since
269 * we're keeping a reference to it.
271 ILockBytes_AddRef(This->pLkbyt);
273 This->filesize.s.LowPart = GlobalSize(This->hbytearray);
274 This->filesize.s.HighPart = 0;
276 This->pbytearray = GlobalLock(This->hbytearray);
278 TRACE("mem on %p len %lu\n", This->pbytearray, This->filesize.s.LowPart);
283 /******************************************************************************
284 * BIGBLOCKFILE_Destructor
286 * Destructor. Clean up, free memory.
288 void BIGBLOCKFILE_Destructor(
291 BIGBLOCKFILE_FreeAllMappedPages(This);
295 CloseHandle(This->hfilemap);
296 CloseHandle(This->hfile);
300 GlobalUnlock(This->hbytearray);
301 ILockBytes_Release(This->pLkbyt);
306 HeapFree(GetProcessHeap(), 0, This);
309 /******************************************************************************
310 * BIGBLOCKFILE_GetROBigBlock
312 * Returns the specified block in read only mode.
313 * Will return NULL if the block doesn't exists.
315 void* BIGBLOCKFILE_GetROBigBlock(
320 * block index starts at -1
321 * translate to zero based index
323 if (index == 0xffffffff)
329 * validate the block index
332 if (This->blocksize * (index + 1)
333 > ROUND_UP(This->filesize.s.LowPart, This->blocksize))
335 TRACE("out of range %lu vs %lu\n", This->blocksize * (index + 1),
336 This->filesize.s.LowPart);
340 return BIGBLOCKFILE_GetBigBlockPointer(This, index, FILE_MAP_READ);
343 /******************************************************************************
344 * BIGBLOCKFILE_GetBigBlock
346 * Returns the specified block.
347 * Will grow the file if necessary.
349 void* BIGBLOCKFILE_GetBigBlock(LPBIGBLOCKFILE This, ULONG index)
352 * block index starts at -1
353 * translate to zero based index
355 if (index == 0xffffffff)
361 * make sure that the block physically exists
363 if ((This->blocksize * (index + 1)) > This->filesize.s.LowPart)
365 ULARGE_INTEGER newSize;
367 newSize.s.HighPart = 0;
368 newSize.s.LowPart = This->blocksize * (index + 1);
370 BIGBLOCKFILE_SetSize(This, newSize);
373 return BIGBLOCKFILE_GetBigBlockPointer(This, index, FILE_MAP_WRITE);
376 /******************************************************************************
377 * BIGBLOCKFILE_ReleaseBigBlock
379 * Releases the specified block.
381 void BIGBLOCKFILE_ReleaseBigBlock(LPBIGBLOCKFILE This, void *pBlock)
388 page = BIGBLOCKFILE_GetPageFromPointer(This, pBlock);
393 BIGBLOCKFILE_ReleaseMappedPage(This, page);
396 /******************************************************************************
397 * BIGBLOCKFILE_SetSize
399 * Sets the size of the file.
402 void BIGBLOCKFILE_SetSize(LPBIGBLOCKFILE This, ULARGE_INTEGER newSize)
404 if (This->filesize.s.LowPart == newSize.s.LowPart)
407 TRACE("from %lu to %lu\n", This->filesize.s.LowPart, newSize.s.LowPart);
409 * unmap all views, must be done before call to SetEndFile
411 BIGBLOCKFILE_UnmapAllMappedPages(This);
418 * close file-mapping object, must be done before call to SetEndFile
420 CloseHandle(This->hfilemap);
425 * This fixes a bug when saving through smbfs.
426 * smbmount a Windows shared directory, save a structured storage file
427 * to that dir: crash.
429 * The problem is that the SetFilePointer-SetEndOfFile combo below
430 * doesn't always succeed. The file is not grown. It seems like the
431 * operation is cached. By doing the WriteFile, the file is actually
433 * This hack is only needed when saving to smbfs.
435 memset(buf, '0', 10);
436 SetFilePointer(This->hfile, newSize.s.LowPart, NULL, FILE_BEGIN);
437 WriteFile(This->hfile, buf, 10, NULL, NULL);
443 * set the new end of file
445 SetFilePointer(This->hfile, newSize.s.LowPart, NULL, FILE_BEGIN);
446 SetEndOfFile(This->hfile);
449 * re-create the file mapping object
451 This->hfilemap = CreateFileMappingA(This->hfile,
459 GlobalUnlock(This->hbytearray);
462 * Resize the byte array object.
464 ILockBytes_SetSize(This->pLkbyt, newSize);
467 * Re-acquire the handle, it may have changed.
469 GetHGlobalFromILockBytes(This->pLkbyt, &This->hbytearray);
470 This->pbytearray = GlobalLock(This->hbytearray);
473 This->filesize.s.LowPart = newSize.s.LowPart;
474 This->filesize.s.HighPart = newSize.s.HighPart;
476 BIGBLOCKFILE_RemapAllMappedPages(This);
479 /******************************************************************************
480 * BIGBLOCKFILE_GetSize
482 * Returns the size of the file.
485 ULARGE_INTEGER BIGBLOCKFILE_GetSize(LPBIGBLOCKFILE This)
487 return This->filesize;
490 /******************************************************************************
491 * BIGBLOCKFILE_AccessCheck [PRIVATE]
493 * block_index is the index within the page.
495 static BOOL BIGBLOCKFILE_AccessCheck(MappedPage *page, ULONG block_index,
496 DWORD desired_access)
498 assert(block_index < BLOCKS_PER_PAGE);
500 if (desired_access == FILE_MAP_READ)
502 if (BIGBLOCKFILE_TestBit(&page->writable_blocks, block_index))
505 BIGBLOCKFILE_SetBit(&page->readable_blocks, block_index);
509 assert(desired_access == FILE_MAP_WRITE);
511 if (BIGBLOCKFILE_TestBit(&page->readable_blocks, block_index))
514 BIGBLOCKFILE_SetBit(&page->writable_blocks, block_index);
520 /******************************************************************************
521 * BIGBLOCKFILE_GetBigBlockPointer [PRIVATE]
523 * Returns a pointer to the specified block.
525 static void* BIGBLOCKFILE_GetBigBlockPointer(
528 DWORD desired_access)
530 DWORD page_index = block_index / BLOCKS_PER_PAGE;
531 DWORD block_on_page = block_index % BLOCKS_PER_PAGE;
533 MappedPage *page = BIGBLOCKFILE_GetMappedView(This, page_index);
534 if (!page || !page->lpBytes) return NULL;
536 if (!BIGBLOCKFILE_AccessCheck(page, block_on_page, desired_access))
538 BIGBLOCKFILE_ReleaseMappedPage(This, page);
542 return (LPBYTE)page->lpBytes + (block_on_page * This->blocksize);
545 /******************************************************************************
546 * BIGBLOCKFILE_GetMappedPageFromPointer [PRIVATE]
548 * pBlock is a pointer to a block on a page.
549 * The page has to be on the in-use list. (As oppsed to the victim list.)
551 * Does not increment the usage count.
553 static MappedPage *BIGBLOCKFILE_GetPageFromPointer(LPBIGBLOCKFILE This,
558 for (page = This->maplist; page != NULL; page = page->next)
560 if ((LPBYTE)pBlock >= (LPBYTE)page->lpBytes
561 && (LPBYTE)pBlock <= (LPBYTE)page->lpBytes + PAGE_SIZE)
569 /******************************************************************************
570 * BIGBLOCKFILE_FindPageInList [PRIVATE]
573 static MappedPage *BIGBLOCKFILE_FindPageInList(MappedPage *head,
576 for (; head != NULL; head = head->next)
578 if (head->page_index == page_index)
580 InterlockedIncrement(&head->refcnt);
589 static void BIGBLOCKFILE_UnlinkPage(MappedPage *page)
591 if (page->next) page->next->prev = page->prev;
592 if (page->prev) page->prev->next = page->next;
595 static void BIGBLOCKFILE_LinkHeadPage(MappedPage **head, MappedPage *page)
597 if (*head) (*head)->prev = page;
603 /******************************************************************************
604 * BIGBLOCKFILE_GetMappedView [PRIVATE]
606 * Gets the page requested if it is already mapped.
607 * If it's not already mapped, this method will map it
609 static void * BIGBLOCKFILE_GetMappedView(
615 page = BIGBLOCKFILE_FindPageInList(This->maplist, page_index);
618 page = BIGBLOCKFILE_FindPageInList(This->victimhead, page_index);
621 This->num_victim_pages--;
623 BIGBLOCKFILE_Zero(&page->readable_blocks);
624 BIGBLOCKFILE_Zero(&page->writable_blocks);
630 /* If the page is not already at the head of the list, move
631 * it there. (Also moves pages from victim to main list.) */
632 if (This->maplist != page)
634 if (This->victimhead == page) This->victimhead = page->next;
635 if (This->victimtail == page) This->victimtail = page->prev;
637 BIGBLOCKFILE_UnlinkPage(page);
639 BIGBLOCKFILE_LinkHeadPage(&This->maplist, page);
645 page = BIGBLOCKFILE_CreatePage(This, page_index);
646 if (!page) return NULL;
648 BIGBLOCKFILE_LinkHeadPage(&This->maplist, page);
653 static BOOL BIGBLOCKFILE_MapPage(LPBIGBLOCKFILE This, MappedPage *page)
655 DWORD lowoffset = PAGE_SIZE * page->page_index;
660 DWORD desired_access;
662 if (lowoffset + PAGE_SIZE > This->filesize.s.LowPart)
663 numBytesToMap = This->filesize.s.LowPart - lowoffset;
665 numBytesToMap = PAGE_SIZE;
667 if (This->flProtect == PAGE_READONLY)
668 desired_access = FILE_MAP_READ;
670 desired_access = FILE_MAP_WRITE;
672 page->lpBytes = MapViewOfFile(This->hfilemap, desired_access, 0,
673 lowoffset, numBytesToMap);
677 page->lpBytes = (LPBYTE)This->pbytearray + lowoffset;
680 TRACE("mapped page %lu to %p\n", page->page_index, page->lpBytes);
682 return page->lpBytes != NULL;
685 static MappedPage *BIGBLOCKFILE_CreatePage(LPBIGBLOCKFILE This,
690 page = HeapAlloc(GetProcessHeap(), 0, sizeof(MappedPage));
694 page->page_index = page_index;
700 BIGBLOCKFILE_MapPage(This, page);
702 BIGBLOCKFILE_Zero(&page->readable_blocks);
703 BIGBLOCKFILE_Zero(&page->writable_blocks);
708 static void BIGBLOCKFILE_UnmapPage(LPBIGBLOCKFILE This, MappedPage *page)
710 TRACE("%ld at %p\n", page->page_index, page->lpBytes);
711 if (page->refcnt > 0)
712 ERR("unmapping inuse page %p\n", page->lpBytes);
714 if (This->fileBased && page->lpBytes)
715 UnmapViewOfFile(page->lpBytes);
717 page->lpBytes = NULL;
720 static void BIGBLOCKFILE_DeletePage(LPBIGBLOCKFILE This, MappedPage *page)
722 BIGBLOCKFILE_UnmapPage(This, page);
724 HeapFree(GetProcessHeap(), 0, page);
727 /******************************************************************************
728 * BIGBLOCKFILE_ReleaseMappedPage [PRIVATE]
730 * Decrements the reference count of the mapped page.
732 static void BIGBLOCKFILE_ReleaseMappedPage(
736 assert(This != NULL);
737 assert(page != NULL);
739 /* If the page is no longer refenced, move it to the victim list.
740 * If the victim list is too long, kick somebody off. */
741 if (!InterlockedDecrement(&page->refcnt))
743 if (This->maplist == page) This->maplist = page->next;
745 BIGBLOCKFILE_UnlinkPage(page);
747 if (MAX_VICTIM_PAGES > 0)
749 if (This->num_victim_pages >= MAX_VICTIM_PAGES)
751 MappedPage *victim = This->victimtail;
754 This->victimtail = victim->prev;
755 if (This->victimhead == victim)
756 This->victimhead = victim->next;
758 BIGBLOCKFILE_UnlinkPage(victim);
759 BIGBLOCKFILE_DeletePage(This, victim);
762 else This->num_victim_pages++;
764 BIGBLOCKFILE_LinkHeadPage(&This->victimhead, page);
765 if (This->victimtail == NULL) This->victimtail = page;
768 BIGBLOCKFILE_DeletePage(This, page);
772 static void BIGBLOCKFILE_DeleteList(LPBIGBLOCKFILE This, MappedPage *list)
776 MappedPage *next = list->next;
778 BIGBLOCKFILE_DeletePage(This, list);
784 /******************************************************************************
785 * BIGBLOCKFILE_FreeAllMappedPages [PRIVATE]
787 * Unmap all currently mapped pages.
788 * Empty mapped pages list.
790 static void BIGBLOCKFILE_FreeAllMappedPages(
793 BIGBLOCKFILE_DeleteList(This, This->maplist);
794 BIGBLOCKFILE_DeleteList(This, This->victimhead);
796 This->maplist = NULL;
797 This->victimhead = NULL;
798 This->victimtail = NULL;
799 This->num_victim_pages = 0;
802 static void BIGBLOCKFILE_UnmapList(LPBIGBLOCKFILE This, MappedPage *list)
804 for (; list != NULL; list = list->next)
806 BIGBLOCKFILE_UnmapPage(This, list);
810 static void BIGBLOCKFILE_UnmapAllMappedPages(LPBIGBLOCKFILE This)
812 BIGBLOCKFILE_UnmapList(This, This->maplist);
813 BIGBLOCKFILE_UnmapList(This, This->victimhead);
816 static void BIGBLOCKFILE_RemapList(LPBIGBLOCKFILE This, MappedPage *list)
820 MappedPage *next = list->next;
822 if (list->page_index * PAGE_SIZE > This->filesize.s.LowPart)
824 TRACE("discarding %lu\n", list->page_index);
826 /* page is entirely outside of the file, delete it */
827 BIGBLOCKFILE_UnlinkPage(list);
828 BIGBLOCKFILE_DeletePage(This, list);
832 /* otherwise, remap it */
833 BIGBLOCKFILE_MapPage(This, list);
840 static void BIGBLOCKFILE_RemapAllMappedPages(LPBIGBLOCKFILE This)
842 BIGBLOCKFILE_RemapList(This, This->maplist);
843 BIGBLOCKFILE_RemapList(This, This->victimhead);
846 /****************************************************************************
847 * BIGBLOCKFILE_GetProtectMode
849 * This function will return a protection mode flag for a file-mapping object
850 * from the open flags of a file.
852 static DWORD BIGBLOCKFILE_GetProtectMode(DWORD openFlags)
854 if (openFlags & (STGM_WRITE | STGM_READWRITE))
855 return PAGE_READWRITE;
857 return PAGE_READONLY;