/ Check-in [0a802e96]
Login
SQLite training in Houston TX on 2019-11-05 (details)
Part of the 2019 Tcl Conference

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

Overview
Comment:Add the slightly faster sqlite3DbMallocRawNN(db,n) routine for the majority cases where db is guaranteed to be not NULL.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | oom-handling
Files: files | file ages | folders
SHA1: 0a802e96ab06e2643834b83671fa8353da1a348d
User & Date: drh 2016-02-05 13:38:36
Context
2016-02-05
14:11
Improvements to the way out-of-memory conditions are handled, resulting in an almost 1% performance gain in the commmon case where OOM errors do not occur. check-in: b082538d user: drh tags: trunk
13:38
Add the slightly faster sqlite3DbMallocRawNN(db,n) routine for the majority cases where db is guaranteed to be not NULL. Closed-Leaf check-in: 0a802e96 user: drh tags: oom-handling
04:50
OOM failures on sqlite3_errmsg16() in an app-defined function are no longer benign. check-in: 9efb223f user: drh tags: oom-handling
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/analyze.c.

   309    309   
   310    310   /* Initialize the BLOB value of a ROWID
   311    311   */
   312    312   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
   313    313   static void sampleSetRowid(sqlite3 *db, Stat4Sample *p, int n, const u8 *pData){
   314    314     assert( db!=0 );
   315    315     if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
   316         -  p->u.aRowid = sqlite3DbMallocRaw(db, n);
          316  +  p->u.aRowid = sqlite3DbMallocRawNN(db, n);
   317    317     if( p->u.aRowid ){
   318    318       p->nRowid = n;
   319    319       memcpy(p->u.aRowid, pData, n);
   320    320     }else{
   321    321       p->nRowid = 0;
   322    322     }
   323    323   }
................................................................................
  1111   1111       VdbeCoverage(v);
  1112   1112       sqlite3VdbeAddOp2(v, OP_Integer, 0, regChng);
  1113   1113       addrNextRow = sqlite3VdbeCurrentAddr(v);
  1114   1114   
  1115   1115       if( nColTest>0 ){
  1116   1116         int endDistinctTest = sqlite3VdbeMakeLabel(v);
  1117   1117         int *aGotoChng;               /* Array of jump instruction addresses */
  1118         -      aGotoChng = sqlite3DbMallocRaw(db, sizeof(int)*nColTest);
         1118  +      aGotoChng = sqlite3DbMallocRawNN(db, sizeof(int)*nColTest);
  1119   1119         if( aGotoChng==0 ) continue;
  1120   1120   
  1121   1121         /*
  1122   1122         **  next_row:
  1123   1123         **   regChng = 0
  1124   1124         **   if( idx(0) != regPrev(0) ) goto chng_addr_0
  1125   1125         **   regChng = 1

Changes to src/attach.c.

   105    105       }
   106    106     }
   107    107   
   108    108     /* Allocate the new entry in the db->aDb[] array and initialize the schema
   109    109     ** hash tables.
   110    110     */
   111    111     if( db->aDb==db->aDbStatic ){
   112         -    aNew = sqlite3DbMallocRaw(db, sizeof(db->aDb[0])*3 );
          112  +    aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 );
   113    113       if( aNew==0 ) return;
   114    114       memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
   115    115     }else{
   116    116       aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
   117    117       if( aNew==0 ) return;
   118    118     }
   119    119     db->aDb = aNew;

Changes to src/build.c.

  3698   3698     sqlite3 *db,        /* Connection to notify of malloc failures */
  3699   3699     SrcList *pList,     /* Append to this SrcList. NULL creates a new SrcList */
  3700   3700     Token *pTable,      /* Table to append */
  3701   3701     Token *pDatabase    /* Database of the table */
  3702   3702   ){
  3703   3703     struct SrcList_item *pItem;
  3704   3704     assert( pDatabase==0 || pTable!=0 );  /* Cannot have C without B */
         3705  +  assert( db!=0 );
  3705   3706     if( pList==0 ){
  3706         -    pList = sqlite3DbMallocRaw(db, sizeof(SrcList) );
         3707  +    pList = sqlite3DbMallocRawNN(db, sizeof(SrcList) );
  3707   3708       if( pList==0 ) return 0;
  3708   3709       pList->nAlloc = 1;
  3709   3710       pList->nSrc = 0;
  3710   3711     }
  3711   3712     pList = sqlite3SrcListEnlarge(db, pList, 1, pList->nSrc);
  3712   3713     if( db->mallocFailed ){
  3713   3714       sqlite3SrcListDelete(db, pList);

Changes to src/date.c.

   963    963     testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH] );
   964    964     if( n<sizeof(zBuf) ){
   965    965       z = zBuf;
   966    966     }else if( n>(u64)db->aLimit[SQLITE_LIMIT_LENGTH] ){
   967    967       sqlite3_result_error_toobig(context);
   968    968       return;
   969    969     }else{
   970         -    z = sqlite3DbMallocRaw(db, (int)n);
          970  +    z = sqlite3DbMallocRawNN(db, (int)n);
   971    971       if( z==0 ){
   972    972         sqlite3_result_error_nomem(context);
   973    973         return;
   974    974       }
   975    975     }
   976    976     computeJD(&x);
   977    977     computeYMD_HMS(&x);

