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