/ 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 Unified Diffs Ignore Whitespace Patch

Changes to ext/lsm1/lsm_win32.c.

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
...
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
...
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
...
355
356
357
358
359
360
361






















































362
363
364
365
366
367
368
369
...
528
529
530
531
532
533
534
535
536
537

538
539
540
541
542
543




544
545

546
547
548
549
550
551
552
  lsm_env *pEnv;                  /* The run-time environment */
  const char *zName;              /* Full path to file */

  HANDLE hFile;                   /* Open file handle */
  HANDLE hShmFile;                /* File handle for *-shm file */

  HANDLE hMap;                    /* File handle for mapping */
  void *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 */
};

static char *win32ShmFile(Win32File *p){
  char *zShm;
................................................................................
}

static int lsmWin32OsTruncate(
  lsm_file *pFile, /* File to write to */
  lsm_i64 nSize    /* Size to truncate file to */
){
  Win32File *pWin32File = (Win32File *)pFile;
  LARGE_INTEGER largeInteger; /* The new offset */

  largeInteger.QuadPart = nSize;
  if( !SetFilePointerEx(pWin32File->hFile, largeInteger, 0, FILE_BEGIN) ){
    return LSM_IOERR_BKPT;
  }
  if (!SetEndOfFile(pWin32File->hFile) ){
    return LSM_IOERR_BKPT;
  }
  return LSM_OK;
}
................................................................................

static int lsmWin32OsSync(lsm_file *pFile){
  int rc = LSM_OK;

#ifndef LSM_NO_SYNC
  Win32File *pWin32File = (Win32File *)pFile;

  if( pWin32File->pMap ){
    if( !FlushViewOfFile(pWin32File->pMap, 0) ){
      rc = LSM_IOERR_BKPT;
    }
  }
  if( rc==LSM_OK && !FlushFileBuffers(pWin32File->hFile) ){
    rc = LSM_IOERR_BKPT;
  }
................................................................................

static int lsmWin32OsRemap(
  lsm_file *pFile,
  lsm_i64 iMin,
  void **ppOut,
  lsm_i64 *pnOut
){






















































  return LSM_ERROR;
}

static BOOL win32IsDriveLetterAndColon(
  const char *zPathname
){
  return ( isalpha(zPathname[0]) && zPathname[1]==':' );
}
................................................................................

#define MX_CLOSE_ATTEMPT 3
static int lsmWin32OsClose(lsm_file *pFile){
  int rc;
  int nRetry = 0;
  Win32File *pWin32File = (Win32File *)pFile;
  lsmWin32OsShmUnmap(pFile, 0);
  if( pWin32File->pMap ){
    UnmapViewOfFile(pWin32File->pMap);
    pWin32File->pMap = 0;

  }
  if( pWin32File->hMap!=NULL ){
    CloseHandle(pWin32File->hMap);
    pWin32File->hMap = NULL;
  }
  do{




    rc = CloseHandle(pWin32File->hFile);
    if( rc ){

      rc = LSM_OK;
      break;
    }
    if( ++nRetry>=MX_CLOSE_ATTEMPT ){
      rc = LSM_IOERR_BKPT;
      break;
    }







|







 







|

|
|







 







|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|







 







|

|
>






>
>
>
>


>







35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
...
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
...
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
...
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
...
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
  lsm_env *pEnv;                  /* The run-time environment */
  const char *zName;              /* Full path to file */

  HANDLE hFile;                   /* Open file handle */
  HANDLE hShmFile;                /* File handle for *-shm file */

  HANDLE hMap;                    /* File handle for mapping */
  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 */
};

static char *win32ShmFile(Win32File *p){
  char *zShm;
................................................................................
}

static int lsmWin32OsTruncate(
  lsm_file *pFile, /* File to write to */
  lsm_i64 nSize    /* Size to truncate file to */
){
  Win32File *pWin32File = (Win32File *)pFile;
  LARGE_INTEGER offset;

  offset.QuadPart = nSize;
  if( !SetFilePointerEx(pWin32File->hFile, offset, 0, FILE_BEGIN) ){
    return LSM_IOERR_BKPT;
  }
  if (!SetEndOfFile(pWin32File->hFile) ){
    return LSM_IOERR_BKPT;
  }
  return LSM_OK;
}
................................................................................

static int lsmWin32OsSync(lsm_file *pFile){
  int rc = LSM_OK;

#ifndef LSM_NO_SYNC
  Win32File *pWin32File = (Win32File *)pFile;

  if( pWin32File->pMap!=NULL ){
    if( !FlushViewOfFile(pWin32File->pMap, 0) ){
      rc = LSM_IOERR_BKPT;
    }
  }
  if( rc==LSM_OK && !FlushFileBuffers(pWin32File->hFile) ){
    rc = LSM_IOERR_BKPT;
  }
................................................................................

static int lsmWin32OsRemap(
  lsm_file *pFile,
  lsm_i64 iMin,
  void **ppOut,
  lsm_i64 *pnOut
){
  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<iMin ){
      int rc;
      fileSize.QuadPart = ((iMin + nIncrSz-1) / nIncrSz) * nIncrSz;
      rc = lsmWin32OsTruncate(pFile, fileSize.QuadPart);
      if( rc!=LSM_OK ){
        return rc;
      }
    }
    dwSizeLow = (DWORD)(fileSize.QuadPart & 0xFFFFFFFF);
    dwSizeHigh = (DWORD)((fileSize.QuadPart & 0x7FFFFFFFFFFFFFFF) >> 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(
  const char *zPathname
){
  return ( isalpha(zPathname[0]) && zPathname[1]==':' );
}
................................................................................

#define MX_CLOSE_ATTEMPT 3
static int lsmWin32OsClose(lsm_file *pFile){
  int rc;
  int nRetry = 0;
  Win32File *pWin32File = (Win32File *)pFile;
  lsmWin32OsShmUnmap(pFile, 0);
  if( pWin32File->pMap!=NULL ){
    UnmapViewOfFile(pWin32File->pMap);
    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;
    }
    if( ++nRetry>=MX_CLOSE_ATTEMPT ){
      rc = LSM_IOERR_BKPT;
      break;
    }