Changes to src/delete.c.

   435    435       }
   436    436     
   437    437       if( eOnePass!=ONEPASS_OFF ){
   438    438         /* For ONEPASS, no need to store the rowid/primary-key. There is only
   439    439         ** one, so just keep it in its register(s) and fall through to the
   440    440         ** delete code.  */
   441    441         nKey = nPk; /* OP_Found will use an unpacked key */
   442         -      aToOpen = sqlite3DbMallocRaw(db, nIdx+2);
          442  +      aToOpen = sqlite3DbMallocRawNN(db, nIdx+2);
   443    443         if( aToOpen==0 ){
   444    444           sqlite3WhereEnd(pWInfo);
   445    445           goto delete_from_cleanup;
   446    446         }
   447    447         memset(aToOpen, 1, nIdx+1);
   448    448         aToOpen[nIdx+1] = 0;
   449    449         if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iTabCur] = 0;

Changes to src/expr.c.

   449    449     const Token *pToken,    /* Token argument.  Might be NULL */
   450    450     int dequote             /* True to dequote */
   451    451   ){
   452    452     Expr *pNew;
   453    453     int nExtra = 0;
   454    454     int iValue = 0;
   455    455   
          456  +  assert( db!=0 );
   456    457     if( pToken ){
   457    458       if( op!=TK_INTEGER || pToken->z==0
   458    459             || sqlite3GetInt32(pToken->z, &iValue)==0 ){
   459    460         nExtra = pToken->n+1;
   460    461         assert( iValue>=0 );
   461    462       }
   462    463     }
   463         -  pNew = sqlite3DbMallocRaw(db, sizeof(Expr)+nExtra);
          464  +  pNew = sqlite3DbMallocRawNN(db, sizeof(Expr)+nExtra);
   464    465     if( pNew ){
   465    466       memset(pNew, 0, sizeof(Expr));
   466    467       pNew->op = (u8)op;
   467    468       pNew->iAgg = -1;
   468    469       if( pToken ){
   469    470         if( nExtra==0 ){
   470    471           pNew->flags |= EP_IntValue;
................................................................................
   853    854   ** (if applicable), and the copies of the p->pLeft and p->pRight expressions,
   854    855   ** if any. Before returning, *pzBuffer is set to the first byte past the
   855    856   ** portion of the buffer copied into by this function.
   856    857   */
   857    858   static Expr *exprDup(sqlite3 *db, Expr *p, int flags, u8 **pzBuffer){
   858    859     Expr *pNew = 0;                      /* Value to return */
   859    860     assert( flags==0 || flags==EXPRDUP_REDUCE );
          861  +  assert( db!=0 );
   860    862     if( p ){
   861    863       const int isReduced = (flags&EXPRDUP_REDUCE);
   862    864       u8 *zAlloc;
   863    865       u32 staticFlag = 0;
   864    866   
   865    867       assert( pzBuffer==0 || isReduced );
   866    868   
   867    869       /* Figure out where to write the new Expr structure. */
   868    870       if( pzBuffer ){
   869    871         zAlloc = *pzBuffer;
   870    872         staticFlag = EP_Static;
   871    873       }else{
   872         -      zAlloc = sqlite3DbMallocRaw(db, dupedExprSize(p, flags));
          874  +      zAlloc = sqlite3DbMallocRawNN(db, dupedExprSize(p, flags));
   873    875       }
   874    876       pNew = (Expr *)zAlloc;
   875    877   
   876    878       if( pNew ){
   877    879         /* Set nNewSize to the size allocated for the structure pointed to
   878    880         ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or
   879    881         ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed
................................................................................
   988    990     assert( flags==0 || flags==EXPRDUP_REDUCE );
   989    991     return exprDup(db, p, flags, 0);
   990    992   }
   991    993   ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){
   992    994     ExprList *pNew;
   993    995     struct ExprList_item *pItem, *pOldItem;
   994    996     int i;
          997  +  assert( db!=0 );
   995    998     if( p==0 ) return 0;
   996         -  pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
          999  +  pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) );
   997   1000     if( pNew==0 ) return 0;
   998   1001     pNew->nExpr = i = p->nExpr;
   999   1002     if( (flags & EXPRDUP_REDUCE)==0 ) for(i=1; i<p->nExpr; i+=i){}
  1000         -  pNew->a = pItem = sqlite3DbMallocRaw(db,  i*sizeof(p->a[0]) );
         1003  +  pNew->a = pItem = sqlite3DbMallocRawNN(db,  i*sizeof(p->a[0]) );
  1001   1004     if( pItem==0 ){
  1002   1005       sqlite3DbFree(db, pNew);
  1003   1006       return 0;
  1004   1007     } 
  1005   1008     pOldItem = p->a;
  1006   1009     for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
  1007   1010       Expr *pOldExpr = pOldItem->pExpr;
................................................................................
  1024   1027   */
  1025   1028   #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
  1026   1029    || !defined(SQLITE_OMIT_SUBQUERY)
  1027   1030   SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){
  1028   1031     SrcList *pNew;
  1029   1032     int i;
  1030   1033     int nByte;
         1034  +  assert( db!=0 );
  1031   1035     if( p==0 ) return 0;
  1032   1036     nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
  1033         -  pNew = sqlite3DbMallocRaw(db, nByte );
         1037  +  pNew = sqlite3DbMallocRawNN(db, nByte );
  1034   1038     if( pNew==0 ) return 0;
  1035   1039     pNew->nSrc = pNew->nAlloc = p->nSrc;
  1036   1040     for(i=0; i<p->nSrc; i++){
  1037   1041       struct SrcList_item *pNewItem = &pNew->a[i];
  1038   1042       struct SrcList_item *pOldItem = &p->a[i];
  1039   1043       Table *pTab;
  1040   1044       pNewItem->pSchema = pOldItem->pSchema;
................................................................................
  1063   1067       pNewItem->colUsed = pOldItem->colUsed;
  1064   1068     }
  1065   1069     return pNew;
  1066   1070   }
  1067   1071   IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
  1068   1072     IdList *pNew;
  1069   1073     int i;
         1074  +  assert( db!=0 );
  1070   1075     if( p==0 ) return 0;
  1071         -  pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
         1076  +  pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) );
  1072   1077     if( pNew==0 ) return 0;
  1073   1078     pNew->nId = p->nId;
  1074         -  pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) );
         1079  +  pNew->a = sqlite3DbMallocRawNN(db, p->nId*sizeof(p->a[0]) );
  1075   1080     if( pNew->a==0 ){
  1076   1081       sqlite3DbFree(db, pNew);
  1077   1082       return 0;
  1078   1083     }
  1079   1084     /* Note that because the size of the allocation for p->a[] is not
  1080   1085     ** necessarily a power of two, sqlite3IdListAppend() may not be called
  1081   1086     ** on the duplicate created by this function. */
................................................................................
  1085   1090       pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
  1086   1091       pNewItem->idx = pOldItem->idx;
  1087   1092     }
  1088   1093     return pNew;
  1089   1094   }
  1090   1095   Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
  1091   1096     Select *pNew, *pPrior;
         1097  +  assert( db!=0 );
  1092   1098     if( p==0 ) return 0;
  1093         -  pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
         1099  +  pNew = sqlite3DbMallocRawNN(db, sizeof(*p) );
  1094   1100     if( pNew==0 ) return 0;
  1095   1101     pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
  1096   1102     pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
  1097   1103     pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
  1098   1104     pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
  1099   1105     pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
  1100   1106     pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
