/ Check-in [9c09ac35]
Login

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

Overview
Comment:Add the Mem.szMalloc element to the Mem object and use it to keep track of the size of the Mem.zMalloc allocation.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 9c09ac353df6041808cace41880f4729ee73f5e1
User & Date: drh 2014-09-18 21:25:33
Context
2014-09-19
04:42
Add the sqlite3VdbeMemClearAndResize() function. Fix a sorting-index prefilter problem. check-in: 987a7a21 user: drh tags: trunk
00:43
Add the sqlite3VdbeMemClearAndResize() interface to be used in place of sqlite3VdbeMemGrow(). A C++ style comment was left in this check-in by mistake, and so it has been moved into a branch to avoid problems in any future bisects on windows. check-in: 5b9b8987 user: drh tags: Cplusplus-comment
2014-09-18
21:25
Add the Mem.szMalloc element to the Mem object and use it to keep track of the size of the Mem.zMalloc allocation. check-in: 9c09ac35 user: drh tags: trunk
18:55
Correct typos in comments. No changes to code. check-in: 55879932 user: mistachkin tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/malloc.c.

   447    447   */
   448    448   int sqlite3MallocSize(void *p){
   449    449     assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
   450    450     assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
   451    451     return sqlite3GlobalConfig.m.xSize(p);
   452    452   }
   453    453   int sqlite3DbMallocSize(sqlite3 *db, void *p){
   454         -  assert( db!=0 );
   455         -  assert( sqlite3_mutex_held(db->mutex) );
   456         -  if( isLookaside(db, p) ){
   457         -    return db->lookaside.sz;
          454  +  if( db==0 ){
          455  +    return sqlite3MallocSize(p);
   458    456     }else{
   459         -    assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
   460         -    assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
   461         -    assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
   462         -    return sqlite3GlobalConfig.m.xSize(p);
          457  +    assert( sqlite3_mutex_held(db->mutex) );
          458  +    if( isLookaside(db, p) ){
          459  +      return db->lookaside.sz;
          460  +    }else{
          461  +      assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
          462  +      assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
          463  +      assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
          464  +      return sqlite3GlobalConfig.m.xSize(p);
          465  +    }
   463    466     }
   464    467   }
   465    468   sqlite3_uint64 sqlite3_msize(void *p){
   466    469     return (sqlite3_uint64)sqlite3GlobalConfig.m.xSize(p);
   467    470   }
   468    471   
   469    472   /*

Changes to src/test_func.c.

   500    500       pHdr += sqlite3GetVarint(pHdr, &iSerialType);
   501    501       pBody += sqlite3VdbeSerialGet(pBody, (u32)iSerialType, &mem);
   502    502   
   503    503       if( iCurrent==iIdx ){
   504    504         sqlite3_result_value(context, &mem);
   505    505       }
   506    506   
   507         -    sqlite3DbFree(db, mem.zMalloc);
          507  +    if( mem.szMalloc ) sqlite3DbFree(db, mem.zMalloc);
   508    508     }
   509    509   }
   510    510   
   511    511   /*
   512    512   ** tclcmd: test_decode(record)
   513    513   **
   514    514   ** This function implements an SQL user-function that accepts a blob
................................................................................
   587    587   
   588    588         default:
   589    589           assert( 0 );
   590    590       }
   591    591   
   592    592       Tcl_ListObjAppendElement(0, pRet, pVal);
   593    593   
   594         -    if( mem.zMalloc ){
          594  +    if( mem.szMalloc ){
   595    595         sqlite3DbFree(db, mem.zMalloc);
   596    596       }
   597    597     }
   598    598   
   599    599     sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT);
   600    600     Tcl_DecrRefCount(pRet);
   601    601   }

Changes to src/utf.c.

   316    316   
   317    317     c = pMem->flags;
   318    318     sqlite3VdbeMemRelease(pMem);
   319    319     pMem->flags = MEM_Str|MEM_Term|(c&MEM_AffMask);
   320    320     pMem->enc = desiredEnc;
   321    321     pMem->z = (char*)zOut;
   322    322     pMem->zMalloc = pMem->z;
          323  +  pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->z);
   323    324   
   324    325   translate_out:
   325    326   #if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
   326    327     {
   327    328       char zBuf[100];
   328    329       sqlite3VdbeMemPrettyPrint(pMem, zBuf);
   329    330       fprintf(stderr, "OUTPUT: %s\n", zBuf);

Changes to src/vdbe.c.

  1022   1022     pOp->p1 = sqlite3Strlen30(pOp->p4.z);
  1023   1023   
  1024   1024   #ifndef SQLITE_OMIT_UTF16
  1025   1025     if( encoding!=SQLITE_UTF8 ){
  1026   1026       rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
  1027   1027       if( rc==SQLITE_TOOBIG ) goto too_big;
  1028   1028       if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
  1029         -    assert( pOut->zMalloc==pOut->z );
         1029  +    assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
  1030   1030       assert( VdbeMemDynamic(pOut)==0 );
  1031         -    pOut->zMalloc = 0;
         1031  +    pOut->szMalloc = 0;
  1032   1032       pOut->flags |= MEM_Static;
  1033   1033       if( pOp->p4type==P4_DYNAMIC ){
  1034   1034         sqlite3DbFree(db, pOp->p4.z);
  1035   1035       }
  1036   1036       pOp->p4type = P4_DYNAMIC;
  1037   1037       pOp->p4.z = pOut->z;
  1038   1038       pOp->p1 = pOut->n;
................................................................................
  1144   1144   ** Move the P3 values in register P1..P1+P3-1 over into
  1145   1145   ** registers P2..P2+P3-1.  Registers P1..P1+P3-1 are
  1146   1146   ** left holding a NULL.  It is an error for register ranges
  1147   1147   ** P1..P1+P3-1 and P2..P2+P3-1 to overlap.  It is an error
  1148   1148   ** for P3 to be less than 1.
  1149   1149   */
  1150   1150   case OP_Move: {
  1151         -  char *zMalloc;   /* Holding variable for allocated memory */
  1152   1151     int n;           /* Number of registers left to copy */
  1153   1152     int p1;          /* Register to copy from */
  1154   1153     int p2;          /* Register to copy to */
  1155   1154   
  1156   1155     n = pOp->p3;
  1157   1156     p1 = pOp->p1;
  1158   1157     p2 = pOp->p2;
................................................................................
  1162   1161     pIn1 = &aMem[p1];
  1163   1162     pOut = &aMem[p2];
  1164   1163     do{
  1165   1164       assert( pOut<=&aMem[(p->nMem-p->nCursor)] );
  1166   1165       assert( pIn1<=&aMem[(p->nMem-p->nCursor)] );
  1167   1166       assert( memIsValid(pIn1) );
  1168   1167       memAboutToChange(p, pOut);
  1169         -    sqlite3VdbeMemRelease(pOut);
  1170         -    zMalloc = pOut->zMalloc;
  1171         -    memcpy(pOut, pIn1, sizeof(Mem));
         1168  +    sqlite3VdbeMemMove(pOut, pIn1);
  1172   1169   #ifdef SQLITE_DEBUG
  1173   1170       if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<&aMem[p1+pOp->p3] ){
  1174   1171         pOut->pScopyFrom += p1 - pOp->p2;
  1175   1172       }
  1176   1173   #endif
  1177         -    pIn1->flags = MEM_Undefined;
  1178         -    pIn1->zMalloc = zMalloc;
  1179   1174       REGISTER_TRACE(p2++, pOut);
  1180   1175       pIn1++;
  1181   1176       pOut++;
  1182   1177     }while( --n );
  1183   1178     break;
  1184   1179   }
  1185   1180   

