/ Check-in [548bf3f7]
Login

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

Overview
Comment:Fixes to prior checkins so that they compile and run even if SQLITE_MEMDEBUG is not defined.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | malloc-enhancement
Files: files | file ages | folders
SHA1:548bf3f7d7b962d3eb0f5c874ecf40a8703d4d5d
User & Date: drh 2010-07-25 02:39:07
Original Comment: Fixes to prior checkins so that they compile and run even if SQLITE_MEMDEBUG is not defined.
Context
2010-07-25
02:39
Fixes to prior checkins so that they compile and run even if SQLITE_MEMDEBUG is not defined. Closed-Leaf check-in: 548bf3f7 user: drh tags: malloc-enhancement
02:12
Further examples of using automatic deallocation to replace "delete" methods. check-in: da2f62c5 user: drh tags: malloc-enhancement
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/malloc.c.

    78     78   ** Macros for querying and setting debugging fields of the EMemHdr object.
    79     79   */
    80     80   #ifdef SQLITE_MEMDEBUG
    81     81   # define isValidEMem(E)     ((E)->iEMemMagic==0xc0a43fad)
    82     82   # define setValidEMem(E)    (E)->iEMemMagic = 0xc0a43fad
    83     83   # define clearValidEMem(E)  (E)->iEMemMagic = 0x12345678
    84     84   # define isChildEMem(E)     ((E)->isAChild!=0)
           85  +# define notChildEMem(E)    ((E)->isAChild==0)
    85     86   # define setChildEMem(E)    (E)->isAChild = 1
    86     87   # define clearChildEMem(E)  (E)->isAChild = 0
    87     88   #else
    88         -# define isValidEMem(E)
           89  +# define isValidEMem(E)     1
    89     90   # define setValidEMem(E)
    90     91   # define clearValidEMem(E)
    91         -# define isChildEMem(E)
           92  +# define isChildEMem(E)     1
           93  +# define notChildEMem(E)    1
    92     94   # define setChildEMem(E)
    93     95   # define clearChildEMem(E)
    94     96   #endif
    95     97   
    96     98   /*
    97     99   ** This routine runs when the memory allocator sees that the
    98    100   ** total memory allocation is about to exceed the soft heap
................................................................................
   446    448   }
   447    449   void sqlite3ScratchFree(void *p){
   448    450     if( p ){
   449    451       if( sqlite3GlobalConfig.pScratch==0
   450    452              || p<sqlite3GlobalConfig.pScratch
   451    453              || p>=(void*)mem0.aScratchFree ){
   452    454         assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
   453         -      assert( !sqlite3MemdebugHasType(p, ~MEMTYPE_SCRATCH) );
          455  +      assert( sqlite3MemdebugNoType(p, ~MEMTYPE_SCRATCH) );
   454    456         sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
   455    457         if( sqlite3GlobalConfig.bMemstat ){
   456    458           int iSize = sqlite3MallocSize(p);
   457    459           sqlite3_mutex_enter(mem0.mutex);
   458    460           sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
   459    461           sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
   460    462           sqlite3GlobalConfig.m.xFree(p);
................................................................................
   502    504   ** sqlite3Malloc() or sqlite3_malloc().
   503    505   **
   504    506   ** The size returned is the usable size and does not include any
   505    507   ** bookkeeping overhead or sentinals at the end of the allocation.
   506    508   */
   507    509   int sqlite3MallocSize(void *p){
   508    510     assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
   509         -  assert( !sqlite3MemdebugHasType(p, MEMTYPE_RECURSIVE) );
          511  +  assert( sqlite3MemdebugNoType(p, MEMTYPE_RECURSIVE) );
   510    512     return sqlite3GlobalConfig.m.xSize(p);
   511    513   }
   512    514   int sqlite3DbMallocSize(sqlite3 *db, void *pObj){
   513    515     EMemHdr *p = (EMemHdr*)pObj;
   514    516     assert( db==0 || sqlite3_mutex_held(db->mutex) );
   515    517     if( p ){
   516    518       p--;
................................................................................
   527    529   }
   528    530   
   529    531   /*
   530    532   ** Free memory previously obtained from sqlite3Malloc().
   531    533   */
   532    534   void sqlite3_free(void *p){
   533    535     if( p==0 ) return;
   534         -  assert( !sqlite3MemdebugHasType(p, MEMTYPE_RECURSIVE) );
          536  +  assert( sqlite3MemdebugNoType(p, MEMTYPE_RECURSIVE) );
   535    537     assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
   536    538     if( sqlite3GlobalConfig.bMemstat ){
   537    539       sqlite3_mutex_enter(mem0.mutex);
   538    540       sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
   539    541       sqlite3GlobalConfig.m.xFree(p);
   540    542       sqlite3_mutex_leave(mem0.mutex);
   541    543     }else{
................................................................................
   551    553   ** allowed to delete a child allocation since that would leave a
   552    554   ** dangling child pointer in the parent.
   553    555   */
   554    556   void sqlite3DbFree(sqlite3 *db, void *pObj){
   555    557     EMemHdr *p = (EMemHdr*)pObj;
   556    558     assert( db==0 || sqlite3_mutex_held(db->mutex) );
   557    559     if( p ) p--;
   558         -  assert( p==0 || !isChildEMem(p) );  /* pObj is not child allocation */
          560  +  assert( p==0 || notChildEMem(p) );  /* pObj is not child allocation */
   559    561     while( p ){
   560    562       EMemHdr *pNext = p->pESibling;
   561    563       assert( isValidEMem(p) );   /* pObj and all siblings are valid */
   562    564       if( p->pEChild ){
   563    565         clearChildEMem(p->pEChild);
   564    566         sqlite3DbFree(db, (void*)&p->pEChild[1]);
   565    567       }
................................................................................
   609    611       sqlite3_mutex_enter(mem0.mutex);
   610    612       sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
   611    613       if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >= 
   612    614             mem0.alarmThreshold ){
   613    615         sqlite3MallocAlarm(nNew-nOld);
   614    616       }
   615    617       assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
   616         -    assert( !sqlite3MemdebugHasType(pOld, ~MEMTYPE_HEAP) );
          618  +    assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) );
   617    619       pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
   618    620       if( pNew==0 && mem0.alarmCallback ){
   619    621         sqlite3MallocAlarm(nBytes);
   620    622         pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
   621    623       }
   622    624       if( pNew ){
   623    625         nNew = sqlite3MallocSize(pNew);
................................................................................
   765    767     assert( sqlite3_mutex_held(db->mutex) );
   766    768     if( db->mallocFailed==0 ){
   767    769       if( p==0 ){
   768    770         return sqlite3DbMallocRaw(db, n);
   769    771       }
   770    772       p--;
   771    773       assert( isValidEMem(p) );    /* pOld obtained from extended allocator */
   772         -    assert( !isChildEMem(p) );   /* pOld must not be a child allocation */
          774  +    assert( notChildEMem(p) );   /* pOld must not be a child allocation */
   773    775       if( isLookaside(db, p) ){
   774    776         if( n+sizeof(EMemHdr)<=db->lookaside.sz ){
   775    777           return pOld;
   776    778         }
   777    779         pNew = sqlite3DbMallocRaw(db, n);
   778    780         if( pNew ){
   779    781           memcpy(pNew-1, p, db->lookaside.sz);
................................................................................
   855    857     EMemHdr *pParent = (EMemHdr*)pParentObj;
   856    858     EMemHdr *pChild = (EMemHdr*)pChildObj;
   857    859     if( pParent && pChild ){
   858    860       pParent--;
   859    861       assert( isValidEMem(pParent) );  /* pParentObj is an extended allocation */ 
   860    862       pChild--;
   861    863       assert( isValidEMem(pChild) );   /* pChildObj is an extended allocation */
   862         -    assert( !isChildEMem(pChild) );  /* pChildObj not a child of another obj */
          864  +    assert( notChildEMem(pChild) );  /* pChildObj not a child of another obj */
   863    865       pChild->pESibling = pParent->pEChild;
   864    866       pParent->pEChild = pChild;
   865    867       setChildEMem(pChild);
   866    868     }
   867    869   }
   868    870   
   869    871   /*

Changes to src/mem2.c.

   404    404       if( (pHdr->eType&eType)==0 ){
   405    405         rc = 0;
   406    406       }
   407    407     }
   408    408     return rc;
   409    409   }
   410    410    
          411  +/*
          412  +** Return TRUE if the mask of type in eType matches no bits of the type of the
          413  +** allocation p.  Also return true if p==NULL.
          414  +**
          415  +** This routine is designed for use within an assert() statement, to
          416  +** verify the type of an allocation.  For example:
          417  +**
          418  +**     assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
          419  +*/
          420  +int sqlite3MemdebugNoType(void *p, u8 eType){
          421  +  int rc = 1;
          422  +  if( p ){
          423  +    struct MemBlockHdr *pHdr;
          424  +    pHdr = sqlite3MemsysGetHeader(p);
          425  +    assert( pHdr->iForeGuard==FOREGUARD );         /* Allocation is valid */
          426  +    if( (pHdr->eType&eType)!=0 ){
          427  +      rc = 0;
          428  +    }
          429  +  }
          430  +  return rc;
          431  +}
   411    432   
   412    433   /*
   413    434   ** Set the number of backtrace levels kept for each allocation.
   414    435   ** A value of zero turns off backtracing.  The number is always rounded
   415    436   ** up to a multiple of 2.
   416    437   */
   417    438   void sqlite3MemdebugBacktrace(int depth){

Changes to src/sqliteInt.h.

  3117   3117   ** sqlite3MemdebugSetType() sets the "type" of an allocation to one of
  3118   3118   ** the MEMTYPE_* macros defined below.  The type must be a bitmask with
  3119   3119   ** a single bit set.
  3120   3120   **
  3121   3121   ** sqlite3MemdebugHasType() returns true if any of the bits in its second
  3122   3122   ** argument match the type set by the previous sqlite3MemdebugSetType().
  3123   3123   ** sqlite3MemdebugHasType() is intended for use inside assert() statements.
         3124  +**
         3125  +** sqlite3MemdebugNoType() returns true if none of the bits in its second
         3126  +** argument match the type set by the previous sqlite3MemdebugSetType().
         3127  +**
  3124   3128   ** For example:
  3125   3129   **
  3126   3130   **     assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
  3127   3131   **
  3128   3132   ** Perhaps the most important point is the difference between MEMTYPE_HEAP
  3129   3133   ** and MEMTYPE_DB.  If an allocation is MEMTYPE_DB, that means it might have
  3130   3134   ** been allocated by lookaside, except the allocation was too large or
................................................................................
  3135   3139   **
  3136   3140   ** All of this is no-op for a production build.  It only comes into
  3137   3141   ** play when the SQLITE_MEMDEBUG compile-time option is used.
  3138   3142   */
  3139   3143   #ifdef SQLITE_MEMDEBUG
  3140   3144     void sqlite3MemdebugSetType(void*,u8);
  3141   3145     int sqlite3MemdebugHasType(void*,u8);
         3146  +  int sqlite3MemdebugNoType(void*,u8);
  3142   3147   #else
  3143   3148   # define sqlite3MemdebugSetType(X,Y)  /* no-op */
  3144   3149   # define sqlite3MemdebugHasType(X,Y)  1
         3150  +# define sqlite3MemdebugNoType(X,Y)   1
  3145   3151   #endif
  3146   3152   #define MEMTYPE_HEAP      0x01    /* General heap allocations */
  3147   3153   #define MEMTYPE_DB        0x02    /* Associated with a database connection */
  3148   3154   #define MEMTYPE_SCRATCH   0x04    /* Scratch allocations */
  3149   3155   #define MEMTYPE_PCACHE    0x08    /* Page cache allocations */
  3150   3156   #define MEMTYPE_RECURSIVE 0x10    /* Experimental */
  3151   3157   
  3152   3158   #endif /* _SQLITEINT_H_ */