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