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  #ifdef SQLITE_MIGHT_BE_SINGLE_CORE
000061  #include <libkern/OSAtomic.h>
000062  #endif /* SQLITE_MIGHT_BE_SINGLE_CORE */
000063  static malloc_zone_t* _sqliteZone_;
000064  #define SQLITE_MALLOC(x) malloc_zone_malloc(_sqliteZone_, (x))
000065  #define SQLITE_FREE(x) malloc_zone_free(_sqliteZone_, (x));
000066  #define SQLITE_REALLOC(x,y) malloc_zone_realloc(_sqliteZone_, (x), (y))
000067  #define SQLITE_MALLOCSIZE(x) \
000068          (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x))
000069  
000070  #else /* if not __APPLE__ */
000071  
000072  /*
000073  ** Use standard C library malloc and free on non-Apple systems.  
000074  ** Also used by Apple systems if SQLITE_WITHOUT_ZONEMALLOC is defined.
000075  */
000076  #define SQLITE_MALLOC(x)             malloc(x)
000077  #define SQLITE_FREE(x)               free(x)
000078  #define SQLITE_REALLOC(x,y)          realloc((x),(y))
000079  
000080  /*
000081  ** The malloc.h header file is needed for malloc_usable_size() function
000082  ** on some systems (e.g. Linux).
000083  */
000084  #if HAVE_MALLOC_H && HAVE_MALLOC_USABLE_SIZE
000085  #  define SQLITE_USE_MALLOC_H 1
000086  #  define SQLITE_USE_MALLOC_USABLE_SIZE 1
000087  /*
000088  ** The MSVCRT has malloc_usable_size(), but it is called _msize().  The
000089  ** use of _msize() is automatic, but can be disabled by compiling with
000090  ** -DSQLITE_WITHOUT_MSIZE.  Using the _msize() function also requires
000091  ** the malloc.h header file.
000092  */
000093  #elif defined(_MSC_VER) && !defined(SQLITE_WITHOUT_MSIZE)
000094  #  define SQLITE_USE_MALLOC_H
000095  #  define SQLITE_USE_MSIZE
000096  #endif
000097  
000098  /*
000099  ** Include the malloc.h header file, if necessary.  Also set define macro
000100  ** SQLITE_MALLOCSIZE to the appropriate function name, which is _msize()
000101  ** for MSVC and malloc_usable_size() for most other systems (e.g. Linux).
000102  ** The memory size function can always be overridden manually by defining
000103  ** the macro SQLITE_MALLOCSIZE to the desired function name.
000104  */
000105  #if defined(SQLITE_USE_MALLOC_H)
000106  #  include <malloc.h>
000107  #  if defined(SQLITE_USE_MALLOC_USABLE_SIZE)
000108  #    if !defined(SQLITE_MALLOCSIZE)
000109  #      define SQLITE_MALLOCSIZE(x)   malloc_usable_size(x)
000110  #    endif
000111  #  elif defined(SQLITE_USE_MSIZE)
000112  #    if !defined(SQLITE_MALLOCSIZE)
000113  #      define SQLITE_MALLOCSIZE      _msize
000114  #    endif
000115  #  endif
000116  #endif /* defined(SQLITE_USE_MALLOC_H) */
000117  
000118  #endif /* __APPLE__ or not __APPLE__ */
000119  
000120  /*
000121  ** Like malloc(), but remember the size of the allocation
000122  ** so that we can find it later using sqlite3MemSize().
000123  **
000124  ** For this low-level routine, we are guaranteed that nByte>0 because
000125  ** cases of nByte<=0 will be intercepted and dealt with by higher level
000126  ** routines.
000127  */
000128  static void *sqlite3MemMalloc(int nByte){
000129  #ifdef SQLITE_MALLOCSIZE
000130    void *p;
000131    testcase( ROUND8(nByte)==nByte );
000132    p = SQLITE_MALLOC( nByte );
000133    if( p==0 ){
000134      testcase( sqlite3GlobalConfig.xLog!=0 );
000135      sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
000136    }
000137    return p;
000138  #else
000139    sqlite3_int64 *p;
000140    assert( nByte>0 );
000141    testcase( ROUND8(nByte)!=nByte );
000142    p = SQLITE_MALLOC( nByte+8 );
000143    if( p ){
000144      p[0] = nByte;
000145      p++;
000146    }else{
000147      testcase( sqlite3GlobalConfig.xLog!=0 );
000148      sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
000149    }
000150    return (void *)p;
000151  #endif
000152  }
000153  
000154  /*
000155  ** Like free() but works for allocations obtained from sqlite3MemMalloc()
000156  ** or sqlite3MemRealloc().
000157  **
000158  ** For this low-level routine, we already know that pPrior!=0 since
000159  ** cases where pPrior==0 will have been intecepted and dealt with
000160  ** by higher-level routines.
000161  */
000162  static void sqlite3MemFree(void *pPrior){
000163  #ifdef SQLITE_MALLOCSIZE
000164    SQLITE_FREE(pPrior);
000165  #else
000166    sqlite3_int64 *p = (sqlite3_int64*)pPrior;
000167    assert( pPrior!=0 );
000168    p--;
000169    SQLITE_FREE(p);
000170  #endif
000171  }
000172  
000173  /*
000174  ** Report the allocated size of a prior return from xMalloc()
000175  ** or xRealloc().
000176  */
000177  static int sqlite3MemSize(void *pPrior){
000178  #ifdef SQLITE_MALLOCSIZE
000179    assert( pPrior!=0 );
000180    return (int)SQLITE_MALLOCSIZE(pPrior);
000181  #else
000182    sqlite3_int64 *p;
000183    assert( pPrior!=0 );
000184    p = (sqlite3_int64*)pPrior;
000185    p--;
000186    return (int)p[0];
000187  #endif
000188  }
000189  
000190  /*
000191  ** Like realloc().  Resize an allocation previously obtained from
000192  ** sqlite3MemMalloc().
000193  **
000194  ** For this low-level interface, we know that pPrior!=0.  Cases where
000195  ** pPrior==0 while have been intercepted by higher-level routine and
000196  ** redirected to xMalloc.  Similarly, we know that nByte>0 because
000197  ** cases where nByte<=0 will have been intercepted by higher-level
000198  ** routines and redirected to xFree.
000199  */
000200  static void *sqlite3MemRealloc(void *pPrior, int nByte){
000201  #ifdef SQLITE_MALLOCSIZE
000202    void *p = SQLITE_REALLOC(pPrior, nByte);
000203    if( p==0 ){
000204      testcase( sqlite3GlobalConfig.xLog!=0 );
000205      sqlite3_log(SQLITE_NOMEM,
000206        "failed memory resize %u to %u bytes",
000207        SQLITE_MALLOCSIZE(pPrior), nByte);
000208    }
000209    return p;
000210  #else
000211    sqlite3_int64 *p = (sqlite3_int64*)pPrior;
000212    assert( pPrior!=0 && nByte>0 );
000213    assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */
000214    p--;
000215    p = SQLITE_REALLOC(p, nByte+8 );
000216    if( p ){
000217      p[0] = nByte;
000218      p++;
000219    }else{
000220      testcase( sqlite3GlobalConfig.xLog!=0 );
000221      sqlite3_log(SQLITE_NOMEM,
000222        "failed memory resize %u to %u bytes",
000223        sqlite3MemSize(pPrior), nByte);
000224    }
000225    return (void*)p;
000226  #endif
000227  }
000228  
000229  /*
000230  ** Round up a request size to the next valid allocation size.
000231  */
000232  static int sqlite3MemRoundup(int n){
000233    return ROUND8(n);
000234  }
000235  
000236  /*
000237  ** Initialize this module.
000238  */
000239  static int sqlite3MemInit(void *NotUsed){
000240  #if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
000241    int cpuCount;
000242    size_t len;
000243    if( _sqliteZone_ ){
000244      return SQLITE_OK;
000245    }
000246    len = sizeof(cpuCount);
000247    /* One usually wants to use hw.acctivecpu for MT decisions, but not here */
000248    sysctlbyname("hw.ncpu", &cpuCount, &len, NULL, 0);
000249    if( cpuCount>1 ){
000250      /* defer MT decisions to system malloc */
000251      _sqliteZone_ = malloc_default_zone();
000252    }else{
000253      /* only 1 core, use our own zone to contention over global locks, 
000254      ** e.g. we have our own dedicated locks */
000255      _sqliteZone_ = malloc_create_zone(4096, 0);
000256      malloc_set_zone_name(_sqliteZone_, "Sqlite_Heap");
000257    }
000258  #endif /*  defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC) */
000259    UNUSED_PARAMETER(NotUsed);
000260    return SQLITE_OK;
000261  }
000262  
000263  /*
000264  ** Deinitialize this module.
000265  */
000266  static void sqlite3MemShutdown(void *NotUsed){
000267    UNUSED_PARAMETER(NotUsed);
000268    return;
000269  }
000270  
000271  /*
000272  ** This routine is the only routine in this file with external linkage.
000273  **
000274  ** Populate the low-level memory allocation function pointers in
000275  ** sqlite3GlobalConfig.m with pointers to the routines in this file.
000276  */
000277  void sqlite3MemSetDefault(void){
000278    static const sqlite3_mem_methods defaultMethods = {
000279       sqlite3MemMalloc,
000280       sqlite3MemFree,
000281       sqlite3MemRealloc,
000282       sqlite3MemSize,
000283       sqlite3MemRoundup,
000284       sqlite3MemInit,
000285       sqlite3MemShutdown,
000286       0
000287    };
000288    sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
000289  }
000290  
000291  #endif /* SQLITE_SYSTEM_MALLOC */