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