/ Check-in [8776047b]
Login

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

Overview
Comment:Add munmap and mremap to the set of os interfaces that can be overloaded in os_unix.c.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | experimental-mmap
Files: files | file ages | folders
SHA1: 8776047bd776bbf266eb9c3b56683badb84ae73e
User & Date: drh 2013-03-25 20:50:25
Context
2013-03-25
23:09
Memory-mapped I/O is now on by default. The "PRAGMA mmap_limit(N)" can be used to issue a hint to the VFS to limit mmap space to N bytes. The VFS is free to ignore that hint if desired. However, if "PRAGMA mmap_limit(0)" is used, xFetch is never called. check-in: 1b37c4ef user: drh tags: experimental-mmap
20:50
Add munmap and mremap to the set of os interfaces that can be overloaded in os_unix.c. check-in: 8776047b user: drh tags: experimental-mmap
20:30
Use mremap() on Linux. check-in: 431aecc8 user: dan tags: experimental-mmap
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/os_unix.c.

   443    443   
   444    444     { "fchown",       (sqlite3_syscall_ptr)posixFchown,     0 },
   445    445   #define osFchown    ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)
   446    446   
   447    447     { "mmap",       (sqlite3_syscall_ptr)mmap,     0 },
   448    448   #define osMmap ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[21].pCurrent)
   449    449   
          450  +  { "munmap",       (sqlite3_syscall_ptr)munmap,          0 },
          451  +#define osMunmap ((void*(*)(void*,size_t))aSyscall[22].pCurrent)
          452  +
          453  +#if defined(__linux__) && defined(_GNU_SOURCE)
          454  +  { "mremap",       (sqlite3_syscall_ptr)mremap,          0 },
          455  +#else
          456  +  { "mremap",       (sqlite3_syscall_ptr)0,               0 },
          457  +#endif
          458  +#define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[23].pCurrent)
          459  +
   450    460   }; /* End of the overrideable system calls */
   451    461   
   452    462   /*
   453    463   ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
   454    464   ** "unix" VFSes.  Return SQLITE_OK opon successfully updating the
   455    465   ** system call pointer, or SQLITE_NOTFOUND if there is no configurable
   456    466   ** system call named zName.
................................................................................
  4001   4011     assert( unixMutexHeld() );
  4002   4012     if( p && p->nRef==0 ){
  4003   4013       int i;
  4004   4014       assert( p->pInode==pFd->pInode );
  4005   4015       sqlite3_mutex_free(p->mutex);
  4006   4016       for(i=0; i<p->nRegion; i++){
  4007   4017         if( p->h>=0 ){
  4008         -        munmap(p->apRegion[i], p->szRegion);
         4018  +        osMunmap(p->apRegion[i], p->szRegion);
  4009   4019         }else{
  4010   4020           sqlite3_free(p->apRegion[i]);
  4011   4021         }
  4012   4022       }
  4013   4023       sqlite3_free(p->apRegion);
  4014   4024       if( p->h>=0 ){
  4015   4025         robust_close(pFd, p->h, __LINE__);
................................................................................
  4274   4284         rc = SQLITE_IOERR_NOMEM;
  4275   4285         goto shmpage_out;
  4276   4286       }
  4277   4287       pShmNode->apRegion = apNew;
  4278   4288       while(pShmNode->nRegion<=iRegion){
  4279   4289         void *pMem;
  4280   4290         if( pShmNode->h>=0 ){
  4281         -        pMem = mmap(0, szRegion,
         4291  +        pMem = osMmap(0, szRegion,
  4282   4292               pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE, 
  4283   4293               MAP_SHARED, pShmNode->h, szRegion*(i64)pShmNode->nRegion
  4284   4294           );
  4285   4295           if( pMem==MAP_FAILED ){
  4286   4296             rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
  4287   4297             goto shmpage_out;
  4288   4298           }
................................................................................
  4497   4507   
  4498   4508   /*
  4499   4509   ** If it is currently memory mapped, unmap file pFd.
  4500   4510   */
  4501   4511   static void unixUnmapfile(unixFile *pFd){
  4502   4512     assert( pFd->nFetchOut==0 );
  4503   4513     if( pFd->pMapRegion ){
  4504         -    munmap(pFd->pMapRegion, pFd->mmapOrigsize);
         4514  +    osMunmap(pFd->pMapRegion, pFd->mmapOrigsize);
  4505   4515       pFd->pMapRegion = 0;
  4506   4516       pFd->mmapSize = 0;
  4507   4517       pFd->mmapOrigsize = 0;
  4508   4518     }
  4509   4519   }
  4510   4520   
  4511   4521   /*
................................................................................
  4544   4554     }
  4545   4555   
  4546   4556     if( nMap!=pFd->mmapSize ){
  4547   4557       void *pNew = 0;
  4548   4558   
  4549   4559   #if defined(__linux__) && defined(_GNU_SOURCE)
  4550   4560       if( pFd->pMapRegion && nMap>0 ){
  4551         -      pNew = mremap(pFd->pMapRegion, pFd->mmapOrigsize, nMap, MREMAP_MAYMOVE);
         4561  +      pNew = osMremap(pFd->pMapRegion, pFd->mmapOrigsize, nMap, MREMAP_MAYMOVE);
  4552   4562       }else
  4553   4563   #endif
  4554   4564       {
  4555   4565         unixUnmapfile(pFd);
  4556   4566         if( nMap>0 ){
  4557   4567           int flags = PROT_READ;
  4558   4568           if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE;
................................................................................
  7192   7202       UNIXVFS("unix-proxy",    proxyIoFinder ),
  7193   7203   #endif
  7194   7204     };
  7195   7205     unsigned int i;          /* Loop counter */
  7196   7206   
  7197   7207     /* Double-check that the aSyscall[] array has been constructed
  7198   7208     ** correctly.  See ticket [bb3a86e890c8e96ab] */
  7199         -  assert( ArraySize(aSyscall)==22 );
         7209  +  assert( ArraySize(aSyscall)==24 );
  7200   7210   
  7201   7211     /* Register all VFSes defined in the aVfs[] array */
  7202   7212     for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
  7203   7213       sqlite3_vfs_register(&aVfs[i], i==0);
  7204   7214     }
  7205   7215     return SQLITE_OK; 
  7206   7216   }

Changes to test/syscall.test.

    56     56   #-------------------------------------------------------------------------
    57     57   # Tests for the xNextSystemCall method.
    58     58   #
    59     59   foreach s {
    60     60       open close access getcwd stat fstat ftruncate
    61     61       fcntl read pread write pwrite fchmod fallocate
    62     62       pread64 pwrite64 unlink openDirectory mkdir rmdir 
    63         -    statvfs fchown umask mmap
           63  +    statvfs fchown umask mmap munmap mremap
    64     64   } {
    65     65     if {[test_syscall exists $s]} {lappend syscall_list $s}
    66     66   }
    67     67   do_test 3.1 { lsort [test_syscall list] } [lsort $syscall_list]
    68     68   
    69     69   #-------------------------------------------------------------------------
    70     70   # This test verifies that if a call to open() fails and errno is set to