/ Check-in [3e797d0f]
Login

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

Overview
Comment:Add internal interfaces: PageMalloc/PageFree and ScratchMalloc/ScratchFree. (CVS 5226)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 3e797d0ffe1af6f7512b3fb7b387418ac05671e0
User & Date: drh 2008-06-17 15:12:01
Context
2008-06-17
17:21
Change the mutex interface to be pluggable. This is an incremental checkin, there are still changes to come. (CVS 5227) check-in: 59728363 user: danielk1977 tags: trunk
15:12
Add internal interfaces: PageMalloc/PageFree and ScratchMalloc/ScratchFree. (CVS 5226) check-in: 3e797d0f user: drh tags: trunk
01:03
Do not allow a VACUUM to try to change the page_size on an encrypted database. (CVS 5225) check-in: d49e5020 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/btree.c.

     5      5   ** a legal notice, here is a blessing:
     6      6   **
     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12         -** $Id: btree.c,v 1.464 2008/06/15 02:51:47 drh Exp $
           12  +** $Id: btree.c,v 1.465 2008/06/17 15:12:01 drh Exp $
    13     13   **
    14     14   ** This file implements a external (disk-based) database using BTrees.
    15     15   ** See the header comment on "btreeInt.h" for additional information.
    16     16   ** Including a description of file format and an overview of operation.
    17     17   */
    18     18   #include "btreeInt.h"
    19     19   
