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
29 #include "wine/obj_base.h"
30 #include "wine/obj_storage.h"
33 #include "storage32.h"
35 #include "debugtools.h"
37 DEFAULT_DEBUG_CHANNEL(storage
);
39 /***********************************************************
40 * Data structures used internally by the BigBlockFile
44 /* We map in PAGE_SIZE-sized chunks. Must be a multiple of 4096. */
45 #define PAGE_SIZE 131072
47 #define BLOCKS_PER_PAGE (PAGE_SIZE / BIG_BLOCK_SIZE)
49 /* We keep a list of recently-discarded pages. This controls the
50 * size of that list. */
51 #define MAX_VICTIM_PAGES 16
53 /* This structure provides one bit for each block in a page.
54 * Use BIGBLOCKFILE_{Test,Set,Clear}Bit to manipulate it. */
57 unsigned int bits
[BLOCKS_PER_PAGE
/ (CHAR_BIT
* sizeof(unsigned int))];
61 * This structure identifies the paged that are mapped
62 * from the file and their position in memory. It is
63 * also used to hold a reference count to those pages.
65 * page_index identifies which PAGE_SIZE chunk from the
66 * file this mapping represents. (The mappings are always
78 BlockBits readable_blocks
;
79 BlockBits writable_blocks
;
82 /***********************************************************
83 * Prototypes for private methods
85 static void* BIGBLOCKFILE_GetMappedView(LPBIGBLOCKFILE This
,
87 static void BIGBLOCKFILE_ReleaseMappedPage(LPBIGBLOCKFILE This
,
89 static void BIGBLOCKFILE_FreeAllMappedPages(LPBIGBLOCKFILE This
);
90 static void BIGBLOCKFILE_UnmapAllMappedPages(LPBIGBLOCKFILE This
);
91 static void BIGBLOCKFILE_RemapAllMappedPages(LPBIGBLOCKFILE This
);
92 static void* BIGBLOCKFILE_GetBigBlockPointer(LPBIGBLOCKFILE This
,
94 DWORD desired_access
);
95 static MappedPage
* BIGBLOCKFILE_GetPageFromPointer(LPBIGBLOCKFILE This
,
97 static MappedPage
* BIGBLOCKFILE_CreatePage(LPBIGBLOCKFILE This
,
99 static DWORD
BIGBLOCKFILE_GetProtectMode(DWORD openFlags
);
100 static BOOL
BIGBLOCKFILE_FileInit(LPBIGBLOCKFILE This
, HANDLE hFile
);
101 static BOOL
BIGBLOCKFILE_MemInit(LPBIGBLOCKFILE This
, ILockBytes
* plkbyt
);
103 /* Note that this evaluates a and b multiple times, so don't
104 * pass expressions with side effects. */
105 #define ROUND_UP(a, b) ((((a) + (b) - 1)/(b))*(b))
107 /***********************************************************
108 * Blockbits functions.
110 static inline BOOL
BIGBLOCKFILE_TestBit(const BlockBits
*bb
,
113 unsigned int array_index
= index
/ (CHAR_BIT
* sizeof(unsigned int));
114 unsigned int bit_index
= index
% (CHAR_BIT
* sizeof(unsigned int));
116 return bb
->bits
[array_index
] & (1 << bit_index
);
119 static inline void BIGBLOCKFILE_SetBit(BlockBits
*bb
, unsigned int index
)
121 unsigned int array_index
= index
/ (CHAR_BIT
* sizeof(unsigned int));
122 unsigned int bit_index
= index
% (CHAR_BIT
* sizeof(unsigned int));
124 bb
->bits
[array_index
] |= (1 << bit_index
);
127 static inline void BIGBLOCKFILE_ClearBit(BlockBits
*bb
, unsigned int index
)
129 unsigned int array_index
= index
/ (CHAR_BIT
* sizeof(unsigned int));
130 unsigned int bit_index
= index
% (CHAR_BIT
* sizeof(unsigned int));
132 bb
->bits
[array_index
] &= ~(1 << bit_index
);
135 static inline void BIGBLOCKFILE_Zero(BlockBits
*bb
)
137 memset(bb
->bits
, 0, sizeof(bb
->bits
));
140 /******************************************************************************
141 * BIGBLOCKFILE_Construct
143 * Construct a big block file. Create the file mapping object.
144 * Create the read only mapped pages list, the writable mapped page list
145 * and the blocks in use list.
147 BigBlockFile
* BIGBLOCKFILE_Construct(
156 This
= (LPBIGBLOCKFILE
)HeapAlloc(GetProcessHeap(), 0, sizeof(BigBlockFile
));
161 This
->fileBased
= fileBased
;
163 This
->flProtect
= BIGBLOCKFILE_GetProtectMode(openFlags
);
165 This
->blocksize
= blocksize
;
167 This
->maplist
= NULL
;
168 This
->victimhead
= NULL
;
169 This
->victimtail
= NULL
;
170 This
->num_victim_pages
= 0;
174 if (!BIGBLOCKFILE_FileInit(This
, hFile
))
176 HeapFree(GetProcessHeap(), 0, This
);
182 if (!BIGBLOCKFILE_MemInit(This
, pLkByt
))
184 HeapFree(GetProcessHeap(), 0, This
);
192 /******************************************************************************
193 * BIGBLOCKFILE_FileInit
195 * Initialize a big block object supported by a file.
197 static BOOL
BIGBLOCKFILE_FileInit(LPBIGBLOCKFILE This
, HANDLE hFile
)
200 This
->hbytearray
= 0;
201 This
->pbytearray
= NULL
;
205 if (This
->hfile
== INVALID_HANDLE_VALUE
)
208 /* create the file mapping object
210 This
->hfilemap
= CreateFileMappingA(This
->hfile
,
218 CloseHandle(This
->hfile
);
222 This
->filesize
.s
.LowPart
= GetFileSize(This
->hfile
,
223 &This
->filesize
.s
.HighPart
);
225 This
->maplist
= NULL
;
227 TRACE("file len %lu\n", This
->filesize
.s
.LowPart
);
232 /******************************************************************************
233 * BIGBLOCKFILE_MemInit
235 * Initialize a big block object supported by an ILockBytes on HGLOABL.
237 static BOOL
BIGBLOCKFILE_MemInit(LPBIGBLOCKFILE This
, ILockBytes
* plkbyt
)
243 * Retrieve the handle to the byte array from the LockByte object.
245 if (GetHGlobalFromILockBytes(plkbyt
, &(This
->hbytearray
)) != S_OK
)
247 FIXME("May not be an ILockBytes on HGLOBAL\n");
251 This
->pLkbyt
= plkbyt
;
254 * Increment the reference count of the ILockByte object since
255 * we're keeping a reference to it.
257 ILockBytes_AddRef(This
->pLkbyt
);
259 This
->filesize
.s
.LowPart
= GlobalSize(This
->hbytearray
);
260 This
->filesize
.s
.HighPart
= 0;
262 This
->pbytearray
= GlobalLock(This
->hbytearray
);
264 TRACE("mem on %p len %lu\n", This
->pbytearray
, This
->filesize
.s
.LowPart
);
269 /******************************************************************************
270 * BIGBLOCKFILE_Destructor
272 * Destructor. Clean up, free memory.
274 void BIGBLOCKFILE_Destructor(
277 BIGBLOCKFILE_FreeAllMappedPages(This
);
281 CloseHandle(This
->hfilemap
);
282 CloseHandle(This
->hfile
);
286 GlobalUnlock(This
->hbytearray
);
287 ILockBytes_Release(This
->pLkbyt
);
292 HeapFree(GetProcessHeap(), 0, This
);
295 /******************************************************************************
296 * BIGBLOCKFILE_GetROBigBlock
298 * Returns the specified block in read only mode.
299 * Will return NULL if the block doesn't exists.
301 void* BIGBLOCKFILE_GetROBigBlock(
306 * block index starts at -1
307 * translate to zero based index
309 if (index
== 0xffffffff)
315 * validate the block index
318 if (This
->blocksize
* (index
+ 1)
319 > ROUND_UP(This
->filesize
.s
.LowPart
, This
->blocksize
))
321 TRACE("out of range %lu vs %lu\n", This
->blocksize
* (index
+ 1),
322 This
->filesize
.s
.LowPart
);
326 return BIGBLOCKFILE_GetBigBlockPointer(This
, index
, FILE_MAP_READ
);
329 /******************************************************************************
330 * BIGBLOCKFILE_GetBigBlock
332 * Returns the specified block.
333 * Will grow the file if necessary.
335 void* BIGBLOCKFILE_GetBigBlock(LPBIGBLOCKFILE This
, ULONG index
)
338 * block index starts at -1
339 * translate to zero based index
341 if (index
== 0xffffffff)
347 * make sure that the block physically exists
349 if ((This
->blocksize
* (index
+ 1)) > This
->filesize
.s
.LowPart
)
351 ULARGE_INTEGER newSize
;
353 newSize
.s
.HighPart
= 0;
354 newSize
.s
.LowPart
= This
->blocksize
* (index
+ 1);
356 BIGBLOCKFILE_SetSize(This
, newSize
);
359 return BIGBLOCKFILE_GetBigBlockPointer(This
, index
, FILE_MAP_WRITE
);
362 /******************************************************************************
363 * BIGBLOCKFILE_ReleaseBigBlock
365 * Releases the specified block.
367 void BIGBLOCKFILE_ReleaseBigBlock(LPBIGBLOCKFILE This
, void *pBlock
)
374 page
= BIGBLOCKFILE_GetPageFromPointer(This
, pBlock
);
379 BIGBLOCKFILE_ReleaseMappedPage(This
, page
);
382 /******************************************************************************
383 * BIGBLOCKFILE_SetSize
385 * Sets the size of the file.
388 void BIGBLOCKFILE_SetSize(LPBIGBLOCKFILE This
, ULARGE_INTEGER newSize
)
390 if (This
->filesize
.s
.LowPart
== newSize
.s
.LowPart
)
393 TRACE("from %lu to %lu\n", This
->filesize
.s
.LowPart
, newSize
.s
.LowPart
);
395 * unmap all views, must be done before call to SetEndFile
397 BIGBLOCKFILE_UnmapAllMappedPages(This
);
404 * close file-mapping object, must be done before call to SetEndFile
406 CloseHandle(This
->hfilemap
);
411 * This fixes a bug when saving through smbfs.
412 * smbmount a Windows shared directory, save a structured storage file
413 * to that dir: crash.
415 * The problem is that the SetFilePointer-SetEndOfFile combo below
416 * doesn't always succeed. The file is not grown. It seems like the
417 * operation is cached. By doing the WriteFile, the file is actually
419 * This hack is only needed when saving to smbfs.
421 memset(buf
, '0', 10);
422 SetFilePointer(This
->hfile
, newSize
.s
.LowPart
, NULL
, FILE_BEGIN
);
423 WriteFile(This
->hfile
, buf
, 10, NULL
, NULL
);
429 * set the new end of file
431 SetFilePointer(This
->hfile
, newSize
.s
.LowPart
, NULL
, FILE_BEGIN
);
432 SetEndOfFile(This
->hfile
);
435 * re-create the file mapping object
437 This
->hfilemap
= CreateFileMappingA(This
->hfile
,
445 GlobalUnlock(This
->hbytearray
);
448 * Resize the byte array object.
450 ILockBytes_SetSize(This
->pLkbyt
, newSize
);
453 * Re-acquire the handle, it may have changed.
455 GetHGlobalFromILockBytes(This
->pLkbyt
, &This
->hbytearray
);
456 This
->pbytearray
= GlobalLock(This
->hbytearray
);
459 This
->filesize
.s
.LowPart
= newSize
.s
.LowPart
;
460 This
->filesize
.s
.HighPart
= newSize
.s
.HighPart
;
462 BIGBLOCKFILE_RemapAllMappedPages(This
);
465 /******************************************************************************
466 * BIGBLOCKFILE_GetSize
468 * Returns the size of the file.
471 ULARGE_INTEGER
BIGBLOCKFILE_GetSize(LPBIGBLOCKFILE This
)
473 return This
->filesize
;
476 /******************************************************************************
477 * BIGBLOCKFILE_AccessCheck [PRIVATE]
479 * block_index is the index within the page.
481 static BOOL
BIGBLOCKFILE_AccessCheck(MappedPage
*page
, ULONG block_index
,
482 DWORD desired_access
)
484 assert(block_index
< BLOCKS_PER_PAGE
);
486 if (desired_access
== FILE_MAP_READ
)
488 if (BIGBLOCKFILE_TestBit(&page
->writable_blocks
, block_index
))
491 BIGBLOCKFILE_SetBit(&page
->readable_blocks
, block_index
);
495 assert(desired_access
== FILE_MAP_WRITE
);
497 if (BIGBLOCKFILE_TestBit(&page
->readable_blocks
, block_index
))
500 BIGBLOCKFILE_SetBit(&page
->writable_blocks
, block_index
);
506 /******************************************************************************
507 * BIGBLOCKFILE_GetBigBlockPointer [PRIVATE]
509 * Returns a pointer to the specified block.
511 static void* BIGBLOCKFILE_GetBigBlockPointer(
514 DWORD desired_access
)
516 DWORD page_index
= block_index
/ BLOCKS_PER_PAGE
;
517 DWORD block_on_page
= block_index
% BLOCKS_PER_PAGE
;
519 MappedPage
*page
= BIGBLOCKFILE_GetMappedView(This
, page_index
);
520 if (!page
|| !page
->lpBytes
) return NULL
;
522 if (!BIGBLOCKFILE_AccessCheck(page
, block_on_page
, desired_access
))
524 BIGBLOCKFILE_ReleaseMappedPage(This
, page
);
528 return (LPBYTE
)page
->lpBytes
+ (block_on_page
* This
->blocksize
);
531 /******************************************************************************
532 * BIGBLOCKFILE_GetMappedPageFromPointer [PRIVATE]
534 * pBlock is a pointer to a block on a page.
535 * The page has to be on the in-use list. (As oppsed to the victim list.)
537 * Does not increment the usage count.
539 static MappedPage
*BIGBLOCKFILE_GetPageFromPointer(LPBIGBLOCKFILE This
,
544 for (page
= This
->maplist
; page
!= NULL
; page
= page
->next
)
546 if ((LPBYTE
)pBlock
>= (LPBYTE
)page
->lpBytes
547 && (LPBYTE
)pBlock
<= (LPBYTE
)page
->lpBytes
+ PAGE_SIZE
)
555 /******************************************************************************
556 * BIGBLOCKFILE_FindPageInList [PRIVATE]
559 static MappedPage
*BIGBLOCKFILE_FindPageInList(MappedPage
*head
,
562 for (; head
!= NULL
; head
= head
->next
)
564 if (head
->page_index
== page_index
)
566 InterlockedIncrement(&head
->refcnt
);
575 static void BIGBLOCKFILE_UnlinkPage(MappedPage
*page
)
577 if (page
->next
) page
->next
->prev
= page
->prev
;
578 if (page
->prev
) page
->prev
->next
= page
->next
;
581 static void BIGBLOCKFILE_LinkHeadPage(MappedPage
**head
, MappedPage
*page
)
583 if (*head
) (*head
)->prev
= page
;
589 /******************************************************************************
590 * BIGBLOCKFILE_GetMappedView [PRIVATE]
592 * Gets the page requested if it is already mapped.
593 * If it's not already mapped, this method will map it
595 static void * BIGBLOCKFILE_GetMappedView(
601 page
= BIGBLOCKFILE_FindPageInList(This
->maplist
, page_index
);
604 page
= BIGBLOCKFILE_FindPageInList(This
->victimhead
, page_index
);
607 This
->num_victim_pages
--;
609 BIGBLOCKFILE_Zero(&page
->readable_blocks
);
610 BIGBLOCKFILE_Zero(&page
->writable_blocks
);
616 /* If the page is not already at the head of the list, move
617 * it there. (Also moves pages from victim to main list.) */
618 if (This
->maplist
!= page
)
620 if (This
->victimhead
== page
) This
->victimhead
= page
->next
;
621 if (This
->victimtail
== page
) This
->victimtail
= page
->prev
;
623 BIGBLOCKFILE_UnlinkPage(page
);
625 BIGBLOCKFILE_LinkHeadPage(&This
->maplist
, page
);
631 page
= BIGBLOCKFILE_CreatePage(This
, page_index
);
632 if (!page
) return NULL
;
634 BIGBLOCKFILE_LinkHeadPage(&This
->maplist
, page
);
639 static BOOL
BIGBLOCKFILE_MapPage(LPBIGBLOCKFILE This
, MappedPage
*page
)
641 DWORD lowoffset
= PAGE_SIZE
* page
->page_index
;
646 DWORD desired_access
;
648 if (lowoffset
+ PAGE_SIZE
> This
->filesize
.s
.LowPart
)
649 numBytesToMap
= This
->filesize
.s
.LowPart
- lowoffset
;
651 numBytesToMap
= PAGE_SIZE
;
653 if (This
->flProtect
== PAGE_READONLY
)
654 desired_access
= FILE_MAP_READ
;
656 desired_access
= FILE_MAP_WRITE
;
658 page
->lpBytes
= MapViewOfFile(This
->hfilemap
, desired_access
, 0,
659 lowoffset
, numBytesToMap
);
663 page
->lpBytes
= (LPBYTE
)This
->pbytearray
+ lowoffset
;
666 TRACE("mapped page %lu to %p\n", page
->page_index
, page
->lpBytes
);
668 return page
->lpBytes
!= NULL
;
671 static MappedPage
*BIGBLOCKFILE_CreatePage(LPBIGBLOCKFILE This
,
676 page
= HeapAlloc(GetProcessHeap(), 0, sizeof(MappedPage
));
680 page
->page_index
= page_index
;
686 BIGBLOCKFILE_MapPage(This
, page
);
688 BIGBLOCKFILE_Zero(&page
->readable_blocks
);
689 BIGBLOCKFILE_Zero(&page
->writable_blocks
);
694 static void BIGBLOCKFILE_UnmapPage(LPBIGBLOCKFILE This
, MappedPage
*page
)
696 TRACE("%ld at %p\n", page
->page_index
, page
->lpBytes
);
697 if (page
->refcnt
> 0)
698 ERR("unmapping inuse page %p\n", page
->lpBytes
);
700 if (This
->fileBased
&& page
->lpBytes
)
701 UnmapViewOfFile(page
->lpBytes
);
703 page
->lpBytes
= NULL
;
706 static void BIGBLOCKFILE_DeletePage(LPBIGBLOCKFILE This
, MappedPage
*page
)
708 BIGBLOCKFILE_UnmapPage(This
, page
);
710 HeapFree(GetProcessHeap(), 0, page
);
713 /******************************************************************************
714 * BIGBLOCKFILE_ReleaseMappedPage [PRIVATE]
716 * Decrements the reference count of the mapped page.
718 static void BIGBLOCKFILE_ReleaseMappedPage(
722 assert(This
!= NULL
);
723 assert(page
!= NULL
);
725 /* If the page is no longer refenced, move it to the victim list.
726 * If the victim list is too long, kick somebody off. */
727 if (!InterlockedDecrement(&page
->refcnt
))
729 if (This
->maplist
== page
) This
->maplist
= page
->next
;
731 BIGBLOCKFILE_UnlinkPage(page
);
733 if (MAX_VICTIM_PAGES
> 0)
735 if (This
->num_victim_pages
>= MAX_VICTIM_PAGES
)
737 MappedPage
*victim
= This
->victimtail
;
740 This
->victimtail
= victim
->prev
;
741 if (This
->victimhead
== victim
)
742 This
->victimhead
= victim
->next
;
744 BIGBLOCKFILE_UnlinkPage(victim
);
745 BIGBLOCKFILE_DeletePage(This
, victim
);
748 else This
->num_victim_pages
++;
750 BIGBLOCKFILE_LinkHeadPage(&This
->victimhead
, page
);
751 if (This
->victimtail
== NULL
) This
->victimtail
= page
;
754 BIGBLOCKFILE_DeletePage(This
, page
);
758 static void BIGBLOCKFILE_DeleteList(LPBIGBLOCKFILE This
, MappedPage
*list
)
762 MappedPage
*next
= list
->next
;
764 BIGBLOCKFILE_DeletePage(This
, list
);
770 /******************************************************************************
771 * BIGBLOCKFILE_FreeAllMappedPages [PRIVATE]
773 * Unmap all currently mapped pages.
774 * Empty mapped pages list.
776 static void BIGBLOCKFILE_FreeAllMappedPages(
779 BIGBLOCKFILE_DeleteList(This
, This
->maplist
);
780 BIGBLOCKFILE_DeleteList(This
, This
->victimhead
);
782 This
->maplist
= NULL
;
783 This
->victimhead
= NULL
;
784 This
->victimtail
= NULL
;
785 This
->num_victim_pages
= 0;
788 static void BIGBLOCKFILE_UnmapList(LPBIGBLOCKFILE This
, MappedPage
*list
)
790 for (; list
!= NULL
; list
= list
->next
)
792 BIGBLOCKFILE_UnmapPage(This
, list
);
796 static void BIGBLOCKFILE_UnmapAllMappedPages(LPBIGBLOCKFILE This
)
798 BIGBLOCKFILE_UnmapList(This
, This
->maplist
);
799 BIGBLOCKFILE_UnmapList(This
, This
->victimhead
);
802 static void BIGBLOCKFILE_RemapList(LPBIGBLOCKFILE This
, MappedPage
*list
)
806 MappedPage
*next
= list
->next
;
808 if (list
->page_index
* PAGE_SIZE
> This
->filesize
.s
.LowPart
)
810 TRACE("discarding %lu\n", list
->page_index
);
812 /* page is entirely outside of the file, delete it */
813 BIGBLOCKFILE_UnlinkPage(list
);
814 BIGBLOCKFILE_DeletePage(This
, list
);
818 /* otherwise, remap it */
819 BIGBLOCKFILE_MapPage(This
, list
);
826 static void BIGBLOCKFILE_RemapAllMappedPages(LPBIGBLOCKFILE This
)
828 BIGBLOCKFILE_RemapList(This
, This
->maplist
);
829 BIGBLOCKFILE_RemapList(This
, This
->victimhead
);
832 /****************************************************************************
833 * BIGBLOCKFILE_GetProtectMode
835 * This function will return a protection mode flag for a file-mapping object
836 * from the open flags of a file.
838 static DWORD
BIGBLOCKFILE_GetProtectMode(DWORD openFlags
)
840 if (openFlags
& (STGM_WRITE
| STGM_READWRITE
))
841 return PAGE_READWRITE
;
843 return PAGE_READONLY
;