Rewrite the BIGBLOCKFILE implementation for better performance.
[wine.git] / dlls / ole32 / stg_bigblockfile.c
blobe02b076e8e362d39815e48eba170dcc0e48c1e51
1 /******************************************************************************
3 * BigBlockFile
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
12 * starting with -1.
14 * TODO:
15 * - Support for a transacted mode
17 * Copyright 1999 Thuy Nguyen
21 #include <assert.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <limits.h>
27 #include "winbase.h"
28 #include "winerror.h"
29 #include "wine/obj_storage.h"
30 #include "ole2.h"
32 #include "storage32.h"
34 #include "debugtools.h"
36 DEFAULT_DEBUG_CHANNEL(storage)
38 /***********************************************************
39 * Data structures used internally by the BigBlockFile
40 * class.
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. */
54 typedef struct
56 unsigned int bits[BLOCKS_PER_PAGE / (CHAR_BIT * sizeof(unsigned int))];
57 } BlockBits;
59 /***
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
66 * PAGE_SIZE-aligned.)
68 struct MappedPage
70 MappedPage *next;
71 MappedPage *prev;
73 DWORD page_index;
74 LPVOID lpBytes;
75 LONG refcnt;
77 BlockBits readable_blocks;
78 BlockBits writable_blocks;
81 /***********************************************************
82 * Prototypes for private methods
84 static void* BIGBLOCKFILE_GetMappedView(LPBIGBLOCKFILE This,
85 DWORD page_index);
86 static void BIGBLOCKFILE_ReleaseMappedPage(LPBIGBLOCKFILE This,
87 MappedPage *page);
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,
92 ULONG index,
93 DWORD desired_access);
94 static MappedPage* BIGBLOCKFILE_GetPageFromPointer(LPBIGBLOCKFILE This,
95 void* pBlock);
96 static MappedPage* BIGBLOCKFILE_CreatePage(LPBIGBLOCKFILE This,
97 ULONG page_index);
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,
110 unsigned int index)
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(
147 HANDLE hFile,
148 ILockBytes* pLkByt,
149 DWORD openFlags,
150 ULONG blocksize,
151 BOOL fileBased)
153 LPBIGBLOCKFILE This;
155 This = (LPBIGBLOCKFILE)HeapAlloc(GetProcessHeap(), 0, sizeof(BigBlockFile));
157 if (This == NULL)
158 return NULL;
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;
171 if (This->fileBased)
173 if (!BIGBLOCKFILE_FileInit(This, hFile))
175 HeapFree(GetProcessHeap(), 0, This);
176 return NULL;
179 else
181 if (!BIGBLOCKFILE_MemInit(This, pLkByt))
183 HeapFree(GetProcessHeap(), 0, This);
184 return NULL;
188 return This;
191 /******************************************************************************
192 * BIGBLOCKFILE_FileInit
194 * Initialize a big block object supported by a file.
196 static BOOL BIGBLOCKFILE_FileInit(LPBIGBLOCKFILE This, HANDLE hFile)
198 This->pLkbyt = NULL;
199 This->hbytearray = 0;
200 This->pbytearray = NULL;
202 This->hfile = hFile;
204 if (This->hfile == INVALID_HANDLE_VALUE)
205 return FALSE;
207 /* create the file mapping object
209 This->hfilemap = CreateFileMappingA(This->hfile,
210 NULL,
211 This->flProtect,
212 0, 0,
213 NULL);
215 if (This->hfilemap == NULL)
217 CloseHandle(This->hfile);
218 return FALSE;
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);
228 return TRUE;
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)
238 This->hfile = 0;
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");
247 return FALSE;
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);
265 return TRUE;
268 /******************************************************************************
269 * BIGBLOCKFILE_Destructor
271 * Destructor. Clean up, free memory.
273 void BIGBLOCKFILE_Destructor(
274 LPBIGBLOCKFILE This)
276 BIGBLOCKFILE_FreeAllMappedPages(This);
278 if (This->fileBased)
280 CloseHandle(This->hfilemap);
281 CloseHandle(This->hfile);
283 else
285 GlobalUnlock(This->hbytearray);
286 ILockBytes_Release(This->pLkbyt);
289 /* destroy this
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(
301 LPBIGBLOCKFILE This,
302 ULONG index)
305 * block index starts at -1
306 * translate to zero based index
308 if (index == 0xffffffff)
309 index = 0;
310 else
311 index++;
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);
322 return NULL;
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)
341 index = 0;
342 else
343 index++;
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)
368 MappedPage *page;
370 if (pBlock == NULL)
371 return;
373 page = BIGBLOCKFILE_GetPageFromPointer(This, pBlock);
375 if (page == NULL)
376 return;
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)
390 return;
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);
398 if (This->fileBased)
400 char buf[10];
403 * close file-mapping object, must be done before call to SetEndFile
405 CloseHandle(This->hfilemap);
406 This->hfilemap = NULL;
409 * BEGIN HACK
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
417 * grown on disk.
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);
424 * END HACK
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,
437 NULL,
438 This->flProtect,
439 0, 0,
440 NULL);
442 else
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))
488 return FALSE;
490 BIGBLOCKFILE_SetBit(&page->readable_blocks, block_index);
492 else
494 assert(desired_access == FILE_MAP_WRITE);
496 if (BIGBLOCKFILE_TestBit(&page->readable_blocks, block_index))
497 return FALSE;
499 BIGBLOCKFILE_SetBit(&page->writable_blocks, block_index);
502 return TRUE;
505 /******************************************************************************
506 * BIGBLOCKFILE_GetBigBlockPointer [PRIVATE]
508 * Returns a pointer to the specified block.
510 static void* BIGBLOCKFILE_GetBigBlockPointer(
511 LPBIGBLOCKFILE This,
512 ULONG block_index,
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);
524 return NULL;
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,
539 void *pBlock)
541 MappedPage *page;
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)
547 break;
551 return page;
554 /******************************************************************************
555 * BIGBLOCKFILE_FindPageInList [PRIVATE]
558 static MappedPage *BIGBLOCKFILE_FindPageInList(MappedPage *head,
559 ULONG page_index)
561 for (; head != NULL; head = head->next)
563 if (head->page_index == page_index)
565 InterlockedIncrement(&head->refcnt);
566 break;
570 return head;
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;
583 page->next = *head;
584 page->prev = NULL;
585 *head = 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(
595 LPBIGBLOCKFILE This,
596 DWORD page_index)
598 MappedPage *page;
600 page = BIGBLOCKFILE_FindPageInList(This->maplist, page_index);
601 if (!page)
603 page = BIGBLOCKFILE_FindPageInList(This->victimhead, page_index);
604 if (page)
606 This->num_victim_pages--;
608 BIGBLOCKFILE_Zero(&page->readable_blocks);
609 BIGBLOCKFILE_Zero(&page->writable_blocks);
613 if (page)
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);
627 return page;
630 page = BIGBLOCKFILE_CreatePage(This, page_index);
631 if (!page) return NULL;
633 BIGBLOCKFILE_LinkHeadPage(&This->maplist, page);
635 return page;
638 static BOOL BIGBLOCKFILE_MapPage(LPBIGBLOCKFILE This, MappedPage *page)
640 DWORD lowoffset = PAGE_SIZE * page->page_index;
642 if (This->fileBased)
644 DWORD numBytesToMap;
645 DWORD desired_access;
647 if (lowoffset + PAGE_SIZE > This->filesize.s.LowPart)
648 numBytesToMap = This->filesize.s.LowPart - lowoffset;
649 else
650 numBytesToMap = PAGE_SIZE;
652 if (This->flProtect == PAGE_READONLY)
653 desired_access = FILE_MAP_READ;
654 else
655 desired_access = FILE_MAP_WRITE;
657 page->lpBytes = MapViewOfFile(This->hfilemap, desired_access, 0,
658 lowoffset, numBytesToMap);
660 else
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,
671 ULONG page_index)
673 MappedPage *page;
675 page = HeapAlloc(GetProcessHeap(), 0, sizeof(MappedPage));
676 if (page == NULL)
677 return NULL;
679 page->page_index = page_index;
680 page->refcnt = 1;
682 page->next = NULL;
683 page->prev = NULL;
685 BIGBLOCKFILE_MapPage(This, page);
687 BIGBLOCKFILE_Zero(&page->readable_blocks);
688 BIGBLOCKFILE_Zero(&page->writable_blocks);
690 return page;
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(
718 LPBIGBLOCKFILE This,
719 MappedPage *page)
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;
737 if (victim)
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;
752 else
753 BIGBLOCKFILE_DeletePage(This, page);
757 static void BIGBLOCKFILE_DeleteList(LPBIGBLOCKFILE This, MappedPage *list)
759 while (list != NULL)
761 MappedPage *next = list->next;
763 BIGBLOCKFILE_DeletePage(This, list);
765 list = next;
769 /******************************************************************************
770 * BIGBLOCKFILE_FreeAllMappedPages [PRIVATE]
772 * Unmap all currently mapped pages.
773 * Empty mapped pages list.
775 static void BIGBLOCKFILE_FreeAllMappedPages(
776 LPBIGBLOCKFILE This)
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)
803 while (list != NULL)
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);
815 else
817 /* otherwise, remap it */
818 BIGBLOCKFILE_MapPage(This, list);
821 list = next;
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;
841 else
842 return PAGE_READONLY;