Changes to src/vdbeInt.h.

   170    170       VdbeFrame *pFrame;  /* Used when flags==MEM_Frame */
   171    171     } u;
   172    172     u16 flags;          /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
   173    173     u8  enc;            /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */
   174    174     int n;              /* Number of characters in string value, excluding '\0' */
   175    175     char *z;            /* String or BLOB value */
   176    176     /* ShallowCopy only needs to copy the information above */
   177         -  char *zMalloc;      /* Dynamic buffer allocated by sqlite3_malloc() */
          177  +  char *zMalloc;      /* Space to hold MEM_Str or MEM_Blob if szMalloc>0 */
          178  +  int szMalloc;       /* Size of the zMalloc allocation */
          179  +  int iPadding1;      /* Padding for 8-byte alignment */
   178    180     sqlite3 *db;        /* The associated database connection */
   179    181     void (*xDel)(void*);/* Destructor for Mem.z - only valid if MEM_Dyn */
   180    182   #ifdef SQLITE_DEBUG
   181    183     Mem *pScopyFrom;    /* This Mem is a shallow copy of pScopyFrom */
   182    184     void *pFiller;      /* So that sizeof(Mem) is a multiple of 8 */
   183    185   #endif
   184    186   };

Changes to src/vdbeapi.c.

   805    805       = {
   806    806           /* .u          = */ {0},
   807    807           /* .flags      = */ MEM_Null,
   808    808           /* .enc        = */ 0,
   809    809           /* .n          = */ 0,
   810    810           /* .z          = */ 0,
   811    811           /* .zMalloc    = */ 0,
          812  +        /* .szMalloc   = */ 0,
          813  +        /* .iPadding1  = */ 0,
   812    814           /* .db         = */ 0,
   813    815           /* .xDel       = */ 0,
   814    816   #ifdef SQLITE_DEBUG
   815    817           /* .pScopyFrom = */ 0,
   816    818           /* .pFiller    = */ 0,
   817    819   #endif
   818    820         };

