]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Hand merge the zone allocator for MacOS from the apple-osx branch.
authordrh <drh@noemail.net>
Thu, 8 Dec 2011 20:41:33 +0000 (20:41 +0000)
committerdrh <drh@noemail.net>
Thu, 8 Dec 2011 20:41:33 +0000 (20:41 +0000)
FossilOrigin-Name: 0d955c20c02da29582b5cd8df2b7124fb9d12ebb

manifest
manifest.uuid
src/mem1.c

index 5a23b09df7f4b9ce704c2fbe9cefe383b4f08b1c..49900ac0bee095354b35d6e125a0b081b30a38c8 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C In\spersistent\sWAL\smode,\struncate\sthe\sWAL\sfile\sto\sthe\ssize\sspecified\sby\sthe\njournal_size_limit\spragma\swhen\sdisconnecting\sfrom\sthe\sWAL.
-D 2011-12-08T19:50:32.953
+C Hand\smerge\sthe\szone\sallocator\sfor\sMacOS\sfrom\sthe\sapple-osx\sbranch.
+D 2011-12-08T20:41:33.299
 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
 F Makefile.in 5b4a3e12a850b021547e43daf886b25133b44c07
 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -150,7 +150,7 @@ F src/loadext.c d0d2022a5a07274d408820b978b9e549189d314f
 F src/main.c 8be1ee70dd90ef7562c801dbe946a4f9f93bb128
 F src/malloc.c 591aedb20ae40813f1045f2ef253438a334775d9
 F src/mem0.c 6a55ebe57c46ca1a7d98da93aaa07f99f1059645
-F src/mem1.c 7456e2ca0524609ebc06a9befeda5289d4575ad4
+F src/mem1.c f96706e354e5e540305ba4cfe315fe52173a9f9e
 F src/mem2.c e307323e86b5da1853d7111b68fd6b84ad6f09cf
 F src/mem3.c 61c9d47b792908c532ca3a62b999cf21795c6534
 F src/mem5.c c2c63b7067570b00bf33d751c39af24182316f7f
@@ -977,7 +977,7 @@ F tool/tostr.awk e75472c2f98dd76e06b8c9c1367f4ab07e122d06
 F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f
 F tool/warnings-clang.sh 9f406d66e750e8ac031c63a9ef3248aaa347ef2a
 F tool/warnings.sh fbc018d67fd7395f440c28f33ef0f94420226381
-P a0d92193dd5ae97608748f354aa17eb188431546
-R 2af387da3b388b68a597840e0b4cefff
+P 9687b305c2320109a8649612181eecd2e0da7c7b
+R 83ba2438b14dfac192d8fdfea2ba6d38
 U drh
-Z 15eed8d4acdafc18909017c3c88afe5e
+Z a86d3022d423432c98b77483099e12da
index ca3c77e6c40adcf2db9c03e991b787155d6591c4..199e8418f594ba659fe3c736094925d3976e17b8 100644 (file)
@@ -1 +1 @@
-9687b305c2320109a8649612181eecd2e0da7c7b
\ No newline at end of file
+0d955c20c02da29582b5cd8df2b7124fb9d12ebb
\ No newline at end of file
index e0d1dd6e8f29983e75989bdfa1174b8a4d7e3212..3f786a4b5590e26516e8913e2d38bfc921e46a61 100644 (file)
 */
 #ifdef SQLITE_SYSTEM_MALLOC
 
+
+#if defined(__APPLE__)
+
+/*
+** Use the zone allocator available on apple products
+*/
+#include <sys/sysctl.h>
+#include <malloc/malloc.h>
+#include <libkern/OSAtomic.h>
+static malloc_zone_t* _sqliteZone_;
+#define SQLITE_MALLOC(x) malloc_zone_malloc(_sqliteZone_, (x))
+#define SQLITE_FREE(x) malloc_zone_free(_sqliteZone_, (x));
+#define SQLITE_REALLOC(x,y) malloc_zone_realloc(_sqliteZone_, (x), (y))
+#define SQLITE_MALLOCSIZE(x) \
+        (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x))
+
+#else /* if not __APPLE__ */
+
+/*
+** Use standard C library malloc and free on non-Apple systems.
+*/
+#define SQLITE_MALLOC(x)    malloc(x)
+#define SQLITE_FREE(x)      free(x)
+#define SQLITE_REALLOC(x,y) realloc((x),(y))
+
 #ifdef HAVE_MALLOC_USABLE_SIZE
 #include <malloc.h>
+#define SQLITE_MALLOCSIZE(x) malloc_usable_size(x)
+#else
+#undef SQLITE_MALLOCSIZE
 #endif
 
