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
50 #include "storage32.h"
52 #include "wine/debug.h"
54 WINE_DEFAULT_DEBUG_CHANNEL(storage
);
56 /***********************************************************
57 * Data structures used internally by the BigBlockFile
61 /* We map in PAGE_SIZE-sized chunks. Must be a multiple of 4096. */
62 #define PAGE_SIZE 131072
64 #define BLOCKS_PER_PAGE (PAGE_SIZE / BIG_BLOCK_SIZE)
66 /* We keep a list of recently-discarded pages. This controls the
67 * size of that list. */
68 #define MAX_VICTIM_PAGES 16
70 /* This structure provides one bit for each block in a page.
71 * Use BIGBLOCKFILE_{Test,Set,Clear}Bit to manipulate it. */
74 unsigned int bits
[BLOCKS_PER_PAGE
/ (CHAR_BIT
* sizeof(unsigned int))];
78 * This structure identifies the paged that are mapped
79 * from the file and their position in memory. It is
80 * also used to hold a reference count to those pages.
82 * page_index identifies which PAGE_SIZE chunk from the
83 * file this mapping represents. (The mappings are always
95 BlockBits readable_blocks
;
96 BlockBits writable_blocks
;
99 /***********************************************************
100 * Prototypes for private methods
102 static void* BIGBLOCKFILE_GetMappedView(LPBIGBLOCKFILE This
,
104 static void BIGBLOCKFILE_ReleaseMappedPage(LPBIGBLOCKFILE This
,
106 static void BIGBLOCKFILE_FreeAllMappedPages(LPBIGBLOCKFILE This
);
107 static void BIGBLOCKFILE_UnmapAllMappedPages(LPBIGBLOCKFILE This
);
108 static void BIGBLOCKFILE_RemapAllMappedPages(LPBIGBLOCKFILE This
);
109 static void* BIGBLOCKFILE_GetBigBlockPointer(LPBIGBLOCKFILE This
,
111 DWORD desired_access
);
112 static MappedPage
* BIGBLOCKFILE_GetPageFromPointer(LPBIGBLOCKFILE This
,
114 static MappedPage
* BIGBLOCKFILE_CreatePage(LPBIGBLOCKFILE This
,
116 static DWORD
BIGBLOCKFILE_GetProtectMode(DWORD openFlags
);
117 static BOOL
BIGBLOCKFILE_FileInit(LPBIGBLOCKFILE This
, HANDLE hFile
);
118 static BOOL
BIGBLOCKFILE_MemInit(LPBIGBLOCKFILE This
, ILockBytes
* plkbyt
);
120 /* Note that this evaluates a and b multiple times, so don't
121 * pass expressions with side effects. */
122 #define ROUND_UP(a, b) ((((a) + (b) - 1)/(b))*(b))
124 /***********************************************************
125 * Blockbits functions.
127 static inline BOOL
BIGBLOCKFILE_TestBit(const BlockBits
*bb
,
130 unsigned int array_index
= index
/ (CHAR_BIT
* sizeof(unsigned int));
131 unsigned int bit_index
= index
% (CHAR_BIT
* sizeof(unsigned int));
133 return bb
->bits
[array_index
] & (1 << bit_index
);
136 static inline void BIGBLOCKFILE_SetBit(BlockBits
*bb
, unsigned int index
)
138 unsigned int array_index
= index
/ (CHAR_BIT
* sizeof(unsigned int));
139 unsigned int bit_index
= index
% (CHAR_BIT
* sizeof(unsigned int));
141 bb
->bits
[array_index
] |= (1 << bit_index
);
144 static inline void BIGBLOCKFILE_ClearBit(BlockBits
*bb
, unsigned int index
)
146 unsigned int array_index
= index
/ (CHAR_BIT
* sizeof(unsigned int));
147 unsigned int bit_index
= index
% (CHAR_BIT
* sizeof(unsigned int));
149 bb
->bits
[array_index
] &= ~(1 << bit_index
);
152 static inline void BIGBLOCKFILE_Zero(BlockBits
*bb
)
154 memset(bb
->bits
, 0, sizeof(bb
->bits
));
157 /******************************************************************************
158 * BIGBLOCKFILE_Construct
160 * Construct a big block file. Create the file mapping object.
161 * Create the read only mapped pages list, the writable mapped page list
162 * and the blocks in use list.
164 BigBlockFile
* BIGBLOCKFILE_Construct(
173 This
= (LPBIGBLOCKFILE
)HeapAlloc(GetProcessHeap(), 0, sizeof(BigBlockFile
));
178 This
->fileBased
= fileBased
;
180 This
->flProtect
= BIGBLOCKFILE_GetProtectMode(openFlags
);
182 This
->blocksize
= blocksize
;
184 This
->maplist
= NULL
;
185 This
->victimhead
= NULL
;
186 This
->victimtail
= NULL
;
187 This
->num_victim_pages
= 0;
191 if (!BIGBLOCKFILE_FileInit(This
, hFile
))
193 HeapFree(GetProcessHeap(), 0, This
);
199 if (!BIGBLOCKFILE_MemInit(This
, pLkByt
))
201 HeapFree(GetProcessHeap(), 0, This
);
209 /******************************************************************************
210 * BIGBLOCKFILE_FileInit
212 * Initialize a big block object supported by a file.
214 static BOOL
BIGBLOCKFILE_FileInit(LPBIGBLOCKFILE This
, HANDLE hFile
)
217 This
->hbytearray
= 0;
218 This
->pbytearray
= NULL
;
222 if (This
->hfile
== INVALID_HANDLE_VALUE
)
225 /* create the file mapping object
227 This
->hfilemap
= CreateFileMappingA(This
->hfile
,
235 CloseHandle(This
->hfile
);
239 This
->filesize
.s
.LowPart
= GetFileSize(This
->hfile
,
240 &This
->filesize
.s
.HighPart
);
242 This
->maplist
= NULL
;
244 TRACE("file len %lu\n", This
->filesize
.s
.LowPart
);
249 /******************************************************************************
250 * BIGBLOCKFILE_MemInit
252 * Initialize a big block object supported by an ILockBytes on HGLOABL.
254 static BOOL
BIGBLOCKFILE_MemInit(LPBIGBLOCKFILE This
, ILockBytes
* plkbyt
)
260 * Retrieve the handle to the byte array from the LockByte object.
262 if (GetHGlobalFromILockBytes(plkbyt
, &(This
->hbytearray
)) != S_OK
)
264 FIXME("May not be an ILockBytes on HGLOBAL\n");
268 This
->pLkbyt
= plkbyt
;
271 * Increment the reference count of the ILockByte object since
272 * we're keeping a reference to it.
274 ILockBytes_AddRef(This
->pLkbyt
);
276 This
->filesize
.s
.LowPart
= GlobalSize(This
->hbytearray
);
277 This
->filesize
.s
.HighPart
= 0;
279 This
->pbytearray
= GlobalLock(This
->hbytearray
);
281 TRACE("mem on %p len %lu\n", This
->pbytearray
, This
->filesize
.s
.LowPart
);
286 /******************************************************************************
287 * BIGBLOCKFILE_Destructor
289 * Destructor. Clean up, free memory.
291 void BIGBLOCKFILE_Destructor(
294 BIGBLOCKFILE_FreeAllMappedPages(This
);
298 CloseHandle(This
->hfilemap
);
299 CloseHandle(This
->hfile
);
303 GlobalUnlock(This
->hbytearray
);
304 ILockBytes_Release(This
->pLkbyt
);
309 HeapFree(GetProcessHeap(), 0, This
);
312 /******************************************************************************
313 * BIGBLOCKFILE_GetROBigBlock
315 * Returns the specified block in read only mode.
316 * Will return NULL if the block doesn't exists.
318 void* BIGBLOCKFILE_GetROBigBlock(
323 * block index starts at -1
324 * translate to zero based index
326 if (index
== 0xffffffff)
332 * validate the block index
335 if (This
->blocksize
* (index
+ 1)
336 > ROUND_UP(This
->filesize
.s
.LowPart
, This
->blocksize
))
338 TRACE("out of range %lu vs %lu\n", This
->blocksize
* (index
+ 1),
339 This
->filesize
.s
.LowPart
);
343 return BIGBLOCKFILE_GetBigBlockPointer(This
, index
, FILE_MAP_READ
);
346 /******************************************************************************
347 * BIGBLOCKFILE_GetBigBlock
349 * Returns the specified block.
350 * Will grow the file if necessary.
352 void* BIGBLOCKFILE_GetBigBlock(LPBIGBLOCKFILE This
, ULONG index
)
355 * block index starts at -1
356 * translate to zero based index
358 if (index
== 0xffffffff)
364 * make sure that the block physically exists
366 if ((This
->blocksize
* (index
+ 1)) > This
->filesize
.s
.LowPart
)
368 ULARGE_INTEGER newSize
;
370 newSize
.s
.HighPart
= 0;
371 newSize
.s
.LowPart
= This
->blocksize
* (index
+ 1);
373 BIGBLOCKFILE_SetSize(This
, newSize
);
376 return BIGBLOCKFILE_GetBigBlockPointer(This
, index
, FILE_MAP_WRITE
);
379 /******************************************************************************
380 * BIGBLOCKFILE_ReleaseBigBlock
382 * Releases the specified block.
384 void BIGBLOCKFILE_ReleaseBigBlock(LPBIGBLOCKFILE This
, void *pBlock
)
391 page
= BIGBLOCKFILE_GetPageFromPointer(This
, pBlock
);
396 BIGBLOCKFILE_ReleaseMappedPage(This
, page
);
399 /******************************************************************************
400 * BIGBLOCKFILE_SetSize
402 * Sets the size of the file.
405 void BIGBLOCKFILE_SetSize(LPBIGBLOCKFILE This
, ULARGE_INTEGER newSize
)
407 if (This
->filesize
.s
.LowPart
== newSize
.s
.LowPart
)
410 TRACE("from %lu to %lu\n", This
->filesize
.s
.LowPart
, newSize
.s
.LowPart
);
412 * unmap all views, must be done before call to SetEndFile
414 BIGBLOCKFILE_UnmapAllMappedPages(This
);
421 * close file-mapping object, must be done before call to SetEndFile
423 CloseHandle(This
->hfilemap
);
428 * This fixes a bug when saving through smbfs.
429 * smbmount a Windows shared directory, save a structured storage file
430 * to that dir: crash.
432 * The problem is that the SetFilePointer-SetEndOfFile combo below
433 * doesn't always succeed. The file is not grown. It seems like the
434 * operation is cached. By doing the WriteFile, the file is actually
436 * This hack is only needed when saving to smbfs.
438 memset(buf
, '0', 10);
439 SetFilePointer(This
->hfile
, newSize
.s
.LowPart
, NULL
, FILE_BEGIN
);
440 WriteFile(This
->hfile
, buf
, 10, NULL
, NULL
);
446 * set the new end of file
448 SetFilePointer(This
->hfile
, newSize
.s
.LowPart
, NULL
, FILE_BEGIN
);
449 SetEndOfFile(This
->hfile
);
452 * re-create the file mapping object
454 This
->hfilemap
= CreateFileMappingA(This
->hfile
,
462 GlobalUnlock(This
->hbytearray
);
465 * Resize the byte array object.
467 ILockBytes_SetSize(This
->pLkbyt
, newSize
);
470 * Re-acquire the handle, it may have changed.
472 GetHGlobalFromILockBytes(This
->pLkbyt
, &This
->hbytearray
);
473 This
->pbytearray
= GlobalLock(This
->hbytearray
);
476 This
->filesize
.s
.LowPart
= newSize
.s
.LowPart
;
477 This
->filesize
.s
.HighPart
= newSize
.s
.HighPart
;
479 BIGBLOCKFILE_RemapAllMappedPages(This
);
482 /******************************************************************************
483 * BIGBLOCKFILE_GetSize
485 * Returns the size of the file.
488 ULARGE_INTEGER
BIGBLOCKFILE_GetSize(LPBIGBLOCKFILE This
)
490 return This
->filesize
;
493 /******************************************************************************
494 * BIGBLOCKFILE_AccessCheck [PRIVATE]
496 * block_index is the index within the page.
498 static BOOL
BIGBLOCKFILE_AccessCheck(MappedPage
*page
, ULONG block_index
,
499 DWORD desired_access
)
501 assert(block_index
< BLOCKS_PER_PAGE
);
503 if (desired_access
== FILE_MAP_READ
)
505 if (BIGBLOCKFILE_TestBit(&page
->writable_blocks
, block_index
))
508 BIGBLOCKFILE_SetBit(&page
->readable_blocks
, block_index
);
512 assert(desired_access
== FILE_MAP_WRITE
);
514 if (BIGBLOCKFILE_TestBit(&page
->readable_blocks
, block_index
))
517 BIGBLOCKFILE_SetBit(&page
->writable_blocks
, block_index
);
523 /******************************************************************************
524 * BIGBLOCKFILE_GetBigBlockPointer [PRIVATE]
526 * Returns a pointer to the specified block.
528 static void* BIGBLOCKFILE_GetBigBlockPointer(
531 DWORD desired_access
)
533 DWORD page_index
= block_index
/ BLOCKS_PER_PAGE
;
534 DWORD block_on_page
= block_index
% BLOCKS_PER_PAGE
;
536 MappedPage
*page
= BIGBLOCKFILE_GetMappedView(This
, page_index
);
537 if (!page
|| !page
->lpBytes
) return NULL
;
539 if (!BIGBLOCKFILE_AccessCheck(page
, block_on_page
, desired_access
))
541 BIGBLOCKFILE_ReleaseMappedPage(This
, page
);
545 return (LPBYTE
)page
->lpBytes
+ (block_on_page
* This
->blocksize
);
548 /******************************************************************************
549 * BIGBLOCKFILE_GetMappedPageFromPointer [PRIVATE]
551 * pBlock is a pointer to a block on a page.
552 * The page has to be on the in-use list. (As oppsed to the victim list.)
554 * Does not increment the usage count.
556 static MappedPage
*BIGBLOCKFILE_GetPageFromPointer(LPBIGBLOCKFILE This
,
561 for (page
= This
->maplist
; page
!= NULL
; page
= page
->next
)
563 if ((LPBYTE
)pBlock
>= (LPBYTE
)page
->lpBytes
564 && (LPBYTE
)pBlock
<= (LPBYTE
)page
->lpBytes
+ PAGE_SIZE
)
572 /******************************************************************************
573 * BIGBLOCKFILE_FindPageInList [PRIVATE]
576 static MappedPage
*BIGBLOCKFILE_FindPageInList(MappedPage
*head
,
579 for (; head
!= NULL
; head
= head
->next
)
581 if (head
->page_index
== page_index
)
583 InterlockedIncrement(&head
->refcnt
);
592 static void BIGBLOCKFILE_UnlinkPage(MappedPage
*page
)
594 if (page
->next
) page
->next
->prev
= page
->prev
;
595 if (page
->prev
) page
->prev
->next
= page
->next
;
598 static void BIGBLOCKFILE_LinkHeadPage(MappedPage
**head
, MappedPage
*page
)
600 if (*head
) (*head
)->prev
= page
;
606 /******************************************************************************
607 * BIGBLOCKFILE_GetMappedView [PRIVATE]
609 * Gets the page requested if it is already mapped.
610 * If it's not already mapped, this method will map it
612 static void * BIGBLOCKFILE_GetMappedView(
618 page
= BIGBLOCKFILE_FindPageInList(This
->maplist
, page_index
);
621 page
= BIGBLOCKFILE_FindPageInList(This
->victimhead
, page_index
);
624 This
->num_victim_pages
--;
626 BIGBLOCKFILE_Zero(&page
->readable_blocks
);
627 BIGBLOCKFILE_Zero(&page
->writable_blocks
);
633 /* If the page is not already at the head of the list, move
634 * it there. (Also moves pages from victim to main list.) */
635 if (This
->maplist
!= page
)
637 if (This
->victimhead
== page
) This
->victimhead
= page
->next
;
638 if (This
->victimtail
== page
) This
->victimtail
= page
->prev
;
640 BIGBLOCKFILE_UnlinkPage(page
);
642 BIGBLOCKFILE_LinkHeadPage(&This
->maplist
, page
);
648 page
= BIGBLOCKFILE_CreatePage(This
, page_index
);
649 if (!page
) return NULL
;
651 BIGBLOCKFILE_LinkHeadPage(&This
->maplist
, page
);
656 static BOOL
BIGBLOCKFILE_MapPage(LPBIGBLOCKFILE This
, MappedPage
*page
)
658 DWORD lowoffset
= PAGE_SIZE
* page
->page_index
;
663 DWORD desired_access
;
665 if (lowoffset
+ PAGE_SIZE
> This
->filesize
.s
.LowPart
)
666 numBytesToMap
= This
->filesize
.s
.LowPart
- lowoffset
;
668 numBytesToMap
= PAGE_SIZE
;
670 if (This
->flProtect
== PAGE_READONLY
)
671 desired_access
= FILE_MAP_READ
;
673 desired_access
= FILE_MAP_WRITE
;
675 page
->lpBytes
= MapViewOfFile(This
->hfilemap
, desired_access
, 0,
676 lowoffset
, numBytesToMap
);
680 page
->lpBytes
= (LPBYTE
)This
->pbytearray
+ lowoffset
;
683 TRACE("mapped page %lu to %p\n", page
->page_index
, page
->lpBytes
);
685 return page
->lpBytes
!= NULL
;
688 static MappedPage
*BIGBLOCKFILE_CreatePage(LPBIGBLOCKFILE This
,
693 page
= HeapAlloc(GetProcessHeap(), 0, sizeof(MappedPage
));
697 page
->page_index
= page_index
;
703 BIGBLOCKFILE_MapPage(This
, page
);
705 BIGBLOCKFILE_Zero(&page
->readable_blocks
);
706 BIGBLOCKFILE_Zero(&page
->writable_blocks
);
711 static void BIGBLOCKFILE_UnmapPage(LPBIGBLOCKFILE This
, MappedPage
*page
)
713 TRACE("%ld at %p\n", page
->page_index
, page
->lpBytes
);
714 if (page
->refcnt
> 0)
715 ERR("unmapping inuse page %p\n", page
->lpBytes
);
717 if (This
->fileBased
&& page
->lpBytes
)
718 UnmapViewOfFile(page
->lpBytes
);
720 page
->lpBytes
= NULL
;
723 static void BIGBLOCKFILE_DeletePage(LPBIGBLOCKFILE This
, MappedPage
*page
)
725 BIGBLOCKFILE_UnmapPage(This
, page
);
727 HeapFree(GetProcessHeap(), 0, page
);
730 /******************************************************************************
731 * BIGBLOCKFILE_ReleaseMappedPage [PRIVATE]
733 * Decrements the reference count of the mapped page.
735 static void BIGBLOCKFILE_ReleaseMappedPage(
739 assert(This
!= NULL
);
740 assert(page
!= NULL
);
742 /* If the page is no longer refenced, move it to the victim list.
743 * If the victim list is too long, kick somebody off. */
744 if (!InterlockedDecrement(&page
->refcnt
))
746 if (This
->maplist
== page
) This
->maplist
= page
->next
;
748 BIGBLOCKFILE_UnlinkPage(page
);
750 if (MAX_VICTIM_PAGES
> 0)
752 if (This
->num_victim_pages
>= MAX_VICTIM_PAGES
)
754 MappedPage
*victim
= This
->victimtail
;
757 This
->victimtail
= victim
->prev
;
758 if (This
->victimhead
== victim
)
759 This
->victimhead
= victim
->next
;
761 BIGBLOCKFILE_UnlinkPage(victim
);
762 BIGBLOCKFILE_DeletePage(This
, victim
);
765 else This
->num_victim_pages
++;
767 BIGBLOCKFILE_LinkHeadPage(&This
->victimhead
, page
);
768 if (This
->victimtail
== NULL
) This
->victimtail
= page
;
771 BIGBLOCKFILE_DeletePage(This
, page
);
775 static void BIGBLOCKFILE_DeleteList(LPBIGBLOCKFILE This
, MappedPage
*list
)
779 MappedPage
*next
= list
->next
;
781 BIGBLOCKFILE_DeletePage(This
, list
);
787 /******************************************************************************
788 * BIGBLOCKFILE_FreeAllMappedPages [PRIVATE]
790 * Unmap all currently mapped pages.
791 * Empty mapped pages list.
793 static void BIGBLOCKFILE_FreeAllMappedPages(
796 BIGBLOCKFILE_DeleteList(This
, This
->maplist
);
797 BIGBLOCKFILE_DeleteList(This
, This
->victimhead
);
799 This
->maplist
= NULL
;
800 This
->victimhead
= NULL
;
801 This
->victimtail
= NULL
;
802 This
->num_victim_pages
= 0;
805 static void BIGBLOCKFILE_UnmapList(LPBIGBLOCKFILE This
, MappedPage
*list
)
807 for (; list
!= NULL
; list
= list
->next
)
809 BIGBLOCKFILE_UnmapPage(This
, list
);
813 static void BIGBLOCKFILE_UnmapAllMappedPages(LPBIGBLOCKFILE This
)
815 BIGBLOCKFILE_UnmapList(This
, This
->maplist
);
816 BIGBLOCKFILE_UnmapList(This
, This
->victimhead
);
819 static void BIGBLOCKFILE_RemapList(LPBIGBLOCKFILE This
, MappedPage
*list
)
823 MappedPage
*next
= list
->next
;
825 if (list
->page_index
* PAGE_SIZE
> This
->filesize
.s
.LowPart
)
827 TRACE("discarding %lu\n", list
->page_index
);
829 /* page is entirely outside of the file, delete it */
830 BIGBLOCKFILE_UnlinkPage(list
);
831 BIGBLOCKFILE_DeletePage(This
, list
);
835 /* otherwise, remap it */
836 BIGBLOCKFILE_MapPage(This
, list
);
843 static void BIGBLOCKFILE_RemapAllMappedPages(LPBIGBLOCKFILE This
)
845 BIGBLOCKFILE_RemapList(This
, This
->maplist
);
846 BIGBLOCKFILE_RemapList(This
, This
->victimhead
);
849 /****************************************************************************
850 * BIGBLOCKFILE_GetProtectMode
852 * This function will return a protection mode flag for a file-mapping object
853 * from the open flags of a file.
855 static DWORD
BIGBLOCKFILE_GetProtectMode(DWORD openFlags
)
857 if (openFlags
& (STGM_WRITE
| STGM_READWRITE
))
858 return PAGE_READWRITE
;
860 return PAGE_READONLY
;