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 #include "wine/obj_base.h"
43 #include "wine/obj_storage.h"
46 #include "storage32.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(storage
);
52 /***********************************************************
53 * Data structures used internally by the BigBlockFile
57 /* We map in PAGE_SIZE-sized chunks. Must be a multiple of 4096. */
58 #define PAGE_SIZE 131072
60 #define BLOCKS_PER_PAGE (PAGE_SIZE / BIG_BLOCK_SIZE)
62 /* We keep a list of recently-discarded pages. This controls the
63 * size of that list. */
64 #define MAX_VICTIM_PAGES 16
66 /* This structure provides one bit for each block in a page.
67 * Use BIGBLOCKFILE_{Test,Set,Clear}Bit to manipulate it. */
70 unsigned int bits
[BLOCKS_PER_PAGE
/ (CHAR_BIT
* sizeof(unsigned int))];
74 * This structure identifies the paged that are mapped
75 * from the file and their position in memory. It is
76 * also used to hold a reference count to those pages.
78 * page_index identifies which PAGE_SIZE chunk from the
79 * file this mapping represents. (The mappings are always
91 BlockBits readable_blocks
;
92 BlockBits writable_blocks
;
95 /***********************************************************
96 * Prototypes for private methods
98 static void* BIGBLOCKFILE_GetMappedView(LPBIGBLOCKFILE This
,
100 static void BIGBLOCKFILE_ReleaseMappedPage(LPBIGBLOCKFILE This
,
102 static void BIGBLOCKFILE_FreeAllMappedPages(LPBIGBLOCKFILE This
);
103 static void BIGBLOCKFILE_UnmapAllMappedPages(LPBIGBLOCKFILE This
);
104 static void BIGBLOCKFILE_RemapAllMappedPages(LPBIGBLOCKFILE This
);
105 static void* BIGBLOCKFILE_GetBigBlockPointer(LPBIGBLOCKFILE This
,
107 DWORD desired_access
);
108 static MappedPage
* BIGBLOCKFILE_GetPageFromPointer(LPBIGBLOCKFILE This
,
110 static MappedPage
* BIGBLOCKFILE_CreatePage(LPBIGBLOCKFILE This
,
112 static DWORD
BIGBLOCKFILE_GetProtectMode(DWORD openFlags
);
113 static BOOL
BIGBLOCKFILE_FileInit(LPBIGBLOCKFILE This
, HANDLE hFile
);
114 static BOOL
BIGBLOCKFILE_MemInit(LPBIGBLOCKFILE This
, ILockBytes
* plkbyt
);
116 /* Note that this evaluates a and b multiple times, so don't
117 * pass expressions with side effects. */
118 #define ROUND_UP(a, b) ((((a) + (b) - 1)/(b))*(b))
120 /***********************************************************
121 * Blockbits functions.
123 static inline BOOL
BIGBLOCKFILE_TestBit(const BlockBits
*bb
,
126 unsigned int array_index
= index
/ (CHAR_BIT
* sizeof(unsigned int));
127 unsigned int bit_index
= index
% (CHAR_BIT
* sizeof(unsigned int));
129 return bb
->bits
[array_index
] & (1 << bit_index
);
132 static inline void BIGBLOCKFILE_SetBit(BlockBits
*bb
, unsigned int index
)
134 unsigned int array_index
= index
/ (CHAR_BIT
* sizeof(unsigned int));
135 unsigned int bit_index
= index
% (CHAR_BIT
* sizeof(unsigned int));
137 bb
->bits
[array_index
] |= (1 << bit_index
);
140 static inline void BIGBLOCKFILE_ClearBit(BlockBits
*bb
, unsigned int index
)
142 unsigned int array_index
= index
/ (CHAR_BIT
* sizeof(unsigned int));
143 unsigned int bit_index
= index
% (CHAR_BIT
* sizeof(unsigned int));
145 bb
->bits
[array_index
] &= ~(1 << bit_index
);
148 static inline void BIGBLOCKFILE_Zero(BlockBits
*bb
)
150 memset(bb
->bits
, 0, sizeof(bb
->bits
));
153 /******************************************************************************
154 * BIGBLOCKFILE_Construct
156 * Construct a big block file. Create the file mapping object.
157 * Create the read only mapped pages list, the writable mapped page list
158 * and the blocks in use list.
160 BigBlockFile
* BIGBLOCKFILE_Construct(
169 This
= (LPBIGBLOCKFILE
)HeapAlloc(GetProcessHeap(), 0, sizeof(BigBlockFile
));
174 This
->fileBased
= fileBased
;
176 This
->flProtect
= BIGBLOCKFILE_GetProtectMode(openFlags
);
178 This
->blocksize
= blocksize
;
180 This
->maplist
= NULL
;
181 This
->victimhead
= NULL
;
182 This
->victimtail
= NULL
;
183 This
->num_victim_pages
= 0;
187 if (!BIGBLOCKFILE_FileInit(This
, hFile
))
189 HeapFree(GetProcessHeap(), 0, This
);
195 if (!BIGBLOCKFILE_MemInit(This
, pLkByt
))
197 HeapFree(GetProcessHeap(), 0, This
);
205 /******************************************************************************
206 * BIGBLOCKFILE_FileInit
208 * Initialize a big block object supported by a file.
210 static BOOL
BIGBLOCKFILE_FileInit(LPBIGBLOCKFILE This
, HANDLE hFile
)
213 This
->hbytearray
= 0;
214 This
->pbytearray
= NULL
;
218 if (This
->hfile
== INVALID_HANDLE_VALUE
)
221 /* create the file mapping object
223 This
->hfilemap
= CreateFileMappingA(This
->hfile
,
231 CloseHandle(This
->hfile
);
235 This
->filesize
.s
.LowPart
= GetFileSize(This
->hfile
,
236 &This
->filesize
.s
.HighPart
);
238 This
->maplist
= NULL
;
240 TRACE("file len %lu\n", This
->filesize
.s
.LowPart
);
245 /******************************************************************************
246 * BIGBLOCKFILE_MemInit
248 * Initialize a big block object supported by an ILockBytes on HGLOABL.
250 static BOOL
BIGBLOCKFILE_MemInit(LPBIGBLOCKFILE This
, ILockBytes
* plkbyt
)
256 * Retrieve the handle to the byte array from the LockByte object.
258 if (GetHGlobalFromILockBytes(plkbyt
, &(This
->hbytearray
)) != S_OK
)
260 FIXME("May not be an ILockBytes on HGLOBAL\n");
264 This
->pLkbyt
= plkbyt
;
267 * Increment the reference count of the ILockByte object since
268 * we're keeping a reference to it.
270 ILockBytes_AddRef(This
->pLkbyt
);
272 This
->filesize
.s
.LowPart
= GlobalSize(This
->hbytearray
);
273 This
->filesize
.s
.HighPart
= 0;
275 This
->pbytearray
= GlobalLock(This
->hbytearray
);
277 TRACE("mem on %p len %lu\n", This
->pbytearray
, This
->filesize
.s
.LowPart
);
282 /******************************************************************************
283 * BIGBLOCKFILE_Destructor
285 * Destructor. Clean up, free memory.
287 void BIGBLOCKFILE_Destructor(
290 BIGBLOCKFILE_FreeAllMappedPages(This
);
294 CloseHandle(This
->hfilemap
);
295 CloseHandle(This
->hfile
);
299 GlobalUnlock(This
->hbytearray
);
300 ILockBytes_Release(This
->pLkbyt
);
305 HeapFree(GetProcessHeap(), 0, This
);
308 /******************************************************************************
309 * BIGBLOCKFILE_GetROBigBlock
311 * Returns the specified block in read only mode.
312 * Will return NULL if the block doesn't exists.
314 void* BIGBLOCKFILE_GetROBigBlock(
319 * block index starts at -1
320 * translate to zero based index
322 if (index
== 0xffffffff)
328 * validate the block index
331 if (This
->blocksize
* (index
+ 1)
332 > ROUND_UP(This
->filesize
.s
.LowPart
, This
->blocksize
))
334 TRACE("out of range %lu vs %lu\n", This
->blocksize
* (index
+ 1),
335 This
->filesize
.s
.LowPart
);
339 return BIGBLOCKFILE_GetBigBlockPointer(This
, index
, FILE_MAP_READ
);
342 /******************************************************************************
343 * BIGBLOCKFILE_GetBigBlock
345 * Returns the specified block.
346 * Will grow the file if necessary.
348 void* BIGBLOCKFILE_GetBigBlock(LPBIGBLOCKFILE This
, ULONG index
)
351 * block index starts at -1
352 * translate to zero based index
354 if (index
== 0xffffffff)
360 * make sure that the block physically exists
362 if ((This
->blocksize
* (index
+ 1)) > This
->filesize
.s
.LowPart
)
364 ULARGE_INTEGER newSize
;
366 newSize
.s
.HighPart
= 0;
367 newSize
.s
.LowPart
= This
->blocksize
* (index
+ 1);
369 BIGBLOCKFILE_SetSize(This
, newSize
);
372 return BIGBLOCKFILE_GetBigBlockPointer(This
, index
, FILE_MAP_WRITE
);
375 /******************************************************************************
376 * BIGBLOCKFILE_ReleaseBigBlock
378 * Releases the specified block.
380 void BIGBLOCKFILE_ReleaseBigBlock(LPBIGBLOCKFILE This
, void *pBlock
)
387 page
= BIGBLOCKFILE_GetPageFromPointer(This
, pBlock
);
392 BIGBLOCKFILE_ReleaseMappedPage(This
, page
);
395 /******************************************************************************
396 * BIGBLOCKFILE_SetSize
398 * Sets the size of the file.
401 void BIGBLOCKFILE_SetSize(LPBIGBLOCKFILE This
, ULARGE_INTEGER newSize
)
403 if (This
->filesize
.s
.LowPart
== newSize
.s
.LowPart
)
406 TRACE("from %lu to %lu\n", This
->filesize
.s
.LowPart
, newSize
.s
.LowPart
);
408 * unmap all views, must be done before call to SetEndFile
410 BIGBLOCKFILE_UnmapAllMappedPages(This
);
417 * close file-mapping object, must be done before call to SetEndFile
419 CloseHandle(This
->hfilemap
);
424 * This fixes a bug when saving through smbfs.
425 * smbmount a Windows shared directory, save a structured storage file
426 * to that dir: crash.
428 * The problem is that the SetFilePointer-SetEndOfFile combo below
429 * doesn't always succeed. The file is not grown. It seems like the
430 * operation is cached. By doing the WriteFile, the file is actually
432 * This hack is only needed when saving to smbfs.
434 memset(buf
, '0', 10);
435 SetFilePointer(This
->hfile
, newSize
.s
.LowPart
, NULL
, FILE_BEGIN
);
436 WriteFile(This
->hfile
, buf
, 10, NULL
, NULL
);
442 * set the new end of file
444 SetFilePointer(This
->hfile
, newSize
.s
.LowPart
, NULL
, FILE_BEGIN
);
445 SetEndOfFile(This
->hfile
);
448 * re-create the file mapping object
450 This
->hfilemap
= CreateFileMappingA(This
->hfile
,
458 GlobalUnlock(This
->hbytearray
);
461 * Resize the byte array object.
463 ILockBytes_SetSize(This
->pLkbyt
, newSize
);
466 * Re-acquire the handle, it may have changed.
468 GetHGlobalFromILockBytes(This
->pLkbyt
, &This
->hbytearray
);
469 This
->pbytearray
= GlobalLock(This
->hbytearray
);
472 This
->filesize
.s
.LowPart
= newSize
.s
.LowPart
;
473 This
->filesize
.s
.HighPart
= newSize
.s
.HighPart
;
475 BIGBLOCKFILE_RemapAllMappedPages(This
);
478 /******************************************************************************
479 * BIGBLOCKFILE_GetSize
481 * Returns the size of the file.
484 ULARGE_INTEGER
BIGBLOCKFILE_GetSize(LPBIGBLOCKFILE This
)
486 return This
->filesize
;
489 /******************************************************************************
490 * BIGBLOCKFILE_AccessCheck [PRIVATE]
492 * block_index is the index within the page.
494 static BOOL
BIGBLOCKFILE_AccessCheck(MappedPage
*page
, ULONG block_index
,
495 DWORD desired_access
)
497 assert(block_index
< BLOCKS_PER_PAGE
);
499 if (desired_access
== FILE_MAP_READ
)
501 if (BIGBLOCKFILE_TestBit(&page
->writable_blocks
, block_index
))
504 BIGBLOCKFILE_SetBit(&page
->readable_blocks
, block_index
);
508 assert(desired_access
== FILE_MAP_WRITE
);
510 if (BIGBLOCKFILE_TestBit(&page
->readable_blocks
, block_index
))
513 BIGBLOCKFILE_SetBit(&page
->writable_blocks
, block_index
);
519 /******************************************************************************
520 * BIGBLOCKFILE_GetBigBlockPointer [PRIVATE]
522 * Returns a pointer to the specified block.
524 static void* BIGBLOCKFILE_GetBigBlockPointer(
527 DWORD desired_access
)
529 DWORD page_index
= block_index
/ BLOCKS_PER_PAGE
;
530 DWORD block_on_page
= block_index
% BLOCKS_PER_PAGE
;
532 MappedPage
*page
= BIGBLOCKFILE_GetMappedView(This
, page_index
);
533 if (!page
|| !page
->lpBytes
) return NULL
;
535 if (!BIGBLOCKFILE_AccessCheck(page
, block_on_page
, desired_access
))
537 BIGBLOCKFILE_ReleaseMappedPage(This
, page
);
541 return (LPBYTE
)page
->lpBytes
+ (block_on_page
* This
->blocksize
);
544 /******************************************************************************
545 * BIGBLOCKFILE_GetMappedPageFromPointer [PRIVATE]
547 * pBlock is a pointer to a block on a page.
548 * The page has to be on the in-use list. (As oppsed to the victim list.)
550 * Does not increment the usage count.
552 static MappedPage
*BIGBLOCKFILE_GetPageFromPointer(LPBIGBLOCKFILE This
,
557 for (page
= This
->maplist
; page
!= NULL
; page
= page
->next
)
559 if ((LPBYTE
)pBlock
>= (LPBYTE
)page
->lpBytes
560 && (LPBYTE
)pBlock
<= (LPBYTE
)page
->lpBytes
+ PAGE_SIZE
)
568 /******************************************************************************
569 * BIGBLOCKFILE_FindPageInList [PRIVATE]
572 static MappedPage
*BIGBLOCKFILE_FindPageInList(MappedPage
*head
,
575 for (; head
!= NULL
; head
= head
->next
)
577 if (head
->page_index
== page_index
)
579 InterlockedIncrement(&head
->refcnt
);
588 static void BIGBLOCKFILE_UnlinkPage(MappedPage
*page
)
590 if (page
->next
) page
->next
->prev
= page
->prev
;
591 if (page
->prev
) page
->prev
->next
= page
->next
;
594 static void BIGBLOCKFILE_LinkHeadPage(MappedPage
**head
, MappedPage
*page
)
596 if (*head
) (*head
)->prev
= page
;
602 /******************************************************************************
603 * BIGBLOCKFILE_GetMappedView [PRIVATE]
605 * Gets the page requested if it is already mapped.
606 * If it's not already mapped, this method will map it
608 static void * BIGBLOCKFILE_GetMappedView(
614 page
= BIGBLOCKFILE_FindPageInList(This
->maplist
, page_index
);
617 page
= BIGBLOCKFILE_FindPageInList(This
->victimhead
, page_index
);
620 This
->num_victim_pages
--;
622 BIGBLOCKFILE_Zero(&page
->readable_blocks
);
623 BIGBLOCKFILE_Zero(&page
->writable_blocks
);
629 /* If the page is not already at the head of the list, move
630 * it there. (Also moves pages from victim to main list.) */
631 if (This
->maplist
!= page
)
633 if (This
->victimhead
== page
) This
->victimhead
= page
->next
;
634 if (This
->victimtail
== page
) This
->victimtail
= page
->prev
;
636 BIGBLOCKFILE_UnlinkPage(page
);
638 BIGBLOCKFILE_LinkHeadPage(&This
->maplist
, page
);
644 page
= BIGBLOCKFILE_CreatePage(This
, page_index
);
645 if (!page
) return NULL
;
647 BIGBLOCKFILE_LinkHeadPage(&This
->maplist
, page
);
652 static BOOL
BIGBLOCKFILE_MapPage(LPBIGBLOCKFILE This
, MappedPage
*page
)
654 DWORD lowoffset
= PAGE_SIZE
* page
->page_index
;
659 DWORD desired_access
;
661 if (lowoffset
+ PAGE_SIZE
> This
->filesize
.s
.LowPart
)
662 numBytesToMap
= This
->filesize
.s
.LowPart
- lowoffset
;
664 numBytesToMap
= PAGE_SIZE
;
666 if (This
->flProtect
== PAGE_READONLY
)
667 desired_access
= FILE_MAP_READ
;
669 desired_access
= FILE_MAP_WRITE
;
671 page
->lpBytes
= MapViewOfFile(This
->hfilemap
, desired_access
, 0,
672 lowoffset
, numBytesToMap
);
676 page
->lpBytes
= (LPBYTE
)This
->pbytearray
+ lowoffset
;
679 TRACE("mapped page %lu to %p\n", page
->page_index
, page
->lpBytes
);
681 return page
->lpBytes
!= NULL
;
684 static MappedPage
*BIGBLOCKFILE_CreatePage(LPBIGBLOCKFILE This
,
689 page
= HeapAlloc(GetProcessHeap(), 0, sizeof(MappedPage
));
693 page
->page_index
= page_index
;
699 BIGBLOCKFILE_MapPage(This
, page
);
701 BIGBLOCKFILE_Zero(&page
->readable_blocks
);
702 BIGBLOCKFILE_Zero(&page
->writable_blocks
);
707 static void BIGBLOCKFILE_UnmapPage(LPBIGBLOCKFILE This
, MappedPage
*page
)
709 TRACE("%ld at %p\n", page
->page_index
, page
->lpBytes
);
710 if (page
->refcnt
> 0)
711 ERR("unmapping inuse page %p\n", page
->lpBytes
);
713 if (This
->fileBased
&& page
->lpBytes
)
714 UnmapViewOfFile(page
->lpBytes
);
716 page
->lpBytes
= NULL
;
719 static void BIGBLOCKFILE_DeletePage(LPBIGBLOCKFILE This
, MappedPage
*page
)
721 BIGBLOCKFILE_UnmapPage(This
, page
);
723 HeapFree(GetProcessHeap(), 0, page
);
726 /******************************************************************************
727 * BIGBLOCKFILE_ReleaseMappedPage [PRIVATE]
729 * Decrements the reference count of the mapped page.
731 static void BIGBLOCKFILE_ReleaseMappedPage(
735 assert(This
!= NULL
);
736 assert(page
!= NULL
);
738 /* If the page is no longer refenced, move it to the victim list.
739 * If the victim list is too long, kick somebody off. */
740 if (!InterlockedDecrement(&page
->refcnt
))
742 if (This
->maplist
== page
) This
->maplist
= page
->next
;
744 BIGBLOCKFILE_UnlinkPage(page
);
746 if (MAX_VICTIM_PAGES
> 0)
748 if (This
->num_victim_pages
>= MAX_VICTIM_PAGES
)
750 MappedPage
*victim
= This
->victimtail
;
753 This
->victimtail
= victim
->prev
;
754 if (This
->victimhead
== victim
)
755 This
->victimhead
= victim
->next
;
757 BIGBLOCKFILE_UnlinkPage(victim
);
758 BIGBLOCKFILE_DeletePage(This
, victim
);
761 else This
->num_victim_pages
++;
763 BIGBLOCKFILE_LinkHeadPage(&This
->victimhead
, page
);
764 if (This
->victimtail
== NULL
) This
->victimtail
= page
;
767 BIGBLOCKFILE_DeletePage(This
, page
);
771 static void BIGBLOCKFILE_DeleteList(LPBIGBLOCKFILE This
, MappedPage
*list
)
775 MappedPage
*next
= list
->next
;
777 BIGBLOCKFILE_DeletePage(This
, list
);
783 /******************************************************************************
784 * BIGBLOCKFILE_FreeAllMappedPages [PRIVATE]
786 * Unmap all currently mapped pages.
787 * Empty mapped pages list.
789 static void BIGBLOCKFILE_FreeAllMappedPages(
792 BIGBLOCKFILE_DeleteList(This
, This
->maplist
);
793 BIGBLOCKFILE_DeleteList(This
, This
->victimhead
);
795 This
->maplist
= NULL
;
796 This
->victimhead
= NULL
;
797 This
->victimtail
= NULL
;
798 This
->num_victim_pages
= 0;
801 static void BIGBLOCKFILE_UnmapList(LPBIGBLOCKFILE This
, MappedPage
*list
)
803 for (; list
!= NULL
; list
= list
->next
)
805 BIGBLOCKFILE_UnmapPage(This
, list
);
809 static void BIGBLOCKFILE_UnmapAllMappedPages(LPBIGBLOCKFILE This
)
811 BIGBLOCKFILE_UnmapList(This
, This
->maplist
);
812 BIGBLOCKFILE_UnmapList(This
, This
->victimhead
);
815 static void BIGBLOCKFILE_RemapList(LPBIGBLOCKFILE This
, MappedPage
*list
)
819 MappedPage
*next
= list
->next
;
821 if (list
->page_index
* PAGE_SIZE
> This
->filesize
.s
.LowPart
)
823 TRACE("discarding %lu\n", list
->page_index
);
825 /* page is entirely outside of the file, delete it */
826 BIGBLOCKFILE_UnlinkPage(list
);
827 BIGBLOCKFILE_DeletePage(This
, list
);
831 /* otherwise, remap it */
832 BIGBLOCKFILE_MapPage(This
, list
);
839 static void BIGBLOCKFILE_RemapAllMappedPages(LPBIGBLOCKFILE This
)
841 BIGBLOCKFILE_RemapList(This
, This
->maplist
);
842 BIGBLOCKFILE_RemapList(This
, This
->victimhead
);
845 /****************************************************************************
846 * BIGBLOCKFILE_GetProtectMode
848 * This function will return a protection mode flag for a file-mapping object
849 * from the open flags of a file.
851 static DWORD
BIGBLOCKFILE_GetProtectMode(DWORD openFlags
)
853 if (openFlags
& (STGM_WRITE
| STGM_READWRITE
))
854 return PAGE_READWRITE
;
856 return PAGE_READONLY
;