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
27 #include <ole32/ole32.h>
28 #include "storage32.h"
32 /***********************************************************
33 * Data structures used internally by the BigBlockFile
37 /* We map in PAGE_SIZE-sized chunks. Must be a multiple of 4096. */
38 #define PAGE_SIZE 131072
40 #define BLOCKS_PER_PAGE (PAGE_SIZE / BIG_BLOCK_SIZE)
42 /* We keep a list of recently-discarded pages. This controls the
43 * size of that list. */
44 #define MAX_VICTIM_PAGES 16
46 /* This structure provides one bit for each block in a page.
47 * Use BIGBLOCKFILE_{Test,Set,Clear}Bit to manipulate it. */
50 unsigned int bits[BLOCKS_PER_PAGE / (CHAR_BIT * sizeof(unsigned int))];
54 * This structure identifies the paged that are mapped
55 * from the file and their position in memory. It is
56 * also used to hold a reference count to those pages.
58 * page_index identifies which PAGE_SIZE chunk from the
59 * file this mapping represents. (The mappings are always
71 BlockBits readable_blocks;
72 BlockBits writable_blocks;
75 /***********************************************************
76 * Prototypes for private methods
78 static void* BIGBLOCKFILE_GetMappedView(LPBIGBLOCKFILE This,
80 static void BIGBLOCKFILE_ReleaseMappedPage(LPBIGBLOCKFILE This,
82 static void BIGBLOCKFILE_FreeAllMappedPages(LPBIGBLOCKFILE This);
83 static void BIGBLOCKFILE_UnmapAllMappedPages(LPBIGBLOCKFILE This);
84 static void BIGBLOCKFILE_RemapAllMappedPages(LPBIGBLOCKFILE This);
85 static void* BIGBLOCKFILE_GetBigBlockPointer(LPBIGBLOCKFILE This,
87 DWORD desired_access);
88 static MappedPage* BIGBLOCKFILE_GetPageFromPointer(LPBIGBLOCKFILE This,
90 static MappedPage* BIGBLOCKFILE_CreatePage(LPBIGBLOCKFILE This,
92 static DWORD BIGBLOCKFILE_GetProtectMode(DWORD openFlags);
93 static BOOL BIGBLOCKFILE_FileInit(LPBIGBLOCKFILE This, HANDLE hFile);
94 static BOOL BIGBLOCKFILE_MemInit(LPBIGBLOCKFILE This, ILockBytes* plkbyt);
96 /* Note that this evaluates a and b multiple times, so don't
97 * pass expressions with side effects. */
98 #define ROUNDUP(a, b) ((((a) + (b) - 1)/(b))*(b))
100 /***********************************************************
101 * Blockbits functions.
103 static inline BOOL BIGBLOCKFILE_TestBit(const BlockBits *bb,
106 unsigned int array_index = index / (CHAR_BIT * sizeof(unsigned int));
107 unsigned int bit_index = index % (CHAR_BIT * sizeof(unsigned int));
109 return bb->bits[array_index] & (1 << bit_index);
112 static inline void BIGBLOCKFILE_SetBit(BlockBits *bb, unsigned int index)
114 unsigned int array_index = index / (CHAR_BIT * sizeof(unsigned int));
115 unsigned int bit_index = index % (CHAR_BIT * sizeof(unsigned int));
117 bb->bits[array_index] |= (1 << bit_index);
120 static inline void BIGBLOCKFILE_ClearBit(BlockBits *bb, unsigned int index)
122 unsigned int array_index = index / (CHAR_BIT * sizeof(unsigned int));
123 unsigned int bit_index = index % (CHAR_BIT * sizeof(unsigned int));
125 bb->bits[array_index] &= ~(1 << bit_index);
128 static inline void BIGBLOCKFILE_Zero(BlockBits *bb)
130 memset(bb->bits, 0, sizeof(bb->bits));
133 /******************************************************************************
134 * BIGBLOCKFILE_Construct
136 * Construct a big block file. Create the file mapping object.
137 * Create the read only mapped pages list, the writable mapped page list
138 * and the blocks in use list.
140 BigBlockFile * BIGBLOCKFILE_Construct(
149 This = (LPBIGBLOCKFILE)HeapAlloc(GetProcessHeap(), 0, sizeof(BigBlockFile));
154 This->fileBased = fileBased;
156 This->flProtect = BIGBLOCKFILE_GetProtectMode(openFlags);
158 This->blocksize = blocksize;
160 This->maplist = NULL;
161 This->victimhead = NULL;
162 This->victimtail = NULL;
163 This->num_victim_pages = 0;
167 if (!BIGBLOCKFILE_FileInit(This, hFile))
169 HeapFree(GetProcessHeap(), 0, This);
175 if (!BIGBLOCKFILE_MemInit(This, pLkByt))
177 HeapFree(GetProcessHeap(), 0, This);
185 /******************************************************************************
186 * BIGBLOCKFILE_FileInit
188 * Initialize a big block object supported by a file.
190 static BOOL BIGBLOCKFILE_FileInit(LPBIGBLOCKFILE This, HANDLE hFile)
193 This->hbytearray = 0;
194 This->pbytearray = NULL;
198 if (This->hfile == INVALID_HANDLE_VALUE)
201 /* create the file mapping object
203 This->hfilemap = CreateFileMappingA(This->hfile,
211 CloseHandle(This->hfile);
215 This->filesize.u.LowPart = GetFileSize(This->hfile,
216 &This->filesize.u.HighPart);
218 This->maplist = NULL;
220 Print(MAX_TRACE, ("file len %lu\n", This->filesize.u.LowPart));
225 /******************************************************************************
226 * BIGBLOCKFILE_MemInit
228 * Initialize a big block object supported by an ILockBytes on HGLOABL.
230 static BOOL BIGBLOCKFILE_MemInit(LPBIGBLOCKFILE This, ILockBytes* plkbyt)
236 * Retrieve the handle to the byte array from the LockByte object.
238 if (GetHGlobalFromILockBytes(plkbyt, &(This->hbytearray)) != S_OK)
240 Print(MIN_TRACE, ("May not be an ILockBytes on HGLOBAL\n"));
244 This->pLkbyt = plkbyt;
247 * Increment the reference count of the ILockByte object since
248 * we're keeping a reference to it.
250 ILockBytes_AddRef(This->pLkbyt);
252 This->filesize.u.LowPart = GlobalSize(This->hbytearray);
253 This->filesize.u.HighPart = 0;
255 This->pbytearray = GlobalLock(This->hbytearray);
257 Print(MAX_TRACE, ("mem on %p len %lu\n", This->pbytearray, This->filesize.u.LowPart));
262 /******************************************************************************
263 * BIGBLOCKFILE_Destructor
265 * Destructor. Clean up, free memory.
267 void BIGBLOCKFILE_Destructor(
270 BIGBLOCKFILE_FreeAllMappedPages(This);
274 CloseHandle(This->hfilemap);
275 CloseHandle(This->hfile);
279 GlobalUnlock(This->hbytearray);
280 ILockBytes_Release(This->pLkbyt);
285 HeapFree(GetProcessHeap(), 0, This);
288 /******************************************************************************
289 * BIGBLOCKFILE_GetROBigBlock
291 * Returns the specified block in read only mode.
292 * Will return NULL if the block doesn't exists.
294 void* BIGBLOCKFILE_GetROBigBlock(
299 * block index starts at -1
300 * translate to zero based index
302 if (index == 0xffffffff)
308 * validate the block index
311 if (This->blocksize * (index + 1)
312 > ROUNDUP(This->filesize.u.LowPart, This->blocksize))
314 Print(MAX_TRACE, ("out of range %lu vs %lu\n", This->blocksize * (index + 1),
315 This->filesize.u.LowPart));
319 return BIGBLOCKFILE_GetBigBlockPointer(This, index, FILE_MAP_READ);
322 /******************************************************************************
323 * BIGBLOCKFILE_GetBigBlock
325 * Returns the specified block.
326 * Will grow the file if necessary.
328 void* BIGBLOCKFILE_GetBigBlock(LPBIGBLOCKFILE This, ULONG index)
331 * block index starts at -1
332 * translate to zero based index
334 if (index == 0xffffffff)
340 * make sure that the block physically exists
342 if ((This->blocksize * (index + 1)) > This->filesize.u.LowPart)
344 ULARGE_INTEGER newSize;
346 newSize.u.HighPart = 0;
347 newSize.u.LowPart = This->blocksize * (index + 1);
349 BIGBLOCKFILE_SetSize(This, newSize);
352 return BIGBLOCKFILE_GetBigBlockPointer(This, index, FILE_MAP_WRITE);
355 /******************************************************************************
356 * BIGBLOCKFILE_ReleaseBigBlock
358 * Releases the specified block.
360 void BIGBLOCKFILE_ReleaseBigBlock(LPBIGBLOCKFILE This, void *pBlock)
367 page = BIGBLOCKFILE_GetPageFromPointer(This, pBlock);
372 BIGBLOCKFILE_ReleaseMappedPage(This, page);
375 /******************************************************************************
376 * BIGBLOCKFILE_SetSize
378 * Sets the size of the file.
381 void BIGBLOCKFILE_SetSize(LPBIGBLOCKFILE This, ULARGE_INTEGER newSize)
383 if (This->filesize.u.LowPart == newSize.u.LowPart)
386 Print(MAX_TRACE, ("from %lu to %lu\n", This->filesize.u.LowPart, newSize.u.LowPart));
388 * unmap all views, must be done before call to SetEndFile
390 BIGBLOCKFILE_UnmapAllMappedPages(This);
397 * close file-mapping object, must be done before call to SetEndFile
399 CloseHandle(This->hfilemap);
404 * This fixes a bug when saving through smbfs.
405 * smbmount a Windows shared directory, save a structured storage file
406 * to that dir: crash.
408 * The problem is that the SetFilePointer-SetEndOfFile combo below
409 * doesn't always succeed. The file is not grown. It seems like the
410 * operation is cached. By doing the WriteFile, the file is actually
412 * This hack is only needed when saving to smbfs.
414 memset(buf, '0', 10);
415 SetFilePointer(This->hfile, newSize.u.LowPart, NULL, FILE_BEGIN);
416 WriteFile(This->hfile, buf, 10, NULL, NULL);
422 * set the new end of file
424 SetFilePointer(This->hfile, newSize.u.LowPart, NULL, FILE_BEGIN);
425 SetEndOfFile(This->hfile);
428 * re-create the file mapping object
430 This->hfilemap = CreateFileMappingA(This->hfile,
438 GlobalUnlock(This->hbytearray);
441 * Resize the byte array object.
443 ILockBytes_SetSize(This->pLkbyt, newSize);
446 * Re-acquire the handle, it may have changed.
448 GetHGlobalFromILockBytes(This->pLkbyt, &This->hbytearray);
449 This->pbytearray = GlobalLock(This->hbytearray);
452 This->filesize.u.LowPart = newSize.u.LowPart;
453 This->filesize.u.HighPart = newSize.u.HighPart;
455 BIGBLOCKFILE_RemapAllMappedPages(This);
458 /******************************************************************************
459 * BIGBLOCKFILE_GetSize
461 * Returns the size of the file.
464 ULARGE_INTEGER BIGBLOCKFILE_GetSize(LPBIGBLOCKFILE This)
466 return This->filesize;
469 /******************************************************************************
470 * BIGBLOCKFILE_AccessCheck [PRIVATE]
472 * block_index is the index within the page.
474 static BOOL BIGBLOCKFILE_AccessCheck(MappedPage *page, ULONG block_index,
475 DWORD desired_access)
477 assert(block_index < BLOCKS_PER_PAGE);
479 if (desired_access == FILE_MAP_READ)
481 if (BIGBLOCKFILE_TestBit(&page->writable_blocks, block_index))
484 BIGBLOCKFILE_SetBit(&page->readable_blocks, block_index);
488 assert(desired_access == FILE_MAP_WRITE);
490 if (BIGBLOCKFILE_TestBit(&page->readable_blocks, block_index))
493 BIGBLOCKFILE_SetBit(&page->writable_blocks, block_index);
499 /******************************************************************************
500 * BIGBLOCKFILE_GetBigBlockPointer [PRIVATE]
502 * Returns a pointer to the specified block.
504 static void* BIGBLOCKFILE_GetBigBlockPointer(
507 DWORD desired_access)
509 DWORD page_index = block_index / BLOCKS_PER_PAGE;
510 DWORD block_on_page = block_index % BLOCKS_PER_PAGE;
512 MappedPage *page = BIGBLOCKFILE_GetMappedView(This, page_index);
513 if (!page || !page->lpBytes) return NULL;
515 if (!BIGBLOCKFILE_AccessCheck(page, block_on_page, desired_access))
517 BIGBLOCKFILE_ReleaseMappedPage(This, page);
521 return (LPBYTE)page->lpBytes + (block_on_page * This->blocksize);
524 /******************************************************************************
525 * BIGBLOCKFILE_GetMappedPageFromPointer [PRIVATE]
527 * pBlock is a pointer to a block on a page.
528 * The page has to be on the in-use list. (As oppsed to the victim list.)
530 * Does not increment the usage count.
532 static MappedPage *BIGBLOCKFILE_GetPageFromPointer(LPBIGBLOCKFILE This,
537 for (page = This->maplist; page != NULL; page = page->next)
539 if ((LPBYTE)pBlock >= (LPBYTE)page->lpBytes
540 && (LPBYTE)pBlock <= (LPBYTE)page->lpBytes + PAGE_SIZE)
548 /******************************************************************************
549 * BIGBLOCKFILE_FindPageInList [PRIVATE]
552 static MappedPage *BIGBLOCKFILE_FindPageInList(MappedPage *head,
555 for (; head != NULL; head = head->next)
557 if (head->page_index == page_index)
559 InterlockedIncrement(&head->refcnt);
568 static void BIGBLOCKFILE_UnlinkPage(MappedPage *page)
570 if (page->next) page->next->prev = page->prev;
571 if (page->prev) page->prev->next = page->next;
574 static void BIGBLOCKFILE_LinkHeadPage(MappedPage **head, MappedPage *page)
576 if (*head) (*head)->prev = page;
582 /******************************************************************************
583 * BIGBLOCKFILE_GetMappedView [PRIVATE]
585 * Gets the page requested if it is already mapped.
586 * If it's not already mapped, this method will map it
588 static void * BIGBLOCKFILE_GetMappedView(
594 page = BIGBLOCKFILE_FindPageInList(This->maplist, page_index);
597 page = BIGBLOCKFILE_FindPageInList(This->victimhead, page_index);
600 This->num_victim_pages--;
602 BIGBLOCKFILE_Zero(&page->readable_blocks);
603 BIGBLOCKFILE_Zero(&page->writable_blocks);
609 /* If the page is not already at the head of the list, move
610 * it there. (Also moves pages from victim to main list.) */
611 if (This->maplist != page)
613 if (This->victimhead == page) This->victimhead = page->next;
614 if (This->victimtail == page) This->victimtail = page->prev;
616 BIGBLOCKFILE_UnlinkPage(page);
618 BIGBLOCKFILE_LinkHeadPage(&This->maplist, page);
624 page = BIGBLOCKFILE_CreatePage(This, page_index);
625 if (!page) return NULL;
627 BIGBLOCKFILE_LinkHeadPage(&This->maplist, page);
632 static BOOL BIGBLOCKFILE_MapPage(LPBIGBLOCKFILE This, MappedPage *page)
634 DWORD lowoffset = PAGE_SIZE * page->page_index;
639 DWORD desired_access;
641 if (lowoffset + PAGE_SIZE > This->filesize.u.LowPart)
642 numBytesToMap = This->filesize.u.LowPart - lowoffset;
644 numBytesToMap = PAGE_SIZE;
646 if (This->flProtect == PAGE_READONLY)
647 desired_access = FILE_MAP_READ;
649 desired_access = FILE_MAP_WRITE;
651 page->lpBytes = MapViewOfFile(This->hfilemap, desired_access, 0,
652 lowoffset, numBytesToMap);
656 page->lpBytes = (LPBYTE)This->pbytearray + lowoffset;
659 Print(MAX_TRACE, ("mapped page %lu to %p\n", page->page_index, page->lpBytes));
661 return page->lpBytes != NULL;
664 static MappedPage *BIGBLOCKFILE_CreatePage(LPBIGBLOCKFILE This,
669 page = HeapAlloc(GetProcessHeap(), 0, sizeof(MappedPage));
673 page->page_index = page_index;
679 BIGBLOCKFILE_MapPage(This, page);
681 BIGBLOCKFILE_Zero(&page->readable_blocks);
682 BIGBLOCKFILE_Zero(&page->writable_blocks);
687 static void BIGBLOCKFILE_UnmapPage(LPBIGBLOCKFILE This, MappedPage *page)
689 Print(MAX_TRACE, ("%ld at %p\n", page->page_index, page->lpBytes));
690 if (page->refcnt > 0)
691 Print(MIN_TRACE, ("unmapping inuse page %p\n", page->lpBytes));
693 if (This->fileBased && page->lpBytes)
694 UnmapViewOfFile(page->lpBytes);
696 page->lpBytes = NULL;
699 static void BIGBLOCKFILE_DeletePage(LPBIGBLOCKFILE This, MappedPage *page)
701 BIGBLOCKFILE_UnmapPage(This, page);
703 HeapFree(GetProcessHeap(), 0, page);
706 /******************************************************************************
707 * BIGBLOCKFILE_ReleaseMappedPage [PRIVATE]
709 * Decrements the reference count of the mapped page.
711 static void BIGBLOCKFILE_ReleaseMappedPage(
715 assert(This != NULL);
716 assert(page != NULL);
718 /* If the page is no longer refenced, move it to the victim list.
719 * If the victim list is too long, kick somebody off. */
720 if (!InterlockedDecrement(&page->refcnt))
722 if (This->maplist == page) This->maplist = page->next;
724 BIGBLOCKFILE_UnlinkPage(page);
726 if (MAX_VICTIM_PAGES > 0)
728 if (This->num_victim_pages >= MAX_VICTIM_PAGES)
730 MappedPage *victim = This->victimtail;
733 This->victimtail = victim->prev;
734 if (This->victimhead == victim)
735 This->victimhead = victim->next;
737 BIGBLOCKFILE_UnlinkPage(victim);
738 BIGBLOCKFILE_DeletePage(This, victim);
741 else This->num_victim_pages++;
743 BIGBLOCKFILE_LinkHeadPage(&This->victimhead, page);
744 if (This->victimtail == NULL) This->victimtail = page;
747 BIGBLOCKFILE_DeletePage(This, page);
751 static void BIGBLOCKFILE_DeleteList(LPBIGBLOCKFILE This, MappedPage *list)
755 MappedPage *next = list->next;
757 BIGBLOCKFILE_DeletePage(This, list);
763 /******************************************************************************
764 * BIGBLOCKFILE_FreeAllMappedPages [PRIVATE]
766 * Unmap all currently mapped pages.
767 * Empty mapped pages list.
769 static void BIGBLOCKFILE_FreeAllMappedPages(
772 BIGBLOCKFILE_DeleteList(This, This->maplist);
773 BIGBLOCKFILE_DeleteList(This, This->victimhead);
775 This->maplist = NULL;
776 This->victimhead = NULL;
777 This->victimtail = NULL;
778 This->num_victim_pages = 0;
781 static void BIGBLOCKFILE_UnmapList(LPBIGBLOCKFILE This, MappedPage *list)
783 for (; list != NULL; list = list->next)
785 BIGBLOCKFILE_UnmapPage(This, list);
789 static void BIGBLOCKFILE_UnmapAllMappedPages(LPBIGBLOCKFILE This)
791 BIGBLOCKFILE_UnmapList(This, This->maplist);
792 BIGBLOCKFILE_UnmapList(This, This->victimhead);
795 static void BIGBLOCKFILE_RemapList(LPBIGBLOCKFILE This, MappedPage *list)
799 MappedPage *next = list->next;
801 if (list->page_index * PAGE_SIZE > This->filesize.u.LowPart)
803 Print(MAX_TRACE, ("discarding %lu\n", list->page_index));
805 /* page is entirely outside of the file, delete it */
806 BIGBLOCKFILE_UnlinkPage(list);
807 BIGBLOCKFILE_DeletePage(This, list);
811 /* otherwise, remap it */
812 BIGBLOCKFILE_MapPage(This, list);
819 static void BIGBLOCKFILE_RemapAllMappedPages(LPBIGBLOCKFILE This)
821 BIGBLOCKFILE_RemapList(This, This->maplist);
822 BIGBLOCKFILE_RemapList(This, This->victimhead);
825 /****************************************************************************
826 * BIGBLOCKFILE_GetProtectMode
828 * This function will return a protection mode flag for a file-mapping object
829 * from the open flags of a file.
831 static DWORD BIGBLOCKFILE_GetProtectMode(DWORD openFlags)
833 if (openFlags & (STGM_WRITE | STGM_READWRITE))
834 return PAGE_READWRITE;
836 return PAGE_READONLY;