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
41 #define NONAMELESSUNION
42 #define NONAMELESSSTRUCT
49 #include "storage32.h"
51 #include "wine/debug.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(storage
);
55 /***********************************************************
56 * Data structures used internally by the BigBlockFile
60 /* We map in PAGE_SIZE-sized chunks. Must be a multiple of 4096. */
61 #define PAGE_SIZE 131072
63 #define BLOCKS_PER_PAGE (PAGE_SIZE / BIG_BLOCK_SIZE)
65 /* We keep a list of recently-discarded pages. This controls the
66 * size of that list. */
67 #define MAX_VICTIM_PAGES 16
69 /* This structure provides one bit for each block in a page.
70 * Use BIGBLOCKFILE_{Test,Set,Clear}Bit to manipulate it. */
73 unsigned int bits
[BLOCKS_PER_PAGE
/ (CHAR_BIT
* sizeof(unsigned int))];
77 * This structure identifies the paged that are mapped
78 * from the file and their position in memory. It is
79 * also used to hold a reference count to those pages.
81 * page_index identifies which PAGE_SIZE chunk from the
82 * file this mapping represents. (The mappings are always
94 BlockBits readable_blocks
;
95 BlockBits writable_blocks
;
98 /***********************************************************
99 * Prototypes for private methods
101 static void* BIGBLOCKFILE_GetMappedView(LPBIGBLOCKFILE This
,
103 static void BIGBLOCKFILE_ReleaseMappedPage(LPBIGBLOCKFILE This
,
105 static void BIGBLOCKFILE_FreeAllMappedPages(LPBIGBLOCKFILE This
);
106 static void BIGBLOCKFILE_UnmapAllMappedPages(LPBIGBLOCKFILE This
);
107 static void BIGBLOCKFILE_RemapAllMappedPages(LPBIGBLOCKFILE This
);
108 static void* BIGBLOCKFILE_GetBigBlockPointer(LPBIGBLOCKFILE This
,
110 DWORD desired_access
);
111 static MappedPage
* BIGBLOCKFILE_GetPageFromPointer(LPBIGBLOCKFILE This
,
113 static MappedPage
* BIGBLOCKFILE_CreatePage(LPBIGBLOCKFILE This
,
115 static DWORD
BIGBLOCKFILE_GetProtectMode(DWORD openFlags
);
116 static BOOL
BIGBLOCKFILE_FileInit(LPBIGBLOCKFILE This
, HANDLE hFile
);
117 static BOOL
BIGBLOCKFILE_MemInit(LPBIGBLOCKFILE This
, ILockBytes
* plkbyt
);
119 /* Note that this evaluates a and b multiple times, so don't
120 * pass expressions with side effects. */
121 #define ROUND_UP(a, b) ((((a) + (b) - 1)/(b))*(b))
123 /***********************************************************
124 * Blockbits functions.
126 static inline BOOL
BIGBLOCKFILE_TestBit(const BlockBits
*bb
,
129 unsigned int array_index
= index
/ (CHAR_BIT
* sizeof(unsigned int));
130 unsigned int bit_index
= index
% (CHAR_BIT
* sizeof(unsigned int));
132 return bb
->bits
[array_index
] & (1 << bit_index
);
135 static inline void BIGBLOCKFILE_SetBit(BlockBits
*bb
, unsigned int index
)
137 unsigned int array_index
= index
/ (CHAR_BIT
* sizeof(unsigned int));
138 unsigned int bit_index
= index
% (CHAR_BIT
* sizeof(unsigned int));
140 bb
->bits
[array_index
] |= (1 << bit_index
);
143 static inline void BIGBLOCKFILE_ClearBit(BlockBits
*bb
, unsigned int index
)
145 unsigned int array_index
= index
/ (CHAR_BIT
* sizeof(unsigned int));
146 unsigned int bit_index
= index
% (CHAR_BIT
* sizeof(unsigned int));
148 bb
->bits
[array_index
] &= ~(1 << bit_index
);
151 static inline void BIGBLOCKFILE_Zero(BlockBits
*bb
)
153 memset(bb
->bits
, 0, sizeof(bb
->bits
));
156 /******************************************************************************
157 * BIGBLOCKFILE_Construct
159 * Construct a big block file. Create the file mapping object.
160 * Create the read only mapped pages list, the writable mapped page list
161 * and the blocks in use list.
163 BigBlockFile
* BIGBLOCKFILE_Construct(
172 This
= (LPBIGBLOCKFILE
)HeapAlloc(GetProcessHeap(), 0, sizeof(BigBlockFile
));
177 This
->fileBased
= fileBased
;
179 This
->flProtect
= BIGBLOCKFILE_GetProtectMode(openFlags
);
181 This
->blocksize
= blocksize
;
183 This
->maplist
= NULL
;
184 This
->victimhead
= NULL
;
185 This
->victimtail
= NULL
;
186 This
->num_victim_pages
= 0;
190 if (!BIGBLOCKFILE_FileInit(This
, hFile
))
192 HeapFree(GetProcessHeap(), 0, This
);
198 if (!BIGBLOCKFILE_MemInit(This
, pLkByt
))
200 HeapFree(GetProcessHeap(), 0, This
);
208 /******************************************************************************
209 * BIGBLOCKFILE_FileInit
211 * Initialize a big block object supported by a file.
213 static BOOL
BIGBLOCKFILE_FileInit(LPBIGBLOCKFILE This
, HANDLE hFile
)
216 This
->hbytearray
= 0;
217 This
->pbytearray
= NULL
;
221 if (This
->hfile
== INVALID_HANDLE_VALUE
)
224 /* create the file mapping object
226 This
->hfilemap
= CreateFileMappingA(This
->hfile
,
234 CloseHandle(This
->hfile
);
238 This
->filesize
.s
.LowPart
= GetFileSize(This
->hfile
,
239 &This
->filesize
.s
.HighPart
);
241 This
->maplist
= NULL
;
243 TRACE("file len %lu\n", This
->filesize
.s
.LowPart
);
248 /******************************************************************************
249 * BIGBLOCKFILE_MemInit
251 * Initialize a big block object supported by an ILockBytes on HGLOABL.
253 static BOOL
BIGBLOCKFILE_MemInit(LPBIGBLOCKFILE This
, ILockBytes
* plkbyt
)
259 * Retrieve the handle to the byte array from the LockByte object.
261 if (GetHGlobalFromILockBytes(plkbyt
, &(This
->hbytearray
)) != S_OK
)
263 FIXME("May not be an ILockBytes on HGLOBAL\n");
267 This
->pLkbyt
= plkbyt
;
270 * Increment the reference count of the ILockByte object since
271 * we're keeping a reference to it.
273 ILockBytes_AddRef(This
->pLkbyt
);
275 This
->filesize
.s
.LowPart
= GlobalSize(This
->hbytearray
);
276 This
->filesize
.s
.HighPart
= 0;
278 This
->pbytearray
= GlobalLock(This
->hbytearray
);
280 TRACE("mem on %p len %lu\n", This
->pbytearray
, This
->filesize
.s
.LowPart
);
285 /******************************************************************************
286 * BIGBLOCKFILE_Destructor
288 * Destructor. Clean up, free memory.
290 void BIGBLOCKFILE_Destructor(
293 BIGBLOCKFILE_FreeAllMappedPages(This
);
297 CloseHandle(This
->hfilemap
);
298 CloseHandle(This
->hfile
);
302 GlobalUnlock(This
->hbytearray
);
303 ILockBytes_Release(This
->pLkbyt
);
308 HeapFree(GetProcessHeap(), 0, This
);
311 /******************************************************************************
312 * BIGBLOCKFILE_GetROBigBlock
314 * Returns the specified block in read only mode.
315 * Will return NULL if the block doesn't exists.
317 void* BIGBLOCKFILE_GetROBigBlock(
322 * block index starts at -1
323 * translate to zero based index
325 if (index
== 0xffffffff)
331 * validate the block index
334 if (This
->blocksize
* (index
+ 1)
335 > ROUND_UP(This
->filesize
.s
.LowPart
, This
->blocksize
))
337 TRACE("out of range %lu vs %lu\n", This
->blocksize
* (index
+ 1),
338 This
->filesize
.s
.LowPart
);
342 return BIGBLOCKFILE_GetBigBlockPointer(This
, index
, FILE_MAP_READ
);
345 /******************************************************************************
346 * BIGBLOCKFILE_GetBigBlock
348 * Returns the specified block.
349 * Will grow the file if necessary.
351 void* BIGBLOCKFILE_GetBigBlock(LPBIGBLOCKFILE This
, ULONG index
)
354 * block index starts at -1
355 * translate to zero based index
357 if (index
== 0xffffffff)
363 * make sure that the block physically exists
365 if ((This
->blocksize
* (index
+ 1)) > This
->filesize
.s
.LowPart
)
367 ULARGE_INTEGER newSize
;
369 newSize
.s
.HighPart
= 0;
370 newSize
.s
.LowPart
= This
->blocksize
* (index
+ 1);
372 BIGBLOCKFILE_SetSize(This
, newSize
);
375 return BIGBLOCKFILE_GetBigBlockPointer(This
, index
, FILE_MAP_WRITE
);
378 /******************************************************************************
379 * BIGBLOCKFILE_ReleaseBigBlock
381 * Releases the specified block.
383 void BIGBLOCKFILE_ReleaseBigBlock(LPBIGBLOCKFILE This
, void *pBlock
)
390 page
= BIGBLOCKFILE_GetPageFromPointer(This
, pBlock
);
395 BIGBLOCKFILE_ReleaseMappedPage(This
, page
);
398 /******************************************************************************
399 * BIGBLOCKFILE_SetSize
401 * Sets the size of the file.
404 void BIGBLOCKFILE_SetSize(LPBIGBLOCKFILE This
, ULARGE_INTEGER newSize
)
406 if (This
->filesize
.s
.LowPart
== newSize
.s
.LowPart
)
409 TRACE("from %lu to %lu\n", This
->filesize
.s
.LowPart
, newSize
.s
.LowPart
);
411 * unmap all views, must be done before call to SetEndFile
413 BIGBLOCKFILE_UnmapAllMappedPages(This
);
420 * close file-mapping object, must be done before call to SetEndFile
422 CloseHandle(This
->hfilemap
);
427 * This fixes a bug when saving through smbfs.
428 * smbmount a Windows shared directory, save a structured storage file
429 * to that dir: crash.
431 * The problem is that the SetFilePointer-SetEndOfFile combo below
432 * doesn't always succeed. The file is not grown. It seems like the
433 * operation is cached. By doing the WriteFile, the file is actually
435 * This hack is only needed when saving to smbfs.
437 memset(buf
, '0', 10);
438 SetFilePointer(This
->hfile
, newSize
.s
.LowPart
, NULL
, FILE_BEGIN
);
439 WriteFile(This
->hfile
, buf
, 10, NULL
, NULL
);
445 * set the new end of file
447 SetFilePointer(This
->hfile
, newSize
.s
.LowPart
, NULL
, FILE_BEGIN
);
448 SetEndOfFile(This
->hfile
);
451 * re-create the file mapping object
453 This
->hfilemap
= CreateFileMappingA(This
->hfile
,
461 GlobalUnlock(This
->hbytearray
);
464 * Resize the byte array object.
466 ILockBytes_SetSize(This
->pLkbyt
, newSize
);
469 * Re-acquire the handle, it may have changed.
471 GetHGlobalFromILockBytes(This
->pLkbyt
, &This
->hbytearray
);
472 This
->pbytearray
= GlobalLock(This
->hbytearray
);
475 This
->filesize
.s
.LowPart
= newSize
.s
.LowPart
;
476 This
->filesize
.s
.HighPart
= newSize
.s
.HighPart
;
478 BIGBLOCKFILE_RemapAllMappedPages(This
);
481 /******************************************************************************
482 * BIGBLOCKFILE_GetSize
484 * Returns the size of the file.
487 ULARGE_INTEGER
BIGBLOCKFILE_GetSize(LPBIGBLOCKFILE This
)
489 return This
->filesize
;
492 /******************************************************************************
493 * BIGBLOCKFILE_AccessCheck [PRIVATE]
495 * block_index is the index within the page.
497 static BOOL
BIGBLOCKFILE_AccessCheck(MappedPage
*page
, ULONG block_index
,
498 DWORD desired_access
)
500 assert(block_index
< BLOCKS_PER_PAGE
);
502 if (desired_access
== FILE_MAP_READ
)
504 if (BIGBLOCKFILE_TestBit(&page
->writable_blocks
, block_index
))
507 BIGBLOCKFILE_SetBit(&page
->readable_blocks
, block_index
);
511 assert(desired_access
== FILE_MAP_WRITE
);
513 if (BIGBLOCKFILE_TestBit(&page
->readable_blocks
, block_index
))
516 BIGBLOCKFILE_SetBit(&page
->writable_blocks
, block_index
);
522 /******************************************************************************
523 * BIGBLOCKFILE_GetBigBlockPointer [PRIVATE]
525 * Returns a pointer to the specified block.
527 static void* BIGBLOCKFILE_GetBigBlockPointer(
530 DWORD desired_access
)
532 DWORD page_index
= block_index
/ BLOCKS_PER_PAGE
;
533 DWORD block_on_page
= block_index
% BLOCKS_PER_PAGE
;
535 MappedPage
*page
= BIGBLOCKFILE_GetMappedView(This
, page_index
);
536 if (!page
|| !page
->lpBytes
) return NULL
;
538 if (!BIGBLOCKFILE_AccessCheck(page
, block_on_page
, desired_access
))
540 BIGBLOCKFILE_ReleaseMappedPage(This
, page
);
544 return (LPBYTE
)page
->lpBytes
+ (block_on_page
* This
->blocksize
);
547 /******************************************************************************
548 * BIGBLOCKFILE_GetMappedPageFromPointer [PRIVATE]
550 * pBlock is a pointer to a block on a page.
551 * The page has to be on the in-use list. (As oppsed to the victim list.)
553 * Does not increment the usage count.
555 static MappedPage
*BIGBLOCKFILE_GetPageFromPointer(LPBIGBLOCKFILE This
,
560 for (page
= This
->maplist
; page
!= NULL
; page
= page
->next
)
562 if ((LPBYTE
)pBlock
>= (LPBYTE
)page
->lpBytes
563 && (LPBYTE
)pBlock
<= (LPBYTE
)page
->lpBytes
+ PAGE_SIZE
)
571 /******************************************************************************
572 * BIGBLOCKFILE_FindPageInList [PRIVATE]
575 static MappedPage
*BIGBLOCKFILE_FindPageInList(MappedPage
*head
,
578 for (; head
!= NULL
; head
= head
->next
)
580 if (head
->page_index
== page_index
)
582 InterlockedIncrement(&head
->refcnt
);
591 static void BIGBLOCKFILE_UnlinkPage(MappedPage
*page
)
593 if (page
->next
) page
->next
->prev
= page
->prev
;
594 if (page
->prev
) page
->prev
->next
= page
->next
;
597 static void BIGBLOCKFILE_LinkHeadPage(MappedPage
**head
, MappedPage
*page
)
599 if (*head
) (*head
)->prev
= page
;
605 /******************************************************************************
606 * BIGBLOCKFILE_GetMappedView [PRIVATE]
608 * Gets the page requested if it is already mapped.
609 * If it's not already mapped, this method will map it
611 static void * BIGBLOCKFILE_GetMappedView(
617 page
= BIGBLOCKFILE_FindPageInList(This
->maplist
, page_index
);
620 page
= BIGBLOCKFILE_FindPageInList(This
->victimhead
, page_index
);
623 This
->num_victim_pages
--;
625 BIGBLOCKFILE_Zero(&page
->readable_blocks
);
626 BIGBLOCKFILE_Zero(&page
->writable_blocks
);
632 /* If the page is not already at the head of the list, move
633 * it there. (Also moves pages from victim to main list.) */
634 if (This
->maplist
!= page
)
636 if (This
->victimhead
== page
) This
->victimhead
= page
->next
;
637 if (This
->victimtail
== page
) This
->victimtail
= page
->prev
;
639 BIGBLOCKFILE_UnlinkPage(page
);
641 BIGBLOCKFILE_LinkHeadPage(&This
->maplist
, page
);
647 page
= BIGBLOCKFILE_CreatePage(This
, page_index
);
648 if (!page
) return NULL
;
650 BIGBLOCKFILE_LinkHeadPage(&This
->maplist
, page
);
655 static BOOL
BIGBLOCKFILE_MapPage(LPBIGBLOCKFILE This
, MappedPage
*page
)
657 DWORD lowoffset
= PAGE_SIZE
* page
->page_index
;
662 DWORD desired_access
;
664 if (lowoffset
+ PAGE_SIZE
> This
->filesize
.s
.LowPart
)
665 numBytesToMap
= This
->filesize
.s
.LowPart
- lowoffset
;
667 numBytesToMap
= PAGE_SIZE
;
669 if (This
->flProtect
== PAGE_READONLY
)
670 desired_access
= FILE_MAP_READ
;
672 desired_access
= FILE_MAP_WRITE
;
674 page
->lpBytes
= MapViewOfFile(This
->hfilemap
, desired_access
, 0,
675 lowoffset
, numBytesToMap
);
679 page
->lpBytes
= (LPBYTE
)This
->pbytearray
+ lowoffset
;
682 TRACE("mapped page %lu to %p\n", page
->page_index
, page
->lpBytes
);
684 return page
->lpBytes
!= NULL
;
687 static MappedPage
*BIGBLOCKFILE_CreatePage(LPBIGBLOCKFILE This
,
692 page
= HeapAlloc(GetProcessHeap(), 0, sizeof(MappedPage
));
696 page
->page_index
= page_index
;
702 BIGBLOCKFILE_MapPage(This
, page
);
704 BIGBLOCKFILE_Zero(&page
->readable_blocks
);
705 BIGBLOCKFILE_Zero(&page
->writable_blocks
);
710 static void BIGBLOCKFILE_UnmapPage(LPBIGBLOCKFILE This
, MappedPage
*page
)
712 TRACE("%ld at %p\n", page
->page_index
, page
->lpBytes
);
713 if (page
->refcnt
> 0)
714 ERR("unmapping inuse page %p\n", page
->lpBytes
);
716 if (This
->fileBased
&& page
->lpBytes
)
717 UnmapViewOfFile(page
->lpBytes
);
719 page
->lpBytes
= NULL
;
722 static void BIGBLOCKFILE_DeletePage(LPBIGBLOCKFILE This
, MappedPage
*page
)
724 BIGBLOCKFILE_UnmapPage(This
, page
);
726 HeapFree(GetProcessHeap(), 0, page
);
729 /******************************************************************************
730 * BIGBLOCKFILE_ReleaseMappedPage [PRIVATE]
732 * Decrements the reference count of the mapped page.
734 static void BIGBLOCKFILE_ReleaseMappedPage(
738 assert(This
!= NULL
);
739 assert(page
!= NULL
);
741 /* If the page is no longer refenced, move it to the victim list.
742 * If the victim list is too long, kick somebody off. */
743 if (!InterlockedDecrement(&page
->refcnt
))
745 if (This
->maplist
== page
) This
->maplist
= page
->next
;
747 BIGBLOCKFILE_UnlinkPage(page
);
749 if (MAX_VICTIM_PAGES
> 0)
751 if (This
->num_victim_pages
>= MAX_VICTIM_PAGES
)
753 MappedPage
*victim
= This
->victimtail
;
756 This
->victimtail
= victim
->prev
;
757 if (This
->victimhead
== victim
)
758 This
->victimhead
= victim
->next
;
760 BIGBLOCKFILE_UnlinkPage(victim
);
761 BIGBLOCKFILE_DeletePage(This
, victim
);
764 else This
->num_victim_pages
++;
766 BIGBLOCKFILE_LinkHeadPage(&This
->victimhead
, page
);
767 if (This
->victimtail
== NULL
) This
->victimtail
= page
;
770 BIGBLOCKFILE_DeletePage(This
, page
);
774 static void BIGBLOCKFILE_DeleteList(LPBIGBLOCKFILE This
, MappedPage
*list
)
778 MappedPage
*next
= list
->next
;
780 BIGBLOCKFILE_DeletePage(This
, list
);
786 /******************************************************************************
787 * BIGBLOCKFILE_FreeAllMappedPages [PRIVATE]
789 * Unmap all currently mapped pages.
790 * Empty mapped pages list.
792 static void BIGBLOCKFILE_FreeAllMappedPages(
795 BIGBLOCKFILE_DeleteList(This
, This
->maplist
);
796 BIGBLOCKFILE_DeleteList(This
, This
->victimhead
);
798 This
->maplist
= NULL
;
799 This
->victimhead
= NULL
;
800 This
->victimtail
= NULL
;
801 This
->num_victim_pages
= 0;
804 static void BIGBLOCKFILE_UnmapList(LPBIGBLOCKFILE This
, MappedPage
*list
)
806 for (; list
!= NULL
; list
= list
->next
)
808 BIGBLOCKFILE_UnmapPage(This
, list
);
812 static void BIGBLOCKFILE_UnmapAllMappedPages(LPBIGBLOCKFILE This
)
814 BIGBLOCKFILE_UnmapList(This
, This
->maplist
);
815 BIGBLOCKFILE_UnmapList(This
, This
->victimhead
);
818 static void BIGBLOCKFILE_RemapList(LPBIGBLOCKFILE This
, MappedPage
*list
)
822 MappedPage
*next
= list
->next
;
824 if (list
->page_index
* PAGE_SIZE
> This
->filesize
.s
.LowPart
)
826 TRACE("discarding %lu\n", list
->page_index
);
828 /* page is entirely outside of the file, delete it */
829 BIGBLOCKFILE_UnlinkPage(list
);
830 BIGBLOCKFILE_DeletePage(This
, list
);
834 /* otherwise, remap it */
835 BIGBLOCKFILE_MapPage(This
, list
);
842 static void BIGBLOCKFILE_RemapAllMappedPages(LPBIGBLOCKFILE This
)
844 BIGBLOCKFILE_RemapList(This
, This
->maplist
);
845 BIGBLOCKFILE_RemapList(This
, This
->victimhead
);
848 /****************************************************************************
849 * BIGBLOCKFILE_GetProtectMode
851 * This function will return a protection mode flag for a file-mapping object
852 * from the open flags of a file.
854 static DWORD
BIGBLOCKFILE_GetProtectMode(DWORD openFlags
)
856 if (openFlags
& (STGM_WRITE
| STGM_READWRITE
))
857 return PAGE_READWRITE
;
859 return PAGE_READONLY
;