................................................................................
  1132   1138   */
  1133   1139   ExprList *sqlite3ExprListAppend(
  1134   1140     Parse *pParse,          /* Parsing context */
  1135   1141     ExprList *pList,        /* List to which to append. Might be NULL */
  1136   1142     Expr *pExpr             /* Expression to be appended. Might be NULL */
  1137   1143   ){
  1138   1144     sqlite3 *db = pParse->db;
         1145  +  assert( db!=0 );
  1139   1146     if( pList==0 ){
  1140         -    pList = sqlite3DbMallocRaw(db, sizeof(ExprList) );
         1147  +    pList = sqlite3DbMallocRawNN(db, sizeof(ExprList) );
  1141   1148       if( pList==0 ){
  1142   1149         goto no_mem;
  1143   1150       }
  1144   1151       pList->nExpr = 0;
  1145         -    pList->a = sqlite3DbMallocRaw(db, sizeof(pList->a[0]));
         1152  +    pList->a = sqlite3DbMallocRawNN(db, sizeof(pList->a[0]));
  1146   1153       if( pList->a==0 ) goto no_mem;
  1147   1154     }else if( (pList->nExpr & (pList->nExpr-1))==0 ){
  1148   1155       struct ExprList_item *a;
  1149   1156       assert( pList->nExpr>0 );
  1150   1157       a = sqlite3DbRealloc(db, pList->a, pList->nExpr*2*sizeof(pList->a[0]));
  1151   1158       if( a==0 ){
  1152   1159         goto no_mem;

Changes to src/fkey.c.

   215    215       */
   216    216       if( pParent->iPKey>=0 ){
   217    217         if( !zKey ) return 0;
   218    218         if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zName, zKey) ) return 0;
   219    219       }
   220    220     }else if( paiCol ){
   221    221       assert( nCol>1 );
   222         -    aiCol = (int *)sqlite3DbMallocRaw(pParse->db, nCol*sizeof(int));
          222  +    aiCol = (int *)sqlite3DbMallocRawNN(pParse->db, nCol*sizeof(int));
   223    223       if( !aiCol ) return 1;
   224    224       *paiCol = aiCol;
   225    225     }
   226    226   
   227    227     for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
   228    228       if( pIdx->nKeyCol==nCol && IsUniqueIndex(pIdx) ){ 
   229    229         /* pIdx is a UNIQUE index (or a PRIMARY KEY) and has the right number

Changes to src/insert.c.

   226    226     if( pTab->tabFlags & TF_Autoincrement ){
   227    227       Parse *pToplevel = sqlite3ParseToplevel(pParse);
   228    228       AutoincInfo *pInfo;
   229    229   
   230    230       pInfo = pToplevel->pAinc;
   231    231       while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
   232    232       if( pInfo==0 ){
   233         -      pInfo = sqlite3DbMallocRaw(pParse->db, sizeof(*pInfo));
          233  +      pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo));
   234    234         if( pInfo==0 ) return 0;
   235    235         pInfo->pNext = pToplevel->pAinc;
   236    236         pToplevel->pAinc = pInfo;
   237    237         pInfo->pTab = pTab;
   238    238         pInfo->iDb = iDb;
   239    239         pToplevel->nMem++;                  /* Register to hold name of table */
   240    240         pInfo->regCtr = ++pToplevel->nMem;  /* Max rowid register */
................................................................................
   783    783     }
   784    784   
   785    785     /* If this is not a view, open the table and and all indices */
   786    786     if( !isView ){
   787    787       int nIdx;
   788    788       nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, -1, 0,
   789    789                                         &iDataCur, &iIdxCur);
   790         -    aRegIdx = sqlite3DbMallocRaw(db, sizeof(int)*(nIdx+1));
          790  +    aRegIdx = sqlite3DbMallocRawNN(db, sizeof(int)*(nIdx+1));
   791    791       if( aRegIdx==0 ){
   792    792         goto insert_cleanup;
   793    793       }
   794    794       for(i=0; i<nIdx; i++){
   795    795         aRegIdx[i] = ++pParse->nMem;
   796    796       }
   797    797     }

