/ Changes On Branch calloc
Login

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

Changes In Branch calloc Excluding Merge-Ins

This is equivalent to a diff from e416359633 to 0a33444105

2012-07-23
21:43
Refactor field names in the SelectDest object to make them distinct and easier to grep for. (check-in: b589f1efb3 user: drh tags: trunk)
19:25
Modify the code in vdbesort.c so that most reads and writes to temporary files are aligned page-sized blocks. (check-in: 55e47ef338 user: dan tags: sorter-coalesce-writes)
2012-07-21
19:40
Add an internal interface that allows the code to take advantage of multiple cores by pushing subcomputations off into separate threads. The interface is not currently used. (check-in: 0e4d977a4a user: drh tags: threads)
2012-07-19
20:27
Further tweaks to malloc paths. (Closed-Leaf check-in: 0a33444105 user: dan tags: calloc)
2012-07-18
16:07
Remove another branch from the DbMalloc() paths. (check-in: defd828e90 user: dan tags: calloc)
2012-07-17
19:32
Enhance the custom memory allocation interface to allow the user to specify a calloc() function. (check-in: 8752237d12 user: dan tags: calloc)
14:37
Ensure that there is always at least one aReadMark slot usable by an unprivileged reader while a checkpoint is running. Also, if one or more transactions are recovered from a log file, initialize one of the aReadMark slots to contain mxFrame as part of the recovery process. (check-in: e416359633 user: dan tags: trunk)
02:56
Amplification of a comment in wal.c. Change the aReadMark[] processing so that one read mark is left at zero when a WAL resets. (check-in: 8c9ee1d78f user: drh tags: trunk)

Changes to src/analyze.c.

   928    928     sqlite3_stmt *pStmt = 0;      /* An SQL statement being run */
   929    929     char *zSql;                   /* Text of the SQL statement */
   930    930     Index *pPrevIdx = 0;          /* Previous index in the loop */
   931    931     int idx = 0;                  /* slot in pIdx->aSample[] for next sample */
   932    932     int eType;                    /* Datatype of a sample */
   933    933     IndexSample *pSample;         /* A slot in pIdx->aSample[] */
   934    934   
   935         -  assert( db->lookaside.bEnabled==0 );
          935  +  assert( db->lookaside.sz==0 );
   936    936     if( !sqlite3FindTable(db, "sqlite_stat3", zDb) ){
   937    937       return SQLITE_OK;
   938    938     }
   939    939   
   940    940     zSql = sqlite3MPrintf(db, 
   941    941         "SELECT idx,count(*) FROM %Q.sqlite_stat3"
   942    942         " GROUP BY idx", zDb);
................................................................................
  1104   1104       sqlite3DbFree(db, zSql);
  1105   1105     }
  1106   1106   
  1107   1107   
  1108   1108     /* Load the statistics from the sqlite_stat3 table. */
  1109   1109   #ifdef SQLITE_ENABLE_STAT3
  1110   1110     if( rc==SQLITE_OK ){
  1111         -    int lookasideEnabled = db->lookaside.bEnabled;
  1112         -    db->lookaside.bEnabled = 0;
         1111  +    int lookasideSz = db->lookaside.sz;
         1112  +    db->lookaside.sz = 0;
  1113   1113       rc = loadStat3(db, sInfo.zDatabase);
  1114         -    db->lookaside.bEnabled = lookasideEnabled;
         1114  +    db->lookaside.sz = lookasideSz;
  1115   1115     }
  1116   1116   #endif
  1117   1117   
  1118   1118     if( rc==SQLITE_NOMEM ){
  1119   1119       db->mallocFailed = 1;
  1120   1120     }
  1121   1121     return rc;
  1122   1122   }
  1123   1123   
  1124   1124   
  1125   1125   #endif /* SQLITE_OMIT_ANALYZE */