Changes to src/vdbeaux.c.

   694    694           break;
   695    695         }
   696    696         case P4_MEM: {
   697    697           if( db->pnBytesFreed==0 ){
   698    698             sqlite3ValueFree((sqlite3_value*)p4);
   699    699           }else{
   700    700             Mem *p = (Mem*)p4;
   701         -          sqlite3DbFree(db, p->zMalloc);
          701  +          if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
   702    702             sqlite3DbFree(db, p);
   703    703           }
   704    704           break;
   705    705         }
   706    706         case P4_VTAB : {
   707    707           if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
   708    708           break;
................................................................................
  1227   1227   static void releaseMemArray(Mem *p, int N){
  1228   1228     if( p && N ){
  1229   1229       Mem *pEnd;
  1230   1230       sqlite3 *db = p->db;
  1231   1231       u8 malloc_failed = db->mallocFailed;
  1232   1232       if( db->pnBytesFreed ){
  1233   1233         for(pEnd=&p[N]; p<pEnd; p++){
  1234         -        sqlite3DbFree(db, p->zMalloc);
         1234  +        if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
  1235   1235         }
  1236   1236         return;
  1237   1237       }
  1238   1238       for(pEnd=&p[N]; p<pEnd; p++){
  1239   1239         assert( (&p[1])==pEnd || p[0].db==p[1].db );
  1240   1240         assert( sqlite3VdbeCheckMemInvariants(p) );
  1241   1241   
................................................................................
  1253   1253         */
  1254   1254         testcase( p->flags & MEM_Agg );
  1255   1255         testcase( p->flags & MEM_Dyn );
  1256   1256         testcase( p->flags & MEM_Frame );
  1257   1257         testcase( p->flags & MEM_RowSet );
  1258   1258         if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){
  1259   1259           sqlite3VdbeMemRelease(p);
  1260         -      }else if( p->zMalloc ){
         1260  +      }else if( p->szMalloc ){
  1261   1261           sqlite3DbFree(db, p->zMalloc);
  1262         -        p->zMalloc = 0;
         1262  +        p->szMalloc = 0;
  1263   1263         }
  1264   1264   
  1265   1265         p->flags = MEM_Undefined;
  1266   1266       }
  1267   1267       db->mallocFailed = malloc_failed;
  1268   1268     }
  1269   1269   }
................................................................................
  3163   3163     while( idx<szHdr && d<=nKey ){
  3164   3164       u32 serial_type;
  3165   3165   
  3166   3166       idx += getVarint32(&aKey[idx], serial_type);
  3167   3167       pMem->enc = pKeyInfo->enc;
  3168   3168       pMem->db = pKeyInfo->db;
  3169   3169       /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
  3170         -    pMem->zMalloc = 0;
         3170  +    pMem->szMalloc = 0;
  3171   3171       d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
  3172   3172       pMem++;
  3173   3173       if( (++u)>=p->nField ) break;
  3174   3174     }
  3175   3175     assert( u<=pKeyInfo->nField + 1 );
  3176   3176     p->nField = u;
  3177   3177   }
