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. |
| 14 | ** |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame^] | 15 | ** $Id: malloc.c,v 1.30 2008/07/28 19:34:53 drh Exp $ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 16 | */ |
| 17 | #include "sqliteInt.h" |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 18 | #include <stdarg.h> |
| 19 | #include <ctype.h> |
| 20 | |
| 21 | /* |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 22 | ** This routine runs when the memory allocator sees that the |
| 23 | ** total memory allocation is about to exceed the soft heap |
| 24 | ** limit. |
| 25 | */ |
| 26 | static void softHeapLimitEnforcer( |
| 27 | void *NotUsed, |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 28 | sqlite3_int64 inUse, |
| 29 | int allocSize |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 30 | ){ |
| 31 | sqlite3_release_memory(allocSize); |
| 32 | } |
| 33 | |
| 34 | /* |
danielk1977 | 8468024 | 2008-06-23 11:11:35 +0000 | [diff] [blame] | 35 | ** Set the soft heap-size limit for the library. Passing a zero or |
| 36 | ** negative value indicates no limit. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 37 | */ |
| 38 | void sqlite3_soft_heap_limit(int n){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 39 | sqlite3_uint64 iLimit; |
| 40 | int overage; |
| 41 | if( n<0 ){ |
| 42 | iLimit = 0; |
| 43 | }else{ |
| 44 | iLimit = n; |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 45 | } |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 46 | sqlite3_initialize(); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 47 | if( iLimit>0 ){ |
| 48 | sqlite3_memory_alarm(softHeapLimitEnforcer, 0, iLimit); |
| 49 | }else{ |
| 50 | sqlite3_memory_alarm(0, 0, 0); |
| 51 | } |
| 52 | overage = sqlite3_memory_used() - n; |
| 53 | if( overage>0 ){ |
| 54 | sqlite3_release_memory(overage); |
| 55 | } |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /* |
danielk1977 | 8468024 | 2008-06-23 11:11:35 +0000 | [diff] [blame] | 59 | ** Attempt to release up to n bytes of non-essential memory currently |
| 60 | ** held by SQLite. An example of non-essential memory is memory used to |
| 61 | ** cache database pages that are not currently in use. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 62 | */ |
| 63 | int sqlite3_release_memory(int n){ |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 64 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
danielk1977 | dfb316d | 2008-03-26 18:34:43 +0000 | [diff] [blame] | 65 | int nRet = sqlite3VdbeReleaseMemory(n); |
| 66 | nRet += sqlite3PagerReleaseMemory(n-nRet); |
| 67 | return nRet; |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 68 | #else |
| 69 | return SQLITE_OK; |
| 70 | #endif |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 71 | } |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 72 | |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 73 | /* |
| 74 | ** State information local to the memory allocation subsystem. |
| 75 | */ |
| 76 | static struct { |
| 77 | sqlite3_mutex *mutex; /* Mutex to serialize access */ |
| 78 | |
| 79 | /* |
| 80 | ** The alarm callback and its arguments. The mem0.mutex lock will |
| 81 | ** be held while the callback is running. Recursive calls into |
| 82 | ** the memory subsystem are allowed, but no new callbacks will be |
| 83 | ** issued. The alarmBusy variable is set to prevent recursive |
| 84 | ** callbacks. |
| 85 | */ |
| 86 | sqlite3_int64 alarmThreshold; |
| 87 | void (*alarmCallback)(void*, sqlite3_int64,int); |
| 88 | void *alarmArg; |
| 89 | int alarmBusy; |
| 90 | |
| 91 | /* |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 92 | ** Pointers to the end of sqlite3Config.pScratch and |
| 93 | ** sqlite3Config.pPage to a block of memory that records |
| 94 | ** which pages are available. |
| 95 | */ |
| 96 | u32 *aScratchFree; |
| 97 | u32 *aPageFree; |
| 98 | |
| 99 | /* Number of free pages for scratch and page-cache memory */ |
| 100 | u32 nScratchFree; |
| 101 | u32 nPageFree; |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 102 | } mem0; |
| 103 | |
| 104 | /* |
| 105 | ** Initialize the memory allocation subsystem. |
| 106 | */ |
| 107 | int sqlite3MallocInit(void){ |
| 108 | if( sqlite3Config.m.xMalloc==0 ){ |
| 109 | sqlite3MemSetDefault(); |
| 110 | } |
| 111 | memset(&mem0, 0, sizeof(mem0)); |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 112 | if( sqlite3Config.bCoreMutex ){ |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 113 | mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 114 | } |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 115 | if( sqlite3Config.pScratch && sqlite3Config.szScratch>=3000 |
| 116 | && sqlite3Config.nScratch>0 ){ |
| 117 | int i; |
| 118 | mem0.aScratchFree = (u32*)&((char*)sqlite3Config.pScratch) |
| 119 | [sqlite3Config.szScratch*sqlite3Config.nScratch]; |
| 120 | for(i=0; i<sqlite3Config.nScratch; i++){ mem0.aScratchFree[i] = i; } |
| 121 | mem0.nScratchFree = sqlite3Config.nScratch; |
| 122 | }else{ |
| 123 | sqlite3Config.pScratch = 0; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 124 | sqlite3Config.szScratch = 0; |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 125 | } |
| 126 | if( sqlite3Config.pPage && sqlite3Config.szPage>=512 |
| 127 | && sqlite3Config.nPage>0 ){ |
| 128 | int i; |
| 129 | mem0.aPageFree = (u32*)&((char*)sqlite3Config.pPage) |
| 130 | [sqlite3Config.szPage*sqlite3Config.nPage]; |
| 131 | for(i=0; i<sqlite3Config.nPage; i++){ mem0.aPageFree[i] = i; } |
| 132 | mem0.nPageFree = sqlite3Config.nPage; |
| 133 | }else{ |
| 134 | sqlite3Config.pPage = 0; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 135 | sqlite3Config.szPage = 0; |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 136 | } |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 137 | return sqlite3Config.m.xInit(sqlite3Config.m.pAppData); |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | ** Deinitialize the memory allocation subsystem. |
| 142 | */ |
| 143 | void sqlite3MallocEnd(void){ |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 144 | sqlite3Config.m.xShutdown(sqlite3Config.m.pAppData); |
| 145 | memset(&mem0, 0, sizeof(mem0)); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /* |
| 149 | ** Return the amount of memory currently checked out. |
| 150 | */ |
| 151 | sqlite3_int64 sqlite3_memory_used(void){ |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 152 | int n, mx; |
drh | c376a19 | 2008-07-14 12:30:54 +0000 | [diff] [blame] | 153 | sqlite3_int64 res; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 154 | sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0); |
drh | c376a19 | 2008-07-14 12:30:54 +0000 | [diff] [blame] | 155 | res = (sqlite3_int64)n; /* Work around bug in Borland C. Ticket #3216 */ |
| 156 | return res; |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /* |
| 160 | ** Return the maximum amount of memory that has ever been |
| 161 | ** checked out since either the beginning of this process |
| 162 | ** or since the most recent reset. |
| 163 | */ |
| 164 | sqlite3_int64 sqlite3_memory_highwater(int resetFlag){ |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 165 | int n, mx; |
drh | c376a19 | 2008-07-14 12:30:54 +0000 | [diff] [blame] | 166 | sqlite3_int64 res; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 167 | sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag); |
drh | 7986a71 | 2008-07-14 12:38:20 +0000 | [diff] [blame] | 168 | res = (sqlite3_int64)mx; /* Work around bug in Borland C. Ticket #3216 */ |
drh | c376a19 | 2008-07-14 12:30:54 +0000 | [diff] [blame] | 169 | return res; |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /* |
| 173 | ** Change the alarm callback |
| 174 | */ |
| 175 | int sqlite3_memory_alarm( |
| 176 | void(*xCallback)(void *pArg, sqlite3_int64 used,int N), |
| 177 | void *pArg, |
| 178 | sqlite3_int64 iThreshold |
| 179 | ){ |
| 180 | sqlite3_mutex_enter(mem0.mutex); |
| 181 | mem0.alarmCallback = xCallback; |
| 182 | mem0.alarmArg = pArg; |
| 183 | mem0.alarmThreshold = iThreshold; |
| 184 | sqlite3_mutex_leave(mem0.mutex); |
| 185 | return SQLITE_OK; |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | ** Trigger the alarm |
| 190 | */ |
| 191 | static void sqlite3MallocAlarm(int nByte){ |
| 192 | void (*xCallback)(void*,sqlite3_int64,int); |
| 193 | sqlite3_int64 nowUsed; |
| 194 | void *pArg; |
| 195 | if( mem0.alarmCallback==0 || mem0.alarmBusy ) return; |
| 196 | mem0.alarmBusy = 1; |
| 197 | xCallback = mem0.alarmCallback; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 198 | nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 199 | pArg = mem0.alarmArg; |
| 200 | sqlite3_mutex_leave(mem0.mutex); |
| 201 | xCallback(pArg, nowUsed, nByte); |
| 202 | sqlite3_mutex_enter(mem0.mutex); |
| 203 | mem0.alarmBusy = 0; |
| 204 | } |
| 205 | |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 206 | /* |
| 207 | ** Do a memory allocation with statistics and alarms. Assume the |
| 208 | ** lock is already held. |
| 209 | */ |
| 210 | static int mallocWithAlarm(int n, void **pp){ |
| 211 | int nFull; |
| 212 | void *p; |
| 213 | assert( sqlite3_mutex_held(mem0.mutex) ); |
| 214 | nFull = sqlite3Config.m.xRoundup(n); |
| 215 | sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n); |
| 216 | if( mem0.alarmCallback!=0 ){ |
| 217 | int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); |
| 218 | if( nUsed+nFull >= mem0.alarmThreshold ){ |
| 219 | sqlite3MallocAlarm(nFull); |
| 220 | } |
| 221 | } |
danielk1977 | d09414c | 2008-06-19 18:17:49 +0000 | [diff] [blame] | 222 | p = sqlite3Config.m.xMalloc(nFull); |
| 223 | if( p==0 && mem0.alarmCallback ){ |
| 224 | sqlite3MallocAlarm(nFull); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 225 | p = sqlite3Config.m.xMalloc(nFull); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 226 | } |
drh | c702c7c | 2008-07-18 18:56:16 +0000 | [diff] [blame] | 227 | if( p ){ |
| 228 | nFull = sqlite3MallocSize(p); |
| 229 | sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull); |
| 230 | } |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 231 | *pp = p; |
| 232 | return nFull; |
| 233 | } |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 234 | |
| 235 | /* |
| 236 | ** Allocate memory. This routine is like sqlite3_malloc() except that it |
| 237 | ** assumes the memory subsystem has already been initialized. |
| 238 | */ |
| 239 | void *sqlite3Malloc(int n){ |
| 240 | void *p; |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 241 | if( n<=0 ){ |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 242 | p = 0; |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 243 | }else if( sqlite3Config.bMemstat ){ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 244 | sqlite3_mutex_enter(mem0.mutex); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 245 | mallocWithAlarm(n, &p); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 246 | sqlite3_mutex_leave(mem0.mutex); |
| 247 | }else{ |
| 248 | p = sqlite3Config.m.xMalloc(n); |
| 249 | } |
| 250 | return p; |
| 251 | } |
| 252 | |
| 253 | /* |
| 254 | ** This version of the memory allocation is for use by the application. |
| 255 | ** First make sure the memory subsystem is initialized, then do the |
| 256 | ** allocation. |
| 257 | */ |
| 258 | void *sqlite3_malloc(int n){ |
| 259 | #ifndef SQLITE_OMIT_AUTOINIT |
| 260 | if( sqlite3_initialize() ) return 0; |
| 261 | #endif |
| 262 | return sqlite3Malloc(n); |
| 263 | } |
| 264 | |
| 265 | /* |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 266 | ** Each thread may only have a single outstanding allocation from |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 267 | ** xScratchMalloc(). We verify this constraint in the single-threaded |
| 268 | ** case by setting scratchAllocOut to 1 when an allocation |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 269 | ** is outstanding clearing it when the allocation is freed. |
| 270 | */ |
| 271 | #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 272 | static int scratchAllocOut = 0; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 273 | #endif |
| 274 | |
| 275 | |
| 276 | /* |
| 277 | ** Allocate memory that is to be used and released right away. |
| 278 | ** This routine is similar to alloca() in that it is not intended |
| 279 | ** for situations where the memory might be held long-term. This |
| 280 | ** routine is intended to get memory to old large transient data |
| 281 | ** structures that would not normally fit on the stack of an |
| 282 | ** embedded processor. |
| 283 | */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 284 | void *sqlite3ScratchMalloc(int n){ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 285 | void *p; |
| 286 | assert( n>0 ); |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 287 | |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 288 | #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 289 | /* Verify that no more than one scratch allocation per thread |
| 290 | ** is outstanding at one time. (This is only checked in the |
| 291 | ** single-threaded case since checking in the multi-threaded case |
| 292 | ** would be much more complicated.) */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 293 | assert( scratchAllocOut==0 ); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 294 | #endif |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 295 | |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 296 | if( sqlite3Config.szScratch<n ){ |
| 297 | goto scratch_overflow; |
| 298 | }else{ |
| 299 | sqlite3_mutex_enter(mem0.mutex); |
| 300 | if( mem0.nScratchFree==0 ){ |
| 301 | sqlite3_mutex_leave(mem0.mutex); |
| 302 | goto scratch_overflow; |
| 303 | }else{ |
| 304 | int i; |
| 305 | i = mem0.aScratchFree[--mem0.nScratchFree]; |
| 306 | sqlite3_mutex_leave(mem0.mutex); |
| 307 | i *= sqlite3Config.szScratch; |
| 308 | sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1); |
| 309 | p = (void*)&((char*)sqlite3Config.pScratch)[i]; |
| 310 | } |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 311 | } |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 312 | #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
| 313 | scratchAllocOut = p!=0; |
| 314 | #endif |
| 315 | |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 316 | return p; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 317 | |
| 318 | scratch_overflow: |
| 319 | if( sqlite3Config.bMemstat ){ |
| 320 | sqlite3_mutex_enter(mem0.mutex); |
| 321 | n = mallocWithAlarm(n, &p); |
| 322 | if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n); |
| 323 | sqlite3_mutex_leave(mem0.mutex); |
| 324 | }else{ |
| 325 | p = sqlite3Config.m.xMalloc(n); |
| 326 | } |
| 327 | #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
| 328 | scratchAllocOut = p!=0; |
| 329 | #endif |
| 330 | return p; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 331 | } |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 332 | void sqlite3ScratchFree(void *p){ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 333 | if( p ){ |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 334 | |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 335 | #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 336 | /* Verify that no more than one scratch allocation per thread |
| 337 | ** is outstanding at one time. (This is only checked in the |
| 338 | ** single-threaded case since checking in the multi-threaded case |
| 339 | ** would be much more complicated.) */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 340 | assert( scratchAllocOut==1 ); |
| 341 | scratchAllocOut = 0; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 342 | #endif |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 343 | |
| 344 | if( sqlite3Config.pScratch==0 |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 345 | || p<sqlite3Config.pScratch |
| 346 | || p>=(void*)mem0.aScratchFree ){ |
| 347 | if( sqlite3Config.bMemstat ){ |
| 348 | int iSize = sqlite3MallocSize(p); |
| 349 | sqlite3_mutex_enter(mem0.mutex); |
| 350 | sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize); |
| 351 | sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize); |
| 352 | sqlite3Config.m.xFree(p); |
| 353 | sqlite3_mutex_leave(mem0.mutex); |
| 354 | }else{ |
| 355 | sqlite3Config.m.xFree(p); |
| 356 | } |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 357 | }else{ |
| 358 | int i; |
danielk1977 | 867d05a | 2008-06-23 14:03:45 +0000 | [diff] [blame] | 359 | i = (u8 *)p - (u8 *)sqlite3Config.pScratch; |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 360 | i /= sqlite3Config.szScratch; |
| 361 | assert( i>=0 && i<sqlite3Config.nScratch ); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 362 | sqlite3_mutex_enter(mem0.mutex); |
| 363 | assert( mem0.nScratchFree<sqlite3Config.nScratch ); |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 364 | mem0.aScratchFree[mem0.nScratchFree++] = i; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 365 | sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1); |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 366 | sqlite3_mutex_leave(mem0.mutex); |
| 367 | } |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | |
| 371 | /* |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 372 | ** Allocate memory to be used by the page cache. Make use of the |
| 373 | ** memory buffer provided by SQLITE_CONFIG_PAGECACHE if there is one |
| 374 | ** and that memory is of the right size and is not completely |
| 375 | ** consumed. Otherwise, failover to sqlite3Malloc(). |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 376 | */ |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 377 | void *sqlite3PageMalloc(int n){ |
| 378 | void *p; |
| 379 | assert( n>0 ); |
| 380 | assert( (n & (n-1))==0 ); |
| 381 | assert( n>=512 && n<=32768 ); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 382 | |
| 383 | if( sqlite3Config.szPage<n ){ |
| 384 | goto page_overflow; |
| 385 | }else{ |
| 386 | sqlite3_mutex_enter(mem0.mutex); |
| 387 | if( mem0.nPageFree==0 ){ |
| 388 | sqlite3_mutex_leave(mem0.mutex); |
| 389 | goto page_overflow; |
| 390 | }else{ |
| 391 | int i; |
| 392 | i = mem0.aPageFree[--mem0.nPageFree]; |
| 393 | sqlite3_mutex_leave(mem0.mutex); |
| 394 | i *= sqlite3Config.szPage; |
| 395 | sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1); |
| 396 | p = (void*)&((char*)sqlite3Config.pPage)[i]; |
| 397 | } |
| 398 | } |
| 399 | return p; |
| 400 | |
| 401 | page_overflow: |
| 402 | if( sqlite3Config.bMemstat ){ |
| 403 | sqlite3_mutex_enter(mem0.mutex); |
| 404 | n = mallocWithAlarm(n, &p); |
| 405 | if( p ) sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, n); |
| 406 | sqlite3_mutex_leave(mem0.mutex); |
| 407 | }else{ |
| 408 | p = sqlite3Config.m.xMalloc(n); |
| 409 | } |
| 410 | return p; |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 411 | } |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 412 | void sqlite3PageFree(void *p){ |
| 413 | if( p ){ |
| 414 | if( sqlite3Config.pPage==0 |
| 415 | || p<sqlite3Config.pPage |
| 416 | || p>=(void*)mem0.aPageFree ){ |
danielk1977 | 4b9507a | 2008-06-21 08:12:15 +0000 | [diff] [blame] | 417 | /* In this case, the page allocation was obtained from a regular |
| 418 | ** call to sqlite3_mem_methods.xMalloc() (a page-cache-memory |
| 419 | ** "overflow"). Free the block with sqlite3_mem_methods.xFree(). |
| 420 | */ |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 421 | if( sqlite3Config.bMemstat ){ |
| 422 | int iSize = sqlite3MallocSize(p); |
| 423 | sqlite3_mutex_enter(mem0.mutex); |
| 424 | sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize); |
| 425 | sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize); |
| 426 | sqlite3Config.m.xFree(p); |
| 427 | sqlite3_mutex_leave(mem0.mutex); |
| 428 | }else{ |
| 429 | sqlite3Config.m.xFree(p); |
| 430 | } |
| 431 | }else{ |
danielk1977 | 4b9507a | 2008-06-21 08:12:15 +0000 | [diff] [blame] | 432 | /* The page allocation was allocated from the sqlite3Config.pPage |
| 433 | ** buffer. In this case all that is add the index of the page in |
| 434 | ** the sqlite3Config.pPage array to the set of free indexes stored |
| 435 | ** in the mem0.aPageFree[] array. |
| 436 | */ |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 437 | int i; |
danielk1977 | 867d05a | 2008-06-23 14:03:45 +0000 | [diff] [blame] | 438 | i = (u8 *)p - (u8 *)sqlite3Config.pPage; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 439 | i /= sqlite3Config.szPage; |
| 440 | assert( i>=0 && i<sqlite3Config.nPage ); |
| 441 | sqlite3_mutex_enter(mem0.mutex); |
| 442 | assert( mem0.nPageFree<sqlite3Config.nPage ); |
| 443 | mem0.aPageFree[mem0.nPageFree++] = i; |
| 444 | sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1); |
| 445 | sqlite3_mutex_leave(mem0.mutex); |
danielk1977 | 4b9507a | 2008-06-21 08:12:15 +0000 | [diff] [blame] | 446 | #ifndef NDEBUG |
| 447 | /* Assert that a duplicate was not just inserted into aPageFree[]. */ |
| 448 | for(i=0; i<mem0.nPageFree-1; i++){ |
| 449 | assert( mem0.aPageFree[i]!=mem0.aPageFree[mem0.nPageFree-1] ); |
| 450 | } |
| 451 | #endif |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 452 | } |
| 453 | } |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | /* |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame^] | 457 | ** TRUE if p is a lookaside memory allocation from db |
| 458 | */ |
| 459 | static int isLookaside(sqlite3 *db, void *p){ |
| 460 | return db && p && p>=db->lookaside.pStart && p<db->lookaside.pEnd; |
| 461 | } |
| 462 | |
| 463 | /* |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 464 | ** Return the size of a memory allocation previously obtained from |
| 465 | ** sqlite3Malloc() or sqlite3_malloc(). |
| 466 | */ |
| 467 | int sqlite3MallocSize(void *p){ |
| 468 | return sqlite3Config.m.xSize(p); |
| 469 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame^] | 470 | int sqlite3DbMallocSize(sqlite3 *db, void *p){ |
| 471 | if( isLookaside(db, p) ){ |
| 472 | return db->lookaside.sz; |
| 473 | }else{ |
| 474 | return sqlite3Config.m.xSize(p); |
| 475 | } |
| 476 | } |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 477 | |
| 478 | /* |
| 479 | ** Free memory previously obtained from sqlite3Malloc(). |
| 480 | */ |
| 481 | void sqlite3_free(void *p){ |
| 482 | if( p==0 ) return; |
| 483 | if( sqlite3Config.bMemstat ){ |
| 484 | sqlite3_mutex_enter(mem0.mutex); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 485 | sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p)); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 486 | sqlite3Config.m.xFree(p); |
| 487 | sqlite3_mutex_leave(mem0.mutex); |
| 488 | }else{ |
| 489 | sqlite3Config.m.xFree(p); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | /* |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame^] | 494 | ** Free memory that might be associated with a particular database |
| 495 | ** connection. |
| 496 | */ |
| 497 | void sqlite3DbFree(sqlite3 *db, void *p){ |
| 498 | if( isLookaside(db, p) ){ |
| 499 | LookasideSlot *pBuf = (LookasideSlot*)p; |
| 500 | pBuf->pNext = db->lookaside.pFree; |
| 501 | db->lookaside.pFree = pBuf; |
| 502 | db->lookaside.nOut--; |
| 503 | }else{ |
| 504 | sqlite3_free(p); |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | /* |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 509 | ** Change the size of an existing memory allocation |
| 510 | */ |
| 511 | void *sqlite3Realloc(void *pOld, int nBytes){ |
| 512 | int nOld, nNew; |
| 513 | void *pNew; |
| 514 | if( pOld==0 ){ |
| 515 | return sqlite3Malloc(nBytes); |
| 516 | } |
| 517 | if( nBytes<=0 ){ |
| 518 | sqlite3_free(pOld); |
| 519 | return 0; |
| 520 | } |
| 521 | nOld = sqlite3MallocSize(pOld); |
| 522 | if( sqlite3Config.bMemstat ){ |
| 523 | sqlite3_mutex_enter(mem0.mutex); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 524 | sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 525 | nNew = sqlite3Config.m.xRoundup(nBytes); |
| 526 | if( nOld==nNew ){ |
| 527 | pNew = pOld; |
| 528 | }else{ |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 529 | if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >= |
| 530 | mem0.alarmThreshold ){ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 531 | sqlite3MallocAlarm(nNew-nOld); |
| 532 | } |
danielk1977 | d09414c | 2008-06-19 18:17:49 +0000 | [diff] [blame] | 533 | pNew = sqlite3Config.m.xRealloc(pOld, nNew); |
| 534 | if( pNew==0 && mem0.alarmCallback ){ |
| 535 | sqlite3MallocAlarm(nBytes); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 536 | pNew = sqlite3Config.m.xRealloc(pOld, nNew); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 537 | } |
| 538 | if( pNew ){ |
drh | c702c7c | 2008-07-18 18:56:16 +0000 | [diff] [blame] | 539 | nNew = sqlite3MallocSize(pNew); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 540 | sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | sqlite3_mutex_leave(mem0.mutex); |
| 544 | }else{ |
| 545 | pNew = sqlite3Config.m.xRealloc(pOld, nBytes); |
| 546 | } |
| 547 | return pNew; |
| 548 | } |
| 549 | |
| 550 | /* |
| 551 | ** The public interface to sqlite3Realloc. Make sure that the memory |
| 552 | ** subsystem is initialized prior to invoking sqliteRealloc. |
| 553 | */ |
| 554 | void *sqlite3_realloc(void *pOld, int n){ |
| 555 | #ifndef SQLITE_OMIT_AUTOINIT |
| 556 | if( sqlite3_initialize() ) return 0; |
| 557 | #endif |
| 558 | return sqlite3Realloc(pOld, n); |
| 559 | } |
| 560 | |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 561 | |
| 562 | /* |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 563 | ** Allocate and zero memory. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 564 | */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 565 | void *sqlite3MallocZero(int n){ |
| 566 | void *p = sqlite3Malloc(n); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 567 | if( p ){ |
| 568 | memset(p, 0, n); |
| 569 | } |
| 570 | return p; |
| 571 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 572 | |
| 573 | /* |
| 574 | ** Allocate and zero memory. If the allocation fails, make |
| 575 | ** the mallocFailed flag in the connection pointer. |
| 576 | */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 577 | void *sqlite3DbMallocZero(sqlite3 *db, int n){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 578 | void *p = sqlite3DbMallocRaw(db, n); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 579 | if( p ){ |
| 580 | memset(p, 0, n); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 581 | } |
| 582 | return p; |
| 583 | } |
| 584 | |
| 585 | /* |
| 586 | ** Allocate and zero memory. If the allocation fails, make |
| 587 | ** the mallocFailed flag in the connection pointer. |
| 588 | */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 589 | void *sqlite3DbMallocRaw(sqlite3 *db, int n){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame^] | 590 | void *p; |
| 591 | if( db ){ |
| 592 | LookasideSlot *pBuf; |
| 593 | if( db->mallocFailed ){ |
| 594 | return 0; |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 595 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame^] | 596 | if( db->lookaside.bEnabled && n<=db->lookaside.sz |
| 597 | && (pBuf = db->lookaside.pFree)!=0 ){ |
| 598 | db->lookaside.pFree = pBuf->pNext; |
| 599 | db->lookaside.nOut++; |
| 600 | if( db->lookaside.nOut>db->lookaside.mxOut ){ |
| 601 | db->lookaside.mxOut = db->lookaside.nOut; |
| 602 | } |
| 603 | return (void*)pBuf; |
| 604 | } |
| 605 | } |
| 606 | p = sqlite3Malloc(n); |
| 607 | if( !p && db ){ |
| 608 | db->mallocFailed = 1; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 609 | } |
| 610 | return p; |
| 611 | } |
| 612 | |
danielk1977 | 26783a5 | 2007-08-29 14:06:22 +0000 | [diff] [blame] | 613 | /* |
| 614 | ** 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^] | 615 | ** resize fails, set the mallocFailed flag in the connection object. |
danielk1977 | 26783a5 | 2007-08-29 14:06:22 +0000 | [diff] [blame] | 616 | */ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 617 | void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){ |
| 618 | void *pNew = 0; |
| 619 | if( db->mallocFailed==0 ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame^] | 620 | if( p==0 ){ |
| 621 | return sqlite3DbMallocRaw(db, n); |
| 622 | } |
| 623 | if( isLookaside(db, p) ){ |
| 624 | if( n<=db->lookaside.sz ){ |
| 625 | return p; |
| 626 | } |
| 627 | pNew = sqlite3DbMallocRaw(db, n); |
| 628 | if( pNew ){ |
| 629 | memcpy(pNew, p, db->lookaside.sz); |
| 630 | sqlite3DbFree(db, p); |
| 631 | } |
| 632 | }else{ |
| 633 | pNew = sqlite3_realloc(p, n); |
| 634 | if( !pNew ){ |
| 635 | db->mallocFailed = 1; |
| 636 | } |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 637 | } |
| 638 | } |
| 639 | return pNew; |
| 640 | } |
| 641 | |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 642 | /* |
| 643 | ** Attempt to reallocate p. If the reallocation fails, then free p |
| 644 | ** and set the mallocFailed flag in the database connection. |
| 645 | */ |
| 646 | void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 647 | void *pNew; |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 648 | pNew = sqlite3DbRealloc(db, p, n); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 649 | if( !pNew ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame^] | 650 | sqlite3DbFree(db, p); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 651 | } |
| 652 | return pNew; |
| 653 | } |
| 654 | |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 655 | /* |
| 656 | ** Make a copy of a string in memory obtained from sqliteMalloc(). These |
| 657 | ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This |
| 658 | ** is because when memory debugging is turned on, these two functions are |
| 659 | ** called via macros that record the current file and line number in the |
| 660 | ** ThreadData structure. |
| 661 | */ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame^] | 662 | char *sqlite3DbStrDup(sqlite3 *db, const char *z){ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 663 | char *zNew; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame^] | 664 | size_t n; |
| 665 | if( z==0 ){ |
| 666 | return 0; |
| 667 | } |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 668 | n = strlen(z)+1; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame^] | 669 | assert( (n&0x7fffffff)==n ); |
| 670 | zNew = sqlite3DbMallocRaw(db, (int)n); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 671 | if( zNew ){ |
| 672 | memcpy(zNew, z, n); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 673 | } |
| 674 | return zNew; |
| 675 | } |
| 676 | char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame^] | 677 | char *zNew; |
| 678 | if( z==0 ){ |
| 679 | return 0; |
| 680 | } |
| 681 | assert( (n&0x7fffffff)==n ); |
| 682 | zNew = sqlite3DbMallocRaw(db, n+1); |
| 683 | if( zNew ){ |
| 684 | memcpy(zNew, z, n); |
| 685 | zNew[n] = 0; |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 686 | } |
| 687 | return zNew; |
| 688 | } |
| 689 | |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 690 | /* |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 691 | ** Create a string from the zFromat argument and the va_list that follows. |
| 692 | ** Store the string in memory obtained from sqliteMalloc() and make *pz |
| 693 | ** point to that string. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 694 | */ |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 695 | void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 696 | va_list ap; |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 697 | char *z; |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 698 | |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 699 | va_start(ap, zFormat); |
| 700 | z = sqlite3VMPrintf(db, zFormat, ap); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 701 | va_end(ap); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame^] | 702 | sqlite3DbFree(db, *pz); |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 703 | *pz = z; |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | |
| 707 | /* |
| 708 | ** This function must be called before exiting any API function (i.e. |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 709 | ** returning control to the user) that has called sqlite3_malloc or |
| 710 | ** sqlite3_realloc. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 711 | ** |
| 712 | ** The returned value is normally a copy of the second argument to this |
| 713 | ** function. However, if a malloc() failure has occured since the previous |
| 714 | ** invocation SQLITE_NOMEM is returned instead. |
| 715 | ** |
| 716 | ** If the first argument, db, is not NULL and a malloc() error has occured, |
| 717 | ** then the connection error-code (the value returned by sqlite3_errcode()) |
| 718 | ** is set to SQLITE_NOMEM. |
| 719 | */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 720 | int sqlite3ApiExit(sqlite3* db, int rc){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 721 | /* If the db handle is not NULL, then we must hold the connection handle |
| 722 | ** mutex here. Otherwise the read (and possible write) of db->mallocFailed |
| 723 | ** is unsafe, as is the call to sqlite3Error(). |
| 724 | */ |
| 725 | assert( !db || sqlite3_mutex_held(db->mutex) ); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 726 | if( db && db->mallocFailed ){ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 727 | sqlite3Error(db, SQLITE_NOMEM, 0); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 728 | db->mallocFailed = 0; |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 729 | rc = SQLITE_NOMEM; |
| 730 | } |
| 731 | return rc & (db ? db->errMask : 0xff); |
| 732 | } |