Changes to src/build.c.

  1812   1812     ** to the elements of the FROM clause.  But we do not want these changes
  1813   1813     ** to be permanent.  So the computation is done on a copy of the SELECT
  1814   1814     ** statement that defines the view.
  1815   1815     */
  1816   1816     assert( pTable->pSelect );
  1817   1817     pSel = sqlite3SelectDup(db, pTable->pSelect, 0);
  1818   1818     if( pSel ){
  1819         -    u8 enableLookaside = db->lookaside.bEnabled;
         1819  +    u16 lookasideSz = db->lookaside.sz;
  1820   1820       n = pParse->nTab;
  1821   1821       sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
  1822   1822       pTable->nCol = -1;
  1823         -    db->lookaside.bEnabled = 0;
         1823  +    db->lookaside.sz = 0;
  1824   1824   #ifndef SQLITE_OMIT_AUTHORIZATION
  1825   1825       xAuth = db->xAuth;
  1826   1826       db->xAuth = 0;
  1827   1827       pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
  1828   1828       db->xAuth = xAuth;
  1829   1829   #else
  1830   1830       pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
  1831   1831   #endif
  1832         -    db->lookaside.bEnabled = enableLookaside;
         1832  +    db->lookaside.sz = lookasideSz;
  1833   1833       pParse->nTab = n;
  1834   1834       if( pSelTab ){
  1835   1835         assert( pTable->aCol==0 );
  1836   1836         pTable->nCol = pSelTab->nCol;
  1837   1837         pTable->aCol = pSelTab->aCol;
  1838   1838         pSelTab->nCol = 0;
  1839   1839         pSelTab->aCol = 0;

Changes to src/fkey.c.

   977    977     Trigger *pTrigger;              /* Trigger definition to return */
   978    978     int iAction = (pChanges!=0);    /* 1 for UPDATE, 0 for DELETE */
   979    979   
   980    980     action = pFKey->aAction[iAction];
   981    981     pTrigger = pFKey->apTrigger[iAction];
   982    982   
   983    983     if( action!=OE_None && !pTrigger ){
   984         -    u8 enableLookaside;           /* Copy of db->lookaside.bEnabled */
          984  +    u16 lookasideSz;              /* Copy of db->lookaside.sz */
   985    985       char const *zFrom;            /* Name of child table */
   986    986       int nFrom;                    /* Length in bytes of zFrom */
   987    987       Index *pIdx = 0;              /* Parent key index for this FK */
   988    988       int *aiCol = 0;               /* child table cols -> parent key cols */
   989    989       TriggerStep *pStep = 0;        /* First (only) step of trigger program */
   990    990       Expr *pWhere = 0;             /* WHERE clause of trigger step */
   991    991       ExprList *pList = 0;          /* Changes list if ON UPDATE CASCADE */
................................................................................
  1086   1086             pWhere,
  1087   1087             0, 0, 0, 0, 0, 0
  1088   1088         );
  1089   1089         pWhere = 0;
  1090   1090       }
  1091   1091   
  1092   1092       /* Disable lookaside memory allocation */
  1093         -    enableLookaside = db->lookaside.bEnabled;
  1094         -    db->lookaside.bEnabled = 0;
         1093  +    lookasideSz = db->lookaside.sz;
         1094  +    db->lookaside.sz = 0;
  1095   1095   
  1096   1096       pTrigger = (Trigger *)sqlite3DbMallocZero(db, 
  1097   1097           sizeof(Trigger) +         /* struct Trigger */
  1098   1098           sizeof(TriggerStep) +     /* Single step in trigger program */
  1099   1099           nFrom + 1                 /* Space for pStep->target.z */
  1100   1100       );
  1101   1101       if( pTrigger ){
................................................................................
  1110   1110         if( pWhen ){
  1111   1111           pWhen = sqlite3PExpr(pParse, TK_NOT, pWhen, 0, 0);
  1112   1112           pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
  1113   1113         }
  1114   1114       }
  1115   1115   
  1116   1116       /* Re-enable the lookaside buffer, if it was disabled earlier. */
  1117         -    db->lookaside.bEnabled = enableLookaside;
         1117  +    db->lookaside.sz = lookasideSz;
  1118   1118   
  1119   1119       sqlite3ExprDelete(db, pWhere);
  1120   1120       sqlite3ExprDelete(db, pWhen);
  1121   1121       sqlite3ExprListDelete(db, pList);
  1122   1122       sqlite3SelectDelete(db, pSelect);
  1123   1123       if( db->mallocFailed==1 ){
  1124   1124         fkTriggerDelete(db, pTrigger);

Changes to src/main.c.

   297    297     if( sqlite3GlobalConfig.isMutexInit ){
   298    298       sqlite3MutexEnd();
   299    299       sqlite3GlobalConfig.isMutexInit = 0;
   300    300     }
   301    301   
   302    302     return SQLITE_OK;
   303    303   }
          304  +
          305  +/*
          306  +** If a custom memory allocator is configured using the legacy 
          307  +** SQLITE_CONFIG_MALLOC interface, this function is used as the xCalloc()
          308  +** method.
          309  +*/
          310  +static void *syntheticCalloc(int nByte){
          311  +  void *pRet = sqlite3GlobalConfig.m.xMalloc(nByte);
          312  +  if( pRet ) memset(pRet, 0, nByte);
          313  +  return pRet;
          314  +}
   304    315   
   305    316   /*
   306    317   ** This API allows applications to modify the global configuration of
   307    318   ** the SQLite library at run-time.
   308    319   **
   309    320   ** This routine should only be called when there are no outstanding
   310    321   ** database connections or memory allocations.  This routine is not
................................................................................
   353    364       case SQLITE_CONFIG_GETMUTEX: {
   354    365         /* Retrieve the current mutex implementation */
   355    366         *va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex;
   356    367         break;
   357    368       }
   358    369   #endif
   359    370   
   360         -
   361    371       case SQLITE_CONFIG_MALLOC: {
          372  +      /* Specify an alternative malloc implementation */
          373  +      int nCopy = offsetof(sqlite3_mem_methods, xCalloc);
          374  +      memcpy(&sqlite3GlobalConfig.m, va_arg(ap, sqlite3_mem_methods*), nCopy);
          375  +      sqlite3GlobalConfig.m.xCalloc = syntheticCalloc;
          376  +      break;
          377  +    }
          378  +    case SQLITE_CONFIG_GETMALLOC: {
          379  +      /* Retrieve the current malloc() implementation */
          380  +      int nCopy = offsetof(sqlite3_mem_methods, xCalloc);
          381  +      if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault();
          382  +      memcpy(va_arg(ap, sqlite3_mem_methods*), &sqlite3GlobalConfig.m, nCopy);
          383  +      break;
          384  +    }
          385  +
          386  +    case SQLITE_CONFIG_MALLOC2: {
   362    387         /* Specify an alternative malloc implementation */
   363    388         sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*);
   364    389         break;
   365    390       }
   366         -    case SQLITE_CONFIG_GETMALLOC: {
          391  +    case SQLITE_CONFIG_GETMALLOC2: {
   367    392         /* Retrieve the current malloc() implementation */
   368    393         if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault();
   369    394         *va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m;
   370    395         break;
   371    396       }
          397  +
   372    398       case SQLITE_CONFIG_MEMSTATUS: {
   373    399         /* Enable or disable the malloc status collection */
   374    400         sqlite3GlobalConfig.bMemstat = va_arg(ap, int);
   375    401         break;
   376    402       }
   377    403       case SQLITE_CONFIG_SCRATCH: {
   378    404         /* Designate a buffer for scratch memory space */
................................................................................
   522    548       sqlite3EndBenignMalloc();
   523    549       if( pStart ) cnt = sqlite3MallocSize(pStart)/sz;
   524    550     }else{
   525    551       pStart = pBuf;
   526    552     }
   527    553     db->lookaside.pStart = pStart;
   528    554     db->lookaside.pFree = 0;
   529         -  db->lookaside.sz = (u16)sz;
          555  +  db->lookaside.szEnabled = db->lookaside.sz = (u16)sz;
   530    556     if( pStart ){
   531    557       int i;
   532    558       LookasideSlot *p;
   533    559       assert( sz > (int)sizeof(LookasideSlot*) );
   534    560       p = (LookasideSlot*)pStart;
   535    561       for(i=cnt-1; i>=0; i--){
   536    562         p->pNext = db->lookaside.pFree;
   537    563         db->lookaside.pFree = p;
   538    564         p = (LookasideSlot*)&((u8*)p)[sz];
   539    565       }
   540    566       db->lookaside.pEnd = p;
   541         -    db->lookaside.bEnabled = 1;
   542    567       db->lookaside.bMalloced = pBuf==0 ?1:0;
          568  +    assert( db->lookaside.sz>0 );
   543    569     }else{
   544    570       db->lookaside.pEnd = 0;
   545         -    db->lookaside.bEnabled = 0;
   546    571       db->lookaside.bMalloced = 0;
          572  +    db->lookaside.sz = 0;
   547    573     }
   548    574     return SQLITE_OK;
   549    575   }
   550    576   
   551    577   /*
   552    578   ** Return the mutex associated with a database connection.
   553    579   */