................................................................................
  3203   3203     Mem mem1;
  3204   3204   
  3205   3205     pKeyInfo = pPKey2->pKeyInfo;
  3206   3206     if( pKeyInfo->db==0 ) return 1;
  3207   3207     mem1.enc = pKeyInfo->enc;
  3208   3208     mem1.db = pKeyInfo->db;
  3209   3209     /* mem1.flags = 0;  // Will be initialized by sqlite3VdbeSerialGet() */
  3210         -  VVA_ONLY( mem1.zMalloc = 0; ) /* Only needed by assert() statements */
         3210  +  VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */
  3211   3211   
  3212   3212     /* Compilers may complain that mem1.u.i is potentially uninitialized.
  3213   3213     ** We could initialize it, as shown here, to silence those complaints.
  3214   3214     ** But in fact, mem1.u.i will never actually be used uninitialized, and doing 
  3215   3215     ** the unnecessary initialization has a measurable negative performance
  3216   3216     ** impact, since this routine is a very high runner.  And so, we choose
  3217   3217     ** to ignore the compiler warnings and leave this variable uninitialized.
................................................................................
  3246   3246       */
  3247   3247       d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
  3248   3248   
  3249   3249       /* Do the comparison
  3250   3250       */
  3251   3251       rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i], pKeyInfo->aColl[i]);
  3252   3252       if( rc!=0 ){
  3253         -      assert( mem1.zMalloc==0 );  /* See comment below */
         3253  +      assert( mem1.szMalloc==0 );  /* See comment below */
  3254   3254         if( pKeyInfo->aSortOrder[i] ){
  3255   3255           rc = -rc;  /* Invert the result for DESC sort order. */
  3256   3256         }
  3257   3257         goto debugCompareEnd;
  3258   3258       }
  3259   3259       i++;
  3260   3260     }while( idx1<szHdr1 && i<pPKey2->nField );
  3261   3261   
  3262   3262     /* No memory allocation is ever used on mem1.  Prove this using
  3263   3263     ** the following assert().  If the assert() fails, it indicates a
  3264   3264     ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
  3265   3265     */
  3266         -  assert( mem1.zMalloc==0 );
         3266  +  assert( mem1.szMalloc==0 );
  3267   3267   
  3268   3268     /* rc==0 here means that one of the keys ran out of fields and
  3269   3269     ** all the fields up to that point were equal. Return the default_rc
  3270   3270     ** value.  */
  3271   3271     rc = pPKey2->default_rc;
  3272   3272   
  3273   3273   debugCompareEnd:
................................................................................
  3298   3298       return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
  3299   3299     }else{
  3300   3300       int rc;
  3301   3301       const void *v1, *v2;
  3302   3302       int n1, n2;
  3303   3303       Mem c1;
  3304   3304       Mem c2;
  3305         -    c1.db = c2.db = pMem1->db;
  3306         -    c1.flags = c2.flags = 0;
  3307         -    c1.zMalloc = c2.zMalloc = 0;
         3305  +    sqlite3VdbeMemInit(&c1, pMem1->db, MEM_Null);
         3306  +    sqlite3VdbeMemInit(&c2, pMem1->db, MEM_Null);
  3308   3307       sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
  3309   3308       sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
  3310   3309       v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
  3311   3310       n1 = v1==0 ? 0 : c1.n;
  3312   3311       v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
  3313   3312       n2 = v2==0 ? 0 : c2.n;
  3314   3313       rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
................................................................................
  3512   3511       if( d1>(unsigned)nKey1 ){ 
  3513   3512         pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
  3514   3513         return 0;  /* Corruption */
  3515   3514       }
  3516   3515       i = 0;
  3517   3516     }
  3518   3517   
  3519         -  VVA_ONLY( mem1.zMalloc = 0; ) /* Only needed by assert() statements */
         3518  +  VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */
  3520   3519     assert( pPKey2->pKeyInfo->nField+pPKey2->pKeyInfo->nXField>=pPKey2->nField 
  3521   3520          || CORRUPT_DB );
  3522   3521     assert( pPKey2->pKeyInfo->aSortOrder!=0 );
  3523   3522     assert( pPKey2->pKeyInfo->nField>0 );
  3524   3523     assert( idx1<=szHdr1 || CORRUPT_DB );
  3525   3524     do{
  3526   3525       u32 serial_type;
................................................................................
  3635   3634       }
  3636   3635   
  3637   3636       if( rc!=0 ){
  3638   3637         if( pKeyInfo->aSortOrder[i] ){
  3639   3638           rc = -rc;
  3640   3639         }
  3641   3640         assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, rc) );
  3642         -      assert( mem1.zMalloc==0 );  /* See comment below */
         3641  +      assert( mem1.szMalloc==0 );  /* See comment below */
  3643   3642         return rc;
  3644   3643       }
  3645   3644   
  3646   3645       i++;
  3647   3646       pRhs++;
  3648   3647       d1 += sqlite3VdbeSerialTypeLen(serial_type);
  3649   3648       idx1 += sqlite3VarintLen(serial_type);
  3650   3649     }while( idx1<(unsigned)szHdr1 && i<pPKey2->nField && d1<=(unsigned)nKey1 );
  3651   3650   
  3652   3651     /* No memory allocation is ever used on mem1.  Prove this using
  3653   3652     ** the following assert().  If the assert() fails, it indicates a
  3654   3653     ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).  */
  3655         -  assert( mem1.zMalloc==0 );
         3654  +  assert( mem1.szMalloc==0 );
  3656   3655   
  3657   3656     /* rc==0 here means that one or both of the keys ran out of fields and
  3658   3657     ** all the fields up to that point were equal. Return the default_rc
  3659   3658     ** value.  */
  3660   3659     assert( CORRUPT_DB 
  3661   3660          || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc) 
  3662   3661          || pKeyInfo->db->mallocFailed
