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