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