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 This
->filesize
.u
.LowPart
= GetFileSize(This
->hfile
,
226 &This
->filesize
.u
.HighPart
);
228 if( This
->filesize
.u
.LowPart
|| This
->filesize
.u
.HighPart
)
230 /* create the file mapping object
232 This
->hfilemap
= CreateFileMappingA(This
->hfile
,
240 CloseHandle(This
->hfile
);
245 This
->hfilemap
= NULL
;
247 This
->maplist
= NULL
;
249 TRACE("file len %lu\n", This
->filesize
.u
.LowPart
);
254 /******************************************************************************
255 * BIGBLOCKFILE_MemInit
257 * Initialize a big block object supported by an ILockBytes on HGLOABL.
259 static BOOL
BIGBLOCKFILE_MemInit(LPBIGBLOCKFILE This
, ILockBytes
* plkbyt
)
265 * Retrieve the handle to the byte array from the LockByte object.
267 if (GetHGlobalFromILockBytes(plkbyt
, &(This
->hbytearray
)) != S_OK
)
269 FIXME("May not be an ILockBytes on HGLOBAL\n");
273 This
->pLkbyt
= plkbyt
;
276 * Increment the reference count of the ILockByte object since
277 * we're keeping a reference to it.
279 ILockBytes_AddRef(This
->pLkbyt
);
281 This
->filesize
.u
.LowPart
= GlobalSize(This
->hbytearray
);
282 This
->filesize
.u
.HighPart
= 0;
284 This
->pbytearray
= GlobalLock(This
->hbytearray
);
286 TRACE("mem on %p len %lu\n", This
->pbytearray
, This
->filesize
.u
.LowPart
);
291 /******************************************************************************
292 * BIGBLOCKFILE_Destructor
294 * Destructor. Clean up, free memory.
296 void BIGBLOCKFILE_Destructor(
299 BIGBLOCKFILE_FreeAllMappedPages(This
);
303 CloseHandle(This
->hfilemap
);
304 CloseHandle(This
->hfile
);
308 GlobalUnlock(This
->hbytearray
);
309 ILockBytes_Release(This
->pLkbyt
);
314 HeapFree(GetProcessHeap(), 0, This
);
317 /******************************************************************************
318 * BIGBLOCKFILE_GetROBigBlock
320 * Returns the specified block in read only mode.
321 * Will return NULL if the block doesn't exists.
323 void* BIGBLOCKFILE_GetROBigBlock(
328 * block index starts at -1
329 * translate to zero based index
331 if (index
== 0xffffffff)
337 * validate the block index
340 if (This
->blocksize
* (index
+ 1)
341 > ROUND_UP(This
->filesize
.u
.LowPart
, This
->blocksize
))
343 TRACE("out of range %lu vs %lu\n", This
->blocksize
* (index
+ 1),
344 This
->filesize
.u
.LowPart
);
348 return BIGBLOCKFILE_GetBigBlockPointer(This
, index
, FILE_MAP_READ
);
351 /******************************************************************************
352 * BIGBLOCKFILE_GetBigBlock
354 * Returns the specified block.
355 * Will grow the file if necessary.
357 void* BIGBLOCKFILE_GetBigBlock(LPBIGBLOCKFILE This
, ULONG index
)
360 * block index starts at -1
361 * translate to zero based index
363 if (index
== 0xffffffff)
369 * make sure that the block physically exists
371 if ((This
->blocksize
* (index
+ 1)) > This
->filesize
.u
.LowPart
)
373 ULARGE_INTEGER newSize
;
375 newSize
.u
.HighPart
= 0;
376 newSize
.u
.LowPart
= This
->blocksize
* (index
+ 1);
378 BIGBLOCKFILE_SetSize(This
, newSize
);
381 return BIGBLOCKFILE_GetBigBlockPointer(This
, index
, FILE_MAP_WRITE
);
384 /******************************************************************************
385 * BIGBLOCKFILE_ReleaseBigBlock
387 * Releases the specified block.
389 void BIGBLOCKFILE_ReleaseBigBlock(LPBIGBLOCKFILE This
, void *pBlock
)
396 page
= BIGBLOCKFILE_GetPageFromPointer(This
, pBlock
);
401 BIGBLOCKFILE_ReleaseMappedPage(This
, page
);
404 /******************************************************************************
405 * BIGBLOCKFILE_SetSize
407 * Sets the size of the file.
410 void BIGBLOCKFILE_SetSize(LPBIGBLOCKFILE This
, ULARGE_INTEGER newSize
)
412 if (This
->filesize
.u
.LowPart
== newSize
.u
.LowPart
)
415 TRACE("from %lu to %lu\n", This
->filesize
.u
.LowPart
, newSize
.u
.LowPart
);
417 * unmap all views, must be done before call to SetEndFile
419 BIGBLOCKFILE_UnmapAllMappedPages(This
);
426 * close file-mapping object, must be done before call to SetEndFile
429 CloseHandle(This
->hfilemap
);
434 * This fixes a bug when saving through smbfs.
435 * smbmount a Windows shared directory, save a structured storage file
436 * to that dir: crash.
438 * The problem is that the SetFilePointer-SetEndOfFile combo below
439 * doesn't always succeed. The file is not grown. It seems like the
440 * operation is cached. By doing the WriteFile, the file is actually
442 * This hack is only needed when saving to smbfs.
444 memset(buf
, '0', 10);
445 SetFilePointer(This
->hfile
, newSize
.u
.LowPart
, NULL
, FILE_BEGIN
);
446 WriteFile(This
->hfile
, buf
, 10, NULL
, NULL
);
452 * set the new end of file
454 SetFilePointer(This
->hfile
, newSize
.u
.LowPart
, NULL
, FILE_BEGIN
);
455 SetEndOfFile(This
->hfile
);
458 * re-create the file mapping object
460 This
->hfilemap
= CreateFileMappingA(This
->hfile
,
468 GlobalUnlock(This
->hbytearray
);
471 * Resize the byte array object.
473 ILockBytes_SetSize(This
->pLkbyt
, newSize
);
476 * Re-acquire the handle, it may have changed.
478 GetHGlobalFromILockBytes(This
->pLkbyt
, &This
->hbytearray
);
479 This
->pbytearray
= GlobalLock(This
->hbytearray
);
482 This
->filesize
.u
.LowPart
= newSize
.u
.LowPart
;
483 This
->filesize
.u
.HighPart
= newSize
.u
.HighPart
;
485 BIGBLOCKFILE_RemapAllMappedPages(This
);
488 /******************************************************************************
489 * BIGBLOCKFILE_GetSize
491 * Returns the size of the file.
494 ULARGE_INTEGER
BIGBLOCKFILE_GetSize(LPBIGBLOCKFILE This
)
496 return This
->filesize
;
499 /******************************************************************************
500 * BIGBLOCKFILE_AccessCheck [PRIVATE]
502 * block_index is the index within the page.
504 static BOOL
BIGBLOCKFILE_AccessCheck(MappedPage
*page
, ULONG block_index
,
505 DWORD desired_access
)
507 assert(block_index
< BLOCKS_PER_PAGE
);
509 if (desired_access
== FILE_MAP_READ
)
511 if (BIGBLOCKFILE_TestBit(&page
->writable_blocks
, block_index
))
514 BIGBLOCKFILE_SetBit(&page
->readable_blocks
, block_index
);
518 assert(desired_access
== FILE_MAP_WRITE
);
520 if (BIGBLOCKFILE_TestBit(&page
->readable_blocks
, block_index
))
523 BIGBLOCKFILE_SetBit(&page
->writable_blocks
, block_index
);
529 /******************************************************************************
530 * BIGBLOCKFILE_GetBigBlockPointer [PRIVATE]
532 * Returns a pointer to the specified block.
534 static void* BIGBLOCKFILE_GetBigBlockPointer(
537 DWORD desired_access
)
539 DWORD page_index
= block_index
/ BLOCKS_PER_PAGE
;
540 DWORD block_on_page
= block_index
% BLOCKS_PER_PAGE
;
542 MappedPage
*page
= BIGBLOCKFILE_GetMappedView(This
, page_index
);
543 if (!page
|| !page
->lpBytes
) return NULL
;
545 if (!BIGBLOCKFILE_AccessCheck(page
, block_on_page
, desired_access
))
547 BIGBLOCKFILE_ReleaseMappedPage(This
, page
);
551 return (LPBYTE
)page
->lpBytes
+ (block_on_page
* This
->blocksize
);
554 /******************************************************************************
555 * BIGBLOCKFILE_GetMappedPageFromPointer [PRIVATE]
557 * pBlock is a pointer to a block on a page.
558 * The page has to be on the in-use list. (As oppsed to the victim list.)
560 * Does not increment the usage count.
562 static MappedPage
*BIGBLOCKFILE_GetPageFromPointer(LPBIGBLOCKFILE This
,
567 for (page
= This
->maplist
; page
!= NULL
; page
= page
->next
)
569 if ((LPBYTE
)pBlock
>= (LPBYTE
)page
->lpBytes
570 && (LPBYTE
)pBlock
<= (LPBYTE
)page
->lpBytes
+ PAGE_SIZE
)
578 /******************************************************************************
579 * BIGBLOCKFILE_FindPageInList [PRIVATE]
582 static MappedPage
*BIGBLOCKFILE_FindPageInList(MappedPage
*head
,
585 for (; head
!= NULL
; head
= head
->next
)
587 if (head
->page_index
== page_index
)
589 InterlockedIncrement(&head
->refcnt
);
598 static void BIGBLOCKFILE_UnlinkPage(MappedPage
*page
)
600 if (page
->next
) page
->next
->prev
= page
->prev
;
601 if (page
->prev
) page
->prev
->next
= page
->next
;
604 static void BIGBLOCKFILE_LinkHeadPage(MappedPage
**head
, MappedPage
*page
)
606 if (*head
) (*head
)->prev
= page
;
612 /******************************************************************************
613 * BIGBLOCKFILE_GetMappedView [PRIVATE]
615 * Gets the page requested if it is already mapped.
616 * If it's not already mapped, this method will map it
618 static void * BIGBLOCKFILE_GetMappedView(
624 page
= BIGBLOCKFILE_FindPageInList(This
->maplist
, page_index
);
627 page
= BIGBLOCKFILE_FindPageInList(This
->victimhead
, page_index
);
630 This
->num_victim_pages
--;
632 BIGBLOCKFILE_Zero(&page
->readable_blocks
);
633 BIGBLOCKFILE_Zero(&page
->writable_blocks
);
639 /* If the page is not already at the head of the list, move
640 * it there. (Also moves pages from victim to main list.) */
641 if (This
->maplist
!= page
)
643 if (This
->victimhead
== page
) This
->victimhead
= page
->next
;
644 if (This
->victimtail
== page
) This
->victimtail
= page
->prev
;
646 BIGBLOCKFILE_UnlinkPage(page
);
648 BIGBLOCKFILE_LinkHeadPage(&This
->maplist
, page
);
654 page
= BIGBLOCKFILE_CreatePage(This
, page_index
);
655 if (!page
) return NULL
;
657 BIGBLOCKFILE_LinkHeadPage(&This
->maplist
, page
);
662 static BOOL
BIGBLOCKFILE_MapPage(LPBIGBLOCKFILE This
, MappedPage
*page
)
664 DWORD lowoffset
= PAGE_SIZE
* page
->page_index
;
669 DWORD desired_access
;
671 if( !This
->hfilemap
)
674 if (lowoffset
+ PAGE_SIZE
> This
->filesize
.u
.LowPart
)
675 numBytesToMap
= This
->filesize
.u
.LowPart
- lowoffset
;
677 numBytesToMap
= PAGE_SIZE
;
679 if (This
->flProtect
== PAGE_READONLY
)
680 desired_access
= FILE_MAP_READ
;
682 desired_access
= FILE_MAP_WRITE
;
684 page
->lpBytes
= MapViewOfFile(This
->hfilemap
, desired_access
, 0,
685 lowoffset
, numBytesToMap
);
689 page
->lpBytes
= (LPBYTE
)This
->pbytearray
+ lowoffset
;
692 TRACE("mapped page %lu to %p\n", page
->page_index
, page
->lpBytes
);
694 return page
->lpBytes
!= NULL
;
697 static MappedPage
*BIGBLOCKFILE_CreatePage(LPBIGBLOCKFILE This
,
702 page
= HeapAlloc(GetProcessHeap(), 0, sizeof(MappedPage
));
706 page
->page_index
= page_index
;
712 BIGBLOCKFILE_MapPage(This
, page
);
714 BIGBLOCKFILE_Zero(&page
->readable_blocks
);
715 BIGBLOCKFILE_Zero(&page
->writable_blocks
);
720 static void BIGBLOCKFILE_UnmapPage(LPBIGBLOCKFILE This
, MappedPage
*page
)
722 TRACE("%ld at %p\n", page
->page_index
, page
->lpBytes
);
723 if (page
->refcnt
> 0)
724 ERR("unmapping inuse page %p\n", page
->lpBytes
);
726 if (This
->fileBased
&& page
->lpBytes
)
727 UnmapViewOfFile(page
->lpBytes
);
729 page
->lpBytes
= NULL
;
732 static void BIGBLOCKFILE_DeletePage(LPBIGBLOCKFILE This
, MappedPage
*page
)
734 BIGBLOCKFILE_UnmapPage(This
, page
);
736 HeapFree(GetProcessHeap(), 0, page
);
739 /******************************************************************************
740 * BIGBLOCKFILE_ReleaseMappedPage [PRIVATE]
742 * Decrements the reference count of the mapped page.
744 static void BIGBLOCKFILE_ReleaseMappedPage(
748 assert(This
!= NULL
);
749 assert(page
!= NULL
);
751 /* If the page is no longer refenced, move it to the victim list.
752 * If the victim list is too long, kick somebody off. */
753 if (!InterlockedDecrement(&page
->refcnt
))
755 if (This
->maplist
== page
) This
->maplist
= page
->next
;
757 BIGBLOCKFILE_UnlinkPage(page
);
759 if (MAX_VICTIM_PAGES
> 0)
761 if (This
->num_victim_pages
>= MAX_VICTIM_PAGES
)
763 MappedPage
*victim
= This
->victimtail
;
766 This
->victimtail
= victim
->prev
;
767 if (This
->victimhead
== victim
)
768 This
->victimhead
= victim
->next
;
770 BIGBLOCKFILE_UnlinkPage(victim
);
771 BIGBLOCKFILE_DeletePage(This
, victim
);
774 else This
->num_victim_pages
++;
776 BIGBLOCKFILE_LinkHeadPage(&This
->victimhead
, page
);
777 if (This
->victimtail
== NULL
) This
->victimtail
= page
;
780 BIGBLOCKFILE_DeletePage(This
, page
);
784 static void BIGBLOCKFILE_DeleteList(LPBIGBLOCKFILE This
, MappedPage
*list
)
788 MappedPage
*next
= list
->next
;
790 BIGBLOCKFILE_DeletePage(This
, list
);
796 /******************************************************************************
797 * BIGBLOCKFILE_FreeAllMappedPages [PRIVATE]
799 * Unmap all currently mapped pages.
800 * Empty mapped pages list.
802 static void BIGBLOCKFILE_FreeAllMappedPages(
805 BIGBLOCKFILE_DeleteList(This
, This
->maplist
);
806 BIGBLOCKFILE_DeleteList(This
, This
->victimhead
);
808 This
->maplist
= NULL
;
809 This
->victimhead
= NULL
;
810 This
->victimtail
= NULL
;
811 This
->num_victim_pages
= 0;
814 static void BIGBLOCKFILE_UnmapList(LPBIGBLOCKFILE This
, MappedPage
*list
)
816 for (; list
!= NULL
; list
= list
->next
)
818 BIGBLOCKFILE_UnmapPage(This
, list
);
822 static void BIGBLOCKFILE_UnmapAllMappedPages(LPBIGBLOCKFILE This
)
824 BIGBLOCKFILE_UnmapList(This
, This
->maplist
);
825 BIGBLOCKFILE_UnmapList(This
, This
->victimhead
);
828 static void BIGBLOCKFILE_RemapList(LPBIGBLOCKFILE This
, MappedPage
*list
)
832 MappedPage
*next
= list
->next
;
834 if (list
->page_index
* PAGE_SIZE
> This
->filesize
.u
.LowPart
)
836 TRACE("discarding %lu\n", list
->page_index
);
838 /* page is entirely outside of the file, delete it */
839 BIGBLOCKFILE_UnlinkPage(list
);
840 BIGBLOCKFILE_DeletePage(This
, list
);
844 /* otherwise, remap it */
845 BIGBLOCKFILE_MapPage(This
, list
);
852 static void BIGBLOCKFILE_RemapAllMappedPages(LPBIGBLOCKFILE This
)
854 BIGBLOCKFILE_RemapList(This
, This
->maplist
);
855 BIGBLOCKFILE_RemapList(This
, This
->victimhead
);
858 /****************************************************************************
859 * BIGBLOCKFILE_GetProtectMode
861 * This function will return a protection mode flag for a file-mapping object
862 * from the open flags of a file.
864 static DWORD
BIGBLOCKFILE_GetProtectMode(DWORD openFlags
)
866 if (openFlags
& (STGM_WRITE
| STGM_READWRITE
))
867 return PAGE_READWRITE
;
869 return PAGE_READONLY
;