+#endif /* __APPLE__ or not __APPLE__ */
+
 /*
 ** Like malloc(), but remember the size of the allocation
 ** so that we can find it later using sqlite3MemSize().
@@ -39,8 +69,8 @@
 ** routines.
 */
 static void *sqlite3MemMalloc(int nByte){
-#ifdef HAVE_MALLOC_USABLE_SIZE
-  void *p = malloc( nByte );
+#ifdef SQLITE_MALLOCSIZE
+  void *p = SQLITE_MALLOC( nByte );
   if( p==0 ){
     testcase( sqlite3GlobalConfig.xLog!=0 );
     sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
@@ -50,7 +80,7 @@ static void *sqlite3MemMalloc(int nByte){
   sqlite3_int64 *p;
   assert( nByte>0 );
   nByte = ROUND8(nByte);
-  p = malloc( nByte+8 );
+  p = SQLITE_MALLOC( nByte+8 );
   if( p ){
     p[0] = nByte;
     p++;
@@ -71,13 +101,13 @@ static void *sqlite3MemMalloc(int nByte){
 ** by higher-level routines.
 */
 static void sqlite3MemFree(void *pPrior){
-#if HAVE_MALLOC_USABLE_SIZE
-  free(pPrior);
+#ifdef SQLITE_MALLOCSIZE
+  SQLITE_FREE(pPrior);
 #else
   sqlite3_int64 *p = (sqlite3_int64*)pPrior;
   assert( pPrior!=0 );
   p--;
-  free(p);
+  SQLITE_FREE(p);
 #endif
 }
 
@@ -86,8 +116,8 @@ static void sqlite3MemFree(void *pPrior){
 ** or xRealloc().
 */
 static int sqlite3MemSize(void *pPrior){
-#if HAVE_MALLOC_USABLE_SIZE
-  return pPrior ? (int)malloc_usable_size(pPrior) : 0;
+#ifdef SQLITE_MALLOCSIZE
+  return pPrior ? (int)SQLITE_MALLOCSIZE(pPrior) : 0;
 #else
   sqlite3_int64 *p;
   if( pPrior==0 ) return 0;
@@ -108,13 +138,13 @@ static int sqlite3MemSize(void *pPrior){
 ** routines and redirected to xFree.
 */
 static void *sqlite3MemRealloc(void *pPrior, int nByte){
-#if HAVE_MALLOC_USABLE_SIZE
-  void *p = realloc(pPrior, nByte);
+#ifdef SQLITE_MALLOCSIZE
+  void *p = SQLITE_REALLOC(pPrior, nByte);
   if( p==0 ){
     testcase( sqlite3GlobalConfig.xLog!=0 );
     sqlite3_log(SQLITE_NOMEM,
       "failed memory resize %u to %u bytes",
-      malloc_usable_size(pPrior), nByte);
+      SQLITE_MALLOCSIZE(pPrior), nByte);
   }
   return p;
 #else
@@ -122,7 +152,7 @@ static void *sqlite3MemRealloc(void *pPrior, int nByte){
   assert( pPrior!=0 && nByte>0 );
   assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */
   p--;
-  p = realloc(p, nByte+8 );
+  p = SQLITE_REALLOC(p, nByte+8 );
   if( p ){
     p[0] = nByte;
     p++;
@@ -147,6 +177,34 @@ static int sqlite3MemRoundup(int n){
 ** Initialize this module.
 */
 static int sqlite3MemInit(void *NotUsed){
+#if defined(__APPLE__)
+  int cpuCount;
+  size_t len;
+  if( _sqliteZone_ ){
+    return SQLITE_OK;
+  }
+  len = sizeof(cpuCount);
+  /* One usually wants to use hw.acctivecpu for MT decisions, but not here */
+  sysctlbyname("hw.ncpu", &cpuCount, &len, NULL, 0);
+  if( cpuCount>1 ){
+    /* defer MT decisions to system malloc */
+    _sqliteZone_ = malloc_default_zone();
+  }else{
+    /* only 1 core, use our own zone to contention over global locks, 
+    ** e.g. we have our own dedicated locks */
+    bool success;              
+    malloc_zone_t* newzone = malloc_create_zone(4096, 0);
+    malloc_set_zone_name(newzone, "Sqlite_Heap");
+    do{
+      success = OSAtomicCompareAndSwapPtrBarrier(NULL, newzone, 
+                                 (void * volatile *)&_sqliteZone_);
+    }while(!_sqliteZone_);
+    if( !success ){    
+      /* somebody registered a zone first */
+      malloc_destroy_zone(newzone);
+    }
+  }
+#endif
   UNUSED_PARAMETER(NotUsed);
   return SQLITE_OK;
 }