/ Check-in [4c2efd42]
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:Back out the use of __sync_fetch_and_sub() as it does not appear to work.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | gnu-safe-math
Files: files | file ages | folders
SHA1: 4c2efd4239bf07eb4b92d4af54edd68ee6312670
User & Date: drh 2017-01-03 21:50:49
Context
2017-01-03
21:57
Use compiler intrinsic functions for signed integer math when overflow detection is needed. check-in: d3ac32a6 user: drh tags: trunk
21:50
Back out the use of __sync_fetch_and_sub() as it does not appear to work. Closed-Leaf check-in: 4c2efd42 user: drh tags: gnu-safe-math
21:03
Fix a typo in a comment. check-in: ae087123 user: drh tags: gnu-safe-math
18:05
Use the GCC built-in __sync_fetch_and_sub() to make the sqlite3StatusDown() routine atomic, and thereby avoid some mutexing. check-in: f69ce75b user: drh tags: gnu-safe-math
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/malloc.c.

   372    372       }else{
   373    373         /* Release memory back to the heap */
   374    374         assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
   375    375         assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_SCRATCH) );
   376    376         sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
   377    377         if( sqlite3GlobalConfig.bMemstat ){
   378    378           int iSize = sqlite3MallocSize(p);
   379         -#ifndef SQLITE_ATOMIC_STATUS_DOWN
   380    379           sqlite3_mutex_enter(mem0.mutex);
   381         -#endif
   382    380           sqlite3StatusDown(SQLITE_STATUS_SCRATCH_OVERFLOW, iSize);
   383    381           sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, iSize);
   384    382           sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
   385    383           sqlite3GlobalConfig.m.xFree(p);
   386         -#ifndef SQLITE_ATOMIC_STATUS_DOWN
   387    384           sqlite3_mutex_leave(mem0.mutex);
   388         -#endif
   389    385         }else{
   390    386           sqlite3GlobalConfig.m.xFree(p);
   391    387         }
   392    388       }
   393    389     }
   394    390   }
   395    391   
................................................................................
   440    436   ** Free memory previously obtained from sqlite3Malloc().
   441    437   */
   442    438   void sqlite3_free(void *p){
   443    439     if( p==0 ) return;  /* IMP: R-49053-54554 */
   444    440     assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
   445    441     assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
   446    442     if( sqlite3GlobalConfig.bMemstat ){
   447         -#ifndef SQLITE_ATOMIC_STATUS_DOWN
   448    443       sqlite3_mutex_enter(mem0.mutex);
   449         -#endif
   450    444       sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p));
   451    445       sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
   452    446       sqlite3GlobalConfig.m.xFree(p);
   453         -#ifndef SQLITE_ATOMIC_STATUS_DOWN
   454    447       sqlite3_mutex_leave(mem0.mutex);
   455         -#endif
   456    448     }else{
   457    449       sqlite3GlobalConfig.m.xFree(p);
   458    450     }
   459    451   }
   460    452   
   461    453   /*
   462    454   ** Add the size of memory allocation "p" to the count in

Changes to src/sqliteInt.h.

   232    232   #  define SQLITE_NOINLINE  __attribute__((noinline))
   233    233   #elif defined(_MSC_VER) && _MSC_VER>=1310
   234    234   #  define SQLITE_NOINLINE  __declspec(noinline)
   235    235   #else
   236    236   #  define SQLITE_NOINLINE
   237    237   #endif
   238    238   
   239         -/*
   240         -** The SQLITE_ATOMIC_STATUS_DOWN macro is defined if and only if
   241         -** the sqlite3StatusDown() function is threadsafe.
   242         -*/
   243         -#if !defined(SQLITE_DISABLE_INTRINSIC) \
   244         -    && (GCC_VERSION>=4004000 || CLANG_VERSION>=3000000)
   245         -# define SQLITE_ATOMIC_STATUS_DOWN 1
   246         -#endif
   247         -
   248    239   /*
   249    240   ** Make sure that the compiler intrinsics we desire are enabled when
   250    241   ** compiling with an appropriate version of MSVC unless prevented by
   251    242   ** the SQLITE_DISABLE_INTRINSIC define.
   252    243   */
   253    244   #if !defined(SQLITE_DISABLE_INTRINSIC)
   254    245   #  if defined(_MSC_VER) && _MSC_VER>=1400

Changes to src/status.c.

    96     96     if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
    97     97       wsdStat.mxValue[op] = wsdStat.nowValue[op];
    98     98     }
    99     99   }
   100    100   void sqlite3StatusDown(int op, int N){
   101    101     wsdStatInit;
   102    102     assert( N>=0 );
   103         -  assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
   104         -#if !defined(SQLITE_DISABLE_INTRINSIC) \
   105         -    && (GCC_VERSION>=4004000 || CLANG_VERSION>=3000000)
   106         -  (void)__sync_fetch_and_sub(&wsdStat.nowValue[op], N);
   107         -#else
   108    103     assert( op>=0 && op<ArraySize(statMutex) );
   109    104     assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
   110    105                                              : sqlite3MallocMutex()) );
          106  +  assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
   111    107     wsdStat.nowValue[op] -= N;
   112         -#endif
   113    108   }
   114    109   
   115    110   /*
   116    111   ** Adjust the highwater mark if necessary.
   117    112   ** The caller must hold the appropriate mutex.
   118    113   */
   119    114   void sqlite3StatusHighwater(int op, int X){