drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2001 September 15 |
| 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 | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 12 | ** |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 13 | ** Memory allocation functions used throughout sqlite. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 14 | */ |
| 15 | #include "sqliteInt.h" |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 16 | #include <stdarg.h> |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 17 | |
| 18 | /* |
danielk1977 | 8468024 | 2008-06-23 11:11:35 +0000 | [diff] [blame] | 19 | ** Attempt to release up to n bytes of non-essential memory currently |
| 20 | ** held by SQLite. An example of non-essential memory is memory used to |
| 21 | ** cache database pages that are not currently in use. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 22 | */ |
| 23 | int sqlite3_release_memory(int n){ |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 24 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
drh | 9f129f4 | 2010-08-31 15:27:32 +0000 | [diff] [blame] | 25 | return sqlite3PcacheReleaseMemory(n); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 26 | #else |
drh | 9f129f4 | 2010-08-31 15:27:32 +0000 | [diff] [blame] | 27 | /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine |
| 28 | ** is a no-op returning zero if SQLite is not compiled with |
| 29 | ** SQLITE_ENABLE_MEMORY_MANAGEMENT. */ |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 30 | UNUSED_PARAMETER(n); |
drh | 9f129f4 | 2010-08-31 15:27:32 +0000 | [diff] [blame] | 31 | return 0; |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 32 | #endif |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 33 | } |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 34 | |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 35 | /* |
| 36 | ** State information local to the memory allocation subsystem. |
| 37 | */ |
danielk1977 | 5c8f858 | 2008-09-02 10:22:00 +0000 | [diff] [blame] | 38 | static SQLITE_WSD struct Mem0Global { |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 39 | sqlite3_mutex *mutex; /* Mutex to serialize access */ |
drh | 5fb72e5 | 2015-09-10 01:22:09 +0000 | [diff] [blame] | 40 | sqlite3_int64 alarmThreshold; /* The soft heap limit */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 41 | |
| 42 | /* |
drh | 50d1b5f | 2010-08-27 12:21:06 +0000 | [diff] [blame] | 43 | ** True if heap is nearly "full" where "full" is defined by the |
| 44 | ** sqlite3_soft_heap_limit() setting. |
| 45 | */ |
| 46 | int nearlyFull; |
drh | b2a0f75 | 2017-08-28 15:51:35 +0000 | [diff] [blame] | 47 | } mem0 = { 0, 0, 0 }; |
danielk1977 | 5c8f858 | 2008-09-02 10:22:00 +0000 | [diff] [blame] | 48 | |
| 49 | #define mem0 GLOBAL(struct Mem0Global, mem0) |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 50 | |
| 51 | /* |
drh | af89fe6 | 2015-03-23 17:25:18 +0000 | [diff] [blame] | 52 | ** Return the memory allocator mutex. sqlite3_status() needs it. |
| 53 | */ |
| 54 | sqlite3_mutex *sqlite3MallocMutex(void){ |
| 55 | return mem0.mutex; |
| 56 | } |
| 57 | |
drh | f82ccf6 | 2010-09-15 17:54:31 +0000 | [diff] [blame] | 58 | #ifndef SQLITE_OMIT_DEPRECATED |
| 59 | /* |
drh | 5fb72e5 | 2015-09-10 01:22:09 +0000 | [diff] [blame] | 60 | ** Deprecated external interface. It used to set an alarm callback |
| 61 | ** that was invoked when memory usage grew too large. Now it is a |
| 62 | ** no-op. |
drh | f82ccf6 | 2010-09-15 17:54:31 +0000 | [diff] [blame] | 63 | */ |
| 64 | int sqlite3_memory_alarm( |
| 65 | void(*xCallback)(void *pArg, sqlite3_int64 used,int N), |
| 66 | void *pArg, |
| 67 | sqlite3_int64 iThreshold |
| 68 | ){ |
drh | 5fb72e5 | 2015-09-10 01:22:09 +0000 | [diff] [blame] | 69 | (void)xCallback; |
| 70 | (void)pArg; |
| 71 | (void)iThreshold; |
drh | 4ef299a | 2015-09-02 14:56:56 +0000 | [diff] [blame] | 72 | return SQLITE_OK; |
drh | f82ccf6 | 2010-09-15 17:54:31 +0000 | [diff] [blame] | 73 | } |
| 74 | #endif |
| 75 | |
| 76 | /* |
| 77 | ** Set the soft heap-size limit for the library. Passing a zero or |
| 78 | ** negative value indicates no limit. |
| 79 | */ |
| 80 | sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){ |
| 81 | sqlite3_int64 priorLimit; |
drh | 5fb72e5 | 2015-09-10 01:22:09 +0000 | [diff] [blame] | 82 | sqlite3_int64 excess; |
| 83 | sqlite3_int64 nUsed; |
drh | f82ccf6 | 2010-09-15 17:54:31 +0000 | [diff] [blame] | 84 | #ifndef SQLITE_OMIT_AUTOINIT |
drh | de0f181 | 2011-12-22 17:10:35 +0000 | [diff] [blame] | 85 | int rc = sqlite3_initialize(); |
| 86 | if( rc ) return -1; |
drh | f82ccf6 | 2010-09-15 17:54:31 +0000 | [diff] [blame] | 87 | #endif |
| 88 | sqlite3_mutex_enter(mem0.mutex); |
| 89 | priorLimit = mem0.alarmThreshold; |
drh | 5fb72e5 | 2015-09-10 01:22:09 +0000 | [diff] [blame] | 90 | if( n<0 ){ |
| 91 | sqlite3_mutex_leave(mem0.mutex); |
| 92 | return priorLimit; |
drh | f82ccf6 | 2010-09-15 17:54:31 +0000 | [diff] [blame] | 93 | } |
drh | 5fb72e5 | 2015-09-10 01:22:09 +0000 | [diff] [blame] | 94 | mem0.alarmThreshold = n; |
| 95 | nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); |
| 96 | mem0.nearlyFull = (n>0 && n<=nUsed); |
drh | 4ef299a | 2015-09-02 14:56:56 +0000 | [diff] [blame] | 97 | sqlite3_mutex_leave(mem0.mutex); |
drh | 5fb72e5 | 2015-09-10 01:22:09 +0000 | [diff] [blame] | 98 | excess = sqlite3_memory_used() - n; |
| 99 | if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff)); |
drh | f82ccf6 | 2010-09-15 17:54:31 +0000 | [diff] [blame] | 100 | return priorLimit; |
| 101 | } |
| 102 | void sqlite3_soft_heap_limit(int n){ |
| 103 | if( n<0 ) n = 0; |
| 104 | sqlite3_soft_heap_limit64(n); |
| 105 | } |
| 106 | |
| 107 | /* |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 108 | ** Initialize the memory allocation subsystem. |
| 109 | */ |
| 110 | int sqlite3MallocInit(void){ |
drh | 592f0cb | 2015-03-26 17:04:23 +0000 | [diff] [blame] | 111 | int rc; |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 112 | if( sqlite3GlobalConfig.m.xMalloc==0 ){ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 113 | sqlite3MemSetDefault(); |
| 114 | } |
| 115 | memset(&mem0, 0, sizeof(mem0)); |
drh | 59a0523 | 2015-10-15 17:21:35 +0000 | [diff] [blame] | 116 | mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); |
drh | 50d1b5f | 2010-08-27 12:21:06 +0000 | [diff] [blame] | 117 | if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512 |
drh | 01c5c00 | 2015-07-04 18:15:04 +0000 | [diff] [blame] | 118 | || sqlite3GlobalConfig.nPage<=0 ){ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 119 | sqlite3GlobalConfig.pPage = 0; |
| 120 | sqlite3GlobalConfig.szPage = 0; |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 121 | } |
drh | 592f0cb | 2015-03-26 17:04:23 +0000 | [diff] [blame] | 122 | rc = sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData); |
| 123 | if( rc!=SQLITE_OK ) memset(&mem0, 0, sizeof(mem0)); |
| 124 | return rc; |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /* |
drh | 50d1b5f | 2010-08-27 12:21:06 +0000 | [diff] [blame] | 128 | ** Return true if the heap is currently under memory pressure - in other |
| 129 | ** words if the amount of heap used is close to the limit set by |
| 130 | ** sqlite3_soft_heap_limit(). |
| 131 | */ |
| 132 | int sqlite3HeapNearlyFull(void){ |
| 133 | return mem0.nearlyFull; |
| 134 | } |
| 135 | |
| 136 | /* |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 137 | ** Deinitialize the memory allocation subsystem. |
| 138 | */ |
| 139 | void sqlite3MallocEnd(void){ |
danielk1977 | 0a54907 | 2009-02-17 16:29:10 +0000 | [diff] [blame] | 140 | if( sqlite3GlobalConfig.m.xShutdown ){ |
| 141 | sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData); |
| 142 | } |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 143 | memset(&mem0, 0, sizeof(mem0)); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /* |
| 147 | ** Return the amount of memory currently checked out. |
| 148 | */ |
| 149 | sqlite3_int64 sqlite3_memory_used(void){ |
drh | df5e1a0 | 2015-05-10 02:01:08 +0000 | [diff] [blame] | 150 | sqlite3_int64 res, mx; |
| 151 | sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, 0); |
drh | c376a19 | 2008-07-14 12:30:54 +0000 | [diff] [blame] | 152 | return res; |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /* |
| 156 | ** Return the maximum amount of memory that has ever been |
| 157 | ** checked out since either the beginning of this process |
| 158 | ** or since the most recent reset. |
| 159 | */ |
| 160 | sqlite3_int64 sqlite3_memory_highwater(int resetFlag){ |
drh | df5e1a0 | 2015-05-10 02:01:08 +0000 | [diff] [blame] | 161 | sqlite3_int64 res, mx; |
| 162 | sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, resetFlag); |
| 163 | return mx; |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /* |
drh | 5fb72e5 | 2015-09-10 01:22:09 +0000 | [diff] [blame] | 167 | ** Trigger the alarm |
| 168 | */ |
| 169 | static void sqlite3MallocAlarm(int nByte){ |
| 170 | if( mem0.alarmThreshold<=0 ) return; |
| 171 | sqlite3_mutex_leave(mem0.mutex); |
| 172 | sqlite3_release_memory(nByte); |
| 173 | sqlite3_mutex_enter(mem0.mutex); |
| 174 | } |
| 175 | |
| 176 | /* |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 177 | ** Do a memory allocation with statistics and alarms. Assume the |
| 178 | ** lock is already held. |
| 179 | */ |
drh | 1d21bac | 2017-01-10 16:09:46 +0000 | [diff] [blame] | 180 | static void mallocWithAlarm(int n, void **pp){ |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 181 | void *p; |
drh | 087a29c | 2017-02-08 16:01:57 +0000 | [diff] [blame] | 182 | int nFull; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 183 | assert( sqlite3_mutex_held(mem0.mutex) ); |
drh | 087a29c | 2017-02-08 16:01:57 +0000 | [diff] [blame] | 184 | assert( n>0 ); |
| 185 | |
mistachkin | 40b8436 | 2017-02-08 18:13:46 +0000 | [diff] [blame] | 186 | /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal |
drh | 087a29c | 2017-02-08 16:01:57 +0000 | [diff] [blame] | 187 | ** implementation of malloc_good_size(), which must be called in debug |
| 188 | ** mode and specifically when the DMD "Dark Matter Detector" is enabled |
mistachkin | 40b8436 | 2017-02-08 18:13:46 +0000 | [diff] [blame] | 189 | ** or else a crash results. Hence, do not attempt to optimize out the |
| 190 | ** following xRoundup() call. */ |
drh | 087a29c | 2017-02-08 16:01:57 +0000 | [diff] [blame] | 191 | nFull = sqlite3GlobalConfig.m.xRoundup(n); |
| 192 | |
drh | e43d6ae | 2017-03-10 15:55:54 +0000 | [diff] [blame] | 193 | #ifdef SQLITE_MAX_MEMORY |
| 194 | if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nFull>SQLITE_MAX_MEMORY ){ |
drh | a6bf20b | 2017-03-10 17:03:11 +0000 | [diff] [blame] | 195 | *pp = 0; |
| 196 | return; |
drh | e43d6ae | 2017-03-10 15:55:54 +0000 | [diff] [blame] | 197 | } |
| 198 | #endif |
| 199 | |
drh | b02392e | 2015-10-15 15:28:56 +0000 | [diff] [blame] | 200 | sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n); |
drh | 5fb72e5 | 2015-09-10 01:22:09 +0000 | [diff] [blame] | 201 | if( mem0.alarmThreshold>0 ){ |
| 202 | sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); |
| 203 | if( nUsed >= mem0.alarmThreshold - nFull ){ |
| 204 | mem0.nearlyFull = 1; |
| 205 | sqlite3MallocAlarm(nFull); |
| 206 | }else{ |
| 207 | mem0.nearlyFull = 0; |
| 208 | } |
| 209 | } |
drh | 087a29c | 2017-02-08 16:01:57 +0000 | [diff] [blame] | 210 | p = sqlite3GlobalConfig.m.xMalloc(nFull); |
drh | 50d1b5f | 2010-08-27 12:21:06 +0000 | [diff] [blame] | 211 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
drh | 5fb72e5 | 2015-09-10 01:22:09 +0000 | [diff] [blame] | 212 | if( p==0 && mem0.alarmThreshold>0 ){ |
| 213 | sqlite3MallocAlarm(nFull); |
drh | 087a29c | 2017-02-08 16:01:57 +0000 | [diff] [blame] | 214 | p = sqlite3GlobalConfig.m.xMalloc(nFull); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 215 | } |
drh | 50d1b5f | 2010-08-27 12:21:06 +0000 | [diff] [blame] | 216 | #endif |
drh | c702c7c | 2008-07-18 18:56:16 +0000 | [diff] [blame] | 217 | if( p ){ |
drh | be7a0ce | 2017-01-13 12:53:35 +0000 | [diff] [blame] | 218 | nFull = sqlite3MallocSize(p); |
drh | af89fe6 | 2015-03-23 17:25:18 +0000 | [diff] [blame] | 219 | sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull); |
| 220 | sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1); |
drh | c702c7c | 2008-07-18 18:56:16 +0000 | [diff] [blame] | 221 | } |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 222 | *pp = p; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 223 | } |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 224 | |
| 225 | /* |
| 226 | ** Allocate memory. This routine is like sqlite3_malloc() except that it |
| 227 | ** assumes the memory subsystem has already been initialized. |
| 228 | */ |
drh | da4ca9d | 2014-09-09 17:27:35 +0000 | [diff] [blame] | 229 | void *sqlite3Malloc(u64 n){ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 230 | void *p; |
drh | da4ca9d | 2014-09-09 17:27:35 +0000 | [diff] [blame] | 231 | if( n==0 || n>=0x7fffff00 ){ |
drh | e08ed7e | 2009-06-26 18:35:16 +0000 | [diff] [blame] | 232 | /* A memory allocation of a number of bytes which is near the maximum |
| 233 | ** signed integer value might cause an integer overflow inside of the |
| 234 | ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving |
| 235 | ** 255 bytes of overhead. SQLite itself will never use anything near |
| 236 | ** this amount. The only way to reach the limit is with sqlite3_malloc() */ |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 237 | p = 0; |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 238 | }else if( sqlite3GlobalConfig.bMemstat ){ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 239 | sqlite3_mutex_enter(mem0.mutex); |
drh | 3329a63 | 2014-09-18 01:21:43 +0000 | [diff] [blame] | 240 | mallocWithAlarm((int)n, &p); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 241 | sqlite3_mutex_leave(mem0.mutex); |
| 242 | }else{ |
drh | da4ca9d | 2014-09-09 17:27:35 +0000 | [diff] [blame] | 243 | p = sqlite3GlobalConfig.m.xMalloc((int)n); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 244 | } |
drh | 8da4741 | 2014-10-03 14:54:47 +0000 | [diff] [blame] | 245 | assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 246 | return p; |
| 247 | } |
| 248 | |
| 249 | /* |
| 250 | ** This version of the memory allocation is for use by the application. |
| 251 | ** First make sure the memory subsystem is initialized, then do the |
| 252 | ** allocation. |
| 253 | */ |
| 254 | void *sqlite3_malloc(int n){ |
| 255 | #ifndef SQLITE_OMIT_AUTOINIT |
| 256 | if( sqlite3_initialize() ) return 0; |
| 257 | #endif |
drh | da4ca9d | 2014-09-09 17:27:35 +0000 | [diff] [blame] | 258 | return n<=0 ? 0 : sqlite3Malloc(n); |
| 259 | } |
| 260 | void *sqlite3_malloc64(sqlite3_uint64 n){ |
| 261 | #ifndef SQLITE_OMIT_AUTOINIT |
| 262 | if( sqlite3_initialize() ) return 0; |
| 263 | #endif |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 264 | return sqlite3Malloc(n); |
| 265 | } |
| 266 | |
| 267 | /* |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 268 | ** TRUE if p is a lookaside memory allocation from db |
| 269 | */ |
drh | 4150ebf | 2008-10-11 15:38:29 +0000 | [diff] [blame] | 270 | #ifndef SQLITE_OMIT_LOOKASIDE |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 271 | static int isLookaside(sqlite3 *db, void *p){ |
drh | ac536e6 | 2015-12-10 15:09:17 +0000 | [diff] [blame] | 272 | return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pEnd); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 273 | } |
drh | 4150ebf | 2008-10-11 15:38:29 +0000 | [diff] [blame] | 274 | #else |
| 275 | #define isLookaside(A,B) 0 |
| 276 | #endif |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 277 | |
| 278 | /* |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 279 | ** Return the size of a memory allocation previously obtained from |
| 280 | ** sqlite3Malloc() or sqlite3_malloc(). |
| 281 | */ |
| 282 | int sqlite3MallocSize(void *p){ |
drh | 107b56e | 2010-03-12 16:32:53 +0000 | [diff] [blame] | 283 | assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 284 | return sqlite3GlobalConfig.m.xSize(p); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 285 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 286 | int sqlite3DbMallocSize(sqlite3 *db, void *p){ |
drh | 039ca6a | 2015-10-15 16:20:57 +0000 | [diff] [blame] | 287 | assert( p!=0 ); |
drh | 054bbab | 2015-09-01 20:09:33 +0000 | [diff] [blame] | 288 | if( db==0 || !isLookaside(db,p) ){ |
drh | d879e3e | 2017-02-13 13:35:55 +0000 | [diff] [blame] | 289 | #ifdef SQLITE_DEBUG |
drh | 054bbab | 2015-09-01 20:09:33 +0000 | [diff] [blame] | 290 | if( db==0 ){ |
| 291 | assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) ); |
| 292 | assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
drh | 17bcb10 | 2014-09-18 21:25:33 +0000 | [diff] [blame] | 293 | }else{ |
drh | d231aa3 | 2014-10-07 15:46:54 +0000 | [diff] [blame] | 294 | assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
mistachkin | d425864 | 2015-03-21 23:38:59 +0000 | [diff] [blame] | 295 | assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
drh | 17bcb10 | 2014-09-18 21:25:33 +0000 | [diff] [blame] | 296 | } |
drh | 054bbab | 2015-09-01 20:09:33 +0000 | [diff] [blame] | 297 | #endif |
| 298 | return sqlite3GlobalConfig.m.xSize(p); |
| 299 | }else{ |
| 300 | assert( sqlite3_mutex_held(db->mutex) ); |
| 301 | return db->lookaside.sz; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 302 | } |
| 303 | } |
drh | da4ca9d | 2014-09-09 17:27:35 +0000 | [diff] [blame] | 304 | sqlite3_uint64 sqlite3_msize(void *p){ |
mistachkin | d425864 | 2015-03-21 23:38:59 +0000 | [diff] [blame] | 305 | assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) ); |
drh | d231aa3 | 2014-10-07 15:46:54 +0000 | [diff] [blame] | 306 | assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
drh | 039ca6a | 2015-10-15 16:20:57 +0000 | [diff] [blame] | 307 | return p ? sqlite3GlobalConfig.m.xSize(p) : 0; |
drh | da4ca9d | 2014-09-09 17:27:35 +0000 | [diff] [blame] | 308 | } |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 309 | |
| 310 | /* |
| 311 | ** Free memory previously obtained from sqlite3Malloc(). |
| 312 | */ |
| 313 | void sqlite3_free(void *p){ |
drh | 71a1a0f | 2010-09-11 16:15:55 +0000 | [diff] [blame] | 314 | if( p==0 ) return; /* IMP: R-49053-54554 */ |
drh | 107b56e | 2010-03-12 16:32:53 +0000 | [diff] [blame] | 315 | assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
mistachkin | d425864 | 2015-03-21 23:38:59 +0000 | [diff] [blame] | 316 | assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) ); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 317 | if( sqlite3GlobalConfig.bMemstat ){ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 318 | sqlite3_mutex_enter(mem0.mutex); |
drh | af89fe6 | 2015-03-23 17:25:18 +0000 | [diff] [blame] | 319 | sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p)); |
| 320 | sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 321 | sqlite3GlobalConfig.m.xFree(p); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 322 | sqlite3_mutex_leave(mem0.mutex); |
| 323 | }else{ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 324 | sqlite3GlobalConfig.m.xFree(p); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | |
| 328 | /* |
drh | b4586f1 | 2014-08-23 19:42:06 +0000 | [diff] [blame] | 329 | ** Add the size of memory allocation "p" to the count in |
| 330 | ** *db->pnBytesFreed. |
| 331 | */ |
| 332 | static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){ |
drh | 56d90be | 2015-10-26 12:55:56 +0000 | [diff] [blame] | 333 | *db->pnBytesFreed += sqlite3DbMallocSize(db,p); |
drh | b4586f1 | 2014-08-23 19:42:06 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | /* |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 337 | ** Free memory that might be associated with a particular database |
drh | dbd6a7d | 2017-04-05 12:39:49 +0000 | [diff] [blame] | 338 | ** connection. Calling sqlite3DbFree(D,X) for X==0 is a harmless no-op. |
| 339 | ** The sqlite3DbFreeNN(D,X) version requires that X be non-NULL. |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 340 | */ |
drh | dbd6a7d | 2017-04-05 12:39:49 +0000 | [diff] [blame] | 341 | void sqlite3DbFreeNN(sqlite3 *db, void *p){ |
drh | 7047e25 | 2009-03-23 17:49:14 +0000 | [diff] [blame] | 342 | assert( db==0 || sqlite3_mutex_held(db->mutex) ); |
drh | dbd6a7d | 2017-04-05 12:39:49 +0000 | [diff] [blame] | 343 | assert( p!=0 ); |
drh | 174b9a1 | 2010-07-26 11:07:20 +0000 | [diff] [blame] | 344 | if( db ){ |
| 345 | if( db->pnBytesFreed ){ |
drh | b4586f1 | 2014-08-23 19:42:06 +0000 | [diff] [blame] | 346 | measureAllocationSize(db, p); |
drh | 174b9a1 | 2010-07-26 11:07:20 +0000 | [diff] [blame] | 347 | return; |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 348 | } |
drh | 174b9a1 | 2010-07-26 11:07:20 +0000 | [diff] [blame] | 349 | if( isLookaside(db, p) ){ |
| 350 | LookasideSlot *pBuf = (LookasideSlot*)p; |
drh | d879e3e | 2017-02-13 13:35:55 +0000 | [diff] [blame] | 351 | #ifdef SQLITE_DEBUG |
drh | 3608f17 | 2012-05-21 16:59:16 +0000 | [diff] [blame] | 352 | /* Trash all content in the buffer being freed */ |
| 353 | memset(p, 0xaa, db->lookaside.sz); |
| 354 | #endif |
drh | 174b9a1 | 2010-07-26 11:07:20 +0000 | [diff] [blame] | 355 | pBuf->pNext = db->lookaside.pFree; |
| 356 | db->lookaside.pFree = pBuf; |
drh | 174b9a1 | 2010-07-26 11:07:20 +0000 | [diff] [blame] | 357 | return; |
| 358 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 359 | } |
drh | d231aa3 | 2014-10-07 15:46:54 +0000 | [diff] [blame] | 360 | assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
mistachkin | d425864 | 2015-03-21 23:38:59 +0000 | [diff] [blame] | 361 | assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
drh | 174b9a1 | 2010-07-26 11:07:20 +0000 | [diff] [blame] | 362 | assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) ); |
| 363 | sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 364 | sqlite3_free(p); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 365 | } |
drh | dbd6a7d | 2017-04-05 12:39:49 +0000 | [diff] [blame] | 366 | void sqlite3DbFree(sqlite3 *db, void *p){ |
| 367 | assert( db==0 || sqlite3_mutex_held(db->mutex) ); |
| 368 | if( p ) sqlite3DbFreeNN(db, p); |
| 369 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 370 | |
| 371 | /* |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 372 | ** Change the size of an existing memory allocation |
| 373 | */ |
drh | da4ca9d | 2014-09-09 17:27:35 +0000 | [diff] [blame] | 374 | void *sqlite3Realloc(void *pOld, u64 nBytes){ |
shaneh | ca591fe | 2011-04-15 19:30:42 +0000 | [diff] [blame] | 375 | int nOld, nNew, nDiff; |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 376 | void *pNew; |
drh | d231aa3 | 2014-10-07 15:46:54 +0000 | [diff] [blame] | 377 | assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) ); |
mistachkin | d425864 | 2015-03-21 23:38:59 +0000 | [diff] [blame] | 378 | assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) ); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 379 | if( pOld==0 ){ |
drh | 8da4741 | 2014-10-03 14:54:47 +0000 | [diff] [blame] | 380 | return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 381 | } |
drh | da4ca9d | 2014-09-09 17:27:35 +0000 | [diff] [blame] | 382 | if( nBytes==0 ){ |
drh | 8da4741 | 2014-10-03 14:54:47 +0000 | [diff] [blame] | 383 | sqlite3_free(pOld); /* IMP: R-26507-47431 */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 384 | return 0; |
| 385 | } |
drh | b6063cf | 2009-06-27 00:48:33 +0000 | [diff] [blame] | 386 | if( nBytes>=0x7fffff00 ){ |
| 387 | /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */ |
| 388 | return 0; |
| 389 | } |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 390 | nOld = sqlite3MallocSize(pOld); |
drh | 9f129f4 | 2010-08-31 15:27:32 +0000 | [diff] [blame] | 391 | /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second |
| 392 | ** argument to xRealloc is always a value returned by a prior call to |
| 393 | ** xRoundup. */ |
drh | da4ca9d | 2014-09-09 17:27:35 +0000 | [diff] [blame] | 394 | nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes); |
drh | 7c6791c | 2009-08-18 14:48:53 +0000 | [diff] [blame] | 395 | if( nOld==nNew ){ |
| 396 | pNew = pOld; |
| 397 | }else if( sqlite3GlobalConfig.bMemstat ){ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 398 | sqlite3_mutex_enter(mem0.mutex); |
drh | b02392e | 2015-10-15 15:28:56 +0000 | [diff] [blame] | 399 | sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes); |
drh | 8e1bb04 | 2011-04-15 16:39:52 +0000 | [diff] [blame] | 400 | nDiff = nNew - nOld; |
drh | 1aa3469 | 2016-12-27 12:08:36 +0000 | [diff] [blame] | 401 | if( nDiff>0 && sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >= |
drh | 5fb72e5 | 2015-09-10 01:22:09 +0000 | [diff] [blame] | 402 | mem0.alarmThreshold-nDiff ){ |
| 403 | sqlite3MallocAlarm(nDiff); |
| 404 | } |
drh | 7c6791c | 2009-08-18 14:48:53 +0000 | [diff] [blame] | 405 | pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); |
drh | 5fb72e5 | 2015-09-10 01:22:09 +0000 | [diff] [blame] | 406 | if( pNew==0 && mem0.alarmThreshold>0 ){ |
| 407 | sqlite3MallocAlarm((int)nBytes); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 408 | pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); |
drh | 7c6791c | 2009-08-18 14:48:53 +0000 | [diff] [blame] | 409 | } |
| 410 | if( pNew ){ |
| 411 | nNew = sqlite3MallocSize(pNew); |
drh | af89fe6 | 2015-03-23 17:25:18 +0000 | [diff] [blame] | 412 | sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 413 | } |
| 414 | sqlite3_mutex_leave(mem0.mutex); |
| 415 | }else{ |
drh | 7c6791c | 2009-08-18 14:48:53 +0000 | [diff] [blame] | 416 | pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 417 | } |
drh | 8da4741 | 2014-10-03 14:54:47 +0000 | [diff] [blame] | 418 | assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 419 | return pNew; |
| 420 | } |
| 421 | |
| 422 | /* |
| 423 | ** The public interface to sqlite3Realloc. Make sure that the memory |
| 424 | ** subsystem is initialized prior to invoking sqliteRealloc. |
| 425 | */ |
| 426 | void *sqlite3_realloc(void *pOld, int n){ |
| 427 | #ifndef SQLITE_OMIT_AUTOINIT |
| 428 | if( sqlite3_initialize() ) return 0; |
| 429 | #endif |
drh | 8da4741 | 2014-10-03 14:54:47 +0000 | [diff] [blame] | 430 | if( n<0 ) n = 0; /* IMP: R-26507-47431 */ |
drh | da4ca9d | 2014-09-09 17:27:35 +0000 | [diff] [blame] | 431 | return sqlite3Realloc(pOld, n); |
| 432 | } |
| 433 | void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){ |
| 434 | #ifndef SQLITE_OMIT_AUTOINIT |
| 435 | if( sqlite3_initialize() ) return 0; |
| 436 | #endif |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 437 | return sqlite3Realloc(pOld, n); |
| 438 | } |
| 439 | |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 440 | |
| 441 | /* |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 442 | ** Allocate and zero memory. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 443 | */ |
drh | da4ca9d | 2014-09-09 17:27:35 +0000 | [diff] [blame] | 444 | void *sqlite3MallocZero(u64 n){ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 445 | void *p = sqlite3Malloc(n); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 446 | if( p ){ |
drh | 20f3df0 | 2014-09-18 02:20:54 +0000 | [diff] [blame] | 447 | memset(p, 0, (size_t)n); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 448 | } |
| 449 | return p; |
| 450 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 451 | |
| 452 | /* |
| 453 | ** Allocate and zero memory. If the allocation fails, make |
| 454 | ** the mallocFailed flag in the connection pointer. |
| 455 | */ |
drh | da4ca9d | 2014-09-09 17:27:35 +0000 | [diff] [blame] | 456 | void *sqlite3DbMallocZero(sqlite3 *db, u64 n){ |
drh | 575fad6 | 2016-02-05 13:38:36 +0000 | [diff] [blame] | 457 | void *p; |
| 458 | testcase( db==0 ); |
| 459 | p = sqlite3DbMallocRaw(db, n); |
| 460 | if( p ) memset(p, 0, (size_t)n); |
| 461 | return p; |
| 462 | } |
| 463 | |
| 464 | |
| 465 | /* Finish the work of sqlite3DbMallocRawNN for the unusual and |
| 466 | ** slower case when the allocation cannot be fulfilled using lookaside. |
| 467 | */ |
| 468 | static SQLITE_NOINLINE void *dbMallocRawFinish(sqlite3 *db, u64 n){ |
| 469 | void *p; |
| 470 | assert( db!=0 ); |
| 471 | p = sqlite3Malloc(n); |
| 472 | if( !p ) sqlite3OomFault(db); |
| 473 | sqlite3MemdebugSetType(p, |
| 474 | (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 475 | return p; |
| 476 | } |
| 477 | |
| 478 | /* |
drh | 1da26a4 | 2016-01-20 03:36:32 +0000 | [diff] [blame] | 479 | ** Allocate memory, either lookaside (if possible) or heap. |
| 480 | ** If the allocation fails, set the mallocFailed flag in |
| 481 | ** the connection pointer. |
drh | ddecae7 | 2008-10-11 17:35:16 +0000 | [diff] [blame] | 482 | ** |
| 483 | ** If db!=0 and db->mallocFailed is true (indicating a prior malloc |
| 484 | ** failure on the same database connection) then always return 0. |
| 485 | ** Hence for a particular database connection, once malloc starts |
| 486 | ** failing, it fails consistently until mallocFailed is reset. |
| 487 | ** This is an important assumption. There are many places in the |
| 488 | ** code that do things like this: |
| 489 | ** |
| 490 | ** int *a = (int*)sqlite3DbMallocRaw(db, 100); |
| 491 | ** int *b = (int*)sqlite3DbMallocRaw(db, 200); |
| 492 | ** if( b ) a[10] = 9; |
| 493 | ** |
| 494 | ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed |
| 495 | ** that all prior mallocs (ex: "a") worked too. |
drh | 575fad6 | 2016-02-05 13:38:36 +0000 | [diff] [blame] | 496 | ** |
| 497 | ** The sqlite3MallocRawNN() variant guarantees that the "db" parameter is |
| 498 | ** not a NULL pointer. |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 499 | */ |
drh | da4ca9d | 2014-09-09 17:27:35 +0000 | [diff] [blame] | 500 | void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){ |
drh | 575fad6 | 2016-02-05 13:38:36 +0000 | [diff] [blame] | 501 | void *p; |
| 502 | if( db ) return sqlite3DbMallocRawNN(db, n); |
| 503 | p = sqlite3Malloc(n); |
| 504 | sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 505 | return p; |
| 506 | } |
| 507 | void *sqlite3DbMallocRawNN(sqlite3 *db, u64 n){ |
drh | f5818aa | 2016-02-06 19:48:50 +0000 | [diff] [blame] | 508 | #ifndef SQLITE_OMIT_LOOKASIDE |
| 509 | LookasideSlot *pBuf; |
drh | 575fad6 | 2016-02-05 13:38:36 +0000 | [diff] [blame] | 510 | assert( db!=0 ); |
| 511 | assert( sqlite3_mutex_held(db->mutex) ); |
| 512 | assert( db->pnBytesFreed==0 ); |
drh | 575fad6 | 2016-02-05 13:38:36 +0000 | [diff] [blame] | 513 | if( db->lookaside.bDisable==0 ){ |
| 514 | assert( db->mallocFailed==0 ); |
| 515 | if( n>db->lookaside.sz ){ |
| 516 | db->lookaside.anStat[1]++; |
drh | 52fb8e1 | 2017-08-29 20:21:12 +0000 | [diff] [blame] | 517 | }else if( (pBuf = db->lookaside.pFree)!=0 ){ |
drh | 575fad6 | 2016-02-05 13:38:36 +0000 | [diff] [blame] | 518 | db->lookaside.pFree = pBuf->pNext; |
drh | 575fad6 | 2016-02-05 13:38:36 +0000 | [diff] [blame] | 519 | db->lookaside.anStat[0]++; |
drh | 575fad6 | 2016-02-05 13:38:36 +0000 | [diff] [blame] | 520 | return (void*)pBuf; |
drh | 52fb8e1 | 2017-08-29 20:21:12 +0000 | [diff] [blame] | 521 | }else if( (pBuf = db->lookaside.pInit)!=0 ){ |
| 522 | db->lookaside.pInit = pBuf->pNext; |
| 523 | db->lookaside.anStat[0]++; |
| 524 | return (void*)pBuf; |
| 525 | }else{ |
| 526 | db->lookaside.anStat[2]++; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 527 | } |
drh | 575fad6 | 2016-02-05 13:38:36 +0000 | [diff] [blame] | 528 | }else if( db->mallocFailed ){ |
| 529 | return 0; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 530 | } |
drh | ddecae7 | 2008-10-11 17:35:16 +0000 | [diff] [blame] | 531 | #else |
drh | f5818aa | 2016-02-06 19:48:50 +0000 | [diff] [blame] | 532 | assert( db!=0 ); |
| 533 | assert( sqlite3_mutex_held(db->mutex) ); |
| 534 | assert( db->pnBytesFreed==0 ); |
drh | 575fad6 | 2016-02-05 13:38:36 +0000 | [diff] [blame] | 535 | if( db->mallocFailed ){ |
drh | ddecae7 | 2008-10-11 17:35:16 +0000 | [diff] [blame] | 536 | return 0; |
| 537 | } |
drh | 4150ebf | 2008-10-11 15:38:29 +0000 | [diff] [blame] | 538 | #endif |
drh | 1da26a4 | 2016-01-20 03:36:32 +0000 | [diff] [blame] | 539 | return dbMallocRawFinish(db, n); |
| 540 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 541 | |
drh | b84e574 | 2016-02-05 02:42:54 +0000 | [diff] [blame] | 542 | /* Forward declaration */ |
| 543 | static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n); |
| 544 | |
danielk1977 | 26783a5 | 2007-08-29 14:06:22 +0000 | [diff] [blame] | 545 | /* |
| 546 | ** Resize the block of memory pointed to by p to n bytes. If the |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 547 | ** resize fails, set the mallocFailed flag in the connection object. |
danielk1977 | 26783a5 | 2007-08-29 14:06:22 +0000 | [diff] [blame] | 548 | */ |
drh | da4ca9d | 2014-09-09 17:27:35 +0000 | [diff] [blame] | 549 | void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){ |
drh | b84e574 | 2016-02-05 02:42:54 +0000 | [diff] [blame] | 550 | assert( db!=0 ); |
drh | 575fad6 | 2016-02-05 13:38:36 +0000 | [diff] [blame] | 551 | if( p==0 ) return sqlite3DbMallocRawNN(db, n); |
drh | b84e574 | 2016-02-05 02:42:54 +0000 | [diff] [blame] | 552 | assert( sqlite3_mutex_held(db->mutex) ); |
| 553 | if( isLookaside(db,p) && n<=db->lookaside.sz ) return p; |
| 554 | return dbReallocFinish(db, p, n); |
| 555 | } |
| 556 | static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 557 | void *pNew = 0; |
drh | d9da78a | 2009-03-24 15:08:09 +0000 | [diff] [blame] | 558 | assert( db!=0 ); |
drh | b84e574 | 2016-02-05 02:42:54 +0000 | [diff] [blame] | 559 | assert( p!=0 ); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 560 | if( db->mallocFailed==0 ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 561 | if( isLookaside(db, p) ){ |
drh | 575fad6 | 2016-02-05 13:38:36 +0000 | [diff] [blame] | 562 | pNew = sqlite3DbMallocRawNN(db, n); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 563 | if( pNew ){ |
| 564 | memcpy(pNew, p, db->lookaside.sz); |
| 565 | sqlite3DbFree(db, p); |
| 566 | } |
| 567 | }else{ |
drh | d231aa3 | 2014-10-07 15:46:54 +0000 | [diff] [blame] | 568 | assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
mistachkin | d425864 | 2015-03-21 23:38:59 +0000 | [diff] [blame] | 569 | assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
drh | 107b56e | 2010-03-12 16:32:53 +0000 | [diff] [blame] | 570 | sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
drh | 3329a63 | 2014-09-18 01:21:43 +0000 | [diff] [blame] | 571 | pNew = sqlite3_realloc64(p, n); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 572 | if( !pNew ){ |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 573 | sqlite3OomFault(db); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 574 | } |
drh | d231aa3 | 2014-10-07 15:46:54 +0000 | [diff] [blame] | 575 | sqlite3MemdebugSetType(pNew, |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 576 | (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP)); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 577 | } |
| 578 | } |
| 579 | return pNew; |
| 580 | } |
| 581 | |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 582 | /* |
| 583 | ** Attempt to reallocate p. If the reallocation fails, then free p |
| 584 | ** and set the mallocFailed flag in the database connection. |
| 585 | */ |
drh | da4ca9d | 2014-09-09 17:27:35 +0000 | [diff] [blame] | 586 | void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 587 | void *pNew; |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 588 | pNew = sqlite3DbRealloc(db, p, n); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 589 | if( !pNew ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 590 | sqlite3DbFree(db, p); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 591 | } |
| 592 | return pNew; |
| 593 | } |
| 594 | |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 595 | /* |
| 596 | ** Make a copy of a string in memory obtained from sqliteMalloc(). These |
| 597 | ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This |
| 598 | ** is because when memory debugging is turned on, these two functions are |
| 599 | ** called via macros that record the current file and line number in the |
| 600 | ** ThreadData structure. |
| 601 | */ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 602 | char *sqlite3DbStrDup(sqlite3 *db, const char *z){ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 603 | char *zNew; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 604 | size_t n; |
| 605 | if( z==0 ){ |
| 606 | return 0; |
| 607 | } |
drh | cee11ad | 2016-10-17 00:48:06 +0000 | [diff] [blame] | 608 | n = strlen(z) + 1; |
| 609 | zNew = sqlite3DbMallocRaw(db, n); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 610 | if( zNew ){ |
| 611 | memcpy(zNew, z, n); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 612 | } |
| 613 | return zNew; |
| 614 | } |
drh | da4ca9d | 2014-09-09 17:27:35 +0000 | [diff] [blame] | 615 | char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 616 | char *zNew; |
drh | 575fad6 | 2016-02-05 13:38:36 +0000 | [diff] [blame] | 617 | assert( db!=0 ); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 618 | if( z==0 ){ |
| 619 | return 0; |
| 620 | } |
| 621 | assert( (n&0x7fffffff)==n ); |
drh | 575fad6 | 2016-02-05 13:38:36 +0000 | [diff] [blame] | 622 | zNew = sqlite3DbMallocRawNN(db, n+1); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 623 | if( zNew ){ |
drh | 20f3df0 | 2014-09-18 02:20:54 +0000 | [diff] [blame] | 624 | memcpy(zNew, z, (size_t)n); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 625 | zNew[n] = 0; |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 626 | } |
| 627 | return zNew; |
| 628 | } |
| 629 | |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 630 | /* |
drh | 9b2e043 | 2017-12-27 19:43:22 +0000 | [diff] [blame] | 631 | ** The text between zStart and zEnd represents a phrase within a larger |
| 632 | ** SQL statement. Make a copy of this phrase in space obtained form |
| 633 | ** sqlite3DbMalloc(). Omit leading and trailing whitespace. |
| 634 | */ |
| 635 | char *sqlite3DbSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){ |
| 636 | int n; |
| 637 | while( sqlite3Isspace(zStart[0]) ) zStart++; |
| 638 | n = (int)(zEnd - zStart); |
drh | e75d1f5 | 2018-01-10 13:58:23 +0000 | [diff] [blame] | 639 | while( ALWAYS(n>0) && sqlite3Isspace(zStart[n-1]) ) n--; |
drh | 9b2e043 | 2017-12-27 19:43:22 +0000 | [diff] [blame] | 640 | return sqlite3DbStrNDup(db, zStart, n); |
| 641 | } |
| 642 | |
| 643 | /* |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 644 | ** Free any prior content in *pz and replace it with a copy of zNew. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 645 | */ |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 646 | void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 647 | sqlite3DbFree(db, *pz); |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 648 | *pz = sqlite3DbStrDup(db, zNew); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 649 | } |
| 650 | |
drh | b50c65d | 2014-08-23 20:25:53 +0000 | [diff] [blame] | 651 | /* |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 652 | ** Call this routine to record the fact that an OOM (out-of-memory) error |
| 653 | ** has happened. This routine will set db->mallocFailed, and also |
| 654 | ** temporarily disable the lookaside memory allocator and interrupt |
| 655 | ** any running VDBEs. |
| 656 | */ |
| 657 | void sqlite3OomFault(sqlite3 *db){ |
| 658 | if( db->mallocFailed==0 && db->bBenignMalloc==0 ){ |
| 659 | db->mallocFailed = 1; |
| 660 | if( db->nVdbeExec>0 ){ |
| 661 | db->u1.isInterrupted = 1; |
| 662 | } |
| 663 | db->lookaside.bDisable++; |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | /* |
| 668 | ** This routine reactivates the memory allocator and clears the |
| 669 | ** db->mallocFailed flag as necessary. |
| 670 | ** |
| 671 | ** The memory allocator is not restarted if there are running |
| 672 | ** VDBEs. |
| 673 | */ |
| 674 | void sqlite3OomClear(sqlite3 *db){ |
| 675 | if( db->mallocFailed && db->nVdbeExec==0 ){ |
| 676 | db->mallocFailed = 0; |
| 677 | db->u1.isInterrupted = 0; |
| 678 | assert( db->lookaside.bDisable>0 ); |
| 679 | db->lookaside.bDisable--; |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | /* |
drh | b50c65d | 2014-08-23 20:25:53 +0000 | [diff] [blame] | 684 | ** Take actions at the end of an API call to indicate an OOM error |
| 685 | */ |
| 686 | static SQLITE_NOINLINE int apiOomError(sqlite3 *db){ |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 687 | sqlite3OomClear(db); |
drh | b50c65d | 2014-08-23 20:25:53 +0000 | [diff] [blame] | 688 | sqlite3Error(db, SQLITE_NOMEM); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 689 | return SQLITE_NOMEM_BKPT; |
drh | b50c65d | 2014-08-23 20:25:53 +0000 | [diff] [blame] | 690 | } |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 691 | |
| 692 | /* |
| 693 | ** This function must be called before exiting any API function (i.e. |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 694 | ** returning control to the user) that has called sqlite3_malloc or |
| 695 | ** sqlite3_realloc. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 696 | ** |
| 697 | ** The returned value is normally a copy of the second argument to this |
shane | be21779 | 2009-03-05 04:20:31 +0000 | [diff] [blame] | 698 | ** function. However, if a malloc() failure has occurred since the previous |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 699 | ** invocation SQLITE_NOMEM is returned instead. |
| 700 | ** |
drh | 597d2b6 | 2015-06-30 03:13:47 +0000 | [diff] [blame] | 701 | ** If an OOM as occurred, then the connection error-code (the value |
| 702 | ** returned by sqlite3_errcode()) is set to SQLITE_NOMEM. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 703 | */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 704 | int sqlite3ApiExit(sqlite3* db, int rc){ |
drh | 597d2b6 | 2015-06-30 03:13:47 +0000 | [diff] [blame] | 705 | /* If the db handle must hold the connection handle mutex here. |
| 706 | ** Otherwise the read (and possible write) of db->mallocFailed |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 707 | ** is unsafe, as is the call to sqlite3Error(). |
| 708 | */ |
drh | 597d2b6 | 2015-06-30 03:13:47 +0000 | [diff] [blame] | 709 | assert( db!=0 ); |
| 710 | assert( sqlite3_mutex_held(db->mutex) ); |
drh | b50c65d | 2014-08-23 20:25:53 +0000 | [diff] [blame] | 711 | if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){ |
| 712 | return apiOomError(db); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 713 | } |
drh | b50c65d | 2014-08-23 20:25:53 +0000 | [diff] [blame] | 714 | return rc & db->errMask; |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 715 | } |