................................................................................
  3933   3932     *rowid = v.u.i;
  3934   3933     sqlite3VdbeMemRelease(&m);
  3935   3934     return SQLITE_OK;
  3936   3935   
  3937   3936     /* Jump here if database corruption is detected after m has been
  3938   3937     ** allocated.  Free the m object and return SQLITE_CORRUPT. */
  3939   3938   idx_rowid_corruption:
  3940         -  testcase( m.zMalloc!=0 );
         3939  +  testcase( m.szMalloc!=0 );
  3941   3940     sqlite3VdbeMemRelease(&m);
  3942   3941     return SQLITE_CORRUPT_BKPT;
  3943   3942   }
  3944   3943   
  3945   3944   /*
  3946   3945   ** Compare the key of the index entry that cursor pC is pointing to against
  3947   3946   ** the key string in pUnpacked.  Write into *pRes a number

Changes to src/vdbemem.c.

    29     29     /* If MEM_Dyn is set then Mem.xDel!=0.  
    30     30     ** Mem.xDel is might not be initialized if MEM_Dyn is clear.
    31     31     */
    32     32     assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 );
    33     33   
    34     34     /* Cannot be both MEM_Int and MEM_Real at the same time */
    35     35     assert( (p->flags & (MEM_Int|MEM_Real))!=(MEM_Int|MEM_Real) );
           36  +
           37  +  /* The szMalloc field holds the correct memory allocation size */
           38  +  assert( p->szMalloc==0
           39  +       || p->szMalloc==sqlite3DbMallocSize(p->db,p->zMalloc) );
    36     40   
    37     41     /* If p holds a string or blob, the Mem.z must point to exactly
    38     42     ** one of the following:
    39     43     **
    40     44     **   (1) Memory in Mem.zMalloc and managed by the Mem object
    41     45     **   (2) Memory to be freed using Mem.xDel
    42     46     **   (3) An ephemeral string or blob
    43     47     **   (4) A static string or blob
    44     48     */
    45         -  if( (p->flags & (MEM_Str|MEM_Blob)) && p->z!=0 ){
           49  +  if( (p->flags & (MEM_Str|MEM_Blob)) && p->n>0 ){
    46     50       assert( 
    47         -      ((p->z==p->zMalloc)? 1 : 0) +
           51  +      ((p->szMalloc>0 && p->z==p->zMalloc)? 1 : 0) +
    48     52         ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
    49     53         ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
    50     54         ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1
    51     55       );
    52     56     }
    53     57     return 1;
    54     58   }
