diff --git a/ext/lsm1/lsm_win32.c b/ext/lsm1/lsm_win32.c index 50c5b67af6..af58176609 100644 --- a/ext/lsm1/lsm_win32.c +++ b/ext/lsm1/lsm_win32.c @@ -39,7 +39,7 @@ struct Win32File { HANDLE hShmFile; /* File handle for *-shm file */ HANDLE hMap; /* File handle for mapping */ - void *pMap; /* Pointer to mapping of file fd */ + LPVOID pMap; /* Pointer to mapping of file fd */ size_t nMap; /* Size of mapping at pMap in bytes */ int nShm; /* Number of entries in array apShm[] */ void **apShm; /* Array of 32K shared memory segments */ @@ -291,10 +291,10 @@ static int lsmWin32OsTruncate( lsm_i64 nSize /* Size to truncate file to */ ){ Win32File *pWin32File = (Win32File *)pFile; - LARGE_INTEGER largeInteger; /* The new offset */ + LARGE_INTEGER offset; - largeInteger.QuadPart = nSize; - if( !SetFilePointerEx(pWin32File->hFile, largeInteger, 0, FILE_BEGIN) ){ + offset.QuadPart = nSize; + if( !SetFilePointerEx(pWin32File->hFile, offset, 0, FILE_BEGIN) ){ return LSM_IOERR_BKPT; } if (!SetEndOfFile(pWin32File->hFile) ){ @@ -335,7 +335,7 @@ static int lsmWin32OsSync(lsm_file *pFile){ #ifndef LSM_NO_SYNC Win32File *pWin32File = (Win32File *)pFile; - if( pWin32File->pMap ){ + if( pWin32File->pMap!=NULL ){ if( !FlushViewOfFile(pWin32File->pMap, 0) ){ rc = LSM_IOERR_BKPT; } @@ -359,7 +359,61 @@ static int lsmWin32OsRemap( void **ppOut, lsm_i64 *pnOut ){ - return LSM_ERROR; + Win32File *pWin32File = (Win32File *)pFile; + + /* If the file is between 0 and 2MB in size, extend it in chunks of 256K. + ** Thereafter, in chunks of 1MB at a time. */ + const int aIncrSz[] = {256*1024, 1024*1024}; + int nIncrSz = aIncrSz[iMin>(2*1024*1024)]; + + if( pWin32File->pMap!=NULL ){ + UnmapViewOfFile(pWin32File->pMap); + *ppOut = pWin32File->pMap = NULL; + *pnOut = pWin32File->nMap = 0; + } + if( pWin32File->hMap!=NULL ){ + CloseHandle(pWin32File->hMap); + pWin32File->hMap = NULL; + } + if( iMin>=0 ){ + LARGE_INTEGER fileSize; + DWORD dwSizeHigh; + DWORD dwSizeLow; + HANDLE hMap; + LPVOID pMap; + memset(&fileSize, 0, sizeof(LARGE_INTEGER)); + if( !GetFileSizeEx(pWin32File->hFile, &fileSize) ){ + return LSM_IOERR_BKPT; + } + assert( fileSize.QuadPart>=0 ); + if( fileSize.QuadPart> 32); + hMap = CreateFileMappingW(pWin32File->hFile, NULL, PAGE_READWRITE, + dwSizeHigh, dwSizeLow, NULL); + if( hMap==NULL ){ + return LSM_IOERR_BKPT; + } + pWin32File->hMap = hMap; + assert( fileSize.QuadPart<=0xFFFFFFFF ); + pMap = MapViewOfFile(hMap, FILE_MAP_WRITE | FILE_MAP_READ, 0, 0, + (SIZE_T)fileSize.QuadPart); + if( pMap==NULL ){ + return LSM_IOERR_BKPT; + } + pWin32File->pMap = pMap; + pWin32File->nMap = (SIZE_T)fileSize.QuadPart; + } + *ppOut = pWin32File->pMap; + *pnOut = pWin32File->nMap; + return LSM_OK; } static BOOL win32IsDriveLetterAndColon( @@ -532,17 +586,23 @@ static int lsmWin32OsClose(lsm_file *pFile){ int nRetry = 0; Win32File *pWin32File = (Win32File *)pFile; lsmWin32OsShmUnmap(pFile, 0); - if( pWin32File->pMap ){ + if( pWin32File->pMap!=NULL ){ UnmapViewOfFile(pWin32File->pMap); - pWin32File->pMap = 0; + pWin32File->pMap = NULL; + pWin32File->nMap = 0; } if( pWin32File->hMap!=NULL ){ CloseHandle(pWin32File->hMap); pWin32File->hMap = NULL; } do{ + if( pWin32File->hFile==NULL ){ + rc = LSM_IOERR_BKPT; + break; + } rc = CloseHandle(pWin32File->hFile); if( rc ){ + pWin32File->hFile = NULL; rc = LSM_OK; break; } diff --git a/manifest b/manifest index af213f1571..6fee259a7a 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Implement\sxLock\sand\sxTestLock\sfor\slsm1\son\sWin32. -D 2017-06-28T21:36:40.949 +C Implement\sxRemap\sfor\slsm1\son\sWin32.\s\sAlso,\szero\sfile\shandle\swhen\sclosing\sit. +D 2017-06-29T00:20:42.289 F Makefile.in 1cc758ce3374a32425e4d130c2fe7b026b20de5b8843243de75f087c0a2661fb F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434 F Makefile.msc d389c6fb3344ea6887b386d56784a6d8a5c85107294448aeda50ac404285a1ef @@ -249,7 +249,7 @@ F ext/lsm1/lsm_tree.c 5d9fb2bc58a1a70c75126bd8d7198f7b627e165b F ext/lsm1/lsm_unix.c ee0201dff10ce2008ef13a65f52a6ea348f287e795270f651596f812fcfccdcc F ext/lsm1/lsm_varint.c b19ae9bd26b5a1e8402fb8a564b25d9542338a41 F ext/lsm1/lsm_vtab.c fff303ce03168eca9e333add3c1429b3471674b0 -F ext/lsm1/lsm_win32.c a1a3d712f8112a93899a2964cb09dbedcea28e352d81048632c72cf6a14eb2c9 +F ext/lsm1/lsm_win32.c 4896cd8af7a65dcd2b2dacca81455be896130b23ca19cf4a7e3f6eed5442d812 F ext/misc/README.md 8e008c8d2b02e09096b31dfba033253ac27c6c06a18aa5826e299fa7601d90b2 F ext/misc/amatch.c 6db4607cb17c54b853a2d7c7c36046d004853f65b9b733e6f019d543d5dfae87 F ext/misc/anycollseq.c 5ffdfde9829eeac52219136ad6aa7cd9a4edb3b15f4f2532de52f4a22525eddb @@ -1624,7 +1624,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P d0f6973d93c36451bf64f47de9a88abec613a624603033bf6717f5071139b6d2 -R 21110d45751e806c992b04ef05ac7e74 +P 9112117dad8085c385aa614cd982b307f5822761607ba358f34df7848c549134 +R 887ca9ca211e19ac7f1bf925dfe592df U mistachkin -Z e53e89fbbfaae4a22bd4502cd00f0ba9 +Z 2dc70b160b5c8095c4a09a655916e2c6 diff --git a/manifest.uuid b/manifest.uuid index 368f6f1ff2..63f7e3d038 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -9112117dad8085c385aa614cd982b307f5822761607ba358f34df7848c549134 \ No newline at end of file +93c9aa7d9aea46b331c53ff579ef704e88ce90f96600b69479a87a4bb4ca2a91 \ No newline at end of file