drh | 90f6a5b | 2007-08-15 13:04:54 +0000 | [diff] [blame] | 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 | ************************************************************************* |
drh | 90f6a5b | 2007-08-15 13:04:54 +0000 | [diff] [blame] | 12 | ** |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 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 |
drh | c710ccb | 2012-01-18 12:46:47 +0000 | [diff] [blame] | 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 |
mistachkin | a844f51 | 2012-02-11 21:56:59 +0000 | [diff] [blame] | 33 | ** option to rename it. |
| 34 | ** |
drh | c710ccb | 2012-01-18 12:46:47 +0000 | [diff] [blame] | 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. |
drh | 90f6a5b | 2007-08-15 13:04:54 +0000 | [diff] [blame] | 43 | */ |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 44 | #include "sqliteInt.h" |
drh | 90f6a5b | 2007-08-15 13:04:54 +0000 | [diff] [blame] | 45 | |
| 46 | /* |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 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 | */ |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 51 | #ifdef SQLITE_SYSTEM_MALLOC |
drh | c710ccb | 2012-01-18 12:46:47 +0000 | [diff] [blame] | 52 | #if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC) |
drh | f1c5726 | 2011-12-08 20:41:33 +0000 | [diff] [blame] | 53 | |
| 54 | /* |
drh | c710ccb | 2012-01-18 12:46:47 +0000 | [diff] [blame] | 55 | ** Use the zone allocator available on apple products unless the |
| 56 | ** SQLITE_WITHOUT_ZONEMALLOC symbol is defined. |
drh | f1c5726 | 2011-12-08 20:41:33 +0000 | [diff] [blame] | 57 | */ |
| 58 | #include <sys/sysctl.h> |
| 59 | #include <malloc/malloc.h> |
| 60 | #include <libkern/OSAtomic.h> |
| 61 | static malloc_zone_t* _sqliteZone_; |
| 62 | #define SQLITE_MALLOC(x) malloc_zone_malloc(_sqliteZone_, (x)) |
| 63 | #define SQLITE_FREE(x) malloc_zone_free(_sqliteZone_, (x)); |
| 64 | #define SQLITE_REALLOC(x,y) malloc_zone_realloc(_sqliteZone_, (x), (y)) |
| 65 | #define SQLITE_MALLOCSIZE(x) \ |
| 66 | (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x)) |
| 67 | |
| 68 | #else /* if not __APPLE__ */ |
| 69 | |
| 70 | /* |
drh | c710ccb | 2012-01-18 12:46:47 +0000 | [diff] [blame] | 71 | ** Use standard C library malloc and free on non-Apple systems. |
| 72 | ** Also used by Apple systems if SQLITE_WITHOUT_ZONEMALLOC is defined. |
drh | f1c5726 | 2011-12-08 20:41:33 +0000 | [diff] [blame] | 73 | */ |
mistachkin | 015a304 | 2013-11-12 21:37:04 +0000 | [diff] [blame] | 74 | #define SQLITE_MALLOC(x) malloc(x) |
| 75 | #define SQLITE_FREE(x) free(x) |
| 76 | #define SQLITE_REALLOC(x,y) realloc((x),(y)) |
drh | f1c5726 | 2011-12-08 20:41:33 +0000 | [diff] [blame] | 77 | |
mistachkin | 015a304 | 2013-11-12 21:37:04 +0000 | [diff] [blame] | 78 | /* |
| 79 | ** The malloc.h header file is needed for malloc_usable_size() function |
| 80 | ** on some systems (e.g. Linux). |
| 81 | */ |
drh | 0ede9eb | 2015-01-10 16:49:23 +0000 | [diff] [blame] | 82 | #if HAVE_MALLOC_H && HAVE_MALLOC_USABLE_SIZE |
| 83 | # define SQLITE_USE_MALLOC_H 1 |
| 84 | # define SQLITE_USE_MALLOC_USABLE_SIZE 1 |
mistachkin | 015a304 | 2013-11-12 21:37:04 +0000 | [diff] [blame] | 85 | /* |
| 86 | ** The MSVCRT has malloc_usable_size(), but it is called _msize(). The |
| 87 | ** use of _msize() is automatic, but can be disabled by compiling with |
| 88 | ** -DSQLITE_WITHOUT_MSIZE. Using the _msize() function also requires |
| 89 | ** the malloc.h header file. |
| 90 | */ |
| 91 | #elif defined(_MSC_VER) && !defined(SQLITE_WITHOUT_MSIZE) |
| 92 | # define SQLITE_USE_MALLOC_H |
| 93 | # define SQLITE_USE_MSIZE |
drh | 86c5a93 | 2012-02-08 12:13:28 +0000 | [diff] [blame] | 94 | #endif |
mistachkin | 015a304 | 2013-11-12 21:37:04 +0000 | [diff] [blame] | 95 | |
| 96 | /* |
| 97 | ** Include the malloc.h header file, if necessary. Also set define macro |
| 98 | ** SQLITE_MALLOCSIZE to the appropriate function name, which is _msize() |
| 99 | ** for MSVC and malloc_usable_size() for most other systems (e.g. Linux). |
| 100 | ** The memory size function can always be overridden manually by defining |
| 101 | ** the macro SQLITE_MALLOCSIZE to the desired function name. |
| 102 | */ |
| 103 | #if defined(SQLITE_USE_MALLOC_H) |
| 104 | # include <malloc.h> |
| 105 | # if defined(SQLITE_USE_MALLOC_USABLE_SIZE) |
| 106 | # if !defined(SQLITE_MALLOCSIZE) |
| 107 | # define SQLITE_MALLOCSIZE(x) malloc_usable_size(x) |
| 108 | # endif |
| 109 | # elif defined(SQLITE_USE_MSIZE) |
| 110 | # if !defined(SQLITE_MALLOCSIZE) |
| 111 | # define SQLITE_MALLOCSIZE _msize |
| 112 | # endif |
| 113 | # endif |
| 114 | #endif /* defined(SQLITE_USE_MALLOC_H) */ |
drh | 6a8ab6d | 2011-11-09 01:53:25 +0000 | [diff] [blame] | 115 | |
drh | f1c5726 | 2011-12-08 20:41:33 +0000 | [diff] [blame] | 116 | #endif /* __APPLE__ or not __APPLE__ */ |
| 117 | |
drh | 90f6a5b | 2007-08-15 13:04:54 +0000 | [diff] [blame] | 118 | /* |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 119 | ** Like malloc(), but remember the size of the allocation |
| 120 | ** so that we can find it later using sqlite3MemSize(). |
| 121 | ** |
| 122 | ** For this low-level routine, we are guaranteed that nByte>0 because |
| 123 | ** cases of nByte<=0 will be intercepted and dealt with by higher level |
| 124 | ** routines. |
drh | 90f6a5b | 2007-08-15 13:04:54 +0000 | [diff] [blame] | 125 | */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 126 | static void *sqlite3MemMalloc(int nByte){ |
drh | f1c5726 | 2011-12-08 20:41:33 +0000 | [diff] [blame] | 127 | #ifdef SQLITE_MALLOCSIZE |
| 128 | void *p = SQLITE_MALLOC( nByte ); |
drh | 6a8ab6d | 2011-11-09 01:53:25 +0000 | [diff] [blame] | 129 | if( p==0 ){ |
| 130 | testcase( sqlite3GlobalConfig.xLog!=0 ); |
| 131 | sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte); |
| 132 | } |
| 133 | return p; |
| 134 | #else |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 135 | sqlite3_int64 *p; |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 136 | assert( nByte>0 ); |
danielk1977 | bc73971 | 2009-03-23 04:33:32 +0000 | [diff] [blame] | 137 | nByte = ROUND8(nByte); |
drh | f1c5726 | 2011-12-08 20:41:33 +0000 | [diff] [blame] | 138 | p = SQLITE_MALLOC( nByte+8 ); |
danielk1977 | 950292f | 2008-06-23 15:10:24 +0000 | [diff] [blame] | 139 | if( p ){ |
| 140 | p[0] = nByte; |
| 141 | p++; |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 142 | }else{ |
drh | af46dc1 | 2010-02-24 21:44:07 +0000 | [diff] [blame] | 143 | testcase( sqlite3GlobalConfig.xLog!=0 ); |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 144 | sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte); |
danielk1977 | 950292f | 2008-06-23 15:10:24 +0000 | [diff] [blame] | 145 | } |
| 146 | return (void *)p; |
drh | 6a8ab6d | 2011-11-09 01:53:25 +0000 | [diff] [blame] | 147 | #endif |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /* |
| 151 | ** Like free() but works for allocations obtained from sqlite3MemMalloc() |
| 152 | ** or sqlite3MemRealloc(). |
| 153 | ** |
| 154 | ** For this low-level routine, we already know that pPrior!=0 since |
| 155 | ** cases where pPrior==0 will have been intecepted and dealt with |
| 156 | ** by higher-level routines. |
| 157 | */ |
| 158 | static void sqlite3MemFree(void *pPrior){ |
drh | f1c5726 | 2011-12-08 20:41:33 +0000 | [diff] [blame] | 159 | #ifdef SQLITE_MALLOCSIZE |
| 160 | SQLITE_FREE(pPrior); |
drh | 6a8ab6d | 2011-11-09 01:53:25 +0000 | [diff] [blame] | 161 | #else |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 162 | sqlite3_int64 *p = (sqlite3_int64*)pPrior; |
danielk1977 | 834a5aa | 2008-06-23 14:40:18 +0000 | [diff] [blame] | 163 | assert( pPrior!=0 ); |
drh | 90f6a5b | 2007-08-15 13:04:54 +0000 | [diff] [blame] | 164 | p--; |
drh | f1c5726 | 2011-12-08 20:41:33 +0000 | [diff] [blame] | 165 | SQLITE_FREE(p); |
drh | 6a8ab6d | 2011-11-09 01:53:25 +0000 | [diff] [blame] | 166 | #endif |
drh | 90f6a5b | 2007-08-15 13:04:54 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /* |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 170 | ** Report the allocated size of a prior return from xMalloc() |
| 171 | ** or xRealloc(). |
| 172 | */ |
| 173 | static int sqlite3MemSize(void *pPrior){ |
drh | f1c5726 | 2011-12-08 20:41:33 +0000 | [diff] [blame] | 174 | #ifdef SQLITE_MALLOCSIZE |
drh | 4dd83a2 | 2015-10-26 14:54:32 +0000 | [diff] [blame] | 175 | assert( pPrior!=0 ); |
drh | 039ca6a | 2015-10-15 16:20:57 +0000 | [diff] [blame] | 176 | return (int)SQLITE_MALLOCSIZE(pPrior); |
drh | 6a8ab6d | 2011-11-09 01:53:25 +0000 | [diff] [blame] | 177 | #else |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 178 | sqlite3_int64 *p; |
drh | 4dd83a2 | 2015-10-26 14:54:32 +0000 | [diff] [blame] | 179 | assert( pPrior!=0 ); |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 180 | p = (sqlite3_int64*)pPrior; |
| 181 | p--; |
| 182 | return (int)p[0]; |
drh | 6a8ab6d | 2011-11-09 01:53:25 +0000 | [diff] [blame] | 183 | #endif |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /* |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 187 | ** Like realloc(). Resize an allocation previously obtained from |
| 188 | ** sqlite3MemMalloc(). |
| 189 | ** |
| 190 | ** For this low-level interface, we know that pPrior!=0. Cases where |
| 191 | ** pPrior==0 while have been intercepted by higher-level routine and |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 192 | ** redirected to xMalloc. Similarly, we know that nByte>0 because |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 193 | ** cases where nByte<=0 will have been intercepted by higher-level |
| 194 | ** routines and redirected to xFree. |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 195 | */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 196 | static void *sqlite3MemRealloc(void *pPrior, int nByte){ |
drh | f1c5726 | 2011-12-08 20:41:33 +0000 | [diff] [blame] | 197 | #ifdef SQLITE_MALLOCSIZE |
| 198 | void *p = SQLITE_REALLOC(pPrior, nByte); |
drh | 6a8ab6d | 2011-11-09 01:53:25 +0000 | [diff] [blame] | 199 | if( p==0 ){ |
| 200 | testcase( sqlite3GlobalConfig.xLog!=0 ); |
| 201 | sqlite3_log(SQLITE_NOMEM, |
| 202 | "failed memory resize %u to %u bytes", |
drh | f1c5726 | 2011-12-08 20:41:33 +0000 | [diff] [blame] | 203 | SQLITE_MALLOCSIZE(pPrior), nByte); |
drh | 6a8ab6d | 2011-11-09 01:53:25 +0000 | [diff] [blame] | 204 | } |
| 205 | return p; |
| 206 | #else |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 207 | sqlite3_int64 *p = (sqlite3_int64*)pPrior; |
| 208 | assert( pPrior!=0 && nByte>0 ); |
drh | 9f129f4 | 2010-08-31 15:27:32 +0000 | [diff] [blame] | 209 | assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 210 | p--; |
drh | f1c5726 | 2011-12-08 20:41:33 +0000 | [diff] [blame] | 211 | p = SQLITE_REALLOC(p, nByte+8 ); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 212 | if( p ){ |
| 213 | p[0] = nByte; |
| 214 | p++; |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 215 | }else{ |
drh | af46dc1 | 2010-02-24 21:44:07 +0000 | [diff] [blame] | 216 | testcase( sqlite3GlobalConfig.xLog!=0 ); |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 217 | sqlite3_log(SQLITE_NOMEM, |
| 218 | "failed memory resize %u to %u bytes", |
| 219 | sqlite3MemSize(pPrior), nByte); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 220 | } |
| 221 | return (void*)p; |
drh | 6a8ab6d | 2011-11-09 01:53:25 +0000 | [diff] [blame] | 222 | #endif |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | /* |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 226 | ** Round up a request size to the next valid allocation size. |
| 227 | */ |
| 228 | static int sqlite3MemRoundup(int n){ |
danielk1977 | bc73971 | 2009-03-23 04:33:32 +0000 | [diff] [blame] | 229 | return ROUND8(n); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | /* |
| 233 | ** Initialize this module. |
| 234 | */ |
| 235 | static int sqlite3MemInit(void *NotUsed){ |
drh | c710ccb | 2012-01-18 12:46:47 +0000 | [diff] [blame] | 236 | #if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC) |
drh | f1c5726 | 2011-12-08 20:41:33 +0000 | [diff] [blame] | 237 | int cpuCount; |
| 238 | size_t len; |
| 239 | if( _sqliteZone_ ){ |
| 240 | return SQLITE_OK; |
| 241 | } |
| 242 | len = sizeof(cpuCount); |
| 243 | /* One usually wants to use hw.acctivecpu for MT decisions, but not here */ |
| 244 | sysctlbyname("hw.ncpu", &cpuCount, &len, NULL, 0); |
| 245 | if( cpuCount>1 ){ |
| 246 | /* defer MT decisions to system malloc */ |
| 247 | _sqliteZone_ = malloc_default_zone(); |
| 248 | }else{ |
| 249 | /* only 1 core, use our own zone to contention over global locks, |
| 250 | ** e.g. we have our own dedicated locks */ |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 251 | bool success; |
drh | f1c5726 | 2011-12-08 20:41:33 +0000 | [diff] [blame] | 252 | malloc_zone_t* newzone = malloc_create_zone(4096, 0); |
| 253 | malloc_set_zone_name(newzone, "Sqlite_Heap"); |
| 254 | do{ |
| 255 | success = OSAtomicCompareAndSwapPtrBarrier(NULL, newzone, |
| 256 | (void * volatile *)&_sqliteZone_); |
| 257 | }while(!_sqliteZone_); |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 258 | if( !success ){ |
drh | f1c5726 | 2011-12-08 20:41:33 +0000 | [diff] [blame] | 259 | /* somebody registered a zone first */ |
| 260 | malloc_destroy_zone(newzone); |
| 261 | } |
| 262 | } |
| 263 | #endif |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 264 | UNUSED_PARAMETER(NotUsed); |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 265 | return SQLITE_OK; |
| 266 | } |
| 267 | |
| 268 | /* |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 269 | ** Deinitialize this module. |
drh | 90f6a5b | 2007-08-15 13:04:54 +0000 | [diff] [blame] | 270 | */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 271 | static void sqlite3MemShutdown(void *NotUsed){ |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 272 | UNUSED_PARAMETER(NotUsed); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 273 | return; |
| 274 | } |
| 275 | |
drh | d1370b6 | 2008-10-28 18:58:20 +0000 | [diff] [blame] | 276 | /* |
| 277 | ** This routine is the only routine in this file with external linkage. |
| 278 | ** |
| 279 | ** Populate the low-level memory allocation function pointers in |
| 280 | ** sqlite3GlobalConfig.m with pointers to the routines in this file. |
| 281 | */ |
| 282 | void sqlite3MemSetDefault(void){ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 283 | static const sqlite3_mem_methods defaultMethods = { |
| 284 | sqlite3MemMalloc, |
| 285 | sqlite3MemFree, |
| 286 | sqlite3MemRealloc, |
| 287 | sqlite3MemSize, |
| 288 | sqlite3MemRoundup, |
| 289 | sqlite3MemInit, |
| 290 | sqlite3MemShutdown, |
| 291 | 0 |
| 292 | }; |
drh | d1370b6 | 2008-10-28 18:58:20 +0000 | [diff] [blame] | 293 | sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods); |
drh | 90f6a5b | 2007-08-15 13:04:54 +0000 | [diff] [blame] | 294 | } |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 295 | |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 296 | #endif /* SQLITE_SYSTEM_MALLOC */ |