Changes to src/malloc.c.

   571    571   }
   572    572   
   573    573   /*
   574    574   ** Allocate and zero memory.  If the allocation fails, make
   575    575   ** the mallocFailed flag in the connection pointer.
   576    576   */
   577    577   void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
   578         -  void *p = sqlite3DbMallocRaw(db, n);
   579         -  if( p ){
   580         -    memset(p, 0, (size_t)n);
   581         -  }
          578  +  void *p;
          579  +  testcase( db==0 );
          580  +  p = sqlite3DbMallocRaw(db, n);
          581  +  if( p ) memset(p, 0, (size_t)n);
          582  +  return p;
          583  +}
          584  +
          585  +
          586  +/* Finish the work of sqlite3DbMallocRawNN for the unusual and
          587  +** slower case when the allocation cannot be fulfilled using lookaside.
          588  +*/
          589  +static SQLITE_NOINLINE void *dbMallocRawFinish(sqlite3 *db, u64 n){
          590  +  void *p;
          591  +  assert( db!=0 );
          592  +  p = sqlite3Malloc(n);
          593  +  if( !p ) sqlite3OomFault(db);
          594  +  sqlite3MemdebugSetType(p, 
          595  +         (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
   582    596     return p;
   583    597   }
   584    598   
   585    599   /*
   586    600   ** Allocate memory, either lookaside (if possible) or heap.  
   587    601   ** If the allocation fails, set the mallocFailed flag in
   588    602   ** the connection pointer.
................................................................................
   596    610   **
   597    611   **         int *a = (int*)sqlite3DbMallocRaw(db, 100);
   598    612   **         int *b = (int*)sqlite3DbMallocRaw(db, 200);
   599    613   **         if( b ) a[10] = 9;
   600    614   **
   601    615   ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
   602    616   ** that all prior mallocs (ex: "a") worked too.
          617  +**
          618  +** The sqlite3MallocRawNN() variant guarantees that the "db" parameter is
          619  +** not a NULL pointer.
   603    620   */
   604         -static SQLITE_NOINLINE void *dbMallocRawFinish(sqlite3 *db, u64 n);
   605    621   void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
   606         -  assert( db==0 || sqlite3_mutex_held(db->mutex) );
   607         -  assert( db==0 || db->pnBytesFreed==0 );
          622  +  void *p;
          623  +  if( db ) return sqlite3DbMallocRawNN(db, n);
          624  +  p = sqlite3Malloc(n);
          625  +  sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
          626  +  return p;
          627  +}
          628  +void *sqlite3DbMallocRawNN(sqlite3 *db, u64 n){
          629  +  assert( db!=0 );
          630  +  assert( sqlite3_mutex_held(db->mutex) );
          631  +  assert( db->pnBytesFreed==0 );
   608    632   #ifndef SQLITE_OMIT_LOOKASIDE
   609         -  if( db ){
   610         -    LookasideSlot *pBuf;
   611         -    if( db->lookaside.bDisable==0 ){
   612         -      assert( db->mallocFailed==0 );
   613         -      if( n>db->lookaside.sz ){
   614         -        db->lookaside.anStat[1]++;
   615         -      }else if( (pBuf = db->lookaside.pFree)==0 ){
   616         -        db->lookaside.anStat[2]++;
   617         -      }else{
   618         -        db->lookaside.pFree = pBuf->pNext;
   619         -        db->lookaside.nOut++;
   620         -        db->lookaside.anStat[0]++;
   621         -        if( db->lookaside.nOut>db->lookaside.mxOut ){
   622         -          db->lookaside.mxOut = db->lookaside.nOut;
   623         -        }
   624         -        return (void*)pBuf;
          633  +  LookasideSlot *pBuf;
          634  +  if( db->lookaside.bDisable==0 ){
          635  +    assert( db->mallocFailed==0 );
          636  +    if( n>db->lookaside.sz ){
          637  +      db->lookaside.anStat[1]++;
          638  +    }else if( (pBuf = db->lookaside.pFree)==0 ){
          639  +      db->lookaside.anStat[2]++;
          640  +    }else{
          641  +      db->lookaside.pFree = pBuf->pNext;
          642  +      db->lookaside.nOut++;
          643  +      db->lookaside.anStat[0]++;
          644  +      if( db->lookaside.nOut>db->lookaside.mxOut ){
          645  +        db->lookaside.mxOut = db->lookaside.nOut;
   625    646         }
   626         -    }else if( db->mallocFailed ){
   627         -      return 0;
          647  +      return (void*)pBuf;
   628    648       }
   629         -    
          649  +  }else if( db->mallocFailed ){
          650  +    return 0;
   630    651     }
   631    652   #else
   632         -  if( db && db->mallocFailed ){
          653  +  if( db->mallocFailed ){
   633    654       return 0;
   634    655     }
   635    656   #endif
   636    657     return dbMallocRawFinish(db, n);
   637    658   }
   638         -static SQLITE_NOINLINE void *dbMallocRawFinish(sqlite3 *db, u64 n){
   639         -  void *p = sqlite3Malloc(n);
   640         -  if( !p && db ){
   641         -    sqlite3OomFault(db);
   642         -  }
   643         -  sqlite3MemdebugSetType(p, 
   644         -         (db && db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
   645         -  return p;
   646         -}
   647    659   
   648    660   /* Forward declaration */
   649    661   static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n);
   650    662   
   651    663   /*
   652    664   ** Resize the block of memory pointed to by p to n bytes. If the
   653    665   ** resize fails, set the mallocFailed flag in the connection object.
   654    666   */
   655    667   void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
   656    668     assert( db!=0 );
   657         -  if( p==0 ) return sqlite3DbMallocRaw(db, n);
          669  +  if( p==0 ) return sqlite3DbMallocRawNN(db, n);
   658    670     assert( sqlite3_mutex_held(db->mutex) );
   659    671     if( isLookaside(db,p) && n<=db->lookaside.sz ) return p;
   660    672     return dbReallocFinish(db, p, n);
   661    673   }
   662    674   static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n){
   663    675     void *pNew = 0;
   664    676     assert( db!=0 );
   665    677     assert( p!=0 );
   666    678     if( db->mallocFailed==0 ){
   667    679       if( isLookaside(db, p) ){
   668         -      pNew = sqlite3DbMallocRaw(db, n);
          680  +      pNew = sqlite3DbMallocRawNN(db, n);
   669    681         if( pNew ){
   670    682           memcpy(pNew, p, db->lookaside.sz);
   671    683           sqlite3DbFree(db, p);
   672    684         }
   673    685       }else{
   674    686         assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
   675    687         assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
................................................................................
   717    729     if( zNew ){
   718    730       memcpy(zNew, z, n);
   719    731     }
   720    732     return zNew;
   721    733   }
   722    734   char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
   723    735     char *zNew;
          736  +  assert( db!=0 );
   724    737     if( z==0 ){
   725    738       return 0;
   726    739     }
   727    740     assert( (n&0x7fffffff)==n );
   728         -  zNew = sqlite3DbMallocRaw(db, n+1);
          741  +  zNew = sqlite3DbMallocRawNN(db, n+1);
   729    742     if( zNew ){
   730    743       memcpy(zNew, z, (size_t)n);
   731    744       zNew[n] = 0;
   732    745     }
   733    746     return zNew;
   734    747   }
   735    748   

Changes to src/rowset.c.

   177    177   ** In an OOM situation, the RowSet.db->mallocFailed flag is set and this
   178    178   ** routine returns NULL.
   179    179   */
   180    180   static struct RowSetEntry *rowSetEntryAlloc(RowSet *p){
   181    181     assert( p!=0 );
   182    182     if( p->nFresh==0 ){
   183    183       struct RowSetChunk *pNew;
   184         -    pNew = sqlite3DbMallocRaw(p->db, sizeof(*pNew));
          184  +    pNew = sqlite3DbMallocRawNN(p->db, sizeof(*pNew));
   185    185       if( pNew==0 ){
   186    186         return 0;
   187    187       }
   188    188       pNew->pNextChunk = p->pChunk;
   189    189       p->pChunk = pNew;
   190    190       p->pFresh = pNew->aEntry;
   191    191       p->nFresh = ROWSET_ENTRY_PER_CHUNK;

Changes to src/select.c.

   108    108     u16 selFlags,         /* Flag parameters, such as SF_Distinct */
   109    109     Expr *pLimit,         /* LIMIT value.  NULL means not used */
   110    110     Expr *pOffset         /* OFFSET value.  NULL means no offset */
   111    111   ){
   112    112     Select *pNew;
   113    113     Select standin;
   114    114     sqlite3 *db = pParse->db;
   115         -  pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
          115  +  pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) );
   116    116     if( pNew==0 ){
   117    117       assert( db->mallocFailed );
   118    118       pNew = &standin;
   119    119     }
   120    120     if( pEList==0 ){
   121    121       pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db,TK_ASTERISK,0));
   122    122     }
................................................................................
  2866   2866     /* Compute the comparison permutation and keyinfo that is used with
  2867   2867     ** the permutation used to determine if the next
  2868   2868     ** row of results comes from selectA or selectB.  Also add explicit
  2869   2869     ** collations to the ORDER BY clause terms so that when the subqueries
  2870   2870     ** to the right and the left are evaluated, they use the correct
  2871   2871     ** collation.
  2872   2872     */
  2873         -  aPermute = sqlite3DbMallocRaw(db, sizeof(int)*(nOrderBy + 1));
         2873  +  aPermute = sqlite3DbMallocRawNN(db, sizeof(int)*(nOrderBy + 1));
  2874   2874     if( aPermute ){
  2875   2875       struct ExprList_item *pItem;
  2876   2876       aPermute[0] = nOrderBy;
  2877   2877       for(i=1, pItem=pOrderBy->a; i<=nOrderBy; i++, pItem++){
  2878   2878         assert( pItem->u.x.iOrderByCol>0 );
  2879   2879         assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr );
  2880   2880         aPermute[i] = pItem->u.x.iOrderByCol - 1;

Changes to src/sqliteInt.h.

  3223   3223   
  3224   3224   int sqlite3MallocInit(void);
  3225   3225   void sqlite3MallocEnd(void);
  3226   3226   void *sqlite3Malloc(u64);
  3227   3227   void *sqlite3MallocZero(u64);
  3228   3228   void *sqlite3DbMallocZero(sqlite3*, u64);
  3229   3229   void *sqlite3DbMallocRaw(sqlite3*, u64);
         3230  +void *sqlite3DbMallocRawNN(sqlite3*, u64);
  3230   3231   char *sqlite3DbStrDup(sqlite3*,const char*);
  3231   3232   char *sqlite3DbStrNDup(sqlite3*,const char*, u64);
  3232   3233   void *sqlite3Realloc(void*, u64);
  3233   3234   void *sqlite3DbReallocOrFree(sqlite3 *, void *, u64);
  3234   3235   void *sqlite3DbRealloc(sqlite3 *, void *, u64);
  3235   3236   void sqlite3DbFree(sqlite3*, void*);
  3236   3237   int sqlite3MallocSize(void*);

Changes to src/update.c.

   193    193       }
   194    194       pParse->nTab++;
   195    195     }
   196    196   
   197    197     /* Allocate space for aXRef[], aRegIdx[], and aToOpen[].  
   198    198     ** Initialize aXRef[] and aToOpen[] to their default values.
   199    199     */
   200         -  aXRef = sqlite3DbMallocRaw(db, sizeof(int) * (pTab->nCol+nIdx) + nIdx+2 );
          200  +  aXRef = sqlite3DbMallocRawNN(db, sizeof(int) * (pTab->nCol+nIdx) + nIdx+2 );
   201    201     if( aXRef==0 ) goto update_cleanup;
   202    202     aRegIdx = aXRef+pTab->nCol;
   203    203     aToOpen = (u8*)(aRegIdx+nIdx);
   204    204     memset(aToOpen, 1, nIdx+1);
   205    205     aToOpen[nIdx+1] = 0;
   206    206     for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
   207    207   

Changes to src/util.c.

  1146   1146   ** binary value has been obtained from malloc and must be freed by
  1147   1147   ** the calling routine.
  1148   1148   */
  1149   1149   void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
  1150   1150     char *zBlob;
  1151   1151     int i;
  1152   1152   
  1153         -  zBlob = (char *)sqlite3DbMallocRaw(db, n/2 + 1);
         1153  +  zBlob = (char *)sqlite3DbMallocRawNN(db, n/2 + 1);
  1154   1154     n--;
  1155   1155     if( zBlob ){
  1156   1156       for(i=0; i<n; i+=2){
  1157   1157         zBlob[i/2] = (sqlite3HexToInt(z[i])<<4) | sqlite3HexToInt(z[i+1]);
  1158   1158       }
  1159   1159       zBlob[i/2] = 0;
  1160   1160     }

Changes to src/vdbe.c.

  1622   1622     sqlite3_context *pCtx;
  1623   1623   
  1624   1624     assert( pOp->p4type==P4_FUNCDEF );
  1625   1625     n = pOp->p5;
  1626   1626     assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
  1627   1627     assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) );
  1628   1628     assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
  1629         -  pCtx = sqlite3DbMallocRaw(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
         1629  +  pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
  1630   1630     if( pCtx==0 ) goto no_mem;
  1631   1631     pCtx->pOut = 0;
  1632   1632     pCtx->pFunc = pOp->p4.pFunc;
  1633   1633     pCtx->iOp = (int)(pOp - aOp);
  1634   1634     pCtx->pVdbe = p;
  1635   1635     pCtx->argc = n;
  1636   1636     pOp->p4type = P4_FUNCCTX;
................................................................................
  2866   2866         assert( db->autoCommit==0 || db->nVTrans==0 );
  2867   2867         rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
  2868   2868                                   db->nStatement+db->nSavepoint);
  2869   2869         if( rc!=SQLITE_OK ) goto abort_due_to_error;
  2870   2870   #endif
  2871   2871   
  2872   2872         /* Create a new savepoint structure. */
  2873         -      pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+nName+1);
         2873  +      pNew = sqlite3DbMallocRawNN(db, sizeof(Savepoint)+nName+1);
  2874   2874         if( pNew ){
  2875   2875           pNew->zName = (char *)&pNew[1];
  2876   2876           memcpy(pNew->zName, zName, nName+1);
  2877   2877       
  2878   2878           /* If there is no open transaction, then mark this as a special
  2879   2879           ** "transaction savepoint". */
  2880   2880           if( db->autoCommit ){
................................................................................
  5416   5416     int nErr;       /* Number of errors reported */
  5417   5417     char *z;        /* Text of the error report */
  5418   5418     Mem *pnErr;     /* Register keeping track of errors remaining */
  5419   5419   
  5420   5420     assert( p->bIsReader );
  5421   5421     nRoot = pOp->p2;
  5422   5422     assert( nRoot>0 );
  5423         -  aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(nRoot+1) );
         5423  +  aRoot = sqlite3DbMallocRawNN(db, sizeof(int)*(nRoot+1) );
  5424   5424     if( aRoot==0 ) goto no_mem;
  5425   5425     assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
  5426   5426     pnErr = &aMem[pOp->p3];
  5427   5427     assert( (pnErr->flags & MEM_Int)!=0 );
  5428   5428     assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
  5429   5429     pIn1 = &aMem[pOp->p1];
  5430   5430     for(j=0; j<nRoot; j++){
................................................................................
  5913   5913     sqlite3_context *pCtx;
  5914   5914   
  5915   5915     assert( pOp->p4type==P4_FUNCDEF );
  5916   5916     n = pOp->p5;
  5917   5917     assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
  5918   5918     assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) );
  5919   5919     assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
  5920         -  pCtx = sqlite3DbMallocRaw(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
         5920  +  pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
  5921   5921     if( pCtx==0 ) goto no_mem;
  5922   5922     pCtx->pMem = 0;
  5923   5923     pCtx->pFunc = pOp->p4.pFunc;
  5924   5924     pCtx->iOp = (int)(pOp - aOp);
  5925   5925     pCtx->pVdbe = p;
  5926   5926     pCtx->argc = n;
  5927   5927     pOp->p4type = P4_FUNCCTX;

Changes to src/vdbeaux.c.

   285    285     int op,             /* The new opcode */
   286    286     int p1,             /* The P1 operand */
   287    287     int p2,             /* The P2 operand */
   288    288     int p3,             /* The P3 operand */
   289    289     const u8 *zP4,      /* The P4 operand */
   290    290     int p4type          /* P4 operand type */
   291    291   ){
   292         -  char *p4copy = sqlite3DbMallocRaw(sqlite3VdbeDb(p), 8);
          292  +  char *p4copy = sqlite3DbMallocRawNN(sqlite3VdbeDb(p), 8);
   293    293     if( p4copy ) memcpy(p4copy, zP4, 8);
   294    294     return sqlite3VdbeAddOp4(p, op, p1, p2, p3, p4copy, p4type);
   295    295   }
   296    296   
   297    297   /*
   298    298   ** Add an OP_ParseSchema opcode.  This routine is broken out from
   299    299   ** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees

Changes to src/vdbemem.c.

   112    112   ** pMem->z into the new allocation.  pMem must be either a string or
   113    113   ** blob if bPreserve is true.  If bPreserve is false, any prior content
   114    114   ** in pMem->z is discarded.
   115    115   */
   116    116   SQLITE_NOINLINE int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){
   117    117     assert( sqlite3VdbeCheckMemInvariants(pMem) );
   118    118     assert( (pMem->flags&MEM_RowSet)==0 );
          119  +  testcase( pMem->db==0 );
   119    120   
   120    121     /* If the bPreserve flag is set to true, then the memory cell must already
   121    122     ** contain a valid string or blob value.  */
   122    123     assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
   123    124     testcase( bPreserve && pMem->z==0 );
   124    125   
   125    126     assert( pMem->szMalloc==0
................................................................................
   715    716   ** empty boolean index.
   716    717   */
   717    718   void sqlite3VdbeMemSetRowSet(Mem *pMem){
   718    719     sqlite3 *db = pMem->db;
   719    720     assert( db!=0 );
   720    721     assert( (pMem->flags & MEM_RowSet)==0 );
   721    722     sqlite3VdbeMemRelease(pMem);
   722         -  pMem->zMalloc = sqlite3DbMallocRaw(db, 64);
          723  +  pMem->zMalloc = sqlite3DbMallocRawNN(db, 64);
   723    724     if( db->mallocFailed ){
   724    725       pMem->flags = MEM_Null;
   725    726       pMem->szMalloc = 0;
   726    727     }else{
   727    728       assert( pMem->zMalloc );
   728    729       pMem->szMalloc = sqlite3DbMallocSize(db, pMem->zMalloc);
   729    730       pMem->u.pRowSet = sqlite3RowSetInit(db, pMem->zMalloc, pMem->szMalloc);
................................................................................
  1436   1437   
  1437   1438     UNUSED_PARAMETER( argc );
  1438   1439     iSerial = sqlite3VdbeSerialType(argv[0], file_format, &nVal);
  1439   1440     nSerial = sqlite3VarintLen(iSerial);
  1440   1441     db = sqlite3_context_db_handle(context);
  1441   1442   
  1442   1443     nRet = 1 + nSerial + nVal;
  1443         -  aRet = sqlite3DbMallocRaw(db, nRet);
         1444  +  aRet = sqlite3DbMallocRawNN(db, nRet);
  1444   1445     if( aRet==0 ){
  1445   1446       sqlite3_result_error_nomem(context);
  1446   1447     }else{
  1447   1448       aRet[0] = nSerial+1;
  1448   1449       putVarint32(&aRet[1], iSerial);
  1449   1450       sqlite3VdbeSerialPut(&aRet[1+nSerial], argv[0], iSerial);
  1450   1451       sqlite3_result_blob(context, aRet, nRet, SQLITE_TRANSIENT);

Changes to src/vtab.c.

    45     45   
    46     46     sqlite3_mutex_enter(db->mutex);
    47     47     nName = sqlite3Strlen30(zName);
    48     48     if( sqlite3HashFind(&db->aModule, zName) ){
    49     49       rc = SQLITE_MISUSE_BKPT;
    50     50     }else{
    51     51       Module *pMod;
    52         -    pMod = (Module *)sqlite3DbMallocRaw(db, sizeof(Module) + nName + 1);
           52  +    pMod = (Module *)sqlite3DbMallocRawNN(db, sizeof(Module) + nName + 1);
    53     53       if( pMod ){
    54     54         Module *pDel;
    55     55         char *zCopy = (char *)(&pMod[1]);
    56     56         memcpy(zCopy, zName, nName+1);
    57     57         pMod->zName = zCopy;
    58     58         pMod->pModule = pModule;
    59     59         pMod->pAux = pAux;

Changes to src/where.c.

  1731   1731   /*
  1732   1732   ** Increase the memory allocation for pLoop->aLTerm[] to be at least n.
  1733   1733   */
  1734   1734   static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){
  1735   1735     WhereTerm **paNew;
  1736   1736     if( p->nLSlot>=n ) return SQLITE_OK;
  1737   1737     n = (n+7)&~7;
  1738         -  paNew = sqlite3DbMallocRaw(db, sizeof(p->aLTerm[0])*n);
         1738  +  paNew = sqlite3DbMallocRawNN(db, sizeof(p->aLTerm[0])*n);
  1739   1739     if( paNew==0 ) return SQLITE_NOMEM;
  1740   1740     memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot);
  1741   1741     if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm);
  1742   1742     p->aLTerm = paNew;
  1743   1743     p->nLSlot = n;
  1744   1744     return SQLITE_OK;
  1745   1745   }
................................................................................
  2028   2028       }
  2029   2029       sqlite3DebugPrintf("    add: ");
  2030   2030       whereLoopPrint(pTemplate, pBuilder->pWC);
  2031   2031     }
  2032   2032   #endif
  2033   2033     if( p==0 ){
  2034   2034       /* Allocate a new WhereLoop to add to the end of the list */
  2035         -    *ppPrev = p = sqlite3DbMallocRaw(db, sizeof(WhereLoop));
         2035  +    *ppPrev = p = sqlite3DbMallocRawNN(db, sizeof(WhereLoop));
  2036   2036       if( p==0 ) return SQLITE_NOMEM;
  2037   2037       whereLoopInit(p);
  2038   2038       p->pNextLoop = 0;
  2039   2039     }else{
  2040   2040       /* We will be overwriting WhereLoop p[].  But before we do, first
  2041   2041       ** go through the rest of the list and delete any other entries besides
  2042   2042       ** p[] that are also supplated by pTemplate */
................................................................................
  3525   3525     }else{
  3526   3526       nOrderBy = pWInfo->pOrderBy->nExpr;
  3527   3527     }
  3528   3528   
  3529   3529     /* Allocate and initialize space for aTo, aFrom and aSortCost[] */
  3530   3530     nSpace = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2;
  3531   3531     nSpace += sizeof(LogEst) * nOrderBy;
  3532         -  pSpace = sqlite3DbMallocRaw(db, nSpace);
         3532  +  pSpace = sqlite3DbMallocRawNN(db, nSpace);
  3533   3533     if( pSpace==0 ) return SQLITE_NOMEM;
  3534   3534     aTo = (WherePath*)pSpace;
  3535   3535     aFrom = aTo+mxChoice;
  3536   3536     memset(aFrom, 0, sizeof(aFrom[0]));
  3537   3537     pX = (WhereLoop**)(aFrom+mxChoice);
  3538   3538     for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){
  3539   3539       pFrom->aLoop = pX;

Changes to src/whereexpr.c.

    60     60   static int whereClauseInsert(WhereClause *pWC, Expr *p, u16 wtFlags){
    61     61     WhereTerm *pTerm;
    62     62     int idx;
    63     63     testcase( wtFlags & TERM_VIRTUAL );
    64     64     if( pWC->nTerm>=pWC->nSlot ){
    65     65       WhereTerm *pOld = pWC->a;
    66     66       sqlite3 *db = pWC->pWInfo->pParse->db;
    67         -    pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 );
           67  +    pWC->a = sqlite3DbMallocRawNN(db, sizeof(pWC->a[0])*pWC->nSlot*2 );
    68     68       if( pWC->a==0 ){
    69     69         if( wtFlags & TERM_DYNAMIC ){
    70     70           sqlite3ExprDelete(db, p);
    71     71         }
    72     72         pWC->a = pOld;
    73     73         return 0;
    74     74       }
................................................................................
   545    545     indexable = ~(Bitmask)0;
   546    546     chngToIN = ~(Bitmask)0;
   547    547     for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
   548    548       if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
   549    549         WhereAndInfo *pAndInfo;
   550    550         assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
   551    551         chngToIN = 0;
   552         -      pAndInfo = sqlite3DbMallocRaw(db, sizeof(*pAndInfo));
          552  +      pAndInfo = sqlite3DbMallocRawNN(db, sizeof(*pAndInfo));
   553    553         if( pAndInfo ){
   554    554           WhereClause *pAndWC;
   555    555           WhereTerm *pAndTerm;
   556    556           int j;
   557    557           Bitmask b = 0;
   558    558           pOrTerm->u.pAndInfo = pAndInfo;
   559    559           pOrTerm->wtFlags |= TERM_ANDINFO;