Changes to src/malloc.c.

   255    255     mem0.alarmArg = pArg;
   256    256   }
   257    257   
   258    258   /*
   259    259   ** Do a memory allocation with statistics and alarms.  Assume the
   260    260   ** lock is already held.
   261    261   */
   262         -static int mallocWithAlarm(int n, void **pp){
   263         -  int nFull;
   264         -  void *p;
          262  +static void *mallocWithAlarm(
          263  +  void *(*xAlloc)(int),          /* Memory allocation function */
          264  +  int n                          /* Bytes of memory to allocate */
          265  +){
          266  +  void *p;                       /* Pointer to allocated memory */
          267  +  int nFull;                     /* Value returned by xRoundup (if required) */
          268  +
   265    269     assert( sqlite3_mutex_held(mem0.mutex) );
   266         -  nFull = sqlite3GlobalConfig.m.xRoundup(n);
          270  +  assert( xAlloc==sqlite3GlobalConfig.m.xMalloc 
          271  +       || xAlloc==sqlite3GlobalConfig.m.xCalloc 
          272  +  );
          273  +
          274  +  /* Note: At one point this function would call xRoundup() and use the
          275  +  ** resulting value as the argument to xAlloc(). However, this is not
          276  +  ** required. And it is a (small) drag on performance.  */
          277  +  /* nFull = sqlite3GlobalConfig.m.xRoundup(n); */
          278  +
   267    279     sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
   268         -  if( mem0.alarmCallback!=0 ){
          280  +  if( mem0.alarmCallback ){
   269    281       int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
          282  +    nFull = sqlite3GlobalConfig.m.xRoundup(n);
   270    283       if( nUsed >= mem0.alarmThreshold - nFull ){
   271    284         mem0.nearlyFull = 1;
   272    285         sqlite3MallocAlarm(nFull);
   273    286       }else{
   274    287         mem0.nearlyFull = 0;
   275    288       }
   276    289     }
   277         -  p = sqlite3GlobalConfig.m.xMalloc(nFull);
          290  +
          291  +  p = xAlloc(n);
   278    292   #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
   279    293     if( p==0 && mem0.alarmCallback ){
   280    294       sqlite3MallocAlarm(nFull);
   281         -    p = sqlite3GlobalConfig.m.xMalloc(nFull);
          295  +    p = xAlloc(nFull);
   282    296     }
   283    297   #endif
   284    298     if( p ){
   285         -    nFull = sqlite3MallocSize(p);
   286         -    sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
          299  +    sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p));
   287    300       sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, 1);
          301  +  }else{
          302  +    testcase( sqlite3GlobalConfig.xLog!=0 );
          303  +    sqlite3_log(SQLITE_NOMEM, "failed to allocate %d bytes of memory", n);
   288    304     }
   289         -  *pp = p;
   290         -  return nFull;
          305  +
          306  +  return p;
   291    307   }
   292    308   
   293    309   /*
   294         -** Allocate memory.  This routine is like sqlite3_malloc() except that it
   295         -** assumes the memory subsystem has already been initialized.
          310  +** Use allocator function xAlloc to allocate n bytes of memory.
   296    311   */
   297         -void *sqlite3Malloc(int n){
          312  +static void *memAllocate(
          313  +  sqlite3 *db,
          314  +  void *(*xAlloc)(int),           /* Memory allocation function */
          315  +  int n                           /* Bytes of space to allocate */
          316  +){
   298    317     void *p;
   299    318     if( n<=0               /* IMP: R-65312-04917 */ 
   300    319      || n>=0x7fffff00
   301    320     ){
   302    321       /* A memory allocation of a number of bytes which is near the maximum
   303    322       ** signed integer value might cause an integer overflow inside of the
   304    323       ** xMalloc().  Hence we limit the maximum size to 0x7fffff00, giving
   305    324       ** 255 bytes of overhead.  SQLite itself will never use anything near
   306    325       ** this amount.  The only way to reach the limit is with sqlite3_malloc() */
   307    326       p = 0;
   308         -  }else if( sqlite3GlobalConfig.bMemstat ){
   309         -    sqlite3_mutex_enter(mem0.mutex);
   310         -    mallocWithAlarm(n, &p);
   311         -    sqlite3_mutex_leave(mem0.mutex);
   312    327     }else{
   313         -    p = sqlite3GlobalConfig.m.xMalloc(n);
          328  +    if( sqlite3GlobalConfig.bMemstat ){
          329  +      sqlite3_mutex_enter(mem0.mutex);
          330  +      p = mallocWithAlarm(xAlloc, n);
          331  +      sqlite3_mutex_leave(mem0.mutex);
          332  +    }else{
          333  +      p = xAlloc(n);
          334  +    }
          335  +    if( !p && db ) db->mallocFailed = 1;
   314    336     }
          337  +
   315    338     assert( EIGHT_BYTE_ALIGNMENT(p) );  /* IMP: R-04675-44850 */
          339  +  if( db ){
          340  +    sqlite3MemdebugSetType(p, MEMTYPE_DB | 
          341  +        ((db && db->lookaside.sz) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP)
          342  +    );
          343  +  }
   316    344     return p;
   317    345   }
   318    346   
          347  +
          348  +/*
          349  +** Allocate memory.  This routine is like sqlite3_malloc() except that it
          350  +** assumes the memory subsystem has already been initialized.
          351  +*/
          352  +void *sqlite3Malloc(int n){
          353  +  return memAllocate(0, sqlite3GlobalConfig.m.xMalloc, n);
          354  +}
          355  +
          356  +/*
          357  +** Allocate and zero memory.
          358  +*/ 
          359  +void *sqlite3MallocZero(int n){
          360  +  return memAllocate(0, sqlite3GlobalConfig.m.xCalloc, n);
          361  +}
          362  +
   319    363   /*
   320    364   ** This version of the memory allocation is for use by the application.
   321    365   ** First make sure the memory subsystem is initialized, then do the
   322    366   ** allocation.
   323    367   */
   324    368   void *sqlite3_malloc(int n){
   325    369   #ifndef SQLITE_OMIT_AUTOINIT
................................................................................
   358    402       mem0.nScratchFree--;
   359    403       sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
   360    404       sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
   361    405       sqlite3_mutex_leave(mem0.mutex);
   362    406     }else{
   363    407       if( sqlite3GlobalConfig.bMemstat ){
   364    408         sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
   365         -      n = mallocWithAlarm(n, &p);
   366         -      if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
          409  +      p = mallocWithAlarm(sqlite3GlobalConfig.m.xMalloc, n);
          410  +      if( p ){
          411  +        n = sqlite3MallocSize(p);
          412  +        sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
          413  +      }
   367    414         sqlite3_mutex_leave(mem0.mutex);
   368    415       }else{
   369    416         sqlite3_mutex_leave(mem0.mutex);
   370    417         p = sqlite3GlobalConfig.m.xMalloc(n);
   371    418       }
   372    419       sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
   373    420     }
................................................................................
   566    613   void *sqlite3_realloc(void *pOld, int n){
   567    614   #ifndef SQLITE_OMIT_AUTOINIT
   568    615     if( sqlite3_initialize() ) return 0;
   569    616   #endif
   570    617     return sqlite3Realloc(pOld, n);
   571    618   }
   572    619   
   573         -
   574    620   /*
   575         -** Allocate and zero memory.
   576         -*/ 
   577         -void *sqlite3MallocZero(int n){
   578         -  void *p = sqlite3Malloc(n);
   579         -  if( p ){
   580         -    memset(p, 0, n);
   581         -  }
   582         -  return p;
   583         -}
   584         -
   585         -/*
   586         -** Allocate and zero memory.  If the allocation fails, make
   587         -** the mallocFailed flag in the connection pointer.
          621  +** Attempt to allocate an n byte block from the lookaside buffer of
          622  +** connection db. If successful, return a pointer to the new allocation.
          623  +** Otherwise, return a NULL pointer.
   588    624   */
   589         -void *sqlite3DbMallocZero(sqlite3 *db, int n){
   590         -  void *p = sqlite3DbMallocRaw(db, n);
   591         -  if( p ){
   592         -    memset(p, 0, n);
          625  +#ifndef SQLITE_OMIT_LOOKASIDE
          626  +static void *lookasideAlloc(sqlite3 *db, int n){
          627  +  assert( db->lookaside.sz==0 || db->lookaside.sz==db->lookaside.szEnabled );
          628  +  if( n>db->lookaside.sz ){
          629  +    /* If db->lookaside.sz is 0, then the lookaside buffer is currently
          630  +    ** disabled. In this case do not increment the "size misses" stat.  */
          631  +    if( db->lookaside.sz ) db->lookaside.anStat[1]++;
          632  +  }else{
          633  +    LookasideSlot *pBuf;
          634  +    if( (pBuf = db->lookaside.pFree)==0 ){
          635  +      db->lookaside.anStat[2]++;
          636  +    }else{
          637  +      db->lookaside.pFree = pBuf->pNext;
          638  +      db->lookaside.nOut++;
          639  +      db->lookaside.anStat[0]++;
          640  +      if( db->lookaside.nOut>db->lookaside.mxOut ){
          641  +        db->lookaside.mxOut = db->lookaside.nOut;
          642  +      }
          643  +    }
          644  +    sqlite3MemdebugSetType((void *)pBuf, MEMTYPE_DB | MEMTYPE_LOOKASIDE);
          645  +    return (void*)pBuf;
   593    646     }
   594         -  return p;
          647  +  return 0;
   595    648   }
          649  +#else
          650  +# define lookasideAlloc(x,y) 0
          651  +#endif
   596    652   
   597    653   /*
   598         -** Allocate and zero memory.  If the allocation fails, make
   599         -** the mallocFailed flag in the connection pointer.
          654  +** Allocate and zero memory. If the allocation fails, set the 
          655  +** mallocFailed flag in the connection pointer.
   600    656   **
   601    657   ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
   602    658   ** failure on the same database connection) then always return 0.
   603    659   ** Hence for a particular database connection, once malloc starts
   604    660   ** failing, it fails consistently until mallocFailed is reset.
   605    661   ** This is an important assumption.  There are many places in the
   606    662   ** code that do things like this:
................................................................................
   608    664   **         int *a = (int*)sqlite3DbMallocRaw(db, 100);
   609    665   **         int *b = (int*)sqlite3DbMallocRaw(db, 200);
   610    666   **         if( b ) a[10] = 9;
   611    667   **
   612    668   ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
   613    669   ** that all prior mallocs (ex: "a") worked too.
   614    670   */
          671  +void *sqlite3DbMallocZero(sqlite3 *db, int n){
          672  +  void *p;
          673  +  if( db ){
          674  +    if( db->mallocFailed ) return 0;
          675  +    if( (p = lookasideAlloc(db, n)) ){
          676  +      memset(p, 0, n);
          677  +      return p;
          678  +    }
          679  +  }
          680  +
          681  +  return memAllocate(db, sqlite3GlobalConfig.m.xCalloc, n);
          682  +}
          683  +
          684  +/*
          685  +** Allocate memory. If the allocation fails, make the mallocFailed 
          686  +** flag in the connection pointer.
          687  +**
          688  +** If db!=0 and db->mallocFailed is true (indicating a prior malloc
          689  +** failure on the same database connection) then always return 0.
          690  +** See also comments above sqlite3DbMallocZero() for details.
          691  +*/
   615    692   void *sqlite3DbMallocRaw(sqlite3 *db, int n){
   616    693     void *p;
   617         -  assert( db==0 || sqlite3_mutex_held(db->mutex) );
   618         -  assert( db==0 || db->pnBytesFreed==0 );
   619         -#ifndef SQLITE_OMIT_LOOKASIDE
   620    694     if( db ){
   621         -    LookasideSlot *pBuf;
   622         -    if( db->mallocFailed ){
   623         -      return 0;
   624         -    }
   625         -    if( db->lookaside.bEnabled ){
   626         -      if( n>db->lookaside.sz ){
   627         -        db->lookaside.anStat[1]++;
   628         -      }else if( (pBuf = db->lookaside.pFree)==0 ){
   629         -        db->lookaside.anStat[2]++;
   630         -      }else{
   631         -        db->lookaside.pFree = pBuf->pNext;
   632         -        db->lookaside.nOut++;
   633         -        db->lookaside.anStat[0]++;
   634         -        if( db->lookaside.nOut>db->lookaside.mxOut ){
   635         -          db->lookaside.mxOut = db->lookaside.nOut;
   636         -        }
   637         -        return (void*)pBuf;
   638         -      }
          695  +    if( db->mallocFailed ) return 0;
          696  +    if( (p = lookasideAlloc(db, n)) ){
          697  +      return p;
   639    698       }
   640    699     }
   641         -#else
   642         -  if( db && db->mallocFailed ){
   643         -    return 0;
   644         -  }
   645         -#endif
   646         -  p = sqlite3Malloc(n);
   647         -  if( !p && db ){
   648         -    db->mallocFailed = 1;
   649         -  }
   650         -  sqlite3MemdebugSetType(p, MEMTYPE_DB |
   651         -         ((db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
   652         -  return p;
          700  +
          701  +  return memAllocate(db, sqlite3GlobalConfig.m.xMalloc, n);
   653    702   }
   654    703   
   655    704   /*
   656    705   ** Resize the block of memory pointed to by p to n bytes. If the
   657    706   ** resize fails, set the mallocFailed flag in the connection object.
   658    707   */
   659    708   void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
................................................................................
   661    710     assert( db!=0 );
   662    711     assert( sqlite3_mutex_held(db->mutex) );
   663    712     if( db->mallocFailed==0 ){
   664    713       if( p==0 ){
   665    714         return sqlite3DbMallocRaw(db, n);
   666    715       }
   667    716       if( isLookaside(db, p) ){
   668         -      if( n<=db->lookaside.sz ){
          717  +      if( n<=db->lookaside.szEnabled ){
   669    718           return p;
   670    719         }
   671    720         pNew = sqlite3DbMallocRaw(db, n);
   672    721         if( pNew ){
   673         -        memcpy(pNew, p, db->lookaside.sz);
          722  +        memcpy(pNew, p, db->lookaside.szEnabled);
   674    723           sqlite3DbFree(db, p);
   675    724         }
   676    725       }else{
   677    726         assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
   678    727         assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
   679    728         sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
   680    729         pNew = sqlite3_realloc(p, n);
   681    730         if( !pNew ){
   682    731           sqlite3MemdebugSetType(p, MEMTYPE_DB|MEMTYPE_HEAP);
   683    732           db->mallocFailed = 1;
   684    733         }
   685    734         sqlite3MemdebugSetType(pNew, MEMTYPE_DB | 
   686         -            (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
          735  +            (db->lookaside.sz ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
   687    736       }
   688    737     }
   689    738     return pNew;
   690    739   }
   691    740   
   692    741   /*
   693    742   ** Attempt to reallocate p.  If the reallocation fails, then free p

Changes to src/mem1.c.

    66     66   ** SQLITE_WITHOUT_ZONEMALLOC symbol is defined.
    67     67   */
    68     68   #include <sys/sysctl.h>
    69     69   #include <malloc/malloc.h>
    70     70   #include <libkern/OSAtomic.h>
    71     71   static malloc_zone_t* _sqliteZone_;
    72     72   #define SQLITE_MALLOC(x) malloc_zone_malloc(_sqliteZone_, (x))
           73  +#define SQLITE_CALLOC(x) malloc_zone_calloc(_sqliteZone_, (x), 1)
    73     74   #define SQLITE_FREE(x) malloc_zone_free(_sqliteZone_, (x));
    74     75   #define SQLITE_REALLOC(x,y) malloc_zone_realloc(_sqliteZone_, (x), (y))
    75     76   #define SQLITE_MALLOCSIZE(x) \
    76     77           (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x))
    77     78   
    78     79   #else /* if not __APPLE__ */
    79     80   
    80     81   /*
    81     82   ** Use standard C library malloc and free on non-Apple systems.  
    82     83   ** Also used by Apple systems if SQLITE_WITHOUT_ZONEMALLOC is defined.
    83     84   */
    84     85   #define SQLITE_MALLOC(x)    malloc(x)
           86  +#define SQLITE_CALLOC(x)    calloc((x), 1)
    85     87   #define SQLITE_FREE(x)      free(x)
    86     88   #define SQLITE_REALLOC(x,y) realloc((x),(y))
    87     89   
    88     90   #if (defined(_MSC_VER) && !defined(SQLITE_WITHOUT_MSIZE)) \
    89     91         || (defined(HAVE_MALLOC_H) && defined(HAVE_MALLOC_USABLE_SIZE))
    90     92   # include <malloc.h>    /* Needed for malloc_usable_size on linux */
    91     93   #endif
................................................................................
    96     98   #else
    97     99   # undef SQLITE_MALLOCSIZE
    98    100   #endif
    99    101   
   100    102   #endif /* __APPLE__ or not __APPLE__ */
   101    103   
   102    104   /*
   103         -** Like malloc(), but remember the size of the allocation
   104         -** so that we can find it later using sqlite3MemSize().
          105  +** Allocate nByte bytes of memory.
   105    106   **
   106    107   ** For this low-level routine, we are guaranteed that nByte>0 because
   107    108   ** cases of nByte<=0 will be intercepted and dealt with by higher level
   108    109   ** routines.
   109    110   */
   110    111   static void *sqlite3MemMalloc(int nByte){
   111    112   #ifdef SQLITE_MALLOCSIZE
   112         -  void *p = SQLITE_MALLOC( nByte );
   113         -  if( p==0 ){
   114         -    testcase( sqlite3GlobalConfig.xLog!=0 );
   115         -    sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
   116         -  }
   117         -  return p;
          113  +  return SQLITE_MALLOC( ROUND8(nByte) );
          114  +#else
          115  +  i64 *p;
          116  +  nByte = ROUND8(nByte);
          117  +  p = SQLITE_MALLOC(nByte+8);
          118  +  if( p ) *(p++) = (i64)nByte;
          119  +  return (void *)p;
          120  +#endif
          121  +}
          122  +
          123  +/*
          124  +** Allocate and zero nByte bytes of memory.
          125  +**
          126  +** For this low-level routine, we are guaranteed that nByte>0 because
          127  +** cases of nByte<=0 will be intercepted and dealt with by higher level
          128  +** routines.
          129  +*/
          130  +static void *sqlite3MemCalloc(int nByte){
          131  +#ifdef SQLITE_MALLOCSIZE
          132  +  return SQLITE_CALLOC( ROUND8(nByte) );
   118    133   #else
   119         -  sqlite3_int64 *p;
   120         -  assert( nByte>0 );
          134  +  i64 *p;
   121    135     nByte = ROUND8(nByte);
   122         -  p = SQLITE_MALLOC( nByte+8 );
   123         -  if( p ){
   124         -    p[0] = nByte;
   125         -    p++;
   126         -  }else{
   127         -    testcase( sqlite3GlobalConfig.xLog!=0 );
   128         -    sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
   129         -  }
          136  +  p = SQLITE_CALLOC(nByte+8);
          137  +  if( p ) *(p++) = (i64)nByte;
   130    138     return (void *)p;
   131    139   #endif
   132    140   }
   133    141   
   134    142   /*
   135    143   ** Like free() but works for allocations obtained from sqlite3MemMalloc()
   136    144   ** or sqlite3MemRealloc().
................................................................................
   267    275        sqlite3MemMalloc,
   268    276        sqlite3MemFree,
   269    277        sqlite3MemRealloc,
   270    278        sqlite3MemSize,
   271    279        sqlite3MemRoundup,
   272    280        sqlite3MemInit,
   273    281        sqlite3MemShutdown,
   274         -     0
          282  +     0,
          283  +     sqlite3MemCalloc
   275    284     };
   276    285     sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
   277    286   }
   278    287   
   279    288   #endif /* SQLITE_SYSTEM_MALLOC */

Changes to src/parse.y.

   148    148   ///////////////////// The CREATE TABLE statement ////////////////////////////
   149    149   //
   150    150   cmd ::= create_table create_table_args.
   151    151   create_table ::= createkw temp(T) TABLE ifnotexists(E) nm(Y) dbnm(Z). {
   152    152      sqlite3StartTable(pParse,&Y,&Z,T,0,0,E);
   153    153   }
   154    154   createkw(A) ::= CREATE(X).  {
   155         -  pParse->db->lookaside.bEnabled = 0;
          155  +  pParse->db->lookaside.sz = 0;
   156    156     A = X;
   157    157   }
   158    158   %type ifnotexists {int}
   159    159   ifnotexists(A) ::= .              {A = 0;}
   160    160   ifnotexists(A) ::= IF NOT EXISTS. {A = 1;}
   161    161   %type temp {int}
   162    162   %ifndef SQLITE_OMIT_TEMPDB
................................................................................
  1374   1374   cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). {
  1375   1375     sqlite3AlterRenameTable(pParse,X,&Z);
  1376   1376   }
  1377   1377   cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column(Y). {
  1378   1378     sqlite3AlterFinishAddColumn(pParse, &Y);
  1379   1379   }
  1380   1380   add_column_fullname ::= fullname(X). {
  1381         -  pParse->db->lookaside.bEnabled = 0;
         1381  +  pParse->db->lookaside.sz = 0;
  1382   1382     sqlite3AlterBeginAddColumn(pParse, X);
  1383   1383   }
  1384   1384   kwcolumn_opt ::= .
  1385   1385   kwcolumn_opt ::= COLUMNKW.
  1386   1386   %endif  SQLITE_OMIT_ALTERTABLE
  1387   1387   
  1388   1388   //////////////////////// CREATE VIRTUAL TABLE ... /////////////////////////////

Changes to src/select.c.

  1394   1394     if( pParse->nErr ) return 0;
  1395   1395     while( pSelect->pPrior ) pSelect = pSelect->pPrior;
  1396   1396     db->flags = savedFlags;
  1397   1397     pTab = sqlite3DbMallocZero(db, sizeof(Table) );
  1398   1398     if( pTab==0 ){
  1399   1399       return 0;
  1400   1400     }
  1401         -  /* The sqlite3ResultSetOfSelect() is only used n contexts where lookaside
  1402         -  ** is disabled */
  1403         -  assert( db->lookaside.bEnabled==0 );
         1401  +  /* The sqlite3ResultSetOfSelect() should only used in contexts where the
         1402  +  ** lookaside buffer is disabled. */
         1403  +  assert( db->lookaside.sz==0 );
  1404   1404     pTab->nRef = 1;
  1405   1405     pTab->zName = 0;
  1406   1406     pTab->nRowEst = 1000000;
  1407   1407     selectColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
  1408   1408     selectAddColumnTypeAndCollation(pParse, pTab->nCol, pTab->aCol, pSelect);
  1409   1409     pTab->iPKey = -1;
  1410   1410     if( db->mallocFailed ){

Changes to src/sqlite.h.in.

  1337   1337     void (*xFree)(void*);          /* Free a prior allocation */
  1338   1338     void *(*xRealloc)(void*,int);  /* Resize an allocation */
  1339   1339     int (*xSize)(void*);           /* Return the size of an allocation */
  1340   1340     int (*xRoundup)(int);          /* Round up request size to allocation size */
  1341   1341     int (*xInit)(void*);           /* Initialize the memory allocator */
  1342   1342     void (*xShutdown)(void*);      /* Deinitialize the memory allocator */
  1343   1343     void *pAppData;                /* Argument to xInit() and xShutdown() */
         1344  +  void *(*xCalloc)(int);         /* Zeroed memory allocation function */
  1344   1345   };
  1345   1346   
  1346   1347   /*
  1347   1348   ** CAPI3REF: Configuration Options
  1348   1349   ** KEYWORDS: {configuration option}
  1349   1350   **
  1350   1351   ** These constants are the available integer configuration options that
................................................................................
  1573   1574   #define SQLITE_CONFIG_SERIALIZED    3  /* nil */
  1574   1575   #define SQLITE_CONFIG_MALLOC        4  /* sqlite3_mem_methods* */
  1575   1576   #define SQLITE_CONFIG_GETMALLOC     5  /* sqlite3_mem_methods* */
  1576   1577   #define SQLITE_CONFIG_SCRATCH       6  /* void*, int sz, int N */
  1577   1578   #define SQLITE_CONFIG_PAGECACHE     7  /* void*, int sz, int N */
  1578   1579   #define SQLITE_CONFIG_HEAP          8  /* void*, int nByte, int min */
  1579   1580   #define SQLITE_CONFIG_MEMSTATUS     9  /* boolean */
  1580         -#define SQLITE_CONFIG_MUTEX        10  /* sqlite3_mutex_methods* */
  1581         -#define SQLITE_CONFIG_GETMUTEX     11  /* sqlite3_mutex_methods* */
         1581  +#define SQLITE_CONFIG_MUTEX        10  /* sqlite3_mutex_methods* no xCalloc() */
         1582  +#define SQLITE_CONFIG_GETMUTEX     11  /* sqlite3_mutex_methods* no xCalloc() */
  1582   1583   /* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */ 
  1583   1584   #define SQLITE_CONFIG_LOOKASIDE    13  /* int int */
  1584   1585   #define SQLITE_CONFIG_PCACHE       14  /* no-op */
  1585   1586   #define SQLITE_CONFIG_GETPCACHE    15  /* no-op */
  1586   1587   #define SQLITE_CONFIG_LOG          16  /* xFunc, void* */
  1587   1588   #define SQLITE_CONFIG_URI          17  /* int */
  1588   1589   #define SQLITE_CONFIG_PCACHE2      18  /* sqlite3_pcache_methods2* */
  1589   1590   #define SQLITE_CONFIG_GETPCACHE2   19  /* sqlite3_pcache_methods2* */
         1591  +#define SQLITE_CONFIG_MALLOC2      20  /* sqlite3_mem_methods* incl xCalloc() */
         1592  +#define SQLITE_CONFIG_GETMALLOC2   21  /* sqlite3_mem_methods* incl xCalloc() */
  1590   1593   
  1591   1594   /*
  1592   1595   ** CAPI3REF: Database Connection Configuration Options
  1593   1596   **
  1594   1597   ** These constants are the available integer configuration options that
  1595   1598   ** can be passed as the second argument to the [sqlite3_db_config()] interface.
  1596   1599   **

Changes to src/sqliteInt.h.

   779    779   ** the lookaside subsystem is stored on a linked list of LookasideSlot
   780    780   ** objects.
   781    781   **
   782    782   ** Lookaside allocations are only allowed for objects that are associated
   783    783   ** with a particular database connection.  Hence, schema information cannot
   784    784   ** be stored in lookaside because in shared cache mode the schema information
   785    785   ** is shared by multiple database connections.  Therefore, while parsing
   786         -** schema information, the Lookaside.bEnabled flag is cleared so that
   787         -** lookaside allocations are not used to construct the schema objects.
          786  +** schema information, the Lookaside.sz variable is temporarily set to
          787  +** zero so that lookaside allocations are not used to construct the schema 
          788  +** objects. Lookaside.szEnabled always contains the allocation size that
          789  +** Lookaside.sz is set to when the buffer is enabled.
   788    790   */
   789    791   struct Lookaside {
   790    792     u16 sz;                 /* Size of each buffer in bytes */
   791         -  u8 bEnabled;            /* False to disable new lookaside allocations */
          793  +  u16 szEnabled;          /* Value of 'sz' when buffer is enabled. */
   792    794     u8 bMalloced;           /* True if pStart obtained from sqlite3_malloc() */
   793    795     int nOut;               /* Number of buffers currently checked out */
   794    796     int mxOut;              /* Highwater mark for nOut */
   795    797     int anStat[3];          /* 0: hits.  1: size misses.  2: full misses */
   796    798     LookasideSlot *pFree;   /* List of available buffers */
   797    799     void *pStart;           /* First byte of available memory space */
   798    800     void *pEnd;             /* First byte past end of available space */

Changes to src/tokenize.c.

   386    386   */
   387    387   int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
   388    388     int nErr = 0;                   /* Number of errors encountered */
   389    389     int i;                          /* Loop counter */
   390    390     void *pEngine;                  /* The LEMON-generated LALR(1) parser */
   391    391     int tokenType;                  /* type of the next token */
   392    392     int lastTokenParsed = -1;       /* type of the previous token */
   393         -  u8 enableLookaside;             /* Saved value of db->lookaside.bEnabled */
          393  +  u16 lookasideSz;                /* Saved value of db->lookaside.sz */
   394    394     sqlite3 *db = pParse->db;       /* The database connection */
   395    395     int mxSqlLen;                   /* Max length of an SQL string */
   396    396   
   397    397   
   398    398     mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
   399    399     if( db->activeVdbeCnt==0 ){
   400    400       db->u1.isInterrupted = 0;
................................................................................
   409    409       return SQLITE_NOMEM;
   410    410     }
   411    411     assert( pParse->pNewTable==0 );
   412    412     assert( pParse->pNewTrigger==0 );
   413    413     assert( pParse->nVar==0 );
   414    414     assert( pParse->nzVar==0 );
   415    415     assert( pParse->azVar==0 );
   416         -  enableLookaside = db->lookaside.bEnabled;
   417         -  if( db->lookaside.pStart ) db->lookaside.bEnabled = 1;
          416  +  lookasideSz = db->lookaside.sz;
          417  +  if( db->lookaside.pStart ){
          418  +    db->lookaside.sz = db->lookaside.szEnabled;
          419  +  }
   418    420     while( !db->mallocFailed && zSql[i]!=0 ){
   419    421       assert( i>=0 );
   420    422       pParse->sLastToken.z = &zSql[i];
   421    423       pParse->sLastToken.n = sqlite3GetToken((unsigned char*)&zSql[i],&tokenType);
   422    424       i += pParse->sLastToken.n;
   423    425       if( i>mxSqlLen ){
   424    426         pParse->rc = SQLITE_TOOBIG;
................................................................................
   464    466     }
   465    467   #ifdef YYTRACKMAXSTACKDEPTH
   466    468     sqlite3StatusSet(SQLITE_STATUS_PARSER_STACK,
   467    469         sqlite3ParserStackPeak(pEngine)
   468    470     );
   469    471   #endif /* YYDEBUG */
   470    472     sqlite3ParserFree(pEngine, sqlite3_free);
   471         -  db->lookaside.bEnabled = enableLookaside;
          473  +  db->lookaside.sz = lookasideSz;
   472    474     if( db->mallocFailed ){
   473    475       pParse->rc = SQLITE_NOMEM;
   474    476     }
   475    477     if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
   476    478       sqlite3SetString(&pParse->zErrMsg, db, "%s", sqlite3ErrStr(pParse->rc));
   477    479     }
   478    480     assert( pzErrMsg!=0 );