1 /* 2 ** 2007 August 14 3 ** 4 ** The author disclaims copyright to this source code. In place of 5 ** a legal notice, here is a blessing: 6 ** 7 ** May you do good and not evil. 8 ** May you find forgiveness for yourself and forgive others. 9 ** May you share freely, never taking more than you give. 10 ** 11 ************************************************************************* 12 ** 13 ** This file contains low-level memory allocation drivers for when 14 ** SQLite will use the standard C-library malloc/realloc/free interface 15 ** to obtain the memory it needs. 16 ** 17 ** This file contains implementations of the low-level memory allocation 18 ** routines specified in the sqlite3_mem_methods object. The content of 19 ** this file is only used if SQLITE_SYSTEM_MALLOC is defined. The 20 ** SQLITE_SYSTEM_MALLOC macro is defined automatically if neither the 21 ** SQLITE_MEMDEBUG nor the SQLITE_WIN32_MALLOC macros are defined. The 22 ** default configuration is to use memory allocation routines in this 23 ** file. 24 ** 25 ** C-preprocessor macro summary: 26 ** 27 ** HAVE_MALLOC_USABLE_SIZE The configure script sets this symbol if 28 ** the malloc_usable_size() interface exists 29 ** on the target platform. Or, this symbol 30 ** can be set manually, if desired. 31 ** If an equivalent interface exists by 32 ** a different name, using a separate -D 33 ** option to rename it. 34 ** 35 ** SQLITE_WITHOUT_ZONEMALLOC Some older macs lack support for the zone 36 ** memory allocator. Set this symbol to enable 37 ** building on older macs. 38 ** 39 ** SQLITE_WITHOUT_MSIZE Set this symbol to disable the use of 40 ** _msize() on windows systems. This might 41 ** be necessary when compiling for Delphi, 42 ** for example. 43 */ 44 #include "sqliteInt.h" 45 46 /* 47 ** This version of the memory allocator is the default. It is 48 ** used when no other memory allocator is specified using compile-time 49 ** macros. 50 */ 51 #ifdef SQLITE_SYSTEM_MALLOC 52 53 /* 54 ** The MSVCRT has malloc_usable_size() but it is called _msize(). 55 ** The use of _msize() is automatic, but can be disabled by compiling 56 ** with -DSQLITE_WITHOUT_MSIZE 57 */ 58 #if defined(_MSC_VER) && !defined(SQLITE_WITHOUT_MSIZE) 59 # define SQLITE_MALLOCSIZE _msize 60 #endif 61 62 #if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC) 63 64 /* 65 ** Use the zone allocator available on apple products unless the 66 ** SQLITE_WITHOUT_ZONEMALLOC symbol is defined. 67 */ 68 #include <sys/sysctl.h> 69 #include <malloc/malloc.h> 70 #include <libkern/OSAtomic.h> 71 static malloc_zone_t* _sqliteZone_; 72 #define SQLITE_MALLOC(x) malloc_zone_malloc(_sqliteZone_, (x)) 73 #define SQLITE_FREE(x) malloc_zone_free(_sqliteZone_, (x)); 74 #define SQLITE_REALLOC(x,y) malloc_zone_realloc(_sqliteZone_, (x), (y)) 75 #define SQLITE_MALLOCSIZE(x) \ 76 (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x)) 77 78 #else /* if not __APPLE__ */ 79 80 /* 81 ** Use standard C library malloc and free on non-Apple systems. 82 ** Also used by Apple systems if SQLITE_WITHOUT_ZONEMALLOC is defined. 83 */ 84 #define SQLITE_MALLOC(x) malloc(x) 85 #define SQLITE_FREE(x) free(x) 86 #define SQLITE_REALLOC(x,y) realloc((x),(y)) 87 88 #if (defined(_MSC_VER) && !defined(SQLITE_WITHOUT_MSIZE)) \ 89 || (defined(HAVE_MALLOC_H) && defined(HAVE_MALLOC_USABLE_SIZE)) 90 # include <malloc.h> /* Needed for malloc_usable_size on linux */ 91 #endif 92 #ifdef HAVE_MALLOC_USABLE_SIZE 93 # ifndef SQLITE_MALLOCSIZE 94 # define SQLITE_MALLOCSIZE(x) malloc_usable_size(x) 95 # endif 96 #else 97 # undef SQLITE_MALLOCSIZE 98 #endif 99 100 #endif /* __APPLE__ or not __APPLE__ */ 101 102 /* 103 ** Like malloc(), but remember the size of the allocation 104 ** so that we can find it later using sqlite3MemSize(). 105 ** 106 ** For this low-level routine, we are guaranteed that nByte>0 because 107 ** cases of nByte<=0 will be intercepted and dealt with by higher level 108 ** routines. 109 */ 110 static void *sqlite3MemMalloc(int nByte){ 111 #ifdef SQLITE_MALLOCSIZE 112 void *p = SQLITE_MALLOC( nByte ); 113 if( p==0 ){ 114 testcase( sqlite3GlobalConfig.xLog!=0 ); 115 sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte); 116 } 117 return p; 118 #else 119 sqlite3_int64 *p; 120 assert( nByte>0 ); 121 nByte = ROUND8(nByte); 122 p = SQLITE_MALLOC( nByte+8 ); 123 if( p ){ 124 p[0] = nByte; 125 p++; 126 }else{ 127 testcase( sqlite3GlobalConfig.xLog!=0 ); 128 sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte); 129 } 130 return (void *)p; 131 #endif 132 } 133 134 /* 135 ** Like free() but works for allocations obtained from sqlite3MemMalloc() 136 ** or sqlite3MemRealloc(). 137 ** 138 ** For this low-level routine, we already know that pPrior!=0 since 139 ** cases where pPrior==0 will have been intecepted and dealt with 140 ** by higher-level routines. 141 */ 142 static void sqlite3MemFree(void *pPrior){ 143 #ifdef SQLITE_MALLOCSIZE 144 SQLITE_FREE(pPrior); 145 #else 146 sqlite3_int64 *p = (sqlite3_int64*)pPrior; 147 assert( pPrior!=0 ); 148 p--; 149 SQLITE_FREE(p); 150 #endif 151 } 152 153 /* 154 ** Report the allocated size of a prior return from xMalloc() 155 ** or xRealloc(). 156 */ 157 static int sqlite3MemSize(void *pPrior){ 158 #ifdef SQLITE_MALLOCSIZE 159 return pPrior ? (int)SQLITE_MALLOCSIZE(pPrior) : 0; 160 #else 161 sqlite3_int64 *p; 162 if( pPrior==0 ) return 0; 163 p = (sqlite3_int64*)pPrior; 164 p--; 165 return (int)p[0]; 166 #endif 167 } 168 169 /* 170 ** Like realloc(). Resize an allocation previously obtained from 171 ** sqlite3MemMalloc(). 172 ** 173 ** For this low-level interface, we know that pPrior!=0. Cases where 174 ** pPrior==0 while have been intercepted by higher-level routine and 175 ** redirected to xMalloc. Similarly, we know that nByte>0 becauses 176 ** cases where nByte<=0 will have been intercepted by higher-level 177 ** routines and redirected to xFree. 178 */ 179 static void *sqlite3MemRealloc(void *pPrior, int nByte){ 180 #ifdef SQLITE_MALLOCSIZE 181 void *p = SQLITE_REALLOC(pPrior, nByte); 182 if( p==0 ){ 183 testcase( sqlite3GlobalConfig.xLog!=0 ); 184 sqlite3_log(SQLITE_NOMEM, 185 "failed memory resize %u to %u bytes", 186 SQLITE_MALLOCSIZE(pPrior), nByte); 187 } 188 return p; 189 #else 190 sqlite3_int64 *p = (sqlite3_int64*)pPrior; 191 assert( pPrior!=0 && nByte>0 ); 192 assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */ 193 p--; 194 p = SQLITE_REALLOC(p, nByte+8 ); 195 if( p ){ 196 p[0] = nByte; 197 p++; 198 }else{ 199 testcase( sqlite3GlobalConfig.xLog!=0 ); 200 sqlite3_log(SQLITE_NOMEM, 201 "failed memory resize %u to %u bytes", 202 sqlite3MemSize(pPrior), nByte); 203 } 204 return (void*)p; 205 #endif 206 } 207 208 /* 209 ** Round up a request size to the next valid allocation size. 210 */ 211 static int sqlite3MemRoundup(int n){ 212 return ROUND8(n); 213 } 214 215 /* 216 ** Initialize this module. 217 */ 218 static int sqlite3MemInit(void *NotUsed){ 219 #if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC) 220 int cpuCount; 221 size_t len; 222 if( _sqliteZone_ ){ 223 return SQLITE_OK; 224 } 225 len = sizeof(cpuCount); 226 /* One usually wants to use hw.acctivecpu for MT decisions, but not here */ 227 sysctlbyname("hw.ncpu", &cpuCount, &len, NULL, 0); 228 if( cpuCount>1 ){ 229 /* defer MT decisions to system malloc */ 230 _sqliteZone_ = malloc_default_zone(); 231 }else{ 232 /* only 1 core, use our own zone to contention over global locks, 233 ** e.g. we have our own dedicated locks */ 234 bool success; 235 malloc_zone_t* newzone = malloc_create_zone(4096, 0); 236 malloc_set_zone_name(newzone, "Sqlite_Heap"); 237 do{ 238 success = OSAtomicCompareAndSwapPtrBarrier(NULL, newzone, 239 (void * volatile *)&_sqliteZone_); 240 }while(!_sqliteZone_); 241 if( !success ){ 242 /* somebody registered a zone first */ 243 malloc_destroy_zone(newzone); 244 } 245 } 246 #endif 247 UNUSED_PARAMETER(NotUsed); 248 return SQLITE_OK; 249 } 250 251 /* 252 ** Deinitialize this module. 253 */ 254 static void sqlite3MemShutdown(void *NotUsed){ 255 UNUSED_PARAMETER(NotUsed); 256 return; 257 } 258 259 /* 260 ** This routine is the only routine in this file with external linkage. 261 ** 262 ** Populate the low-level memory allocation function pointers in 263 ** sqlite3GlobalConfig.m with pointers to the routines in this file. 264 */ 265 void sqlite3MemSetDefault(void){ 266 static const sqlite3_mem_methods defaultMethods = { 267 sqlite3MemMalloc, 268 sqlite3MemFree, 269 sqlite3MemRealloc, 270 sqlite3MemSize, 271 sqlite3MemRoundup, 272 sqlite3MemInit, 273 sqlite3MemShutdown, 274 0 275 }; 276 sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods); 277 } 278 279 #endif /* SQLITE_SYSTEM_MALLOC */ 280