/ Check-in [93c9aa7d]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Implement xRemap for lsm1 on Win32. Also, zero file handle when closing it.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | lsm-vtab
Files: files | file ages | folders
SHA3-256: 93c9aa7d9aea46b331c53ff579ef704e88ce90f96600b69479a87a4bb4ca2a91
User & Date: mistachkin 2017-06-29 00:20:42
Context
2017-06-29
12:54
Implement xUnlink, xShmMap, and xShmUnmap for lsm1 on Win32. Closed-Leaf check-in: 680cc064 user: mistachkin tags: lsm-vtab
00:20
Implement xRemap for lsm1 on Win32. Also, zero file handle when closing it. check-in: 93c9aa7d user: mistachkin tags: lsm-vtab
2017-06-28
21:36
Implement xLock and xTestLock for lsm1 on Win32. check-in: 9112117d user: mistachkin tags: lsm-vtab
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to ext/lsm1/lsm_win32.c.

    35     35     lsm_env *pEnv;                  /* The run-time environment */
    36     36     const char *zName;              /* Full path to file */
    37     37   
    38     38     HANDLE hFile;                   /* Open file handle */
    39     39     HANDLE hShmFile;                /* File handle for *-shm file */
    40     40   
    41     41     HANDLE hMap;                    /* File handle for mapping */
    42         -  void *pMap;                     /* Pointer to mapping of file fd */
           42  +  LPVOID pMap;                    /* Pointer to mapping of file fd */
    43     43     size_t nMap;                    /* Size of mapping at pMap in bytes */
    44     44     int nShm;                       /* Number of entries in array apShm[] */
    45     45     void **apShm;                   /* Array of 32K shared memory segments */
    46     46   };
    47     47   
    48     48   static char *win32ShmFile(Win32File *p){
    49     49     char *zShm;
................................................................................
   287    287   }
   288    288   
   289    289   static int lsmWin32OsTruncate(
   290    290     lsm_file *pFile, /* File to write to */
   291    291     lsm_i64 nSize    /* Size to truncate file to */
   292    292   ){
   293    293     Win32File *pWin32File = (Win32File *)pFile;
   294         -  LARGE_INTEGER largeInteger; /* The new offset */
          294  +  LARGE_INTEGER offset;
   295    295   
   296         -  largeInteger.QuadPart = nSize;
   297         -  if( !SetFilePointerEx(pWin32File->hFile, largeInteger, 0, FILE_BEGIN) ){
          296  +  offset.QuadPart = nSize;
          297  +  if( !SetFilePointerEx(pWin32File->hFile, offset, 0, FILE_BEGIN) ){
   298    298       return LSM_IOERR_BKPT;
   299    299     }
   300    300     if (!SetEndOfFile(pWin32File->hFile) ){
   301    301       return LSM_IOERR_BKPT;
   302    302     }
   303    303     return LSM_OK;
   304    304   }
................................................................................
   331    331   
   332    332   static int lsmWin32OsSync(lsm_file *pFile){
   333    333     int rc = LSM_OK;
   334    334   
   335    335   #ifndef LSM_NO_SYNC
   336    336     Win32File *pWin32File = (Win32File *)pFile;
   337    337   
   338         -  if( pWin32File->pMap ){
          338  +  if( pWin32File->pMap!=NULL ){
   339    339       if( !FlushViewOfFile(pWin32File->pMap, 0) ){
   340    340         rc = LSM_IOERR_BKPT;
   341    341       }
   342    342     }
   343    343     if( rc==LSM_OK && !FlushFileBuffers(pWin32File->hFile) ){
   344    344       rc = LSM_IOERR_BKPT;
   345    345     }
................................................................................
   355    355   
   356    356   static int lsmWin32OsRemap(
   357    357     lsm_file *pFile,
   358    358     lsm_i64 iMin,
   359    359     void **ppOut,
   360    360     lsm_i64 *pnOut
   361    361   ){
   362         -  return LSM_ERROR;
          362  +  Win32File *pWin32File = (Win32File *)pFile;
          363  +
          364  +  /* If the file is between 0 and 2MB in size, extend it in chunks of 256K.
          365  +  ** Thereafter, in chunks of 1MB at a time.  */
          366  +  const int aIncrSz[] = {256*1024, 1024*1024};
          367  +  int nIncrSz = aIncrSz[iMin>(2*1024*1024)];
          368  +
          369  +  if( pWin32File->pMap!=NULL ){
          370  +    UnmapViewOfFile(pWin32File->pMap);
          371  +    *ppOut = pWin32File->pMap = NULL;
          372  +    *pnOut = pWin32File->nMap = 0;
          373  +  }
          374  +  if( pWin32File->hMap!=NULL ){
          375  +    CloseHandle(pWin32File->hMap);
          376  +    pWin32File->hMap = NULL;
          377  +  }
          378  +  if( iMin>=0 ){
          379  +    LARGE_INTEGER fileSize;
          380  +    DWORD dwSizeHigh;
          381  +    DWORD dwSizeLow;
          382  +    HANDLE hMap;
          383  +    LPVOID pMap;
          384  +    memset(&fileSize, 0, sizeof(LARGE_INTEGER));
          385  +    if( !GetFileSizeEx(pWin32File->hFile, &fileSize) ){
          386  +      return LSM_IOERR_BKPT;
          387  +    }
          388  +    assert( fileSize.QuadPart>=0 );
          389  +    if( fileSize.QuadPart<iMin ){
          390  +      int rc;
          391  +      fileSize.QuadPart = ((iMin + nIncrSz-1) / nIncrSz) * nIncrSz;
          392  +      rc = lsmWin32OsTruncate(pFile, fileSize.QuadPart);
          393  +      if( rc!=LSM_OK ){
          394  +        return rc;
          395  +      }
          396  +    }
          397  +    dwSizeLow = (DWORD)(fileSize.QuadPart & 0xFFFFFFFF);
          398  +    dwSizeHigh = (DWORD)((fileSize.QuadPart & 0x7FFFFFFFFFFFFFFF) >> 32);
          399  +    hMap = CreateFileMappingW(pWin32File->hFile, NULL, PAGE_READWRITE,
          400  +                              dwSizeHigh, dwSizeLow, NULL);
          401  +    if( hMap==NULL ){
          402  +      return LSM_IOERR_BKPT;
          403  +    }
          404  +    pWin32File->hMap = hMap;
          405  +    assert( fileSize.QuadPart<=0xFFFFFFFF );
          406  +    pMap = MapViewOfFile(hMap, FILE_MAP_WRITE | FILE_MAP_READ, 0, 0,
          407  +                         (SIZE_T)fileSize.QuadPart);
          408  +    if( pMap==NULL ){
          409  +      return LSM_IOERR_BKPT;
          410  +    }
          411  +    pWin32File->pMap = pMap;
          412  +    pWin32File->nMap = (SIZE_T)fileSize.QuadPart;
          413  +  }
          414  +  *ppOut = pWin32File->pMap;
          415  +  *pnOut = pWin32File->nMap;
          416  +  return LSM_OK;
   363    417   }
   364    418   
   365    419   static BOOL win32IsDriveLetterAndColon(
   366    420     const char *zPathname
   367    421   ){
   368    422     return ( isalpha(zPathname[0]) && zPathname[1]==':' );
   369    423   }
................................................................................
   528    582   
   529    583   #define MX_CLOSE_ATTEMPT 3
   530    584   static int lsmWin32OsClose(lsm_file *pFile){
   531    585     int rc;
   532    586     int nRetry = 0;
   533    587     Win32File *pWin32File = (Win32File *)pFile;
   534    588     lsmWin32OsShmUnmap(pFile, 0);
   535         -  if( pWin32File->pMap ){
          589  +  if( pWin32File->pMap!=NULL ){
   536    590       UnmapViewOfFile(pWin32File->pMap);
   537         -    pWin32File->pMap = 0;
          591  +    pWin32File->pMap = NULL;
          592  +    pWin32File->nMap = 0;
   538    593     }
   539    594     if( pWin32File->hMap!=NULL ){
   540    595       CloseHandle(pWin32File->hMap);
   541    596       pWin32File->hMap = NULL;
   542    597     }
   543    598     do{
          599  +    if( pWin32File->hFile==NULL ){
          600  +      rc = LSM_IOERR_BKPT;
          601  +      break;
          602  +    }
   544    603       rc = CloseHandle(pWin32File->hFile);
   545    604       if( rc ){
          605  +      pWin32File->hFile = NULL;
   546    606         rc = LSM_OK;
   547    607         break;
   548    608       }
   549    609       if( ++nRetry>=MX_CLOSE_ATTEMPT ){
   550    610         rc = LSM_IOERR_BKPT;
   551    611         break;
   552    612       }