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