................................................................................
   108    112     assert( (pMem->flags&MEM_RowSet)==0 );
   109    113   
   110    114     /* If the bPreserve flag is set to true, then the memory cell must already
   111    115     ** contain a valid string or blob value.  */
   112    116     assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
   113    117     testcase( bPreserve && pMem->z==0 );
   114    118   
   115         -  if( pMem->zMalloc==0 || sqlite3DbMallocSize(pMem->db, pMem->zMalloc)<n ){
          119  +  assert( pMem->szMalloc==0
          120  +       || pMem->szMalloc==sqlite3DbMallocSize(pMem->db, pMem->zMalloc) );
          121  +  if( pMem->szMalloc<n ){
   116    122       if( n<32 ) n = 32;
   117         -    if( bPreserve && pMem->z==pMem->zMalloc ){
          123  +    if( bPreserve && pMem->szMalloc>0 && pMem->z==pMem->zMalloc ){
   118    124         pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
   119    125         bPreserve = 0;
   120    126       }else{
   121         -      sqlite3DbFree(pMem->db, pMem->zMalloc);
          127  +      if( pMem->szMalloc>0 ) sqlite3DbFree(pMem->db, pMem->zMalloc);
   122    128         pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
   123    129       }
   124    130       if( pMem->zMalloc==0 ){
   125    131         sqlite3VdbeMemSetNull(pMem);
   126    132         pMem->z = 0;
          133  +      pMem->szMalloc = 0;
   127    134         return SQLITE_NOMEM;
          135  +    }else{
          136  +      pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
   128    137       }
   129    138     }
   130    139   
   131    140     if( pMem->z && bPreserve && pMem->z!=pMem->zMalloc ){
   132    141       memcpy(pMem->zMalloc, pMem->z, pMem->n);
   133    142     }
   134    143     if( (pMem->flags&MEM_Dyn)!=0 ){
................................................................................
   151    160   */
   152    161   int sqlite3VdbeMemMakeWriteable(Mem *pMem){
   153    162     int f;
   154    163     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   155    164     assert( (pMem->flags&MEM_RowSet)==0 );
   156    165     ExpandBlob(pMem);
   157    166     f = pMem->flags;
   158         -  if( (f&(MEM_Str|MEM_Blob)) && pMem->z!=pMem->zMalloc ){
          167  +  if( (f&(MEM_Str|MEM_Blob)) && (pMem->szMalloc==0 || pMem->z!=pMem->zMalloc) ){
   159    168       if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){
   160    169         return SQLITE_NOMEM;
   161    170       }
   162    171       pMem->z[pMem->n] = 0;
   163    172       pMem->z[pMem->n+1] = 0;
   164    173       pMem->flags |= MEM_Term;
   165    174   #ifdef SQLITE_DEBUG
................................................................................
   297    306       t.flags = MEM_Null;
   298    307       t.db = pMem->db;
   299    308       ctx.pOut = &t;
   300    309       ctx.pMem = pMem;
   301    310       ctx.pFunc = pFunc;
   302    311       pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
   303    312       assert( (pMem->flags & MEM_Dyn)==0 );
   304         -    sqlite3DbFree(pMem->db, pMem->zMalloc);
          313  +    if( pMem->szMalloc>0 ) sqlite3DbFree(pMem->db, pMem->zMalloc);
   305    314       memcpy(pMem, &t, sizeof(t));
   306    315       rc = ctx.isError;
   307    316     }
   308    317     return rc;
   309    318   }
   310    319   
   311    320   /*
................................................................................
   347    356   ** the unusual case where there really is memory in p that needs
   348    357   ** to be freed.
   349    358   */
   350    359   static SQLITE_NOINLINE void vdbeMemClear(Mem *p){
   351    360     if( VdbeMemDynamic(p) ){
   352    361       vdbeMemClearExternAndSetNull(p);
   353    362     }
   354         -  if( p->zMalloc ){
          363  +  if( p->szMalloc ){
   355    364       sqlite3DbFree(p->db, p->zMalloc);
   356         -    p->zMalloc = 0;
          365  +    p->szMalloc = 0;
   357    366     }
   358    367     p->z = 0;
   359    368   }
   360    369   
   361    370   /*
   362    371   ** Release any memory resources held by the Mem.  Both the memory that is
   363    372   ** free by Mem.xDel and the Mem.zMalloc allocation are freed.
................................................................................
   366    375   ** reset a Mem back to its minimum memory utilization.
   367    376   **
   368    377   ** Use sqlite3VdbeMemSetNull() to release just the Mem.xDel space
   369    378   ** prior to inserting new content into the Mem.
   370    379   */
   371    380   void sqlite3VdbeMemRelease(Mem *p){
   372    381     assert( sqlite3VdbeCheckMemInvariants(p) );
   373         -  if( VdbeMemDynamic(p) || p->zMalloc ){
          382  +  if( VdbeMemDynamic(p) || p->szMalloc ){
   374    383       vdbeMemClear(p);
   375    384     }
   376    385   }
   377    386   
   378    387   /*
   379    388   ** Convert a 64-bit IEEE double into a 64-bit signed integer.
   380    389   ** If the double is out of range of a 64-bit signed integer then
................................................................................
   588    597   **
   589    598   ** The minimum amount of initialization feasible is performed.
   590    599   */
   591    600   void sqlite3VdbeMemInit(Mem *pMem, sqlite3 *db, u16 flags){
   592    601     assert( (flags & ~MEM_TypeMask)==0 );
   593    602     pMem->flags = flags;
   594    603     pMem->db = db;
   595         -  pMem->zMalloc = 0;
          604  +  pMem->szMalloc = 0;
   596    605   }
   597    606   
   598    607   
   599    608   /*
   600    609   ** Delete any previous value and set the value stored in *pMem to NULL.
   601    610   **
   602    611   ** This routine calls the Mem.xDel destructor to dispose of values that
................................................................................
   679    688     sqlite3 *db = pMem->db;
   680    689     assert( db!=0 );
   681    690     assert( (pMem->flags & MEM_RowSet)==0 );
   682    691     sqlite3VdbeMemRelease(pMem);
   683    692     pMem->zMalloc = sqlite3DbMallocRaw(db, 64);
   684    693     if( db->mallocFailed ){
   685    694       pMem->flags = MEM_Null;
          695  +    pMem->szMalloc = 0;
   686    696     }else{
   687    697       assert( pMem->zMalloc );
   688         -    pMem->u.pRowSet = sqlite3RowSetInit(db, pMem->zMalloc, 
   689         -                                       sqlite3DbMallocSize(db, pMem->zMalloc));
          698  +    pMem->szMalloc = sqlite3DbMallocSize(db, pMem->zMalloc);
          699  +    pMem->u.pRowSet = sqlite3RowSetInit(db, pMem->zMalloc, pMem->szMalloc);
   690    700       assert( pMem->u.pRowSet!=0 );
   691    701       pMem->flags = MEM_RowSet;
   692    702     }
   693    703   }
   694    704   
   695    705   /*
   696    706   ** Return true if the Mem object contains a TEXT or BLOB that is
................................................................................
   785    795     assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
   786    796     assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
   787    797     assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
   788    798   
   789    799     sqlite3VdbeMemRelease(pTo);
   790    800     memcpy(pTo, pFrom, sizeof(Mem));
   791    801     pFrom->flags = MEM_Null;
   792         -  pFrom->zMalloc = 0;
          802  +  pFrom->szMalloc = 0;
   793    803   }
   794    804   
   795    805   /*
   796    806   ** Change the value of a Mem to be a string or a BLOB.
   797    807   **
   798    808   ** The memory management strategy depends on the value of the xDel
   799    809   ** parameter. If the value passed is SQLITE_TRANSIENT, then the 
................................................................................
   859    869       if( sqlite3VdbeMemGrow(pMem, nAlloc, 0) ){
   860    870         return SQLITE_NOMEM;
   861    871       }
   862    872       memcpy(pMem->z, z, nAlloc);
   863    873     }else if( xDel==SQLITE_DYNAMIC ){
   864    874       sqlite3VdbeMemRelease(pMem);
   865    875       pMem->zMalloc = pMem->z = (char *)z;
          876  +    pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
   866    877     }else{
   867    878       sqlite3VdbeMemRelease(pMem);
   868    879       pMem->z = (char *)z;
   869    880       pMem->xDel = xDel;
   870    881       flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
   871    882     }
   872    883   
................................................................................
  1481   1492   void sqlite3Stat4ProbeFree(UnpackedRecord *pRec){
  1482   1493     if( pRec ){
  1483   1494       int i;
  1484   1495       int nCol = pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField;
  1485   1496       Mem *aMem = pRec->aMem;
  1486   1497       sqlite3 *db = aMem[0].db;
  1487   1498       for(i=0; i<nCol; i++){
  1488         -      sqlite3DbFree(db, aMem[i].zMalloc);
         1499  +      if( aMem[i].szMalloc ) sqlite3DbFree(db, aMem[i].zMalloc);
  1489   1500       }
  1490   1501       sqlite3KeyInfoUnref(pRec->pKeyInfo);
  1491   1502       sqlite3DbFree(db, pRec);
  1492   1503     }
  1493   1504   }
  1494   1505   #endif /* ifdef SQLITE_ENABLE_STAT4 */
  1495   1506