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 | /* |
drh | 25f8188 | 2010-07-24 18:25:20 +0000 | [diff] [blame] | 19 | ** There are two general-purpose memory allocators: |
| 20 | ** |
| 21 | ** Simple: |
| 22 | ** |
| 23 | ** sqlite3_malloc |
| 24 | ** sqlite3_free |
| 25 | ** sqlite3_realloc |
| 26 | ** sqlite3Malloc |
| 27 | ** sqlite3MallocSize |
| 28 | ** sqlite3_mprintf |
| 29 | ** |
| 30 | ** Enhanced: |
| 31 | ** |
| 32 | ** sqlite3DbMallocRaw |
| 33 | ** sqlite3DbMallocZero |
| 34 | ** sqlite3DbFree |
| 35 | ** sqlite3DbRealloc |
| 36 | ** sqlite3MPrintf |
| 37 | ** sqlite3DbMalloc |
| 38 | ** |
| 39 | ** All external allocations use the simple memory allocator. |
| 40 | ** The enhanced allocator is used internally only, and is not |
| 41 | ** available to extensions or applications. |
| 42 | ** |
| 43 | ** The enhanced allocator is a wrapper around the simple allocator that |
| 44 | ** adds the following capabilities: |
| 45 | ** |
| 46 | ** (1) Access to lookaside memory associated with a database connection. |
| 47 | ** |
| 48 | ** (2) The ability to link allocations into a hierarchy with automatic |
| 49 | ** deallocation of all elements of the subhierarchy whenever any |
| 50 | ** element within the hierarchy is deallocated. |
| 51 | ** |
| 52 | ** The two allocators are incompatible in the sense that allocations that |
| 53 | ** originate from the simple allocator must be deallocated using the simple |
| 54 | ** deallocator and allocations that originate from the enhanced allocator must |
| 55 | ** be deallocated using the enhanced deallocator. You cannot check-out |
| 56 | ** memory from one allocator then return it to the other. |
| 57 | */ |
| 58 | |
| 59 | /* |
| 60 | ** The automatic hierarchical deallocation feature of the enhanced allocator |
| 61 | ** is implemented by adding an instance of the following structure to the |
| 62 | ** header of each enhanced allocation. |
| 63 | ** |
| 64 | ** In order to preserve alignment, this structure must be a multiple of |
| 65 | ** 8 bytes in size. |
| 66 | */ |
| 67 | typedef struct EMemHdr EMemHdr; |
| 68 | struct EMemHdr { |
| 69 | EMemHdr *pEChild; /* List of children of this node */ |
| 70 | EMemHdr *pESibling; /* Other nodes that are children of the same parent */ |
| 71 | #ifdef SQLITE_MEMDEBUG |
| 72 | u32 iEMemMagic; /* Magic number for sanity checking */ |
| 73 | u32 isAChild; /* True if this allocate is a child of another */ |
| 74 | #endif |
| 75 | }; |
| 76 | |
| 77 | /* |
| 78 | ** Macros for querying and setting debugging fields of the EMemHdr object. |
| 79 | */ |
| 80 | #ifdef SQLITE_MEMDEBUG |
| 81 | # define isValidEMem(E) ((E)->iEMemMagic==0xc0a43fad) |
| 82 | # define setValidEMem(E) (E)->iEMemMagic = 0xc0a43fad |
| 83 | # define clearValidEMem(E) (E)->iEMemMagic = 0x12345678 |
| 84 | # define isChildEMem(E) ((E)->isAChild!=0) |
| 85 | # define setChildEMem(E) (E)->isAChild = 1 |
| 86 | # define clearChildEMem(E) (E)->isAChild = 0 |
| 87 | #else |
| 88 | # define isValidEMem(E) |
| 89 | # define setValidEMem(E) |
| 90 | # define clearValidEMem(E) |
| 91 | # define isChildEMem(E) |
| 92 | # define setChildEMem(E) |
| 93 | # define clearChildEMem(E) |
| 94 | #endif |
| 95 | |
| 96 | /* |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 97 | ** This routine runs when the memory allocator sees that the |
| 98 | ** total memory allocation is about to exceed the soft heap |
| 99 | ** limit. |
| 100 | */ |
| 101 | static void softHeapLimitEnforcer( |
| 102 | void *NotUsed, |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 103 | sqlite3_int64 NotUsed2, |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 104 | int allocSize |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 105 | ){ |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 106 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 107 | sqlite3_release_memory(allocSize); |
| 108 | } |
| 109 | |
| 110 | /* |
danielk1977 | 8468024 | 2008-06-23 11:11:35 +0000 | [diff] [blame] | 111 | ** Set the soft heap-size limit for the library. Passing a zero or |
| 112 | ** negative value indicates no limit. |
drh | 25f8188 | 2010-07-24 18:25:20 +0000 | [diff] [blame] | 113 | ** |
| 114 | ** If the total amount of memory allocated (by all threads) exceeds |
| 115 | ** the soft heap limit, then sqlite3_release_memory() is invoked to |
| 116 | ** try to free up some memory before proceeding. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 117 | */ |
| 118 | void sqlite3_soft_heap_limit(int n){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 119 | sqlite3_uint64 iLimit; |
| 120 | int overage; |
| 121 | if( n<0 ){ |
| 122 | iLimit = 0; |
| 123 | }else{ |
| 124 | iLimit = n; |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 125 | } |
drh | 9ac0650 | 2009-08-17 13:42:29 +0000 | [diff] [blame] | 126 | #ifndef SQLITE_OMIT_AUTOINIT |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 127 | sqlite3_initialize(); |
drh | 9ac0650 | 2009-08-17 13:42:29 +0000 | [diff] [blame] | 128 | #endif |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 129 | if( iLimit>0 ){ |
shane | 4a27a28 | 2008-09-04 04:32:49 +0000 | [diff] [blame] | 130 | sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, iLimit); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 131 | }else{ |
shane | 4a27a28 | 2008-09-04 04:32:49 +0000 | [diff] [blame] | 132 | sqlite3MemoryAlarm(0, 0, 0); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 133 | } |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 134 | overage = (int)(sqlite3_memory_used() - (i64)n); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 135 | if( overage>0 ){ |
| 136 | sqlite3_release_memory(overage); |
| 137 | } |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /* |
danielk1977 | 8468024 | 2008-06-23 11:11:35 +0000 | [diff] [blame] | 141 | ** Attempt to release up to n bytes of non-essential memory currently |
| 142 | ** held by SQLite. An example of non-essential memory is memory used to |
| 143 | ** cache database pages that are not currently in use. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 144 | */ |
| 145 | int sqlite3_release_memory(int n){ |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 146 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
danielk1977 | 67e3da7 | 2008-08-21 12:19:44 +0000 | [diff] [blame] | 147 | int nRet = 0; |
danielk1977 | 67e3da7 | 2008-08-21 12:19:44 +0000 | [diff] [blame] | 148 | nRet += sqlite3PcacheReleaseMemory(n-nRet); |
danielk1977 | dfb316d | 2008-03-26 18:34:43 +0000 | [diff] [blame] | 149 | return nRet; |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 150 | #else |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 151 | UNUSED_PARAMETER(n); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 152 | return SQLITE_OK; |
| 153 | #endif |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 154 | } |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 155 | |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 156 | /* |
| 157 | ** State information local to the memory allocation subsystem. |
| 158 | */ |
danielk1977 | 5c8f858 | 2008-09-02 10:22:00 +0000 | [diff] [blame] | 159 | static SQLITE_WSD struct Mem0Global { |
danielk1977 | 23bf0f4 | 2008-09-02 17:52:51 +0000 | [diff] [blame] | 160 | /* Number of free pages for scratch and page-cache memory */ |
| 161 | u32 nScratchFree; |
| 162 | u32 nPageFree; |
| 163 | |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 164 | sqlite3_mutex *mutex; /* Mutex to serialize access */ |
| 165 | |
| 166 | /* |
| 167 | ** The alarm callback and its arguments. The mem0.mutex lock will |
| 168 | ** be held while the callback is running. Recursive calls into |
| 169 | ** the memory subsystem are allowed, but no new callbacks will be |
drh | e64ca7b | 2009-07-16 18:21:17 +0000 | [diff] [blame] | 170 | ** issued. |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 171 | */ |
| 172 | sqlite3_int64 alarmThreshold; |
| 173 | void (*alarmCallback)(void*, sqlite3_int64,int); |
| 174 | void *alarmArg; |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 175 | |
| 176 | /* |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 177 | ** Pointers to the end of sqlite3GlobalConfig.pScratch and |
| 178 | ** sqlite3GlobalConfig.pPage to a block of memory that records |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 179 | ** which pages are available. |
| 180 | */ |
| 181 | u32 *aScratchFree; |
| 182 | u32 *aPageFree; |
drh | e64ca7b | 2009-07-16 18:21:17 +0000 | [diff] [blame] | 183 | } mem0 = { 0, 0, 0, 0, 0, 0, 0, 0 }; |
danielk1977 | 5c8f858 | 2008-09-02 10:22:00 +0000 | [diff] [blame] | 184 | |
| 185 | #define mem0 GLOBAL(struct Mem0Global, mem0) |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 186 | |
| 187 | /* |
| 188 | ** Initialize the memory allocation subsystem. |
| 189 | */ |
| 190 | int sqlite3MallocInit(void){ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 191 | if( sqlite3GlobalConfig.m.xMalloc==0 ){ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 192 | sqlite3MemSetDefault(); |
| 193 | } |
| 194 | memset(&mem0, 0, sizeof(mem0)); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 195 | if( sqlite3GlobalConfig.bCoreMutex ){ |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 196 | mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 197 | } |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 198 | if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100 |
| 199 | && sqlite3GlobalConfig.nScratch>=0 ){ |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 200 | int i; |
danielk1977 | bc73971 | 2009-03-23 04:33:32 +0000 | [diff] [blame] | 201 | sqlite3GlobalConfig.szScratch = ROUNDDOWN8(sqlite3GlobalConfig.szScratch-4); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 202 | mem0.aScratchFree = (u32*)&((char*)sqlite3GlobalConfig.pScratch) |
| 203 | [sqlite3GlobalConfig.szScratch*sqlite3GlobalConfig.nScratch]; |
| 204 | for(i=0; i<sqlite3GlobalConfig.nScratch; i++){ mem0.aScratchFree[i] = i; } |
| 205 | mem0.nScratchFree = sqlite3GlobalConfig.nScratch; |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 206 | }else{ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 207 | sqlite3GlobalConfig.pScratch = 0; |
| 208 | sqlite3GlobalConfig.szScratch = 0; |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 209 | } |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 210 | if( sqlite3GlobalConfig.pPage && sqlite3GlobalConfig.szPage>=512 |
| 211 | && sqlite3GlobalConfig.nPage>=1 ){ |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 212 | int i; |
drh | 0a60a38 | 2008-07-31 17:16:05 +0000 | [diff] [blame] | 213 | int overhead; |
danielk1977 | bc73971 | 2009-03-23 04:33:32 +0000 | [diff] [blame] | 214 | int sz = ROUNDDOWN8(sqlite3GlobalConfig.szPage); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 215 | int n = sqlite3GlobalConfig.nPage; |
drh | 0a60a38 | 2008-07-31 17:16:05 +0000 | [diff] [blame] | 216 | overhead = (4*n + sz - 1)/sz; |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 217 | sqlite3GlobalConfig.nPage -= overhead; |
| 218 | mem0.aPageFree = (u32*)&((char*)sqlite3GlobalConfig.pPage) |
| 219 | [sqlite3GlobalConfig.szPage*sqlite3GlobalConfig.nPage]; |
| 220 | for(i=0; i<sqlite3GlobalConfig.nPage; i++){ mem0.aPageFree[i] = i; } |
| 221 | mem0.nPageFree = sqlite3GlobalConfig.nPage; |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 222 | }else{ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 223 | sqlite3GlobalConfig.pPage = 0; |
| 224 | sqlite3GlobalConfig.szPage = 0; |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 225 | } |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 226 | return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /* |
| 230 | ** Deinitialize the memory allocation subsystem. |
| 231 | */ |
| 232 | void sqlite3MallocEnd(void){ |
danielk1977 | 0a54907 | 2009-02-17 16:29:10 +0000 | [diff] [blame] | 233 | if( sqlite3GlobalConfig.m.xShutdown ){ |
| 234 | sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData); |
| 235 | } |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 236 | memset(&mem0, 0, sizeof(mem0)); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /* |
| 240 | ** Return the amount of memory currently checked out. |
| 241 | */ |
| 242 | sqlite3_int64 sqlite3_memory_used(void){ |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 243 | int n, mx; |
drh | c376a19 | 2008-07-14 12:30:54 +0000 | [diff] [blame] | 244 | sqlite3_int64 res; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 245 | sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0); |
drh | c376a19 | 2008-07-14 12:30:54 +0000 | [diff] [blame] | 246 | res = (sqlite3_int64)n; /* Work around bug in Borland C. Ticket #3216 */ |
| 247 | return res; |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | /* |
| 251 | ** Return the maximum amount of memory that has ever been |
| 252 | ** checked out since either the beginning of this process |
| 253 | ** or since the most recent reset. |
| 254 | */ |
| 255 | sqlite3_int64 sqlite3_memory_highwater(int resetFlag){ |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 256 | int n, mx; |
drh | c376a19 | 2008-07-14 12:30:54 +0000 | [diff] [blame] | 257 | sqlite3_int64 res; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 258 | sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag); |
drh | 7986a71 | 2008-07-14 12:38:20 +0000 | [diff] [blame] | 259 | res = (sqlite3_int64)mx; /* Work around bug in Borland C. Ticket #3216 */ |
drh | c376a19 | 2008-07-14 12:30:54 +0000 | [diff] [blame] | 260 | return res; |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /* |
| 264 | ** Change the alarm callback |
| 265 | */ |
shane | 4a27a28 | 2008-09-04 04:32:49 +0000 | [diff] [blame] | 266 | int sqlite3MemoryAlarm( |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 267 | void(*xCallback)(void *pArg, sqlite3_int64 used,int N), |
| 268 | void *pArg, |
| 269 | sqlite3_int64 iThreshold |
| 270 | ){ |
| 271 | sqlite3_mutex_enter(mem0.mutex); |
| 272 | mem0.alarmCallback = xCallback; |
| 273 | mem0.alarmArg = pArg; |
| 274 | mem0.alarmThreshold = iThreshold; |
| 275 | sqlite3_mutex_leave(mem0.mutex); |
| 276 | return SQLITE_OK; |
| 277 | } |
| 278 | |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 279 | #ifndef SQLITE_OMIT_DEPRECATED |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 280 | /* |
shane | 4a27a28 | 2008-09-04 04:32:49 +0000 | [diff] [blame] | 281 | ** Deprecated external interface. Internal/core SQLite code |
| 282 | ** should call sqlite3MemoryAlarm. |
| 283 | */ |
| 284 | int sqlite3_memory_alarm( |
| 285 | void(*xCallback)(void *pArg, sqlite3_int64 used,int N), |
| 286 | void *pArg, |
| 287 | sqlite3_int64 iThreshold |
| 288 | ){ |
| 289 | return sqlite3MemoryAlarm(xCallback, pArg, iThreshold); |
| 290 | } |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 291 | #endif |
shane | 4a27a28 | 2008-09-04 04:32:49 +0000 | [diff] [blame] | 292 | |
| 293 | /* |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 294 | ** Trigger the alarm |
| 295 | */ |
| 296 | static void sqlite3MallocAlarm(int nByte){ |
| 297 | void (*xCallback)(void*,sqlite3_int64,int); |
| 298 | sqlite3_int64 nowUsed; |
| 299 | void *pArg; |
drh | e64ca7b | 2009-07-16 18:21:17 +0000 | [diff] [blame] | 300 | if( mem0.alarmCallback==0 ) return; |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 301 | xCallback = mem0.alarmCallback; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 302 | nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 303 | pArg = mem0.alarmArg; |
drh | e64ca7b | 2009-07-16 18:21:17 +0000 | [diff] [blame] | 304 | mem0.alarmCallback = 0; |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 305 | sqlite3_mutex_leave(mem0.mutex); |
| 306 | xCallback(pArg, nowUsed, nByte); |
| 307 | sqlite3_mutex_enter(mem0.mutex); |
drh | e64ca7b | 2009-07-16 18:21:17 +0000 | [diff] [blame] | 308 | mem0.alarmCallback = xCallback; |
| 309 | mem0.alarmArg = pArg; |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 310 | } |
| 311 | |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 312 | /* |
| 313 | ** Do a memory allocation with statistics and alarms. Assume the |
| 314 | ** lock is already held. |
| 315 | */ |
| 316 | static int mallocWithAlarm(int n, void **pp){ |
| 317 | int nFull; |
| 318 | void *p; |
| 319 | assert( sqlite3_mutex_held(mem0.mutex) ); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 320 | nFull = sqlite3GlobalConfig.m.xRoundup(n); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 321 | sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n); |
| 322 | if( mem0.alarmCallback!=0 ){ |
| 323 | int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); |
| 324 | if( nUsed+nFull >= mem0.alarmThreshold ){ |
| 325 | sqlite3MallocAlarm(nFull); |
| 326 | } |
| 327 | } |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 328 | p = sqlite3GlobalConfig.m.xMalloc(nFull); |
danielk1977 | d09414c | 2008-06-19 18:17:49 +0000 | [diff] [blame] | 329 | if( p==0 && mem0.alarmCallback ){ |
| 330 | sqlite3MallocAlarm(nFull); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 331 | p = sqlite3GlobalConfig.m.xMalloc(nFull); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 332 | } |
drh | c702c7c | 2008-07-18 18:56:16 +0000 | [diff] [blame] | 333 | if( p ){ |
| 334 | nFull = sqlite3MallocSize(p); |
| 335 | sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull); |
| 336 | } |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 337 | *pp = p; |
| 338 | return nFull; |
| 339 | } |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 340 | |
| 341 | /* |
| 342 | ** Allocate memory. This routine is like sqlite3_malloc() except that it |
| 343 | ** assumes the memory subsystem has already been initialized. |
| 344 | */ |
| 345 | void *sqlite3Malloc(int n){ |
| 346 | void *p; |
drh | e08ed7e | 2009-06-26 18:35:16 +0000 | [diff] [blame] | 347 | if( n<=0 || n>=0x7fffff00 ){ |
| 348 | /* A memory allocation of a number of bytes which is near the maximum |
| 349 | ** signed integer value might cause an integer overflow inside of the |
| 350 | ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving |
| 351 | ** 255 bytes of overhead. SQLite itself will never use anything near |
| 352 | ** this amount. The only way to reach the limit is with sqlite3_malloc() */ |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 353 | p = 0; |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 354 | }else if( sqlite3GlobalConfig.bMemstat ){ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 355 | sqlite3_mutex_enter(mem0.mutex); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 356 | mallocWithAlarm(n, &p); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 357 | sqlite3_mutex_leave(mem0.mutex); |
| 358 | }else{ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 359 | p = sqlite3GlobalConfig.m.xMalloc(n); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 360 | } |
| 361 | return p; |
| 362 | } |
| 363 | |
| 364 | /* |
| 365 | ** This version of the memory allocation is for use by the application. |
| 366 | ** First make sure the memory subsystem is initialized, then do the |
| 367 | ** allocation. |
| 368 | */ |
| 369 | void *sqlite3_malloc(int n){ |
| 370 | #ifndef SQLITE_OMIT_AUTOINIT |
| 371 | if( sqlite3_initialize() ) return 0; |
| 372 | #endif |
| 373 | return sqlite3Malloc(n); |
| 374 | } |
| 375 | |
| 376 | /* |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 377 | ** Each thread may only have a single outstanding allocation from |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 378 | ** xScratchMalloc(). We verify this constraint in the single-threaded |
| 379 | ** case by setting scratchAllocOut to 1 when an allocation |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 380 | ** is outstanding clearing it when the allocation is freed. |
| 381 | */ |
| 382 | #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 383 | static int scratchAllocOut = 0; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 384 | #endif |
| 385 | |
| 386 | |
| 387 | /* |
| 388 | ** Allocate memory that is to be used and released right away. |
| 389 | ** This routine is similar to alloca() in that it is not intended |
| 390 | ** for situations where the memory might be held long-term. This |
| 391 | ** routine is intended to get memory to old large transient data |
| 392 | ** structures that would not normally fit on the stack of an |
| 393 | ** embedded processor. |
| 394 | */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 395 | void *sqlite3ScratchMalloc(int n){ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 396 | void *p; |
| 397 | assert( n>0 ); |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 398 | |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 399 | #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
drh | 37f9918 | 2010-06-26 20:25:30 +0000 | [diff] [blame] | 400 | /* Verify that no more than two scratch allocation per thread |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 401 | ** is outstanding at one time. (This is only checked in the |
| 402 | ** single-threaded case since checking in the multi-threaded case |
| 403 | ** would be much more complicated.) */ |
drh | 37f9918 | 2010-06-26 20:25:30 +0000 | [diff] [blame] | 404 | assert( scratchAllocOut<=1 ); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 405 | #endif |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 406 | |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 407 | if( sqlite3GlobalConfig.szScratch<n ){ |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 408 | goto scratch_overflow; |
| 409 | }else{ |
| 410 | sqlite3_mutex_enter(mem0.mutex); |
| 411 | if( mem0.nScratchFree==0 ){ |
| 412 | sqlite3_mutex_leave(mem0.mutex); |
| 413 | goto scratch_overflow; |
| 414 | }else{ |
| 415 | int i; |
| 416 | i = mem0.aScratchFree[--mem0.nScratchFree]; |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 417 | i *= sqlite3GlobalConfig.szScratch; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 418 | sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1); |
drh | e50135e | 2008-08-05 17:53:22 +0000 | [diff] [blame] | 419 | sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n); |
danielk1977 | 8183e33 | 2008-08-29 17:56:12 +0000 | [diff] [blame] | 420 | sqlite3_mutex_leave(mem0.mutex); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 421 | p = (void*)&((char*)sqlite3GlobalConfig.pScratch)[i]; |
shane | 1530159 | 2008-12-16 17:20:38 +0000 | [diff] [blame] | 422 | assert( (((u8*)p - (u8*)0) & 7)==0 ); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 423 | } |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 424 | } |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 425 | #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
| 426 | scratchAllocOut = p!=0; |
| 427 | #endif |
| 428 | |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 429 | return p; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 430 | |
| 431 | scratch_overflow: |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 432 | if( sqlite3GlobalConfig.bMemstat ){ |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 433 | sqlite3_mutex_enter(mem0.mutex); |
drh | e50135e | 2008-08-05 17:53:22 +0000 | [diff] [blame] | 434 | sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 435 | n = mallocWithAlarm(n, &p); |
| 436 | if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n); |
| 437 | sqlite3_mutex_leave(mem0.mutex); |
| 438 | }else{ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 439 | p = sqlite3GlobalConfig.m.xMalloc(n); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 440 | } |
drh | 107b56e | 2010-03-12 16:32:53 +0000 | [diff] [blame] | 441 | sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 442 | #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
| 443 | scratchAllocOut = p!=0; |
| 444 | #endif |
| 445 | return p; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 446 | } |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 447 | void sqlite3ScratchFree(void *p){ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 448 | if( p ){ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 449 | if( sqlite3GlobalConfig.pScratch==0 |
| 450 | || p<sqlite3GlobalConfig.pScratch |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 451 | || p>=(void*)mem0.aScratchFree ){ |
drh | 107b56e | 2010-03-12 16:32:53 +0000 | [diff] [blame] | 452 | assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) ); |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 453 | assert( !sqlite3MemdebugHasType(p, ~MEMTYPE_SCRATCH) ); |
drh | 107b56e | 2010-03-12 16:32:53 +0000 | [diff] [blame] | 454 | sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 455 | if( sqlite3GlobalConfig.bMemstat ){ |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 456 | int iSize = sqlite3MallocSize(p); |
| 457 | sqlite3_mutex_enter(mem0.mutex); |
| 458 | sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize); |
| 459 | sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 460 | sqlite3GlobalConfig.m.xFree(p); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 461 | sqlite3_mutex_leave(mem0.mutex); |
| 462 | }else{ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 463 | sqlite3GlobalConfig.m.xFree(p); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 464 | } |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 465 | }else{ |
| 466 | int i; |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 467 | i = (int)((u8*)p - (u8*)sqlite3GlobalConfig.pScratch); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 468 | i /= sqlite3GlobalConfig.szScratch; |
| 469 | assert( i>=0 && i<sqlite3GlobalConfig.nScratch ); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 470 | sqlite3_mutex_enter(mem0.mutex); |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 471 | assert( mem0.nScratchFree<(u32)sqlite3GlobalConfig.nScratch ); |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 472 | mem0.aScratchFree[mem0.nScratchFree++] = i; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 473 | sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1); |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 474 | sqlite3_mutex_leave(mem0.mutex); |
drh | 37f9918 | 2010-06-26 20:25:30 +0000 | [diff] [blame] | 475 | |
| 476 | #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
| 477 | /* Verify that no more than two scratch allocation per thread |
| 478 | ** is outstanding at one time. (This is only checked in the |
| 479 | ** single-threaded case since checking in the multi-threaded case |
| 480 | ** would be much more complicated.) */ |
| 481 | assert( scratchAllocOut>=1 && scratchAllocOut<=2 ); |
| 482 | scratchAllocOut = 0; |
| 483 | #endif |
| 484 | |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 485 | } |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 486 | } |
| 487 | } |
| 488 | |
| 489 | /* |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 490 | ** TRUE if p is a lookaside memory allocation from db |
| 491 | */ |
drh | 4150ebf | 2008-10-11 15:38:29 +0000 | [diff] [blame] | 492 | #ifndef SQLITE_OMIT_LOOKASIDE |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 493 | static int isLookaside(sqlite3 *db, void *p){ |
| 494 | return db && p && p>=db->lookaside.pStart && p<db->lookaside.pEnd; |
| 495 | } |
drh | 4150ebf | 2008-10-11 15:38:29 +0000 | [diff] [blame] | 496 | #else |
| 497 | #define isLookaside(A,B) 0 |
| 498 | #endif |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 499 | |
| 500 | /* |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 501 | ** Return the size of a memory allocation previously obtained from |
| 502 | ** sqlite3Malloc() or sqlite3_malloc(). |
drh | 25f8188 | 2010-07-24 18:25:20 +0000 | [diff] [blame] | 503 | ** |
| 504 | ** The size returned is the usable size and does not include any |
| 505 | ** bookkeeping overhead or sentinals at the end of the allocation. |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 506 | */ |
| 507 | int sqlite3MallocSize(void *p){ |
drh | 107b56e | 2010-03-12 16:32:53 +0000 | [diff] [blame] | 508 | assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 509 | assert( !sqlite3MemdebugHasType(p, MEMTYPE_RECURSIVE) ); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 510 | return sqlite3GlobalConfig.m.xSize(p); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 511 | } |
drh | 25f8188 | 2010-07-24 18:25:20 +0000 | [diff] [blame] | 512 | int sqlite3DbMallocSize(sqlite3 *db, void *pObj){ |
| 513 | EMemHdr *p = (EMemHdr*)pObj; |
drh | 7047e25 | 2009-03-23 17:49:14 +0000 | [diff] [blame] | 514 | assert( db==0 || sqlite3_mutex_held(db->mutex) ); |
drh | 25f8188 | 2010-07-24 18:25:20 +0000 | [diff] [blame] | 515 | if( p ){ |
| 516 | p--; |
| 517 | assert( isValidEMem(p) ); |
| 518 | } |
drh | f18a61d | 2009-07-17 11:44:07 +0000 | [diff] [blame] | 519 | if( isLookaside(db, p) ){ |
drh | 25f8188 | 2010-07-24 18:25:20 +0000 | [diff] [blame] | 520 | return db->lookaside.sz - sizeof(EMemHdr); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 521 | }else{ |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 522 | assert( sqlite3MemdebugHasType(p, MEMTYPE_RECURSIVE) ); |
drh | 107b56e | 2010-03-12 16:32:53 +0000 | [diff] [blame] | 523 | assert( sqlite3MemdebugHasType(p, |
| 524 | db ? (MEMTYPE_DB|MEMTYPE_HEAP) : MEMTYPE_HEAP) ); |
drh | 25f8188 | 2010-07-24 18:25:20 +0000 | [diff] [blame] | 525 | return sqlite3GlobalConfig.m.xSize(p) - sizeof(EMemHdr); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 526 | } |
| 527 | } |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 528 | |
| 529 | /* |
| 530 | ** Free memory previously obtained from sqlite3Malloc(). |
| 531 | */ |
| 532 | void sqlite3_free(void *p){ |
| 533 | if( p==0 ) return; |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 534 | assert( !sqlite3MemdebugHasType(p, MEMTYPE_RECURSIVE) ); |
drh | 107b56e | 2010-03-12 16:32:53 +0000 | [diff] [blame] | 535 | assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 536 | if( sqlite3GlobalConfig.bMemstat ){ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 537 | sqlite3_mutex_enter(mem0.mutex); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 538 | sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p)); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 539 | sqlite3GlobalConfig.m.xFree(p); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 540 | sqlite3_mutex_leave(mem0.mutex); |
| 541 | }else{ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 542 | sqlite3GlobalConfig.m.xFree(p); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 543 | } |
| 544 | } |
| 545 | |
| 546 | /* |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 547 | ** Free memory that might be associated with a particular database |
drh | 25f8188 | 2010-07-24 18:25:20 +0000 | [diff] [blame] | 548 | ** connection. All child allocations are also freed. |
drh | e79ffb6 | 2010-07-24 19:08:13 +0000 | [diff] [blame^] | 549 | ** |
| 550 | ** pObj must be a top-level allocation in the heirarchy. It is not |
| 551 | ** allowed to delete a child allocation since that would leave a |
| 552 | ** dangling child pointer in the parent. |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 553 | */ |
drh | 25f8188 | 2010-07-24 18:25:20 +0000 | [diff] [blame] | 554 | void sqlite3DbFree(sqlite3 *db, void *pObj){ |
| 555 | EMemHdr *p = (EMemHdr*)pObj; |
drh | 7047e25 | 2009-03-23 17:49:14 +0000 | [diff] [blame] | 556 | assert( db==0 || sqlite3_mutex_held(db->mutex) ); |
drh | 25f8188 | 2010-07-24 18:25:20 +0000 | [diff] [blame] | 557 | if( p ) p--; |
drh | e79ffb6 | 2010-07-24 19:08:13 +0000 | [diff] [blame^] | 558 | assert( p==0 || !isChildEMem(p) ); /* pObj is not child allocation */ |
drh | 25f8188 | 2010-07-24 18:25:20 +0000 | [diff] [blame] | 559 | while( p ){ |
| 560 | EMemHdr *pNext = p->pESibling; |
drh | e79ffb6 | 2010-07-24 19:08:13 +0000 | [diff] [blame^] | 561 | assert( isValidEMem(p) ); /* pObj and all siblings are valid */ |
| 562 | if( p->pEChild ){ |
| 563 | clearChildEMem(p->pEChild); |
| 564 | sqlite3DbFree(db, (void*)&p->pEChild[1]); |
| 565 | } |
drh | 25f8188 | 2010-07-24 18:25:20 +0000 | [diff] [blame] | 566 | if( isLookaside(db, p) ){ |
| 567 | LookasideSlot *pBuf = (LookasideSlot*)p; |
| 568 | clearValidEMem(p); |
| 569 | pBuf->pNext = db->lookaside.pFree; |
| 570 | db->lookaside.pFree = pBuf; |
| 571 | db->lookaside.nOut--; |
| 572 | }else{ |
| 573 | assert( sqlite3MemdebugHasType(p, MEMTYPE_RECURSIVE) ); |
| 574 | assert( sqlite3MemdebugHasType(p, |
| 575 | db ? (MEMTYPE_DB|MEMTYPE_HEAP) : MEMTYPE_HEAP) ); |
| 576 | sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 577 | clearValidEMem(p); |
| 578 | sqlite3_free(p); |
| 579 | } |
| 580 | p = pNext; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 581 | } |
| 582 | } |
| 583 | |
| 584 | /* |
drh | 25f8188 | 2010-07-24 18:25:20 +0000 | [diff] [blame] | 585 | ** Change the size of an existing memory allocation. |
| 586 | ** |
| 587 | ** This is the same as sqlite3_realloc() except that it assumes that |
| 588 | ** the memory subsystem has already been initialized. |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 589 | */ |
| 590 | void *sqlite3Realloc(void *pOld, int nBytes){ |
| 591 | int nOld, nNew; |
| 592 | void *pNew; |
| 593 | if( pOld==0 ){ |
| 594 | return sqlite3Malloc(nBytes); |
| 595 | } |
drh | b6063cf | 2009-06-27 00:48:33 +0000 | [diff] [blame] | 596 | if( nBytes<=0 ){ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 597 | sqlite3_free(pOld); |
| 598 | return 0; |
| 599 | } |
drh | b6063cf | 2009-06-27 00:48:33 +0000 | [diff] [blame] | 600 | if( nBytes>=0x7fffff00 ){ |
| 601 | /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */ |
| 602 | return 0; |
| 603 | } |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 604 | nOld = sqlite3MallocSize(pOld); |
drh | 7c6791c | 2009-08-18 14:48:53 +0000 | [diff] [blame] | 605 | nNew = sqlite3GlobalConfig.m.xRoundup(nBytes); |
| 606 | if( nOld==nNew ){ |
| 607 | pNew = pOld; |
| 608 | }else if( sqlite3GlobalConfig.bMemstat ){ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 609 | sqlite3_mutex_enter(mem0.mutex); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 610 | sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes); |
drh | 7c6791c | 2009-08-18 14:48:53 +0000 | [diff] [blame] | 611 | if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >= |
| 612 | mem0.alarmThreshold ){ |
| 613 | sqlite3MallocAlarm(nNew-nOld); |
| 614 | } |
drh | 107b56e | 2010-03-12 16:32:53 +0000 | [diff] [blame] | 615 | assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) ); |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 616 | assert( !sqlite3MemdebugHasType(pOld, ~MEMTYPE_HEAP) ); |
drh | 7c6791c | 2009-08-18 14:48:53 +0000 | [diff] [blame] | 617 | pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); |
| 618 | if( pNew==0 && mem0.alarmCallback ){ |
| 619 | sqlite3MallocAlarm(nBytes); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 620 | pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); |
drh | 7c6791c | 2009-08-18 14:48:53 +0000 | [diff] [blame] | 621 | } |
| 622 | if( pNew ){ |
| 623 | nNew = sqlite3MallocSize(pNew); |
| 624 | sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 625 | } |
| 626 | sqlite3_mutex_leave(mem0.mutex); |
| 627 | }else{ |
drh | 7c6791c | 2009-08-18 14:48:53 +0000 | [diff] [blame] | 628 | pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 629 | } |
| 630 | return pNew; |
| 631 | } |
| 632 | |
| 633 | /* |
| 634 | ** The public interface to sqlite3Realloc. Make sure that the memory |
| 635 | ** subsystem is initialized prior to invoking sqliteRealloc. |
| 636 | */ |
| 637 | void *sqlite3_realloc(void *pOld, int n){ |
| 638 | #ifndef SQLITE_OMIT_AUTOINIT |
| 639 | if( sqlite3_initialize() ) return 0; |
| 640 | #endif |
| 641 | return sqlite3Realloc(pOld, n); |
| 642 | } |
| 643 | |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 644 | |
| 645 | /* |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 646 | ** Allocate and zero memory. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 647 | */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 648 | void *sqlite3MallocZero(int n){ |
| 649 | void *p = sqlite3Malloc(n); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 650 | if( p ){ |
| 651 | memset(p, 0, n); |
| 652 | } |
| 653 | return p; |
| 654 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 655 | |
| 656 | /* |
| 657 | ** Allocate and zero memory. If the allocation fails, make |
| 658 | ** the mallocFailed flag in the connection pointer. |
| 659 | */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 660 | void *sqlite3DbMallocZero(sqlite3 *db, int n){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 661 | void *p = sqlite3DbMallocRaw(db, n); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 662 | if( p ){ |
| 663 | memset(p, 0, n); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 664 | } |
| 665 | return p; |
| 666 | } |
| 667 | |
| 668 | /* |
| 669 | ** Allocate and zero memory. If the allocation fails, make |
| 670 | ** the mallocFailed flag in the connection pointer. |
drh | ddecae7 | 2008-10-11 17:35:16 +0000 | [diff] [blame] | 671 | ** |
| 672 | ** If db!=0 and db->mallocFailed is true (indicating a prior malloc |
| 673 | ** failure on the same database connection) then always return 0. |
| 674 | ** Hence for a particular database connection, once malloc starts |
| 675 | ** failing, it fails consistently until mallocFailed is reset. |
| 676 | ** This is an important assumption. There are many places in the |
| 677 | ** code that do things like this: |
| 678 | ** |
| 679 | ** int *a = (int*)sqlite3DbMallocRaw(db, 100); |
| 680 | ** int *b = (int*)sqlite3DbMallocRaw(db, 200); |
| 681 | ** if( b ) a[10] = 9; |
| 682 | ** |
| 683 | ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed |
| 684 | ** that all prior mallocs (ex: "a") worked too. |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 685 | */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 686 | void *sqlite3DbMallocRaw(sqlite3 *db, int n){ |
drh | 25f8188 | 2010-07-24 18:25:20 +0000 | [diff] [blame] | 687 | EMemHdr *p; |
drh | d9da78a | 2009-03-24 15:08:09 +0000 | [diff] [blame] | 688 | assert( db==0 || sqlite3_mutex_held(db->mutex) ); |
drh | 25f8188 | 2010-07-24 18:25:20 +0000 | [diff] [blame] | 689 | n += sizeof(EMemHdr); |
drh | 4150ebf | 2008-10-11 15:38:29 +0000 | [diff] [blame] | 690 | #ifndef SQLITE_OMIT_LOOKASIDE |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 691 | if( db ){ |
| 692 | LookasideSlot *pBuf; |
| 693 | if( db->mallocFailed ){ |
| 694 | return 0; |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 695 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 696 | if( db->lookaside.bEnabled && n<=db->lookaside.sz |
| 697 | && (pBuf = db->lookaside.pFree)!=0 ){ |
| 698 | db->lookaside.pFree = pBuf->pNext; |
| 699 | db->lookaside.nOut++; |
| 700 | if( db->lookaside.nOut>db->lookaside.mxOut ){ |
| 701 | db->lookaside.mxOut = db->lookaside.nOut; |
| 702 | } |
drh | 25f8188 | 2010-07-24 18:25:20 +0000 | [diff] [blame] | 703 | p = (EMemHdr*)pBuf; |
| 704 | goto finish_emalloc_raw; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 705 | } |
| 706 | } |
drh | ddecae7 | 2008-10-11 17:35:16 +0000 | [diff] [blame] | 707 | #else |
| 708 | if( db && db->mallocFailed ){ |
| 709 | return 0; |
| 710 | } |
drh | 4150ebf | 2008-10-11 15:38:29 +0000 | [diff] [blame] | 711 | #endif |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 712 | p = sqlite3Malloc(n); |
drh | 25f8188 | 2010-07-24 18:25:20 +0000 | [diff] [blame] | 713 | if( !p ){ |
| 714 | if( db ) db->mallocFailed = 1; |
| 715 | return 0; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 716 | } |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 717 | sqlite3MemdebugSetType(p, MEMTYPE_RECURSIVE | |
| 718 | ((db && db->lookaside.bEnabled) ? MEMTYPE_DB : MEMTYPE_HEAP)); |
drh | 25f8188 | 2010-07-24 18:25:20 +0000 | [diff] [blame] | 719 | |
| 720 | finish_emalloc_raw: |
| 721 | memset(p, 0, sizeof(EMemHdr)); |
| 722 | setValidEMem(p); |
| 723 | return (void*)&p[1]; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 724 | } |
| 725 | |
danielk1977 | 26783a5 | 2007-08-29 14:06:22 +0000 | [diff] [blame] | 726 | /* |
| 727 | ** 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] | 728 | ** resize fails, set the mallocFailed flag in the connection object. |
drh | 25f8188 | 2010-07-24 18:25:20 +0000 | [diff] [blame] | 729 | ** |
| 730 | ** The pOld memory block must not be linked into an allocation hierarchy |
| 731 | ** as a child. It is OK for the allocation to be the root of a hierarchy |
| 732 | ** of allocations; the only restriction is that there must be no other |
| 733 | ** allocations above the pOld allocation in the hierarchy. To resize |
| 734 | ** an allocation that is a child within a hierarchy, first |
| 735 | ** unlink the allocation, resize it, then relink it. |
danielk1977 | 26783a5 | 2007-08-29 14:06:22 +0000 | [diff] [blame] | 736 | */ |
drh | 25f8188 | 2010-07-24 18:25:20 +0000 | [diff] [blame] | 737 | void *sqlite3DbRealloc(sqlite3 *db, void *pOld, int n){ |
| 738 | EMemHdr *p = (EMemHdr*)pOld; |
| 739 | EMemHdr *pNew = 0; |
drh | d9da78a | 2009-03-24 15:08:09 +0000 | [diff] [blame] | 740 | assert( db!=0 ); |
drh | 7047e25 | 2009-03-23 17:49:14 +0000 | [diff] [blame] | 741 | assert( sqlite3_mutex_held(db->mutex) ); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 742 | if( db->mallocFailed==0 ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 743 | if( p==0 ){ |
| 744 | return sqlite3DbMallocRaw(db, n); |
| 745 | } |
drh | 25f8188 | 2010-07-24 18:25:20 +0000 | [diff] [blame] | 746 | p--; |
| 747 | assert( isValidEMem(p) ); /* pOld obtained from extended allocator */ |
| 748 | assert( !isChildEMem(p) ); /* pOld must not be a child allocation */ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 749 | if( isLookaside(db, p) ){ |
drh | 25f8188 | 2010-07-24 18:25:20 +0000 | [diff] [blame] | 750 | if( n+sizeof(EMemHdr)<=db->lookaside.sz ){ |
| 751 | return pOld; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 752 | } |
| 753 | pNew = sqlite3DbMallocRaw(db, n); |
| 754 | if( pNew ){ |
drh | 25f8188 | 2010-07-24 18:25:20 +0000 | [diff] [blame] | 755 | memcpy(pNew-1, p, db->lookaside.sz); |
| 756 | setValidEMem(pNew-1); |
| 757 | sqlite3DbFree(db, pOld); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 758 | } |
| 759 | }else{ |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 760 | assert( sqlite3MemdebugHasType(p, MEMTYPE_RECURSIVE) ); |
drh | 107b56e | 2010-03-12 16:32:53 +0000 | [diff] [blame] | 761 | assert( sqlite3MemdebugHasType(p, MEMTYPE_DB|MEMTYPE_HEAP) ); |
| 762 | sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
drh | 25f8188 | 2010-07-24 18:25:20 +0000 | [diff] [blame] | 763 | pNew = sqlite3_realloc(p, n+sizeof(EMemHdr)); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 764 | if( !pNew ){ |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 765 | sqlite3MemdebugSetType(p, MEMTYPE_RECURSIVE|MEMTYPE_HEAP); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 766 | db->mallocFailed = 1; |
drh | 25f8188 | 2010-07-24 18:25:20 +0000 | [diff] [blame] | 767 | }else{ |
| 768 | sqlite3MemdebugSetType(pNew, MEMTYPE_RECURSIVE | |
| 769 | (db->lookaside.bEnabled ? MEMTYPE_DB : MEMTYPE_HEAP)); |
| 770 | setValidEMem(pNew); |
| 771 | pNew++; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 772 | } |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 773 | } |
| 774 | } |
drh | 25f8188 | 2010-07-24 18:25:20 +0000 | [diff] [blame] | 775 | return (void*)pNew; |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 776 | } |
| 777 | |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 778 | /* |
| 779 | ** Attempt to reallocate p. If the reallocation fails, then free p |
| 780 | ** and set the mallocFailed flag in the database connection. |
| 781 | */ |
| 782 | void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 783 | void *pNew; |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 784 | pNew = sqlite3DbRealloc(db, p, n); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 785 | if( !pNew ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 786 | sqlite3DbFree(db, p); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 787 | } |
| 788 | return pNew; |
| 789 | } |
| 790 | |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 791 | /* |
| 792 | ** Make a copy of a string in memory obtained from sqliteMalloc(). These |
| 793 | ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This |
| 794 | ** is because when memory debugging is turned on, these two functions are |
| 795 | ** called via macros that record the current file and line number in the |
| 796 | ** ThreadData structure. |
| 797 | */ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 798 | char *sqlite3DbStrDup(sqlite3 *db, const char *z){ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 799 | char *zNew; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 800 | size_t n; |
| 801 | if( z==0 ){ |
| 802 | return 0; |
| 803 | } |
drh | dee0e40 | 2009-05-03 20:23:53 +0000 | [diff] [blame] | 804 | n = sqlite3Strlen30(z) + 1; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 805 | assert( (n&0x7fffffff)==n ); |
| 806 | zNew = sqlite3DbMallocRaw(db, (int)n); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 807 | if( zNew ){ |
| 808 | memcpy(zNew, z, n); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 809 | } |
| 810 | return zNew; |
| 811 | } |
| 812 | char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 813 | char *zNew; |
| 814 | if( z==0 ){ |
| 815 | return 0; |
| 816 | } |
| 817 | assert( (n&0x7fffffff)==n ); |
| 818 | zNew = sqlite3DbMallocRaw(db, n+1); |
| 819 | if( zNew ){ |
| 820 | memcpy(zNew, z, n); |
| 821 | zNew[n] = 0; |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 822 | } |
| 823 | return zNew; |
| 824 | } |
| 825 | |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 826 | /* |
drh | 25f8188 | 2010-07-24 18:25:20 +0000 | [diff] [blame] | 827 | ** Link extended allocation nodes such that deallocating the parent |
| 828 | ** causes the child to be automatically deallocated. |
| 829 | */ |
| 830 | void sqlite3MemLink(void *pParentObj, void *pChildObj){ |
| 831 | EMemHdr *pParent = (EMemHdr*)pParentObj; |
| 832 | EMemHdr *pChild = (EMemHdr*)pChildObj; |
| 833 | if( pParent && pChild ){ |
| 834 | pParent--; |
| 835 | assert( isValidEMem(pParent) ); /* pParentObj is an extended allocation */ |
| 836 | pChild--; |
| 837 | assert( isValidEMem(pChild) ); /* pChildObj is an extended allocation */ |
| 838 | assert( !isChildEMem(pChild) ); /* pChildObj not a child of another obj */ |
| 839 | pChild->pESibling = pParent->pEChild; |
| 840 | pParent->pEChild = pChild; |
| 841 | setChildEMem(pChild); |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | /* |
| 846 | ** pChildObj is a child object of pParentObj due to a prior call |
| 847 | ** to sqlite3MemLink(). This routine breaks that linkage, making |
| 848 | ** pChildObj an independent node that is not a child of any other node. |
| 849 | */ |
| 850 | void sqlite3MemUnlink(void *pParentObj, void *pChildObj){ |
| 851 | EMemHdr *pParent = (EMemHdr*)pParentObj; |
| 852 | EMemHdr *pChild = (EMemHdr*)pChildObj; |
| 853 | EMemHdr **pp; |
| 854 | |
| 855 | assert( pParentObj!=0 ); |
| 856 | assert( pChildObj!=0 ); |
| 857 | pParent--; |
| 858 | assert( isValidEMem(pParent) ); /* pParentObj is an extended allocation */ |
| 859 | pChild--; |
| 860 | assert( isValidEMem(pChild) ); /* pChildObj is an extended allocation */ |
| 861 | assert( isChildEMem(pChild) ); /* pChildObj a child of something */ |
| 862 | for(pp=&pParent->pEChild; (*pp)!=pChild; pp = &(*pp)->pESibling){ |
| 863 | assert( *pp ); /* pChildObj is a child of pParentObj */ |
| 864 | assert( isValidEMem(*pp) ); /* All children of pParentObj are valid */ |
| 865 | assert( isChildEMem(*pp) ); /* All children of pParentObj are children */ |
| 866 | } |
| 867 | *pp = pChild->pESibling; |
| 868 | pChild->pESibling = 0; |
| 869 | clearChildEMem(pChild); |
| 870 | } |
| 871 | |
| 872 | |
| 873 | /* |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 874 | ** Create a string from the zFromat argument and the va_list that follows. |
| 875 | ** Store the string in memory obtained from sqliteMalloc() and make *pz |
| 876 | ** point to that string. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 877 | */ |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 878 | void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 879 | va_list ap; |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 880 | char *z; |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 881 | |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 882 | va_start(ap, zFormat); |
| 883 | z = sqlite3VMPrintf(db, zFormat, ap); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 884 | va_end(ap); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 885 | sqlite3DbFree(db, *pz); |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 886 | *pz = z; |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | |
| 890 | /* |
| 891 | ** This function must be called before exiting any API function (i.e. |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 892 | ** returning control to the user) that has called sqlite3_malloc or |
| 893 | ** sqlite3_realloc. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 894 | ** |
| 895 | ** The returned value is normally a copy of the second argument to this |
shane | be21779 | 2009-03-05 04:20:31 +0000 | [diff] [blame] | 896 | ** function. However, if a malloc() failure has occurred since the previous |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 897 | ** invocation SQLITE_NOMEM is returned instead. |
| 898 | ** |
shane | be21779 | 2009-03-05 04:20:31 +0000 | [diff] [blame] | 899 | ** 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] | 900 | ** then the connection error-code (the value returned by sqlite3_errcode()) |
| 901 | ** is set to SQLITE_NOMEM. |
| 902 | */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 903 | int sqlite3ApiExit(sqlite3* db, int rc){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 904 | /* If the db handle is not NULL, then we must hold the connection handle |
| 905 | ** mutex here. Otherwise the read (and possible write) of db->mallocFailed |
| 906 | ** is unsafe, as is the call to sqlite3Error(). |
| 907 | */ |
| 908 | assert( !db || sqlite3_mutex_held(db->mutex) ); |
danielk1977 | 98c2190 | 2008-09-23 16:41:29 +0000 | [diff] [blame] | 909 | if( db && (db->mallocFailed || rc==SQLITE_IOERR_NOMEM) ){ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 910 | sqlite3Error(db, SQLITE_NOMEM, 0); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 911 | db->mallocFailed = 0; |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 912 | rc = SQLITE_NOMEM; |
| 913 | } |
| 914 | return rc & (db ? db->errMask : 0xff); |
| 915 | } |