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