/ Check-in [ca5b789e]
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:Enhance (and fix) the MEMTYPE tags associated with heap memory allocations when SQLITE_MEMDEBUG is used.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: ca5b789e33c4e5ce366d8f5372d086442f84e230
User & Date: drh 2014-10-07 15:46:54
Context
2014-10-07
16:59
Restrict the scope of the valueToText() routine. check-in: 13c962b3 user: drh tags: trunk
15:46
Enhance (and fix) the MEMTYPE tags associated with heap memory allocations when SQLITE_MEMDEBUG is used. check-in: ca5b789e user: drh tags: trunk
2014-10-06
18:33
Remove unreachable branches in decodeIntArray() when compiling without STAT3 or STAT4. check-in: 80e1baa5 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/malloc.c.

   443    443   
   444    444   /*
   445    445   ** Return the size of a memory allocation previously obtained from
   446    446   ** sqlite3Malloc() or sqlite3_malloc().
   447    447   */
   448    448   int sqlite3MallocSize(void *p){
   449    449     assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
   450         -  assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
   451    450     return sqlite3GlobalConfig.m.xSize(p);
   452    451   }
   453    452   int sqlite3DbMallocSize(sqlite3 *db, void *p){
   454    453     if( db==0 ){
          454  +    assert( sqlite3MemdebugNoType(p, ~MEMTYPE_HEAP) );
          455  +    assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
   455    456       return sqlite3MallocSize(p);
   456    457     }else{
   457    458       assert( sqlite3_mutex_held(db->mutex) );
   458    459       if( isLookaside(db, p) ){
   459    460         return db->lookaside.sz;
   460    461       }else{
   461         -      assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
   462         -      assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
   463         -      assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
          462  +      assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
          463  +      assert( sqlite3MemdebugNoType(p, ~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
   464    464         return sqlite3GlobalConfig.m.xSize(p);
   465    465       }
   466    466     }
   467    467   }
   468    468   sqlite3_uint64 sqlite3_msize(void *p){
          469  +  assert( sqlite3MemdebugNoType(p, ~MEMTYPE_HEAP) );
          470  +  assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
   469    471     return (sqlite3_uint64)sqlite3GlobalConfig.m.xSize(p);
   470    472   }
   471    473   
   472    474   /*
   473    475   ** Free memory previously obtained from sqlite3Malloc().
   474    476   */
   475    477   void sqlite3_free(void *p){
   476    478     if( p==0 ) return;  /* IMP: R-49053-54554 */
   477         -  assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
   478    479     assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
          480  +  assert( sqlite3MemdebugNoType(p, ~MEMTYPE_HEAP) );
   479    481     if( sqlite3GlobalConfig.bMemstat ){
   480    482       sqlite3_mutex_enter(mem0.mutex);
   481    483       sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
   482    484       sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
   483    485       sqlite3GlobalConfig.m.xFree(p);
   484    486       sqlite3_mutex_leave(mem0.mutex);
   485    487     }else{
................................................................................
   515    517   #endif
   516    518         pBuf->pNext = db->lookaside.pFree;
   517    519         db->lookaside.pFree = pBuf;
   518    520         db->lookaside.nOut--;
   519    521         return;
   520    522       }
   521    523     }
   522         -  assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
   523         -  assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
          524  +  assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
          525  +  assert( sqlite3MemdebugNoType(p, ~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
   524    526     assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
   525    527     sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
   526    528     sqlite3_free(p);
   527    529   }
   528    530   
   529    531   /*
   530    532   ** Change the size of an existing memory allocation
   531    533   */
   532    534   void *sqlite3Realloc(void *pOld, u64 nBytes){
   533    535     int nOld, nNew, nDiff;
   534    536     void *pNew;
          537  +  assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
          538  +  assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) );
   535    539     if( pOld==0 ){
   536    540       return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
   537    541     }
   538    542     if( nBytes==0 ){
   539    543       sqlite3_free(pOld); /* IMP: R-26507-47431 */
   540    544       return 0;
   541    545     }
................................................................................
   554    558       sqlite3_mutex_enter(mem0.mutex);
   555    559       sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes);
   556    560       nDiff = nNew - nOld;
   557    561       if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >= 
   558    562             mem0.alarmThreshold-nDiff ){
   559    563         sqlite3MallocAlarm(nDiff);
   560    564       }
   561         -    assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
   562         -    assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) );
   563    565       pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
   564    566       if( pNew==0 && mem0.alarmCallback ){
   565    567         sqlite3MallocAlarm((int)nBytes);
   566    568         pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
   567    569       }
   568    570       if( pNew ){
   569    571         nNew = sqlite3MallocSize(pNew);
................................................................................
   668    670       return 0;
   669    671     }
   670    672   #endif
   671    673     p = sqlite3Malloc(n);
   672    674     if( !p && db ){
   673    675       db->mallocFailed = 1;
   674    676     }
   675         -  sqlite3MemdebugSetType(p, MEMTYPE_DB |
   676         -         ((db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
          677  +  sqlite3MemdebugSetType(p, 
          678  +         (db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
   677    679     return p;
   678    680   }
   679    681   
   680    682   /*
   681    683   ** Resize the block of memory pointed to by p to n bytes. If the
   682    684   ** resize fails, set the mallocFailed flag in the connection object.
   683    685   */
................................................................................
   695    697         }
   696    698         pNew = sqlite3DbMallocRaw(db, n);
   697    699         if( pNew ){
   698    700           memcpy(pNew, p, db->lookaside.sz);
   699    701           sqlite3DbFree(db, p);
   700    702         }
   701    703       }else{
   702         -      assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
   703         -      assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
          704  +      assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
          705  +      assert( sqlite3MemdebugNoType(p, ~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
   704    706         sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
   705    707         pNew = sqlite3_realloc64(p, n);
   706    708         if( !pNew ){
   707         -        sqlite3MemdebugSetType(p, MEMTYPE_DB|MEMTYPE_HEAP);
   708    709           db->mallocFailed = 1;
   709    710         }
   710         -      sqlite3MemdebugSetType(pNew, MEMTYPE_DB | 
          711  +      sqlite3MemdebugSetType(pNew,
   711    712               (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
   712    713       }
   713    714     }
   714    715     return pNew;
   715    716   }
   716    717   
   717    718   /*

Changes to src/mem2.c.

   390    390   /*
   391    391   ** Return TRUE if the mask of type in eType matches the type of the
   392    392   ** allocation p.  Also return true if p==NULL.
   393    393   **
   394    394   ** This routine is designed for use within an assert() statement, to
   395    395   ** verify the type of an allocation.  For example:
   396    396   **
   397         -**     assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
          397  +**     assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
   398    398   */
   399    399   int sqlite3MemdebugHasType(void *p, u8 eType){
   400    400     int rc = 1;
   401    401     if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
   402    402       struct MemBlockHdr *pHdr;
   403    403       pHdr = sqlite3MemsysGetHeader(p);
   404    404       assert( pHdr->iForeGuard==FOREGUARD );         /* Allocation is valid */
................................................................................
   412    412   /*
   413    413   ** Return TRUE if the mask of type in eType matches no bits of the type of the
   414    414   ** allocation p.  Also return true if p==NULL.
   415    415   **
   416    416   ** This routine is designed for use within an assert() statement, to
   417    417   ** verify the type of an allocation.  For example:
   418    418   **
   419         -**     assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
          419  +**     assert( sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
   420    420   */
   421    421   int sqlite3MemdebugNoType(void *p, u8 eType){
   422    422     int rc = 1;
   423    423     if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
   424    424       struct MemBlockHdr *pHdr;
   425    425       pHdr = sqlite3MemsysGetHeader(p);
   426    426       assert( pHdr->iForeGuard==FOREGUARD );         /* Allocation is valid */

Changes to src/sqliteInt.h.

  3796   3796     int sqlite3MemdebugNoType(void*,u8);
  3797   3797   #else
  3798   3798   # define sqlite3MemdebugSetType(X,Y)  /* no-op */
  3799   3799   # define sqlite3MemdebugHasType(X,Y)  1
  3800   3800   # define sqlite3MemdebugNoType(X,Y)   1
  3801   3801   #endif
  3802   3802   #define MEMTYPE_HEAP       0x01  /* General heap allocations */
  3803         -#define MEMTYPE_LOOKASIDE  0x02  /* Might have been lookaside memory */
         3803  +#define MEMTYPE_LOOKASIDE  0x02  /* Heap that might have been lookaside */
  3804   3804   #define MEMTYPE_SCRATCH    0x04  /* Scratch allocations */
  3805   3805   #define MEMTYPE_PCACHE     0x08  /* Page cache allocations */
  3806         -#define MEMTYPE_DB         0x10  /* Uses sqlite3DbMalloc, not sqlite_malloc */
  3807   3806   
  3808   3807   /*
  3809   3808   ** Threading interface
  3810   3809   */
  3811   3810   #if SQLITE_MAX_WORKER_THREADS>0
  3812   3811   int sqlite3ThreadCreate(SQLiteThread**,void*(*)(void*),void*);
  3813   3812   int sqlite3ThreadJoin(SQLiteThread*, void**);
  3814   3813   #endif
  3815   3814   
  3816   3815   #endif /* _SQLITEINT_H_ */