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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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
98 BlockBits readable_blocks;
99 BlockBits writable_blocks;
102 /***********************************************************
103 * Prototypes for private methods
105 static void* BIGBLOCKFILE_GetMappedView(LPBIGBLOCKFILE This,
107 static void BIGBLOCKFILE_ReleaseMappedPage(LPBIGBLOCKFILE This,
109 static void BIGBLOCKFILE_FreeAllMappedPages(LPBIGBLOCKFILE This);
110 static void BIGBLOCKFILE_UnmapAllMappedPages(LPBIGBLOCKFILE This);
111 static void BIGBLOCKFILE_RemapAllMappedPages(LPBIGBLOCKFILE This);
112 static MappedPage* BIGBLOCKFILE_CreatePage(LPBIGBLOCKFILE This,
114 static DWORD BIGBLOCKFILE_GetProtectMode(DWORD openFlags);
115 static BOOL BIGBLOCKFILE_FileInit(LPBIGBLOCKFILE This, HANDLE hFile);
116 static BOOL BIGBLOCKFILE_MemInit(LPBIGBLOCKFILE This, ILockBytes* plkbyt);
118 /* Note that this evaluates a and b multiple times, so don't
119 * pass expressions with side effects. */
120 #define ROUND_UP(a, b) ((((a) + (b) - 1)/(b))*(b))
122 /***********************************************************
123 * Blockbits functions.
125 static inline BOOL BIGBLOCKFILE_TestBit(const BlockBits *bb,
128 unsigned int array_index = index / (CHAR_BIT * sizeof(unsigned int));
129 unsigned int bit_index = index % (CHAR_BIT * sizeof(unsigned int));
131 return bb->bits[array_index] & (1 << bit_index);
134 static inline void BIGBLOCKFILE_SetBit(BlockBits *bb, unsigned int index)
136 unsigned int array_index = index / (CHAR_BIT * sizeof(unsigned int));
137 unsigned int bit_index = index % (CHAR_BIT * sizeof(unsigned int));
139 bb->bits[array_index] |= (1 << bit_index);
142 static inline void BIGBLOCKFILE_ClearBit(BlockBits *bb, unsigned int index)
144 unsigned int array_index = index / (CHAR_BIT * sizeof(unsigned int));
145 unsigned int bit_index = index % (CHAR_BIT * sizeof(unsigned int));
147 bb->bits[array_index] &= ~(1 << bit_index);
150 static inline void BIGBLOCKFILE_Zero(BlockBits *bb)
152 memset(bb->bits, 0, sizeof(bb->bits));
155 /******************************************************************************
156 * BIGBLOCKFILE_Construct
158 * Construct a big block file. Create the file mapping object.
159 * Create the read only mapped pages list, the writable mapped page list
160 * and the blocks in use list.
162 BigBlockFile * BIGBLOCKFILE_Construct(
171 This = HeapAlloc(GetProcessHeap(), 0, sizeof(BigBlockFile));
176 This->fileBased = fileBased;
178 This->flProtect = BIGBLOCKFILE_GetProtectMode(openFlags);
180 This->blocksize = blocksize;
182 This->maplist = NULL;
183 This->victimhead = NULL;
184 This->victimtail = NULL;
185 This->num_victim_pages = 0;
189 if (!BIGBLOCKFILE_FileInit(This, hFile))
191 HeapFree(GetProcessHeap(), 0, This);
197 if (!BIGBLOCKFILE_MemInit(This, pLkByt))
199 HeapFree(GetProcessHeap(), 0, This);
207 /******************************************************************************
208 * BIGBLOCKFILE_FileInit
210 * Initialize a big block object supported by a file.
212 static BOOL BIGBLOCKFILE_FileInit(LPBIGBLOCKFILE This, HANDLE hFile)
215 This->hbytearray = 0;
216 This->pbytearray = NULL;
220 if (This->hfile == INVALID_HANDLE_VALUE)
223 This->filesize.u.LowPart = GetFileSize(This->hfile,
224 &This->filesize.u.HighPart);
226 if( This->filesize.u.LowPart || This->filesize.u.HighPart )
228 /* create the file mapping object
230 This->hfilemap = CreateFileMappingA(This->hfile,
238 CloseHandle(This->hfile);
243 This->hfilemap = NULL;
245 This->maplist = NULL;
247 TRACE("file len %u\n", This->filesize.u.LowPart);
252 /******************************************************************************
253 * BIGBLOCKFILE_MemInit
255 * Initialize a big block object supported by an ILockBytes on HGLOABL.
257 static BOOL BIGBLOCKFILE_MemInit(LPBIGBLOCKFILE This, ILockBytes* plkbyt)
263 * Retrieve the handle to the byte array from the LockByte object.
265 if (GetHGlobalFromILockBytes(plkbyt, &(This->hbytearray)) != S_OK)
267 FIXME("May not be an ILockBytes on HGLOBAL\n");
271 This->pLkbyt = plkbyt;
274 * Increment the reference count of the ILockByte object since
275 * we're keeping a reference to it.
277 ILockBytes_AddRef(This->pLkbyt);
279 This->filesize.u.LowPart = GlobalSize(This->hbytearray);
280 This->filesize.u.HighPart = 0;
282 This->pbytearray = GlobalLock(This->hbytearray);
284 TRACE("mem on %p len %u\n", This->pbytearray, This->filesize.u.LowPart);
289 /******************************************************************************
290 * BIGBLOCKFILE_Destructor
292 * Destructor. Clean up, free memory.
294 void BIGBLOCKFILE_Destructor(
297 BIGBLOCKFILE_FreeAllMappedPages(This);
301 CloseHandle(This->hfilemap);
302 CloseHandle(This->hfile);
306 GlobalUnlock(This->hbytearray);
307 ILockBytes_Release(This->pLkbyt);
312 HeapFree(GetProcessHeap(), 0, This);
315 /******************************************************************************
316 * BIGBLOCKFILE_EnsureExists
318 * Grows the file if necessary to make sure the block is valid.
320 void BIGBLOCKFILE_EnsureExists(LPBIGBLOCKFILE This, ULONG index)
323 * block index starts at -1
324 * translate to zero based index
326 if (index == 0xffffffff)
332 * make sure that the block physically exists
334 if ((This->blocksize * (index + 1)) > This->filesize.u.LowPart)
336 ULARGE_INTEGER newSize;
338 newSize.u.HighPart = 0;
339 newSize.u.LowPart = This->blocksize * (index + 1);
341 BIGBLOCKFILE_SetSize(This, newSize);
345 /******************************************************************************
346 * BIGBLOCKFILE_SetSize
348 * Sets the size of the file.
351 void BIGBLOCKFILE_SetSize(LPBIGBLOCKFILE This, ULARGE_INTEGER newSize)
353 if (This->filesize.u.LowPart == newSize.u.LowPart)
356 TRACE("from %u to %u\n", This->filesize.u.LowPart, newSize.u.LowPart);
358 * unmap all views, must be done before call to SetEndFile
360 BIGBLOCKFILE_UnmapAllMappedPages(This);
364 LARGE_INTEGER newpos;
366 newpos.QuadPart = newSize.QuadPart;
367 if (SetFilePointerEx(This->hfile, newpos, NULL, FILE_BEGIN))
369 if( This->hfilemap ) CloseHandle(This->hfilemap);
371 SetEndOfFile(This->hfile);
374 * re-create the file mapping object
376 This->hfilemap = CreateFileMappingA(This->hfile,
385 GlobalUnlock(This->hbytearray);
388 * Resize the byte array object.
390 ILockBytes_SetSize(This->pLkbyt, newSize);
393 * Re-acquire the handle, it may have changed.
395 GetHGlobalFromILockBytes(This->pLkbyt, &This->hbytearray);
396 This->pbytearray = GlobalLock(This->hbytearray);
399 This->filesize.u.LowPart = newSize.u.LowPart;
400 This->filesize.u.HighPart = newSize.u.HighPart;
402 BIGBLOCKFILE_RemapAllMappedPages(This);
405 /******************************************************************************
406 * BIGBLOCKFILE_GetSize
408 * Returns the size of the file.
411 ULARGE_INTEGER BIGBLOCKFILE_GetSize(LPBIGBLOCKFILE This)
413 return This->filesize;
416 /******************************************************************************
417 * BIGBLOCKFILE_FindPageInList [PRIVATE]
420 static MappedPage *BIGBLOCKFILE_FindPageInList(MappedPage *head,
423 for (; head != NULL; head = head->next)
425 if (head->page_index == page_index)
427 InterlockedIncrement(&head->refcnt);
436 static void BIGBLOCKFILE_UnlinkPage(MappedPage *page)
438 if (page->next) page->next->prev = page->prev;
439 if (page->prev) page->prev->next = page->next;
442 static void BIGBLOCKFILE_LinkHeadPage(MappedPage **head, MappedPage *page)
444 if (*head) (*head)->prev = page;
450 /******************************************************************************
451 * BIGBLOCKFILE_GetMappedView [PRIVATE]
453 * Gets the page requested if it is already mapped.
454 * If it's not already mapped, this method will map it
456 static void * BIGBLOCKFILE_GetMappedView(
462 page = BIGBLOCKFILE_FindPageInList(This->maplist, page_index);
465 page = BIGBLOCKFILE_FindPageInList(This->victimhead, page_index);
468 This->num_victim_pages--;
470 BIGBLOCKFILE_Zero(&page->readable_blocks);
471 BIGBLOCKFILE_Zero(&page->writable_blocks);
477 /* If the page is not already at the head of the list, move
478 * it there. (Also moves pages from victim to main list.) */
479 if (This->maplist != page)
481 if (This->victimhead == page) This->victimhead = page->next;
482 if (This->victimtail == page) This->victimtail = page->prev;
484 BIGBLOCKFILE_UnlinkPage(page);
486 BIGBLOCKFILE_LinkHeadPage(&This->maplist, page);
492 page = BIGBLOCKFILE_CreatePage(This, page_index);
493 if (!page) return NULL;
495 BIGBLOCKFILE_LinkHeadPage(&This->maplist, page);
500 static BOOL BIGBLOCKFILE_MapPage(LPBIGBLOCKFILE This, MappedPage *page)
502 DWORD lowoffset = PAGE_SIZE * page->page_index;
507 DWORD desired_access;
509 if( !This->hfilemap )
512 if (lowoffset + PAGE_SIZE > This->filesize.u.LowPart)
513 numBytesToMap = This->filesize.u.LowPart - lowoffset;
515 numBytesToMap = PAGE_SIZE;
517 if (This->flProtect == PAGE_READONLY)
518 desired_access = FILE_MAP_READ;
520 desired_access = FILE_MAP_WRITE;
522 page->lpBytes = MapViewOfFile(This->hfilemap, desired_access, 0,
523 lowoffset, numBytesToMap);
524 page->mapped_bytes = numBytesToMap;
528 page->lpBytes = (LPBYTE)This->pbytearray + lowoffset;
529 page->mapped_bytes = PAGE_SIZE;
532 TRACE("mapped page %u to %p\n", page->page_index, page->lpBytes);
534 return page->lpBytes != NULL;
537 static MappedPage *BIGBLOCKFILE_CreatePage(LPBIGBLOCKFILE This,
542 page = HeapAlloc(GetProcessHeap(), 0, sizeof(MappedPage));
546 page->page_index = page_index;
552 if (!BIGBLOCKFILE_MapPage(This, page))
554 HeapFree(GetProcessHeap(),0,page);
558 BIGBLOCKFILE_Zero(&page->readable_blocks);
559 BIGBLOCKFILE_Zero(&page->writable_blocks);
564 static void BIGBLOCKFILE_UnmapPage(LPBIGBLOCKFILE This, MappedPage *page)
566 TRACE("%d at %p\n", page->page_index, page->lpBytes);
567 if (page->refcnt > 0)
568 ERR("unmapping inuse page %p\n", page->lpBytes);
570 if (This->fileBased && page->lpBytes)
571 UnmapViewOfFile(page->lpBytes);
573 page->lpBytes = NULL;
576 static void BIGBLOCKFILE_DeletePage(LPBIGBLOCKFILE This, MappedPage *page)
578 BIGBLOCKFILE_UnmapPage(This, page);
580 HeapFree(GetProcessHeap(), 0, page);
583 /******************************************************************************
584 * BIGBLOCKFILE_ReleaseMappedPage [PRIVATE]
586 * Decrements the reference count of the mapped page.
588 static void BIGBLOCKFILE_ReleaseMappedPage(
592 assert(This != NULL);
593 assert(page != NULL);
595 /* If the page is no longer refenced, move it to the victim list.
596 * If the victim list is too long, kick somebody off. */
597 if (!InterlockedDecrement(&page->refcnt))
599 if (This->maplist == page) This->maplist = page->next;
601 BIGBLOCKFILE_UnlinkPage(page);
603 if (MAX_VICTIM_PAGES > 0)
605 if (This->num_victim_pages >= MAX_VICTIM_PAGES)
607 MappedPage *victim = This->victimtail;
610 This->victimtail = victim->prev;
611 if (This->victimhead == victim)
612 This->victimhead = victim->next;
614 BIGBLOCKFILE_UnlinkPage(victim);
615 BIGBLOCKFILE_DeletePage(This, victim);
618 else This->num_victim_pages++;
620 BIGBLOCKFILE_LinkHeadPage(&This->victimhead, page);
621 if (This->victimtail == NULL) This->victimtail = page;
624 BIGBLOCKFILE_DeletePage(This, page);
628 static void BIGBLOCKFILE_DeleteList(LPBIGBLOCKFILE This, MappedPage *list)
632 MappedPage *next = list->next;
634 BIGBLOCKFILE_DeletePage(This, list);
640 /******************************************************************************
641 * BIGBLOCKFILE_FreeAllMappedPages [PRIVATE]
643 * Unmap all currently mapped pages.
644 * Empty mapped pages list.
646 static void BIGBLOCKFILE_FreeAllMappedPages(
649 BIGBLOCKFILE_DeleteList(This, This->maplist);
650 BIGBLOCKFILE_DeleteList(This, This->victimhead);
652 This->maplist = NULL;
653 This->victimhead = NULL;
654 This->victimtail = NULL;
655 This->num_victim_pages = 0;
658 static void BIGBLOCKFILE_UnmapList(LPBIGBLOCKFILE This, MappedPage *list)
660 for (; list != NULL; list = list->next)
662 BIGBLOCKFILE_UnmapPage(This, list);
666 static void BIGBLOCKFILE_UnmapAllMappedPages(LPBIGBLOCKFILE This)
668 BIGBLOCKFILE_UnmapList(This, This->maplist);
669 BIGBLOCKFILE_UnmapList(This, This->victimhead);
672 static void BIGBLOCKFILE_RemapList(LPBIGBLOCKFILE This, MappedPage *list)
676 MappedPage *next = list->next;
678 if (list->page_index * PAGE_SIZE > This->filesize.u.LowPart)
680 TRACE("discarding %u\n", list->page_index);
682 /* page is entirely outside of the file, delete it */
683 BIGBLOCKFILE_UnlinkPage(list);
684 BIGBLOCKFILE_DeletePage(This, list);
688 /* otherwise, remap it */
689 BIGBLOCKFILE_MapPage(This, list);
696 static void BIGBLOCKFILE_RemapAllMappedPages(LPBIGBLOCKFILE This)
698 BIGBLOCKFILE_RemapList(This, This->maplist);
699 BIGBLOCKFILE_RemapList(This, This->victimhead);
702 /****************************************************************************
703 * BIGBLOCKFILE_GetProtectMode
705 * This function will return a protection mode flag for a file-mapping object
706 * from the open flags of a file.
708 static DWORD BIGBLOCKFILE_GetProtectMode(DWORD openFlags)
710 switch(STGM_ACCESS_MODE(openFlags))
714 return PAGE_READWRITE;
716 return PAGE_READONLY;
720 /* ILockByte Interfaces */
722 /******************************************************************************
723 * This method is part of the ILockBytes interface.
725 * It reads a block of information from the byte array at the specified
728 * See the documentation of ILockBytes for more info.
730 static HRESULT WINAPI ImplBIGBLOCKFILE_ReadAt(
731 BigBlockFile* const This,
732 ULARGE_INTEGER ulOffset, /* [in] */
733 void* pv, /* [length_is][size_is][out] */
735 ULONG* pcbRead) /* [out] */
737 ULONG first_page = ulOffset.u.LowPart / PAGE_SIZE;
738 ULONG offset_in_page = ulOffset.u.LowPart % PAGE_SIZE;
739 ULONG bytes_left = cb;
740 ULONG page_index = first_page;
741 ULONG bytes_from_page;
742 LPVOID writePtr = pv;
746 TRACE("(%p)-> %i %p %i %p\n",This, ulOffset.u.LowPart, pv, cb, pcbRead);
748 /* verify a sane enviroment */
749 if (!This) return E_FAIL;
751 if (offset_in_page + bytes_left > PAGE_SIZE)
752 bytes_from_page = PAGE_SIZE - offset_in_page;
754 bytes_from_page = bytes_left;
763 MappedPage *page = BIGBLOCKFILE_GetMappedView(This, page_index);
765 if (!page || !page->lpBytes)
767 rc = STG_E_READFAULT;
771 TRACE("page %i, offset %u, bytes_from_page %u, bytes_left %u\n",
772 page->page_index, offset_in_page, bytes_from_page, bytes_left);
774 if (page->mapped_bytes < bytes_from_page)
777 bytes_from_page = page->mapped_bytes;
780 readPtr = (BYTE*)page->lpBytes + offset_in_page;
781 memcpy(writePtr,readPtr,bytes_from_page);
782 BIGBLOCKFILE_ReleaseMappedPage(This, page);
785 *pcbRead += bytes_from_page;
786 bytes_left -= bytes_from_page;
788 if (bytes_left && !eof)
790 writePtr = (LPBYTE)writePtr + bytes_from_page;
793 if (bytes_left > PAGE_SIZE)
794 bytes_from_page = PAGE_SIZE;
796 bytes_from_page = bytes_left;
800 rc = STG_E_READFAULT;
809 /******************************************************************************
810 * This method is part of the ILockBytes interface.
812 * It writes the specified bytes at the specified offset.
813 * position. If the file is too small, it will be resized.
815 * See the documentation of ILockBytes for more info.
817 static HRESULT WINAPI ImplBIGBLOCKFILE_WriteAt(
818 BigBlockFile* const This,
819 ULARGE_INTEGER ulOffset, /* [in] */
820 const void* pv, /* [size_is][in] */
822 ULONG* pcbWritten) /* [out] */
824 ULONG size_needed = ulOffset.u.LowPart + cb;
825 ULONG first_page = ulOffset.u.LowPart / PAGE_SIZE;
826 ULONG offset_in_page = ulOffset.u.LowPart % PAGE_SIZE;
827 ULONG bytes_left = cb;
828 ULONG page_index = first_page;
830 LPCVOID readPtr = pv;
834 TRACE("(%p)-> %i %p %i %p\n",This, ulOffset.u.LowPart, pv, cb, pcbWritten);
836 /* verify a sane enviroment */
837 if (!This) return E_FAIL;
839 if (This->flProtect != PAGE_READWRITE)
840 return STG_E_ACCESSDENIED;
842 if (size_needed > This->filesize.u.LowPart)
844 ULARGE_INTEGER newSize;
845 newSize.u.HighPart = 0;
846 newSize.u.LowPart = size_needed;
847 BIGBLOCKFILE_SetSize(This, newSize);
850 if (offset_in_page + bytes_left > PAGE_SIZE)
851 bytes_to_page = PAGE_SIZE - offset_in_page;
853 bytes_to_page = bytes_left;
861 MappedPage *page = BIGBLOCKFILE_GetMappedView(This, page_index);
863 TRACE("page %i, offset %u, bytes_to_page %u, bytes_left %u\n",
864 page->page_index, offset_in_page, bytes_to_page, bytes_left);
866 if (page->mapped_bytes < bytes_to_page)
868 ERR("Not enough bytes mapped to the page. This should never happen\n");
875 ERR("Unable to get a page to write. This should never happen\n");
880 writePtr = (BYTE*)page->lpBytes + offset_in_page;
881 memcpy(writePtr,readPtr,bytes_to_page);
882 BIGBLOCKFILE_ReleaseMappedPage(This, page);
885 *pcbWritten += bytes_to_page;
886 bytes_left -= bytes_to_page;
890 readPtr = (LPBYTE)readPtr + bytes_to_page;
893 if (bytes_left > PAGE_SIZE)
894 bytes_to_page = PAGE_SIZE;
896 bytes_to_page = bytes_left;
904 HRESULT BIGBLOCKFILE_ReadAt(LPBIGBLOCKFILE This, ULARGE_INTEGER offset,
905 void* buffer, ULONG size, ULONG* bytesRead)
908 return ImplBIGBLOCKFILE_ReadAt(This,offset,buffer,size,bytesRead);
910 return ILockBytes_ReadAt(This->pLkbyt,offset,buffer,size,bytesRead);
913 HRESULT BIGBLOCKFILE_WriteAt(LPBIGBLOCKFILE This, ULARGE_INTEGER offset,
914 void* buffer, const ULONG size, ULONG* bytesRead)
917 return ImplBIGBLOCKFILE_WriteAt(This,offset,buffer,size,bytesRead);
919 return ILockBytes_WriteAt(This->pLkbyt,offset,buffer,size,bytesRead);