000001  /*
000002  ** 2007 August 14
000003  **
000004  ** The author disclaims copyright to this source code.  In place of
000005  ** a legal notice, here is a blessing:
000006  **
000007  **    May you do good and not evil.
000008  **    May you find forgiveness for yourself and forgive others.
000009  **    May you share freely, never taking more than you give.
000010  **
000011  *************************************************************************
000012  **
000013  ** This file contains low-level memory allocation drivers for when
000014  ** SQLite will use the standard C-library malloc/realloc/free interface
000015  ** to obtain the memory it needs.
000016  **
000017  ** This file contains implementations of the low-level memory allocation
000018  ** routines specified in the sqlite3_mem_methods object.  The content of
000019  ** this file is only used if SQLITE_SYSTEM_MALLOC is defined.  The
000020  ** SQLITE_SYSTEM_MALLOC macro is defined automatically if neither the
000021  ** SQLITE_MEMDEBUG nor the SQLITE_WIN32_MALLOC macros are defined.  The
000022  ** default configuration is to use memory allocation routines in this
000023  ** file.
000024  **
000025  ** C-preprocessor macro summary:
000026  **
000027  **    HAVE_MALLOC_USABLE_SIZE     The configure script sets this symbol if
000028  **                                the malloc_usable_size() interface exists
000029  **                                on the target platform.  Or, this symbol
000030  **                                can be set manually, if desired.
000031  **                                If an equivalent interface exists by
000032  **                                a different name, using a separate -D
000033  **                                option to rename it.
000034  **
000035  **    SQLITE_WITHOUT_ZONEMALLOC   Some older macs lack support for the zone
000036  **                                memory allocator.  Set this symbol to enable
000037  **                                building on older macs.
000038  **
000039  **    SQLITE_WITHOUT_MSIZE        Set this symbol to disable the use of
000040  **                                _msize() on windows systems.  This might
000041  **                                be necessary when compiling for Delphi,
000042  **                                for example.
000043  */
000044  #include "sqliteInt.h"
000045  
000046  /*
000047  ** This version of the memory allocator is the default.  It is
000048  ** used when no other memory allocator is specified using compile-time
000049  ** macros.
000050  */
000051  #ifdef SQLITE_SYSTEM_MALLOC
000052  #if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
000053  
000054  /*
000055  ** Use the zone allocator available on apple products unless the
000056  ** SQLITE_WITHOUT_ZONEMALLOC symbol is defined.
000057  */
000058  #include <sys/sysctl.h>
000059  #include <malloc/malloc.h>
000060  #include <libkern/OSAtomic.h>
000061  static malloc_zone_t* _sqliteZone_;
000062  #define SQLITE_MALLOC(x) malloc_zone_malloc(_sqliteZone_, (x))
000063  #define SQLITE_FREE(x) malloc_zone_free(_sqliteZone_, (x));
000064  #define SQLITE_REALLOC(x,y) malloc_zone_realloc(_sqliteZone_, (x), (y))
000065  #define SQLITE_MALLOCSIZE(x) \
000066          (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x))
000067  
000068  #else /* if not __APPLE__ */
000069  
000070  /*
000071  ** Use standard C library malloc and free on non-Apple systems.  
000072  ** Also used by Apple systems if SQLITE_WITHOUT_ZONEMALLOC is defined.
000073  */
000074  #define SQLITE_MALLOC(x)             malloc(x)
000075  #define SQLITE_FREE(x)               free(x)
000076  #define SQLITE_REALLOC(x,y)          realloc((x),(y))
000077  
000078  /*
000079  ** The malloc.h header file is needed for malloc_usable_size() function
000080  ** on some systems (e.g. Linux).
000081  */
000082  #if HAVE_MALLOC_H && HAVE_MALLOC_USABLE_SIZE
000083  #  define SQLITE_USE_MALLOC_H 1
000084  #  define SQLITE_USE_MALLOC_USABLE_SIZE 1
000085  /*
000086  ** The MSVCRT has malloc_usable_size(), but it is called _msize().  The
000087  ** use of _msize() is automatic, but can be disabled by compiling with
000088  ** -DSQLITE_WITHOUT_MSIZE.  Using the _msize() function also requires
000089  ** the malloc.h header file.
000090  */
000091  #elif defined(_MSC_VER) && !defined(SQLITE_WITHOUT_MSIZE)
000092  #  define SQLITE_USE_MALLOC_H
000093  #  define SQLITE_USE_MSIZE
000094  #endif
000095  
000096  /*
000097  ** Include the malloc.h header file, if necessary.  Also set define macro
000098  ** SQLITE_MALLOCSIZE to the appropriate function name, which is _msize()
000099  ** for MSVC and malloc_usable_size() for most other systems (e.g. Linux).
000100  ** The memory size function can always be overridden manually by defining
000101  ** the macro SQLITE_MALLOCSIZE to the desired function name.
000102  */
000103  #if defined(SQLITE_USE_MALLOC_H)
000104  #  include <malloc.h>
000105  #  if defined(SQLITE_USE_MALLOC_USABLE_SIZE)
000106  #    if !defined(SQLITE_MALLOCSIZE)
000107  #      define SQLITE_MALLOCSIZE(x)   malloc_usable_size(x)
000108  #    endif
000109  #  elif defined(SQLITE_USE_MSIZE)
000110  #    if !defined(SQLITE_MALLOCSIZE)
000111  #      define SQLITE_MALLOCSIZE      _msize
000112  #    endif
000113  #  endif
000114  #endif /* defined(SQLITE_USE_MALLOC_H) */
000115  
000116  #endif /* __APPLE__ or not __APPLE__ */
000117  
000118  /*
000119  ** Like malloc(), but remember the size of the allocation
000120  ** so that we can find it later using sqlite3MemSize().
000121  **
000122  ** For this low-level routine, we are guaranteed that nByte>0 because
000123  ** cases of nByte<=0 will be intercepted and dealt with by higher level
000124  ** routines.
000125  */
000126  static void *sqlite3MemMalloc(int nByte){
000127  #ifdef SQLITE_MALLOCSIZE
000128    void *p = SQLITE_MALLOC( nByte );
000129    if( p==0 ){
000130      testcase( sqlite3GlobalConfig.xLog!=0 );
000131      sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
000132    }
000133    return p;
000134  #else
000135    sqlite3_int64 *p;
000136    assert( nByte>0 );
000137    nByte = ROUND8(nByte);
000138    p = SQLITE_MALLOC( nByte+8 );
000139    if( p ){
000140      p[0] = nByte;
000141      p++;
000142    }else{
000143      testcase( sqlite3GlobalConfig.xLog!=0 );
000144      sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
000145    }
000146    return (void *)p;
000147  #endif
000148  }
000149  
000150  /*
000151  ** Like free() but works for allocations obtained from sqlite3MemMalloc()
000152  ** or sqlite3MemRealloc().
000153  **
000154  ** For this low-level routine, we already know that pPrior!=0 since
000155  ** cases where pPrior==0 will have been intecepted and dealt with
000156  ** by higher-level routines.
000157  */
000158  static void sqlite3MemFree(void *pPrior){
000159  #ifdef SQLITE_MALLOCSIZE
000160    SQLITE_FREE(pPrior);
000161  #else
000162    sqlite3_int64 *p = (sqlite3_int64*)pPrior;
000163    assert( pPrior!=0 );
000164    p--;
000165    SQLITE_FREE(p);
000166  #endif
000167  }
000168  
000169  /*
000170  ** Report the allocated size of a prior return from xMalloc()
000171  ** or xRealloc().
000172  */
000173  static int sqlite3MemSize(void *pPrior){
000174  #ifdef SQLITE_MALLOCSIZE
000175    assert( pPrior!=0 );
000176    return (int)SQLITE_MALLOCSIZE(pPrior);
000177  #else
000178    sqlite3_int64 *p;
000179    assert( pPrior!=0 );
000180    p = (sqlite3_int64*)pPrior;
000181    p--;
000182    return (int)p[0];
000183  #endif
000184  }
000185  
000186  /*
000187  ** Like realloc().  Resize an allocation previously obtained from
000188  ** sqlite3MemMalloc().
000189  **
000190  ** For this low-level interface, we know that pPrior!=0.  Cases where
000191  ** pPrior==0 while have been intercepted by higher-level routine and
000192  ** redirected to xMalloc.  Similarly, we know that nByte>0 because
000193  ** cases where nByte<=0 will have been intercepted by higher-level
000194  ** routines and redirected to xFree.
000195  */
000196  static void *sqlite3MemRealloc(void *pPrior, int nByte){
000197  #ifdef SQLITE_MALLOCSIZE
000198    void *p = SQLITE_REALLOC(pPrior, nByte);
000199    if( p==0 ){
000200      testcase( sqlite3GlobalConfig.xLog!=0 );
000201      sqlite3_log(SQLITE_NOMEM,
000202        "failed memory resize %u to %u bytes",
000203        SQLITE_MALLOCSIZE(pPrior), nByte);
000204    }
000205    return p;
000206  #else
000207    sqlite3_int64 *p = (sqlite3_int64*)pPrior;
000208    assert( pPrior!=0 && nByte>0 );
000209    assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */
000210    p--;
000211    p = SQLITE_REALLOC(p, nByte+8 );
000212    if( p ){
000213      p[0] = nByte;
000214      p++;
000215    }else{
000216      testcase( sqlite3GlobalConfig.xLog!=0 );
000217      sqlite3_log(SQLITE_NOMEM,
000218        "failed memory resize %u to %u bytes",
000219        sqlite3MemSize(pPrior), nByte);
000220    }
000221    return (void*)p;
000222  #endif
000223  }
000224  
000225  /*
000226  ** Round up a request size to the next valid allocation size.
000227  */
000228  static int sqlite3MemRoundup(int n){
000229    return ROUND8(n);
000230  }
000231  
000232  /*
000233  ** Initialize this module.
000234  */
000235  static int sqlite3MemInit(void *NotUsed){
000236  #if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
000237    int cpuCount;
000238    size_t len;
000239    if( _sqliteZone_ ){
000240      return SQLITE_OK;
000241    }
000242    len = sizeof(cpuCount);
000243    /* One usually wants to use hw.acctivecpu for MT decisions, but not here */
000244    sysctlbyname("hw.ncpu", &cpuCount, &len, NULL, 0);
000245    if( cpuCount>1 ){
000246      /* defer MT decisions to system malloc */
000247      _sqliteZone_ = malloc_default_zone();
000248    }else{
000249      /* only 1 core, use our own zone to contention over global locks, 
000250      ** e.g. we have our own dedicated locks */
000251      bool success;
000252      malloc_zone_t* newzone = malloc_create_zone(4096, 0);
000253      malloc_set_zone_name(newzone, "Sqlite_Heap");
000254      do{
000255        success = OSAtomicCompareAndSwapPtrBarrier(NULL, newzone, 
000256                                   (void * volatile *)&_sqliteZone_);
000257      }while(!_sqliteZone_);
000258      if( !success ){
000259        /* somebody registered a zone first */
000260        malloc_destroy_zone(newzone);
000261      }
000262    }
000263  #endif
000264    UNUSED_PARAMETER(NotUsed);
000265    return SQLITE_OK;
000266  }
000267  
000268  /*
000269  ** Deinitialize this module.
000270  */
000271  static void sqlite3MemShutdown(void *NotUsed){
000272    UNUSED_PARAMETER(NotUsed);
000273    return;
000274  }
000275  
000276  /*
000277  ** This routine is the only routine in this file with external linkage.
000278  **
000279  ** Populate the low-level memory allocation function pointers in
000280  ** sqlite3GlobalConfig.m with pointers to the routines in this file.
000281  */
000282  void sqlite3MemSetDefault(void){
000283    static const sqlite3_mem_methods defaultMethods = {
000284       sqlite3MemMalloc,
000285       sqlite3MemFree,
000286       sqlite3MemRealloc,
000287       sqlite3MemSize,
000288       sqlite3MemRoundup,
000289       sqlite3MemInit,
000290       sqlite3MemShutdown,
000291       0
000292    };
000293    sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
000294  }
000295  
000296  #endif /* SQLITE_SYSTEM_MALLOC */