................................................................................
  3715   3715         }else{
  3716   3716           int available;
  3717   3717           pCellKey = (void *)fetchPayload(pCur, &available, 0);
  3718   3718           nCellKey = pCur->info.nKey;
  3719   3719           if( available>=nCellKey ){
  3720   3720             c = sqlite3VdbeRecordCompare(nCellKey, pCellKey, pUnKey);
  3721   3721           }else{
  3722         -          pCellKey = sqlite3TempMalloc( nCellKey );
         3722  +          pCellKey = sqlite3Malloc( nCellKey );
  3723   3723             if( pCellKey==0 ){
  3724   3724               rc = SQLITE_NOMEM;
  3725   3725               goto moveto_finish;
  3726   3726             }
  3727   3727             rc = sqlite3BtreeKey(pCur, 0, nCellKey, (void *)pCellKey);
  3728   3728             c = sqlite3VdbeRecordCompare(nCellKey, pCellKey, pUnKey);
  3729         -          sqlite3TempFree(pCellKey);
         3729  +          sqlite3_free(pCellKey);
  3730   3730             if( rc ) goto moveto_finish;
  3731   3731           }
  3732   3732         }
  3733   3733         if( c==0 ){
  3734   3734           pCur->info.nKey = nCellKey;
  3735   3735           if( pPage->leafData && !pPage->leaf ){
  3736   3736             lwr = pCur->idx;
................................................................................
  4856   4856     int leafCorrection;          /* 4 if pPage is a leaf.  0 if not */
  4857   4857     int leafData;                /* True if pPage is a leaf of a LEAFDATA tree */
  4858   4858     int usableSpace;             /* Bytes in pPage beyond the header */
  4859   4859     int pageFlags;               /* Value of pPage->aData[0] */
  4860   4860     int subtotal;                /* Subtotal of bytes in cells on one page */
  4861   4861     int iSpace1 = 0;             /* First unused byte of aSpace1[] */
  4862   4862     int iSpace2 = 0;             /* First unused byte of aSpace2[] */
         4863  +  int szScratch;               /* Size of scratch memory requested */
  4863   4864     MemPage *apOld[NB];          /* pPage and up to two siblings */
  4864   4865     Pgno pgnoOld[NB];            /* Page numbers for each page in apOld[] */
  4865   4866     MemPage *apCopy[NB];         /* Private copies of apOld[] pages */
  4866   4867     MemPage *apNew[NB+2];        /* pPage and up to NB siblings after balancing */
  4867   4868     Pgno pgnoNew[NB+2];          /* Page numbers for each page in apNew[] */
  4868   4869     u8 *apDiv[NB];               /* Divider cells in pParent */
  4869   4870     int cntNew[NB+2];            /* Index in aCell[] of cell after i-th page */
................................................................................
  4986   4987     /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
  4987   4988     ** alignment */
  4988   4989     nMaxCells = (nMaxCells + 3)&~3;
  4989   4990   
  4990   4991     /*
  4991   4992     ** Allocate space for memory structures
  4992   4993     */
  4993         -  apCell = sqlite3TempMalloc( 
         4994  +  szScratch =
  4994   4995          nMaxCells*sizeof(u8*)                       /* apCell */
  4995   4996        + nMaxCells*sizeof(u16)                       /* szCell */
  4996   4997        + (ROUND8(sizeof(MemPage))+pBt->pageSize)*NB  /* aCopy */
  4997   4998        + pBt->pageSize                               /* aSpace1 */
  4998         -     + (ISAUTOVACUUM ? nMaxCells : 0)              /* aFrom */
  4999         -  );
         4999  +     + (ISAUTOVACUUM ? nMaxCells : 0);             /* aFrom */
         5000  +  apCell = sqlite3ScratchMalloc( szScratch ); 
  5000   5001     if( apCell==0 ){
  5001   5002       rc = SQLITE_NOMEM;
  5002   5003       goto balance_cleanup;
  5003   5004     }
  5004   5005     szCell = (u16*)&apCell[nMaxCells];
  5005   5006     aCopy[0] = (u8*)&szCell[nMaxCells];
  5006   5007     assert( ((aCopy[0] - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */
................................................................................
  5011   5012     aSpace1 = &aCopy[NB-1][pBt->pageSize+ROUND8(sizeof(MemPage))];
  5012   5013     assert( ((aSpace1 - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */
  5013   5014   #ifndef SQLITE_OMIT_AUTOVACUUM
  5014   5015     if( pBt->autoVacuum ){
  5015   5016       aFrom = &aSpace1[pBt->pageSize];
  5016   5017     }
  5017   5018   #endif
  5018         -  aSpace2 = sqlite3Malloc(pBt->pageSize);
         5019  +  aSpace2 = sqlite3PageMalloc(pBt->pageSize);
  5019   5020     if( aSpace2==0 ){
  5020   5021       rc = SQLITE_NOMEM;
  5021   5022       goto balance_cleanup;
  5022   5023     }
  5023   5024     
  5024   5025     /*
  5025   5026     ** Make copies of the content of pPage and its siblings into aOld[].
................................................................................
  5393   5394   
  5394   5395     /*
  5395   5396     ** Balance the parent page.  Note that the current page (pPage) might
  5396   5397     ** have been added to the freelist so it might no longer be initialized.
  5397   5398     ** But the parent page will always be initialized.
  5398   5399     */
  5399   5400     assert( pParent->isInit );
  5400         -  sqlite3TempFree(apCell);
         5401  +  sqlite3ScratchFree(apCell);
  5401   5402     apCell = 0;
  5402   5403     rc = balance(pParent, 0);
  5403   5404     
  5404   5405     /*
  5405   5406     ** Cleanup before returning.
  5406   5407     */
  5407   5408   balance_cleanup:
  5408         -  sqlite3_free(aSpace2);
  5409         -  sqlite3TempFree(apCell);
         5409  +  sqlite3PageFree(aSpace2);
         5410  +  sqlite3ScratchFree(apCell);
  5410   5411     for(i=0; i<nOld; i++){
  5411   5412       releasePage(apOld[i]);
  5412   5413     }
  5413   5414     for(i=0; i<nNew; i++){
  5414   5415       releasePage(apNew[i]);
  5415   5416     }
  5416   5417     releasePage(pParent);

Changes to src/malloc.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   **
    13     13   ** Memory allocation functions used throughout sqlite.
    14     14   **
    15         -** $Id: malloc.c,v 1.17 2008/06/15 02:51:48 drh Exp $
           15  +** $Id: malloc.c,v 1.18 2008/06/17 15:12:01 drh Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   #include <stdarg.h>
    19     19   #include <ctype.h>
    20     20   
    21     21   /*
    22     22   ** This routine runs when the memory allocator sees that the
................................................................................
    87     87   
    88     88     /*
    89     89     ** Performance statistics
    90     90     */
    91     91     sqlite3_int64 nowUsed;  /* Main memory currently in use */
    92     92     sqlite3_int64 mxUsed;   /* Highwater mark for nowUsed */
    93     93     int mxReq;              /* Max request size for ordinary mallocs */
    94         -  int mxTempReq;          /* Max request size for xTemp mallocs */
           94  +  int mxScratchReq;       /* Max request size for xTemp mallocs */
    95     95   } mem0;
    96     96   
    97     97   /*
    98     98   ** Initialize the memory allocation subsystem.
    99     99   */
   100    100   int sqlite3MallocInit(void){
   101    101     if( sqlite3Config.m.xMalloc==0 ){
................................................................................
   225    225     if( sqlite3_initialize() ) return 0;
   226    226   #endif
   227    227     return sqlite3Malloc(n);
   228    228   }
   229    229   
   230    230   /*
   231    231   ** Each thread may only have a single outstanding allocation from
   232         -** xTempMalloc().  We verify this constraint in the single-threaded
   233         -** case by setting tempAllocOut to 1 when an allocation
          232  +** xScratchMalloc().  We verify this constraint in the single-threaded
          233  +** case by setting scratchAllocOut to 1 when an allocation
   234    234   ** is outstanding clearing it when the allocation is freed.
   235    235   */
   236    236   #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
   237         -static int tempAllocOut = 0;
          237  +static int scratchAllocOut = 0;
   238    238   #endif
   239    239   
   240    240   
   241    241   /*
   242    242   ** Allocate memory that is to be used and released right away.
   243    243   ** This routine is similar to alloca() in that it is not intended
   244    244   ** for situations where the memory might be held long-term.  This
   245    245   ** routine is intended to get memory to old large transient data
   246    246   ** structures that would not normally fit on the stack of an
   247    247   ** embedded processor.
   248    248   */
   249         -void *sqlite3TempMalloc(int n){
          249  +void *sqlite3ScratchMalloc(int n){
   250    250     void *p;
   251    251     assert( n>0 );
   252    252     if( sqlite3FaultStep(SQLITE_FAULTINJECTOR_MALLOC) ){
   253    253       return 0;
   254    254     }
   255    255   #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
   256         -  assert( tempAllocOut==0 );
   257         -  tempAllocOut = 1;
          256  +  assert( scratchAllocOut==0 );
          257  +  scratchAllocOut = 1;
   258    258   #endif
   259    259     if( sqlite3Config.bMemstat ){
   260    260       sqlite3_mutex_enter(mem0.mutex);
   261         -    if( n>mem0.mxTempReq ) mem0.mxTempReq = n;
   262         -    p = sqlite3Config.m.xTempMalloc(n);
          261  +    if( n>mem0.mxScratchReq ) mem0.mxScratchReq = n;
          262  +    p = sqlite3Config.m.xMalloc(n);
   263    263       sqlite3_mutex_leave(mem0.mutex);
   264    264     }else{
   265         -    p = sqlite3Config.m.xTempMalloc(n);
          265  +    p = sqlite3Config.m.xMalloc(n);
   266    266     }
   267    267     return p;
   268    268   }
   269         -void sqlite3TempFree(void *p){
          269  +void sqlite3ScratchFree(void *p){
   270    270     if( p ){
   271    271   #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
   272         -    assert( tempAllocOut==1 );
   273         -    tempAllocOut = 0;
          272  +    assert( scratchAllocOut==1 );
          273  +    scratchAllocOut = 0;
   274    274   #endif
   275         -    sqlite3Config.m.xTempFree(p);
          275  +    sqlite3Config.m.xFree(p);
   276    276     }
   277    277   }
          278  +
          279  +/*
          280  +** Place holders for the page-cache memory allocator.
          281  +*/
          282  +void *sqlite3PageMalloc(int iSize){
          283  +  return sqlite3Malloc(iSize);
          284  +}
          285  +void sqlite3PageFree(void *pOld){
          286  +  sqlite3_free(pOld);
          287  +}
   278    288   
   279    289   /*
   280    290   ** Return the size of a memory allocation previously obtained from
   281    291   ** sqlite3Malloc() or sqlite3_malloc().
   282    292   */
   283    293   int sqlite3MallocSize(void *p){
   284    294     return sqlite3Config.m.xSize(p);

Changes to src/mem1.c.

    13     13   ** This file contains low-level memory allocation drivers for when
    14     14   ** SQLite will use the standard C-library malloc/realloc/free interface
    15     15   ** to obtain the memory it needs.
    16     16   **
    17     17   ** This file contains implementations of the low-level memory allocation
    18     18   ** routines specified in the sqlite3_mem_methods object.
    19     19   **
    20         -** $Id: mem1.c,v 1.20 2008/06/15 02:51:48 drh Exp $
           20  +** $Id: mem1.c,v 1.21 2008/06/17 15:12:01 drh Exp $
    21     21   */
    22     22   #include "sqliteInt.h"
    23     23   
    24     24   /*
    25     25   ** This version of the memory allocator is the default.  It is
    26     26   ** used when no other memory allocator is specified using compile-time
    27     27   ** macros.
................................................................................
   126    126   void sqlite3MemSetDefault(void){
   127    127     static const sqlite3_mem_methods defaultMethods = {
   128    128        sqlite3MemMalloc,
   129    129        sqlite3MemFree,
   130    130        sqlite3MemRealloc,
   131    131        sqlite3MemSize,
   132    132        sqlite3MemRoundup,
   133         -     sqlite3MemMalloc,
   134         -     sqlite3MemFree,
   135    133        sqlite3MemInit,
   136    134        sqlite3MemShutdown,
   137    135        0
   138    136     };
   139    137     sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
   140    138   }
   141    139   
   142    140   #endif /* SQLITE_SYSTEM_MALLOC */

Changes to src/mem2.c.

    15     15   ** to obtain the memory it needs while adding lots of additional debugging
    16     16   ** information to each allocation in order to help detect and fix memory
    17     17   ** leaks and memory usage errors.
    18     18   **
    19     19   ** This file contains implementations of the low-level memory allocation
    20     20   ** routines specified in the sqlite3_mem_methods object.
    21     21   **
    22         -** $Id: mem2.c,v 1.29 2008/06/15 02:51:48 drh Exp $
           22  +** $Id: mem2.c,v 1.30 2008/06/17 15:12:01 drh Exp $
    23     23   */
    24     24   #include "sqliteInt.h"
    25     25   
    26     26   /*
    27     27   ** This version of the memory allocator is used only if the
    28     28   ** SQLITE_MEMDEBUG macro is defined
    29     29   */
................................................................................
   308    308   void sqlite3MemSetDefault(void){
   309    309     static const sqlite3_mem_methods defaultMethods = {
   310    310        sqlite3MemMalloc,
   311    311        sqlite3MemFree,
   312    312        sqlite3MemRealloc,
   313    313        sqlite3MemSize,
   314    314        sqlite3MemRoundup,
   315         -     sqlite3MemMalloc,
   316         -     sqlite3MemFree,
   317    315        sqlite3MemInit,
   318    316        sqlite3MemShutdown,
   319    317        0
   320    318     };
   321    319     sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
   322    320   }
   323    321   

Changes to src/pager.c.

    14     14   ** The pager is used to access a database disk file.  It implements
    15     15   ** atomic commit and rollback through the use of a journal file that
    16     16   ** is separate from the database file.  The pager also implements file
    17     17   ** locking to prevent two processes from writing the same database
    18     18   ** file simultaneously, or one process from reading the database while
    19     19   ** another is writing.
    20     20   **
    21         -** @(#) $Id: pager.c,v 1.457 2008/06/15 02:51:48 drh Exp $
           21  +** @(#) $Id: pager.c,v 1.458 2008/06/17 15:12:01 drh Exp $
    22     22   */
    23     23   #ifndef SQLITE_OMIT_DISKIO
    24     24   #include "sqliteInt.h"
    25     25   #include <assert.h>
    26     26   #include <string.h>
    27     27   
    28     28   /*
................................................................................
   769    769   ** then the value returned is the size of the journal file when it
   770    770   ** contains rollback data for exactly one page.
   771    771   */
   772    772   #ifdef SQLITE_ENABLE_ATOMIC_WRITE
   773    773   static int jrnlBufferSize(Pager *pPager){
   774    774     int dc;           /* Device characteristics */
   775    775     int nSector;      /* Sector size */
   776         -  int nPage;        /* Page size */
          776  +  int szPage;        /* Page size */
   777    777     sqlite3_file *fd = pPager->fd;
   778    778   
   779    779     if( fd->pMethods ){
   780    780       dc = sqlite3OsDeviceCharacteristics(fd);
   781    781       nSector = sqlite3OsSectorSize(fd);
   782         -    nPage = pPager->pageSize;
          782  +    szPage = pPager->pageSize;
   783    783     }
   784    784   
   785    785     assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
   786    786     assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
   787    787   
   788         -  if( !fd->pMethods || (dc&(SQLITE_IOCAP_ATOMIC|(nPage>>8))&&nSector<=nPage) ){
          788  +  if( !fd->pMethods || 
          789  +       (dc & (SQLITE_IOCAP_ATOMIC|(szPage>>8)) && nSector<=szPage) ){
   789    790       return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
   790    791     }
   791    792     return 0;
   792    793   }
   793    794   #endif
   794    795   
   795    796   /*
................................................................................
  1290   1291     PgHdr *pPg, *pNext;
  1291   1292     if( pPager->errCode ) return;
  1292   1293     for(pPg=pPager->pAll; pPg; pPg=pNext){
  1293   1294       IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno));
  1294   1295       PAGER_INCR(sqlite3_pager_pgfree_count);
  1295   1296       pNext = pPg->pNextAll;
  1296   1297       lruListRemove(pPg);
  1297         -    sqlite3_free(pPg->pData);
         1298  +    sqlite3PageFree(pPg->pData);
  1298   1299       sqlite3_free(pPg);
  1299   1300     }
  1300   1301     assert(pPager->lru.pFirst==0);
  1301   1302     assert(pPager->lru.pFirstSynced==0);
  1302   1303     assert(pPager->lru.pLast==0);
  1303   1304     pPager->pStmt = 0;
  1304   1305     pPager->pAll = 0;
................................................................................
  2176   2177     int i;
  2177   2178     int tempFile = 0;
  2178   2179     int memDb = 0;
  2179   2180     int readOnly = 0;
  2180   2181     int useJournal = (flags & PAGER_OMIT_JOURNAL)==0;
  2181   2182     int noReadlock = (flags & PAGER_NO_READLOCK)!=0;
  2182   2183     int journalFileSize = sqlite3JournalSize(pVfs);
  2183         -  int nDefaultPage = SQLITE_DEFAULT_PAGE_SIZE;
         2184  +  int szPageDflt = SQLITE_DEFAULT_PAGE_SIZE;
  2184   2185     char *zPathname = 0;
  2185   2186     int nPathname = 0;
  2186   2187   
  2187   2188     /* The default return is a NULL pointer */
  2188   2189     *ppPager = 0;
  2189   2190   
  2190   2191     /* Compute and store the full pathname in an allocated buffer pointed
................................................................................
  2255   2256         **
  2256   2257         **    + SQLITE_DEFAULT_PAGE_SIZE,
  2257   2258         **    + The value returned by sqlite3OsSectorSize()
  2258   2259         **    + The largest page size that can be written atomically.
  2259   2260         */
  2260   2261         if( rc==SQLITE_OK && !readOnly ){
  2261   2262           int iSectorSize = sqlite3OsSectorSize(pPager->fd);
  2262         -        if( nDefaultPage<iSectorSize ){
  2263         -          nDefaultPage = iSectorSize;
         2263  +        if( szPageDflt<iSectorSize ){
         2264  +          szPageDflt = iSectorSize;
  2264   2265           }
  2265   2266   #ifdef SQLITE_ENABLE_ATOMIC_WRITE
  2266   2267           {
  2267   2268             int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
  2268   2269             int ii;
  2269   2270             assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
  2270   2271             assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
  2271   2272             assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
  2272         -          for(ii=nDefaultPage; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
  2273         -            if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ) nDefaultPage = ii;
         2273  +          for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
         2274  +            if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ) szPageDflt = ii;
  2274   2275             }
  2275   2276           }
  2276   2277   #endif
  2277         -        if( nDefaultPage>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
  2278         -          nDefaultPage = SQLITE_MAX_DEFAULT_PAGE_SIZE;
         2278  +        if( szPageDflt>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
         2279  +          szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
  2279   2280           }
  2280   2281         }
  2281   2282       }
  2282   2283     }else if( !memDb ){
  2283   2284       /* If a temporary file is requested, it is not opened immediately.
  2284   2285       ** In this case we accept the default page size and delay actually
  2285   2286       ** opening the file until the first call to OsWrite().
  2286   2287       */ 
  2287   2288       tempFile = 1;
  2288   2289       pPager->state = PAGER_EXCLUSIVE;
  2289   2290     }
  2290   2291   
  2291   2292     if( pPager && rc==SQLITE_OK ){
  2292         -    pPager->pTmpSpace = sqlite3MallocZero(nDefaultPage);
         2293  +    pPager->pTmpSpace = sqlite3PageMalloc(szPageDflt);
  2293   2294     }
  2294   2295   
  2295   2296     /* If an error occured in either of the blocks above.
  2296   2297     ** Free the Pager structure and close the file.
  2297   2298     ** Since the pager is not allocated there is no need to set 
  2298   2299     ** any Pager.errMask variables.
  2299   2300     */
................................................................................
  2322   2323     /* pPager->journalOpen = 0; */
  2323   2324     pPager->useJournal = useJournal && !memDb;
  2324   2325     pPager->noReadlock = noReadlock && readOnly;
  2325   2326     /* pPager->stmtOpen = 0; */
  2326   2327     /* pPager->stmtInUse = 0; */
  2327   2328     /* pPager->nRef = 0; */
  2328   2329     pPager->dbSize = memDb-1;
  2329         -  pPager->pageSize = nDefaultPage;
         2330  +  pPager->pageSize = szPageDflt;
  2330   2331     /* pPager->stmtSize = 0; */
  2331   2332     /* pPager->stmtJSize = 0; */
  2332   2333     /* pPager->nPage = 0; */
  2333   2334     pPager->mxPage = 100;
  2334   2335     pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
  2335   2336     /* pPager->state = PAGER_UNLOCK; */
  2336   2337     assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
................................................................................
  2417   2418   int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize){
  2418   2419     int rc = SQLITE_OK;
  2419   2420     u16 pageSize = *pPageSize;
  2420   2421     assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
  2421   2422     if( pageSize && pageSize!=pPager->pageSize 
  2422   2423      && !pPager->memDb && pPager->nRef==0 
  2423   2424     ){
  2424         -    char *pNew = (char *)sqlite3Malloc(pageSize);
         2425  +    char *pNew = (char *)sqlite3PageMalloc(pageSize);
  2425   2426       if( !pNew ){
  2426   2427         rc = SQLITE_NOMEM;
  2427   2428       }else{
  2428   2429         pagerEnter(pPager);
  2429   2430         pager_reset(pPager);
  2430   2431         pPager->pageSize = pageSize;
  2431   2432         setSectorSize(pPager);
  2432         -      sqlite3_free(pPager->pTmpSpace);
         2433  +      sqlite3PageFree(pPager->pTmpSpace);
  2433   2434         pPager->pTmpSpace = pNew;
  2434   2435         pagerLeave(pPager);
  2435   2436       }
  2436   2437     }
  2437   2438     *pPageSize = pPager->pageSize;
  2438   2439     return rc;
  2439   2440   }
................................................................................
  2563   2564   
  2564   2565   
  2565   2566   #ifndef SQLITE_OMIT_MEMORYDB
  2566   2567   /*
  2567   2568   ** Clear a PgHistory block
  2568   2569   */
  2569   2570   static void clearHistory(PgHistory *pHist){
  2570         -  sqlite3_free(pHist->pOrig);
  2571         -  sqlite3_free(pHist->pStmt);
         2571  +  sqlite3PageFree(pHist->pOrig);
         2572  +  sqlite3PageFree(pHist->pStmt);
  2572   2573     pHist->pOrig = 0;
  2573   2574     pHist->pStmt = 0;
  2574   2575   }
  2575   2576   #else
  2576   2577   #define clearHistory(x)
  2577   2578   #endif
  2578   2579   
................................................................................
  2649   2650         ppPg = &pPg->pNextAll;
  2650   2651       }else{
  2651   2652         *ppPg = pPg->pNextAll;
  2652   2653         IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno));
  2653   2654         PAGER_INCR(sqlite3_pager_pgfree_count);
  2654   2655         unlinkPage(pPg);
  2655   2656         makeClean(pPg);
  2656         -      sqlite3_free(pPg->pData);
         2657  +      sqlite3PageFree(pPg->pData);
  2657   2658         sqlite3_free(pPg);
  2658   2659         pPager->nPage--;
  2659   2660       }
  2660   2661     }
  2661   2662   }
  2662   2663   
  2663   2664   /*
................................................................................
  2786   2787     /* Temp files are automatically deleted by the OS
  2787   2788     ** if( pPager->tempFile ){
  2788   2789     **   sqlite3OsDelete(pPager->zFilename);
  2789   2790     ** }
  2790   2791     */
  2791   2792   
  2792   2793     sqlite3_free(pPager->aHash);
  2793         -  sqlite3_free(pPager->pTmpSpace);
         2794  +  sqlite3PageFree(pPager->pTmpSpace);
  2794   2795     sqlite3_free(pPager);
  2795   2796     return SQLITE_OK;
  2796   2797   }
  2797   2798   
  2798   2799   #if !defined(NDEBUG) || defined(SQLITE_TEST)
  2799   2800   /*
  2800   2801   ** Return the page number for the given page data.
................................................................................
  3343   3344         nReleased += (
  3344   3345             sizeof(*pPg) + pPager->pageSize
  3345   3346             + sizeof(u32) + pPager->nExtra
  3346   3347             + MEMDB*sizeof(PgHistory) 
  3347   3348         );
  3348   3349         IOTRACE(("PGFREE %p %d *\n", pPager, pPg->pgno));
  3349   3350         PAGER_INCR(sqlite3_pager_pgfree_count);
  3350         -      sqlite3_free(pPg->pData);
         3351  +      sqlite3PageFree(pPg->pData);
  3351   3352         sqlite3_free(pPg);
  3352   3353         pPager->nPage--;
  3353   3354       }else{
  3354   3355         /* An error occured whilst writing to the database file or 
  3355   3356         ** journal in pager_recycle(). The error is not returned to the 
  3356   3357         ** caller of this function. Instead, set the Pager.errCode variable.
  3357   3358         ** The error will be returned to the user (or users, in the case 
................................................................................
  3645   3646         }
  3646   3647       }
  3647   3648       pagerLeave(pPager);
  3648   3649       nByteHdr = sizeof(*pPg) + sizeof(u32) + pPager->nExtra
  3649   3650                 + MEMDB*sizeof(PgHistory);
  3650   3651       pPg = sqlite3Malloc( nByteHdr );
  3651   3652       if( pPg ){
  3652         -      pData = sqlite3Malloc( pPager->pageSize );
         3653  +      pData = sqlite3PageMalloc( pPager->pageSize );
  3653   3654         if( pData==0 ){
  3654   3655           sqlite3_free(pPg);
  3655   3656           pPg = 0;
  3656   3657         }
  3657   3658       }
  3658   3659       pagerEnter(pPager);
  3659   3660       if( pPg==0 ){
................................................................................
  4219   4220       */
  4220   4221       if( !pPg->inJournal && (pPager->journalOpen || MEMDB) ){
  4221   4222         if( (int)pPg->pgno <= pPager->origDbSize ){
  4222   4223           if( MEMDB ){
  4223   4224             PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
  4224   4225             PAGERTRACE3("JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
  4225   4226             assert( pHist->pOrig==0 );
  4226         -          pHist->pOrig = sqlite3Malloc( pPager->pageSize );
         4227  +          pHist->pOrig = sqlite3PageMalloc( pPager->pageSize );
  4227   4228             if( !pHist->pOrig ){
  4228   4229               return SQLITE_NOMEM;
  4229   4230             }
  4230   4231             memcpy(pHist->pOrig, PGHDR_TO_DATA(pPg), pPager->pageSize);
  4231   4232           }else{
  4232   4233             u32 cksum;
  4233   4234             char *pData2;
................................................................................
  4289   4290        && !pageInStatement(pPg) 
  4290   4291        && (int)pPg->pgno<=pPager->stmtSize 
  4291   4292       ){
  4292   4293         assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
  4293   4294         if( MEMDB ){
  4294   4295           PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
  4295   4296           assert( pHist->pStmt==0 );
  4296         -        pHist->pStmt = sqlite3Malloc( pPager->pageSize );
         4297  +        pHist->pStmt = sqlite3PageMalloc( pPager->pageSize );
  4297   4298           if( pHist->pStmt ){
  4298   4299             memcpy(pHist->pStmt, PGHDR_TO_DATA(pPg), pPager->pageSize);
  4299   4300           }
  4300   4301           PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
  4301   4302           page_add_to_stmt_list(pPg);
  4302   4303         }else{
  4303   4304           i64 offset = pPager->stmtNRec*(4+pPager->pageSize);
................................................................................
  5031   5032       }else{
  5032   5033         for(pPg=pPager->pStmt; pPg; pPg=pNext){
  5033   5034           PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
  5034   5035           pNext = pHist->pNextStmt;
  5035   5036           assert( pHist->inStmt );
  5036   5037           pHist->inStmt = 0;
  5037   5038           pHist->pPrevStmt = pHist->pNextStmt = 0;
  5038         -        sqlite3_free(pHist->pStmt);
         5039  +        sqlite3PageFree(pHist->pStmt);
  5039   5040           pHist->pStmt = 0;
  5040   5041         }
  5041   5042       }
  5042   5043       pPager->stmtNRec = 0;
  5043   5044       pPager->stmtInUse = 0;
  5044   5045       pPager->pStmt = 0;
  5045   5046     }
................................................................................
  5059   5060       if( MEMDB ){
  5060   5061         PgHdr *pPg;
  5061   5062         PgHistory *pHist;
  5062   5063         for(pPg=pPager->pStmt; pPg; pPg=pHist->pNextStmt){
  5063   5064           pHist = PGHDR_TO_HIST(pPg, pPager);
  5064   5065           if( pHist->pStmt ){
  5065   5066             memcpy(PGHDR_TO_DATA(pPg), pHist->pStmt, pPager->pageSize);
  5066         -          sqlite3_free(pHist->pStmt);
         5067  +          sqlite3PageFree(pHist->pStmt);
  5067   5068             pHist->pStmt = 0;
  5068   5069           }
  5069   5070         }
  5070   5071         pPager->dbSize = pPager->stmtSize;
  5071   5072         pager_truncate_cache(pPager);
  5072   5073         rc = SQLITE_OK;
  5073   5074       }else{

Changes to src/sqlite.h.in.

    26     26   ** on how SQLite interfaces are suppose to operate.
    27     27   **
    28     28   ** The name of this file under configuration management is "sqlite.h.in".
    29     29   ** The makefile makes some minor changes to this file (such as inserting
    30     30   ** the version number) and changes its name to "sqlite3.h" as
    31     31   ** part of the build process.
    32     32   **
    33         -** @(#) $Id: sqlite.h.in,v 1.328 2008/06/15 02:51:48 drh Exp $
           33  +** @(#) $Id: sqlite.h.in,v 1.329 2008/06/17 15:12:01 drh Exp $
    34     34   */
    35     35   #ifndef _SQLITE3_H_
    36     36   #define _SQLITE3_H_
    37     37   #include <stdarg.h>     /* Needed for the definition of va_list */
    38     38   
    39     39   /*
    40     40   ** Make sure we can call this stuff from C++.
................................................................................
   971    971   ** xSize should return the allocated size of a memory allocation
   972    972   ** previously obtained from xMalloc or xRealloc.  The allocated size
   973    973   ** is always at least as big as the requested size but may be larger.
   974    974   **
   975    975   ** The xRoundup method returns what would be the allocated size of
   976    976   ** a memory allocation given a particular requested size.  Most memory
   977    977   ** allocators round up memory allocations at least to the next multiple
   978         -** of 8.  Some round up to a larger multiple or to a power of 2. 
   979         -**
   980         -** The xTempMalloc and xTempFree methods are used to allocate a large
   981         -** chunk of temporary-use memory whose lifetime is a single procedure
   982         -** call.  These routines may be the same as xMalloc and xFree, if desired,
   983         -** though some specialized applications may benefit from using a different
   984         -** allocation algorithm in this case. 
   985         -** SQLite will never request more than one outstanding memory allocation
   986         -** per thread using xTempMalloc.
          978  +** of 8.  Some allocators round up to a larger multiple or to a power of 2. 
   987    979   **
   988    980   ** The xInit method initializes the memory allocator.  (For example,
   989    981   ** it might allocate any require mutexes or initialize internal data
   990    982   ** structures.  The xShutdown method is invoked (indirectly) by
   991    983   ** [sqlite3_shutdown()] and should deallocate any resources acquired
   992    984   ** by xInit.  The pAppData pointer is used as the only parameter to
   993    985   ** xInit and xShutdown.
................................................................................
   995    987   typedef struct sqlite3_mem_methods sqlite3_mem_methods;
   996    988   struct sqlite3_mem_methods {
   997    989     void *(*xMalloc)(int);         /* Memory allocation function */
   998    990     void (*xFree)(void*);          /* Free a prior allocation */
   999    991     void *(*xRealloc)(void*,int);  /* Resize an allocation */
  1000    992     int (*xSize)(void*);           /* Return the size of an allocation */
  1001    993     int (*xRoundup)(int);          /* Round up request size to allocation size */
  1002         -  void *(*xTempMalloc)(int);     /* Allocate temporary space */
  1003         -  void (*xTempFree)(void*);      /* Free space from xTempMalloc */
  1004    994     int (*xInit)(void*);           /* Initialize the memory allocator */
  1005    995     void (*xShutdown)(void*);      /* Deinitialize the memory allocator */
  1006    996     void *pAppData;                /* Argument to xInit() and xShutdown() */
  1007    997   };
  1008    998   
  1009    999   /*
  1010   1000   ** CAPI3REF: Configuration Options {F10160}

Changes to src/sqliteInt.h.

     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** Internal interface definitions for SQLite.
    13     13   **
    14         -** @(#) $Id: sqliteInt.h,v 1.710 2008/06/15 02:51:48 drh Exp $
           14  +** @(#) $Id: sqliteInt.h,v 1.711 2008/06/17 15:12:01 drh Exp $
    15     15   */
    16     16   #ifndef _SQLITEINT_H_
    17     17   #define _SQLITEINT_H_
    18     18   
    19     19   /*
    20     20   ** Include the configuration header output by 'configure' if we're using the
    21     21   ** autoconf-based build
................................................................................
  1787   1787   char *sqlite3StrNDup(const char*, int);
  1788   1788   char *sqlite3DbStrDup(sqlite3*,const char*);
  1789   1789   char *sqlite3DbStrNDup(sqlite3*,const char*, int);
  1790   1790   void *sqlite3Realloc(void*, int);
  1791   1791   void *sqlite3DbReallocOrFree(sqlite3 *, void *, int);
  1792   1792   void *sqlite3DbRealloc(sqlite3 *, void *, int);
  1793   1793   int sqlite3MallocSize(void *);
  1794         -void *sqlite3TempMalloc(int);
  1795         -void sqlite3TempFree(void*);
         1794  +void *sqlite3ScratchMalloc(int);
         1795  +void sqlite3ScratchFree(void*);
         1796  +void *sqlite3PageMalloc(int);
         1797  +void sqlite3PageFree(void*);
  1796   1798   void sqlite3MemSetDefault(void);
  1797   1799   
  1798   1800   int sqlite3IsNaN(double);
  1799   1801   
  1800   1802   char *sqlite3MPrintf(sqlite3*,const char*, ...);
  1801   1803   char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
  1802   1804   #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)