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_storage.h"
32 #include "storage32.h"
34 #include "debugtools.h"
36 DEFAULT_DEBUG_CHANNEL(storage
)
38 /***********************************************************
39 * Data structures used internally by the BigBlockFile
43 /* We map in PAGE_SIZE-sized chunks. Must be a multiple of 4096. */
44 #define PAGE_SIZE 131072
46 #define BLOCKS_PER_PAGE (PAGE_SIZE / BIG_BLOCK_SIZE)
48 /* We keep a list of recently-discarded pages. This controls the
49 * size of that list. */
50 #define MAX_VICTIM_PAGES 16
52 /* This structure provides one bit for each block in a page.
53 * Use BIGBLOCKFILE_{Test,Set,Clear}Bit to manipulate it. */
56 unsigned int bits
[BLOCKS_PER_PAGE
/ (CHAR_BIT
* sizeof(unsigned int))];
60 * This structure identifies the paged that are mapped
61 * from the file and their position in memory. It is
62 * also used to hold a reference count to those pages.
64 * page_index identifies which PAGE_SIZE chunk from the
65 * file this mapping represents. (The mappings are always
77 BlockBits readable_blocks
;
78 BlockBits writable_blocks
;
81 /***********************************************************
82 * Prototypes for private methods
84 static void* BIGBLOCKFILE_GetMappedView(LPBIGBLOCKFILE This
,
86 static void BIGBLOCKFILE_ReleaseMappedPage(LPBIGBLOCKFILE This
,
88 static void BIGBLOCKFILE_FreeAllMappedPages(LPBIGBLOCKFILE This
);
89 static void BIGBLOCKFILE_UnmapAllMappedPages(LPBIGBLOCKFILE This
);
90 static void BIGBLOCKFILE_RemapAllMappedPages(LPBIGBLOCKFILE This
);
91 static void* BIGBLOCKFILE_GetBigBlockPointer(LPBIGBLOCKFILE This
,
93 DWORD desired_access
);
94 static MappedPage
* BIGBLOCKFILE_GetPageFromPointer(LPBIGBLOCKFILE This
,
96 static MappedPage
* BIGBLOCKFILE_CreatePage(LPBIGBLOCKFILE This
,
98 static DWORD
BIGBLOCKFILE_GetProtectMode(DWORD openFlags
);
99 static BOOL
BIGBLOCKFILE_FileInit(LPBIGBLOCKFILE This
, HANDLE hFile
);
100 static BOOL
BIGBLOCKFILE_MemInit(LPBIGBLOCKFILE This
, ILockBytes
* plkbyt
);
102 /* Note that this evaluates a and b multiple times, so don't
103 * pass expressions with side effects. */
104 #define ROUND_UP(a, b) ((((a) + (b) - 1)/(b))*(b))
106 /***********************************************************
107 * Blockbits functions.
109 static inline BOOL
BIGBLOCKFILE_TestBit(const BlockBits
*bb
,
112 unsigned int array_index
= index
/ (CHAR_BIT
* sizeof(unsigned int));
113 unsigned int bit_index
= index
% (CHAR_BIT
* sizeof(unsigned int));
115 return bb
->bits
[array_index
] & (1 << bit_index
);
118 static inline void BIGBLOCKFILE_SetBit(BlockBits
*bb
, unsigned int index
)
120 unsigned int array_index
= index
/ (CHAR_BIT
* sizeof(unsigned int));
121 unsigned int bit_index
= index
% (CHAR_BIT
* sizeof(unsigned int));
123 bb
->bits
[array_index
] |= (1 << bit_index
);
126 static inline void BIGBLOCKFILE_ClearBit(BlockBits
*bb
, unsigned int index
)
128 unsigned int array_index
= index
/ (CHAR_BIT
* sizeof(unsigned int));
129 unsigned int bit_index
= index
% (CHAR_BIT
* sizeof(unsigned int));
131 bb
->bits
[array_index
] &= ~(1 << bit_index
);
134 static inline void BIGBLOCKFILE_Zero(BlockBits
*bb
)
136 memset(bb
->bits
, 0, sizeof(bb
->bits
));
139 /******************************************************************************
140 * BIGBLOCKFILE_Construct
142 * Construct a big block file. Create the file mapping object.
143 * Create the read only mapped pages list, the writable mapped page list
144 * and the blocks in use list.
146 BigBlockFile
* BIGBLOCKFILE_Construct(
155 This
= (LPBIGBLOCKFILE
)HeapAlloc(GetProcessHeap(), 0, sizeof(BigBlockFile
));
160 This
->fileBased
= fileBased
;
162 This
->flProtect
= BIGBLOCKFILE_GetProtectMode(openFlags
);
164 This
->blocksize
= blocksize
;
166 This
->maplist
= NULL
;
167 This
->victimhead
= NULL
;
168 This
->victimtail
= NULL
;
169 This
->num_victim_pages
= 0;
173 if (!BIGBLOCKFILE_FileInit(This
, hFile
))
175 HeapFree(GetProcessHeap(), 0, This
);
181 if (!BIGBLOCKFILE_MemInit(This
, pLkByt
))
183 HeapFree(GetProcessHeap(), 0, This
);
191 /******************************************************************************
192 * BIGBLOCKFILE_FileInit
194 * Initialize a big block object supported by a file.
196 static BOOL
BIGBLOCKFILE_FileInit(LPBIGBLOCKFILE This
, HANDLE hFile
)
199 This
->hbytearray
= 0;
200 This
->pbytearray
= NULL
;
204 if (This
->hfile
== INVALID_HANDLE_VALUE
)
207 /* create the file mapping object
209 This
->hfilemap
= CreateFileMappingA(This
->hfile
,
215 if (This
->hfilemap
== NULL
)
217 CloseHandle(This
->hfile
);
221 This
->filesize
.s
.LowPart
= GetFileSize(This
->hfile
,
222 &This
->filesize
.s
.HighPart
);
224 This
->maplist
= NULL
;
226 TRACE("file len %lu\n", This
->filesize
.s
.LowPart
);
231 /******************************************************************************
232 * BIGBLOCKFILE_MemInit
234 * Initialize a big block object supported by an ILockBytes on HGLOABL.
236 static BOOL
BIGBLOCKFILE_MemInit(LPBIGBLOCKFILE This
, ILockBytes
* plkbyt
)
239 This
->hfilemap
= NULL
;
242 * Retrieve the handle to the byte array from the LockByte object.
244 if (GetHGlobalFromILockBytes(plkbyt
, &(This
->hbytearray
)) != S_OK
)
246 FIXME("May not be an ILockBytes on HGLOBAL\n");
250 This
->pLkbyt
= plkbyt
;
253 * Increment the reference count of the ILockByte object since
254 * we're keeping a reference to it.
256 ILockBytes_AddRef(This
->pLkbyt
);
258 This
->filesize
.s
.LowPart
= GlobalSize(This
->hbytearray
);
259 This
->filesize
.s
.HighPart
= 0;
261 This
->pbytearray
= GlobalLock(This
->hbytearray
);
263 TRACE("mem on %p len %lu\n", This
->pbytearray
, This
->filesize
.s
.LowPart
);
268 /******************************************************************************
269 * BIGBLOCKFILE_Destructor
271 * Destructor. Clean up, free memory.
273 void BIGBLOCKFILE_Destructor(
276 BIGBLOCKFILE_FreeAllMappedPages(This
);
280 CloseHandle(This
->hfilemap
);
281 CloseHandle(This
->hfile
);
285 GlobalUnlock(This
->hbytearray
);
286 ILockBytes_Release(This
->pLkbyt
);
291 HeapFree(GetProcessHeap(), 0, This
);
294 /******************************************************************************
295 * BIGBLOCKFILE_GetROBigBlock
297 * Returns the specified block in read only mode.
298 * Will return NULL if the block doesn't exists.
300 void* BIGBLOCKFILE_GetROBigBlock(
305 * block index starts at -1
306 * translate to zero based index
308 if (index
== 0xffffffff)
314 * validate the block index
317 if (This
->blocksize
* (index
+ 1)
318 > ROUND_UP(This
->filesize
.s
.LowPart
, This
->blocksize
))
320 TRACE("out of range %lu vs %lu\n", This
->blocksize
* (index
+ 1),
321 This
->filesize
.s
.LowPart
);
325 return BIGBLOCKFILE_GetBigBlockPointer(This
, index
, FILE_MAP_READ
);
328 /******************************************************************************
329 * BIGBLOCKFILE_GetBigBlock
331 * Returns the specified block.
332 * Will grow the file if necessary.
334 void* BIGBLOCKFILE_GetBigBlock(LPBIGBLOCKFILE This
, ULONG index
)
337 * block index starts at -1
338 * translate to zero based index
340 if (index
== 0xffffffff)
346 * make sure that the block physically exists
348 if ((This
->blocksize
* (index
+ 1)) > This
->filesize
.s
.LowPart
)
350 ULARGE_INTEGER newSize
;
352 newSize
.s
.HighPart
= 0;
353 newSize
.s
.LowPart
= This
->blocksize
* (index
+ 1);
355 BIGBLOCKFILE_SetSize(This
, newSize
);
358 return BIGBLOCKFILE_GetBigBlockPointer(This
, index
, FILE_MAP_WRITE
);
361 /******************************************************************************
362 * BIGBLOCKFILE_ReleaseBigBlock
364 * Releases the specified block.
366 void BIGBLOCKFILE_ReleaseBigBlock(LPBIGBLOCKFILE This
, void *pBlock
)
373 page
= BIGBLOCKFILE_GetPageFromPointer(This
, pBlock
);
378 BIGBLOCKFILE_ReleaseMappedPage(This
, page
);
381 /******************************************************************************
382 * BIGBLOCKFILE_SetSize
384 * Sets the size of the file.
387 void BIGBLOCKFILE_SetSize(LPBIGBLOCKFILE This
, ULARGE_INTEGER newSize
)
389 if (This
->filesize
.s
.LowPart
== newSize
.s
.LowPart
)
392 TRACE("from %lu to %lu\n", This
->filesize
.s
.LowPart
, newSize
.s
.LowPart
);
394 * unmap all views, must be done before call to SetEndFile
396 BIGBLOCKFILE_UnmapAllMappedPages(This
);
403 * close file-mapping object, must be done before call to SetEndFile
405 CloseHandle(This
->hfilemap
);
406 This
->hfilemap
= NULL
;
410 * This fixes a bug when saving through smbfs.
411 * smbmount a Windows shared directory, save a structured storage file
412 * to that dir: crash.
414 * The problem is that the SetFilePointer-SetEndOfFile combo below
415 * doesn't always succeed. The file is not grown. It seems like the
416 * operation is cached. By doing the WriteFile, the file is actually
418 * This hack is only needed when saving to smbfs.
420 memset(buf
, '0', 10);
421 SetFilePointer(This
->hfile
, newSize
.s
.LowPart
, NULL
, FILE_BEGIN
);
422 WriteFile(This
->hfile
, buf
, 10, NULL
, NULL
);
428 * set the new end of file
430 SetFilePointer(This
->hfile
, newSize
.s
.LowPart
, NULL
, FILE_BEGIN
);
431 SetEndOfFile(This
->hfile
);
434 * re-create the file mapping object
436 This
->hfilemap
= CreateFileMappingA(This
->hfile
,
444 GlobalUnlock(This
->hbytearray
);
447 * Resize the byte array object.
449 ILockBytes_SetSize(This
->pLkbyt
, newSize
);
452 * Re-acquire the handle, it may have changed.
454 GetHGlobalFromILockBytes(This
->pLkbyt
, &This
->hbytearray
);
455 This
->pbytearray
= GlobalLock(This
->hbytearray
);
458 This
->filesize
.s
.LowPart
= newSize
.s
.LowPart
;
459 This
->filesize
.s
.HighPart
= newSize
.s
.HighPart
;
461 BIGBLOCKFILE_RemapAllMappedPages(This
);
464 /******************************************************************************
465 * BIGBLOCKFILE_GetSize
467 * Returns the size of the file.
470 ULARGE_INTEGER
BIGBLOCKFILE_GetSize(LPBIGBLOCKFILE This
)
472 return This
->filesize
;
475 /******************************************************************************
476 * BIGBLOCKFILE_AccessCheck [PRIVATE]
478 * block_index is the index within the page.
480 static BOOL
BIGBLOCKFILE_AccessCheck(MappedPage
*page
, ULONG block_index
,
481 DWORD desired_access
)
483 assert(block_index
< BLOCKS_PER_PAGE
);
485 if (desired_access
== FILE_MAP_READ
)
487 if (BIGBLOCKFILE_TestBit(&page
->writable_blocks
, block_index
))
490 BIGBLOCKFILE_SetBit(&page
->readable_blocks
, block_index
);
494 assert(desired_access
== FILE_MAP_WRITE
);
496 if (BIGBLOCKFILE_TestBit(&page
->readable_blocks
, block_index
))
499 BIGBLOCKFILE_SetBit(&page
->writable_blocks
, block_index
);
505 /******************************************************************************
506 * BIGBLOCKFILE_GetBigBlockPointer [PRIVATE]
508 * Returns a pointer to the specified block.
510 static void* BIGBLOCKFILE_GetBigBlockPointer(
513 DWORD desired_access
)
515 DWORD page_index
= block_index
/ BLOCKS_PER_PAGE
;
516 DWORD block_on_page
= block_index
% BLOCKS_PER_PAGE
;
518 MappedPage
*page
= BIGBLOCKFILE_GetMappedView(This
, page_index
);
519 if (!page
|| !page
->lpBytes
) return NULL
;
521 if (!BIGBLOCKFILE_AccessCheck(page
, block_on_page
, desired_access
))
523 BIGBLOCKFILE_ReleaseMappedPage(This
, page
);
527 return (LPBYTE
)page
->lpBytes
+ (block_on_page
* This
->blocksize
);
530 /******************************************************************************
531 * BIGBLOCKFILE_GetMappedPageFromPointer [PRIVATE]
533 * pBlock is a pointer to a block on a page.
534 * The page has to be on the in-use list. (As oppsed to the victim list.)
536 * Does not increment the usage count.
538 static MappedPage
*BIGBLOCKFILE_GetPageFromPointer(LPBIGBLOCKFILE This
,
543 for (page
= This
->maplist
; page
!= NULL
; page
= page
->next
)
545 if ((LPBYTE
)pBlock
>= (LPBYTE
)page
->lpBytes
546 && (LPBYTE
)pBlock
<= (LPBYTE
)page
->lpBytes
+ PAGE_SIZE
)
554 /******************************************************************************
555 * BIGBLOCKFILE_FindPageInList [PRIVATE]
558 static MappedPage
*BIGBLOCKFILE_FindPageInList(MappedPage
*head
,
561 for (; head
!= NULL
; head
= head
->next
)
563 if (head
->page_index
== page_index
)
565 InterlockedIncrement(&head
->refcnt
);
574 static void BIGBLOCKFILE_UnlinkPage(MappedPage
*page
)
576 if (page
->next
) page
->next
->prev
= page
->prev
;
577 if (page
->prev
) page
->prev
->next
= page
->next
;
580 static void BIGBLOCKFILE_LinkHeadPage(MappedPage
**head
, MappedPage
*page
)
582 if (*head
) (*head
)->prev
= page
;
588 /******************************************************************************
589 * BIGBLOCKFILE_GetMappedView [PRIVATE]
591 * Gets the page requested if it is already mapped.
592 * If it's not already mapped, this method will map it
594 static void * BIGBLOCKFILE_GetMappedView(
600 page
= BIGBLOCKFILE_FindPageInList(This
->maplist
, page_index
);
603 page
= BIGBLOCKFILE_FindPageInList(This
->victimhead
, page_index
);
606 This
->num_victim_pages
--;
608 BIGBLOCKFILE_Zero(&page
->readable_blocks
);
609 BIGBLOCKFILE_Zero(&page
->writable_blocks
);
615 /* If the page is not already at the head of the list, move
616 * it there. (Also moves pages from victim to main list.) */
617 if (This
->maplist
!= page
)
619 if (This
->victimhead
== page
) This
->victimhead
= page
->next
;
620 if (This
->victimtail
== page
) This
->victimtail
= page
->prev
;
622 BIGBLOCKFILE_UnlinkPage(page
);
624 BIGBLOCKFILE_LinkHeadPage(&This
->maplist
, page
);
630 page
= BIGBLOCKFILE_CreatePage(This
, page_index
);
631 if (!page
) return NULL
;
633 BIGBLOCKFILE_LinkHeadPage(&This
->maplist
, page
);
638 static BOOL
BIGBLOCKFILE_MapPage(LPBIGBLOCKFILE This
, MappedPage
*page
)
640 DWORD lowoffset
= PAGE_SIZE
* page
->page_index
;
645 DWORD desired_access
;
647 if (lowoffset
+ PAGE_SIZE
> This
->filesize
.s
.LowPart
)
648 numBytesToMap
= This
->filesize
.s
.LowPart
- lowoffset
;
650 numBytesToMap
= PAGE_SIZE
;
652 if (This
->flProtect
== PAGE_READONLY
)
653 desired_access
= FILE_MAP_READ
;
655 desired_access
= FILE_MAP_WRITE
;
657 page
->lpBytes
= MapViewOfFile(This
->hfilemap
, desired_access
, 0,
658 lowoffset
, numBytesToMap
);
662 page
->lpBytes
= (LPBYTE
)This
->pbytearray
+ lowoffset
;
665 TRACE("mapped page %lu to %p\n", page
->page_index
, page
->lpBytes
);
667 return page
->lpBytes
!= NULL
;
670 static MappedPage
*BIGBLOCKFILE_CreatePage(LPBIGBLOCKFILE This
,
675 page
= HeapAlloc(GetProcessHeap(), 0, sizeof(MappedPage
));
679 page
->page_index
= page_index
;
685 BIGBLOCKFILE_MapPage(This
, page
);
687 BIGBLOCKFILE_Zero(&page
->readable_blocks
);
688 BIGBLOCKFILE_Zero(&page
->writable_blocks
);
693 static void BIGBLOCKFILE_UnmapPage(LPBIGBLOCKFILE This
, MappedPage
*page
)
695 TRACE("%ld at %p\n", page
->page_index
, page
->lpBytes
);
696 if (page
->refcnt
> 0)
697 ERR("unmapping inuse page %p\n", page
->lpBytes
);
699 if (This
->fileBased
&& page
->lpBytes
)
700 UnmapViewOfFile(page
->lpBytes
);
702 page
->lpBytes
= NULL
;
705 static void BIGBLOCKFILE_DeletePage(LPBIGBLOCKFILE This
, MappedPage
*page
)
707 BIGBLOCKFILE_UnmapPage(This
, page
);
709 HeapFree(GetProcessHeap(), 0, page
);
712 /******************************************************************************
713 * BIGBLOCKFILE_ReleaseMappedPage [PRIVATE]
715 * Decrements the reference count of the mapped page.
717 static void BIGBLOCKFILE_ReleaseMappedPage(
721 assert(This
!= NULL
);
722 assert(page
!= NULL
);
724 /* If the page is no longer refenced, move it to the victim list.
725 * If the victim list is too long, kick somebody off. */
726 if (!InterlockedDecrement(&page
->refcnt
))
728 if (This
->maplist
== page
) This
->maplist
= page
->next
;
730 BIGBLOCKFILE_UnlinkPage(page
);
732 if (MAX_VICTIM_PAGES
> 0)
734 if (This
->num_victim_pages
>= MAX_VICTIM_PAGES
)
736 MappedPage
*victim
= This
->victimtail
;
739 This
->victimtail
= victim
->prev
;
740 if (This
->victimhead
== victim
)
741 This
->victimhead
= victim
->next
;
743 BIGBLOCKFILE_UnlinkPage(victim
);
744 BIGBLOCKFILE_DeletePage(This
, victim
);
747 else This
->num_victim_pages
++;
749 BIGBLOCKFILE_LinkHeadPage(&This
->victimhead
, page
);
750 if (This
->victimtail
== NULL
) This
->victimtail
= page
;
753 BIGBLOCKFILE_DeletePage(This
, page
);
757 static void BIGBLOCKFILE_DeleteList(LPBIGBLOCKFILE This
, MappedPage
*list
)
761 MappedPage
*next
= list
->next
;
763 BIGBLOCKFILE_DeletePage(This
, list
);
769 /******************************************************************************
770 * BIGBLOCKFILE_FreeAllMappedPages [PRIVATE]
772 * Unmap all currently mapped pages.
773 * Empty mapped pages list.
775 static void BIGBLOCKFILE_FreeAllMappedPages(
778 BIGBLOCKFILE_DeleteList(This
, This
->maplist
);
779 BIGBLOCKFILE_DeleteList(This
, This
->victimhead
);
781 This
->maplist
= NULL
;
782 This
->victimhead
= NULL
;
783 This
->victimtail
= NULL
;
784 This
->num_victim_pages
= 0;
787 static void BIGBLOCKFILE_UnmapList(LPBIGBLOCKFILE This
, MappedPage
*list
)
789 for (; list
!= NULL
; list
= list
->next
)
791 BIGBLOCKFILE_UnmapPage(This
, list
);
795 static void BIGBLOCKFILE_UnmapAllMappedPages(LPBIGBLOCKFILE This
)
797 BIGBLOCKFILE_UnmapList(This
, This
->maplist
);
798 BIGBLOCKFILE_UnmapList(This
, This
->victimhead
);
801 static void BIGBLOCKFILE_RemapList(LPBIGBLOCKFILE This
, MappedPage
*list
)
805 MappedPage
*next
= list
->next
;
807 if (list
->page_index
* PAGE_SIZE
> This
->filesize
.s
.LowPart
)
809 TRACE("discarding %lu\n", list
->page_index
);
811 /* page is entirely outside of the file, delete it */
812 BIGBLOCKFILE_UnlinkPage(list
);
813 BIGBLOCKFILE_DeletePage(This
, list
);
817 /* otherwise, remap it */
818 BIGBLOCKFILE_MapPage(This
, list
);
825 static void BIGBLOCKFILE_RemapAllMappedPages(LPBIGBLOCKFILE This
)
827 BIGBLOCKFILE_RemapList(This
, This
->maplist
);
828 BIGBLOCKFILE_RemapList(This
, This
->victimhead
);
831 /****************************************************************************
832 * BIGBLOCKFILE_GetProtectMode
834 * This function will return a protection mode flag for a file-mapping object
835 * from the open flags of a file.
837 static DWORD
BIGBLOCKFILE_GetProtectMode(DWORD openFlags
)
839 if (openFlags
& (STGM_WRITE
| STGM_READWRITE
))
840 return PAGE_READWRITE
;
842 return PAGE_READONLY
;