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 | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame^] | 15 | ** $Id: malloc.c,v 1.18 2008/06/17 15:12:01 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 | /* |
| 35 | ** Set the soft heap-size limit for the current thread. Passing a |
| 36 | ** zero or 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 | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 46 | if( iLimit>0 ){ |
| 47 | sqlite3_memory_alarm(softHeapLimitEnforcer, 0, iLimit); |
| 48 | }else{ |
| 49 | sqlite3_memory_alarm(0, 0, 0); |
| 50 | } |
| 51 | overage = sqlite3_memory_used() - n; |
| 52 | if( overage>0 ){ |
| 53 | sqlite3_release_memory(overage); |
| 54 | } |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /* |
| 58 | ** Release memory held by SQLite instances created by the current thread. |
| 59 | */ |
| 60 | int sqlite3_release_memory(int n){ |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 61 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
danielk1977 | dfb316d | 2008-03-26 18:34:43 +0000 | [diff] [blame] | 62 | int nRet = sqlite3VdbeReleaseMemory(n); |
| 63 | nRet += sqlite3PagerReleaseMemory(n-nRet); |
| 64 | return nRet; |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 65 | #else |
| 66 | return SQLITE_OK; |
| 67 | #endif |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 68 | } |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 69 | |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 70 | /* |
| 71 | ** State information local to the memory allocation subsystem. |
| 72 | */ |
| 73 | static struct { |
| 74 | sqlite3_mutex *mutex; /* Mutex to serialize access */ |
| 75 | |
| 76 | /* |
| 77 | ** The alarm callback and its arguments. The mem0.mutex lock will |
| 78 | ** be held while the callback is running. Recursive calls into |
| 79 | ** the memory subsystem are allowed, but no new callbacks will be |
| 80 | ** issued. The alarmBusy variable is set to prevent recursive |
| 81 | ** callbacks. |
| 82 | */ |
| 83 | sqlite3_int64 alarmThreshold; |
| 84 | void (*alarmCallback)(void*, sqlite3_int64,int); |
| 85 | void *alarmArg; |
| 86 | int alarmBusy; |
| 87 | |
| 88 | /* |
| 89 | ** Performance statistics |
| 90 | */ |
| 91 | sqlite3_int64 nowUsed; /* Main memory currently in use */ |
| 92 | sqlite3_int64 mxUsed; /* Highwater mark for nowUsed */ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 93 | int mxReq; /* Max request size for ordinary mallocs */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame^] | 94 | int mxScratchReq; /* Max request size for xTemp mallocs */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 95 | } mem0; |
| 96 | |
| 97 | /* |
| 98 | ** Initialize the memory allocation subsystem. |
| 99 | */ |
| 100 | int sqlite3MallocInit(void){ |
| 101 | if( sqlite3Config.m.xMalloc==0 ){ |
| 102 | sqlite3MemSetDefault(); |
| 103 | } |
| 104 | memset(&mem0, 0, sizeof(mem0)); |
| 105 | if( sqlite3Config.bMemstat && sqlite3Config.bCoreMutex ){ |
| 106 | mem0.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM); |
| 107 | } |
| 108 | return sqlite3Config.m.xInit(sqlite3Config.m.pAppData); |
| 109 | } |
| 110 | |
| 111 | /* |
| 112 | ** Deinitialize the memory allocation subsystem. |
| 113 | */ |
| 114 | void sqlite3MallocEnd(void){ |
| 115 | sqlite3Config.m.xShutdown(sqlite3Config.m.pAppData); |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | ** Return the amount of memory currently checked out. |
| 120 | */ |
| 121 | sqlite3_int64 sqlite3_memory_used(void){ |
| 122 | sqlite3_int64 n; |
| 123 | sqlite3_mutex_enter(mem0.mutex); |
| 124 | n = mem0.nowUsed; |
| 125 | sqlite3_mutex_leave(mem0.mutex); |
| 126 | return n; |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | ** Return the maximum amount of memory that has ever been |
| 131 | ** checked out since either the beginning of this process |
| 132 | ** or since the most recent reset. |
| 133 | */ |
| 134 | sqlite3_int64 sqlite3_memory_highwater(int resetFlag){ |
| 135 | sqlite3_int64 n; |
| 136 | sqlite3_mutex_enter(mem0.mutex); |
| 137 | n = mem0.mxUsed; |
| 138 | if( resetFlag ){ |
| 139 | mem0.mxUsed = mem0.nowUsed; |
| 140 | } |
| 141 | sqlite3_mutex_leave(mem0.mutex); |
| 142 | return n; |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | ** Change the alarm callback |
| 147 | */ |
| 148 | int sqlite3_memory_alarm( |
| 149 | void(*xCallback)(void *pArg, sqlite3_int64 used,int N), |
| 150 | void *pArg, |
| 151 | sqlite3_int64 iThreshold |
| 152 | ){ |
| 153 | sqlite3_mutex_enter(mem0.mutex); |
| 154 | mem0.alarmCallback = xCallback; |
| 155 | mem0.alarmArg = pArg; |
| 156 | mem0.alarmThreshold = iThreshold; |
| 157 | sqlite3_mutex_leave(mem0.mutex); |
| 158 | return SQLITE_OK; |
| 159 | } |
| 160 | |
| 161 | /* |
| 162 | ** Trigger the alarm |
| 163 | */ |
| 164 | static void sqlite3MallocAlarm(int nByte){ |
| 165 | void (*xCallback)(void*,sqlite3_int64,int); |
| 166 | sqlite3_int64 nowUsed; |
| 167 | void *pArg; |
| 168 | if( mem0.alarmCallback==0 || mem0.alarmBusy ) return; |
| 169 | mem0.alarmBusy = 1; |
| 170 | xCallback = mem0.alarmCallback; |
| 171 | nowUsed = mem0.nowUsed; |
| 172 | pArg = mem0.alarmArg; |
| 173 | sqlite3_mutex_leave(mem0.mutex); |
| 174 | xCallback(pArg, nowUsed, nByte); |
| 175 | sqlite3_mutex_enter(mem0.mutex); |
| 176 | mem0.alarmBusy = 0; |
| 177 | } |
| 178 | |
| 179 | |
| 180 | /* |
| 181 | ** Allocate memory. This routine is like sqlite3_malloc() except that it |
| 182 | ** assumes the memory subsystem has already been initialized. |
| 183 | */ |
| 184 | void *sqlite3Malloc(int n){ |
| 185 | void *p; |
| 186 | int nFull; |
| 187 | if( n<=0 ){ |
| 188 | return 0; |
| 189 | }else if( sqlite3Config.bMemstat ){ |
| 190 | nFull = sqlite3Config.m.xRoundup(n); |
| 191 | sqlite3_mutex_enter(mem0.mutex); |
| 192 | if( n>mem0.mxReq ) mem0.mxReq = n; |
| 193 | if( mem0.alarmCallback!=0 && mem0.nowUsed+nFull>=mem0.alarmThreshold ){ |
| 194 | sqlite3MallocAlarm(nFull); |
| 195 | } |
| 196 | if( sqlite3FaultStep(SQLITE_FAULTINJECTOR_MALLOC) ){ |
| 197 | p = 0; |
| 198 | }else{ |
| 199 | p = sqlite3Config.m.xMalloc(nFull); |
| 200 | if( p==0 ){ |
| 201 | sqlite3MallocAlarm(nFull); |
| 202 | p = malloc(nFull); |
| 203 | } |
| 204 | } |
| 205 | if( p ){ |
| 206 | mem0.nowUsed += nFull; |
| 207 | if( mem0.nowUsed>mem0.mxUsed ){ |
| 208 | mem0.mxUsed = mem0.nowUsed; |
| 209 | } |
| 210 | } |
| 211 | sqlite3_mutex_leave(mem0.mutex); |
| 212 | }else{ |
| 213 | p = sqlite3Config.m.xMalloc(n); |
| 214 | } |
| 215 | return p; |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | ** This version of the memory allocation is for use by the application. |
| 220 | ** First make sure the memory subsystem is initialized, then do the |
| 221 | ** allocation. |
| 222 | */ |
| 223 | void *sqlite3_malloc(int n){ |
| 224 | #ifndef SQLITE_OMIT_AUTOINIT |
| 225 | if( sqlite3_initialize() ) return 0; |
| 226 | #endif |
| 227 | return sqlite3Malloc(n); |
| 228 | } |
| 229 | |
| 230 | /* |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 231 | ** Each thread may only have a single outstanding allocation from |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame^] | 232 | ** xScratchMalloc(). We verify this constraint in the single-threaded |
| 233 | ** case by setting scratchAllocOut to 1 when an allocation |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 234 | ** is outstanding clearing it when the allocation is freed. |
| 235 | */ |
| 236 | #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame^] | 237 | static int scratchAllocOut = 0; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 238 | #endif |
| 239 | |
| 240 | |
| 241 | /* |
| 242 | ** Allocate memory that is to be used and released right away. |
| 243 | ** This routine is similar to alloca() in that it is not intended |
| 244 | ** for situations where the memory might be held long-term. This |
| 245 | ** routine is intended to get memory to old large transient data |
| 246 | ** structures that would not normally fit on the stack of an |
| 247 | ** embedded processor. |
| 248 | */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame^] | 249 | void *sqlite3ScratchMalloc(int n){ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 250 | void *p; |
| 251 | assert( n>0 ); |
| 252 | if( sqlite3FaultStep(SQLITE_FAULTINJECTOR_MALLOC) ){ |
| 253 | return 0; |
| 254 | } |
| 255 | #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame^] | 256 | assert( scratchAllocOut==0 ); |
| 257 | scratchAllocOut = 1; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 258 | #endif |
| 259 | if( sqlite3Config.bMemstat ){ |
| 260 | sqlite3_mutex_enter(mem0.mutex); |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame^] | 261 | if( n>mem0.mxScratchReq ) mem0.mxScratchReq = n; |
| 262 | p = sqlite3Config.m.xMalloc(n); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 263 | sqlite3_mutex_leave(mem0.mutex); |
| 264 | }else{ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame^] | 265 | p = sqlite3Config.m.xMalloc(n); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 266 | } |
| 267 | return p; |
| 268 | } |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame^] | 269 | void sqlite3ScratchFree(void *p){ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 270 | if( p ){ |
| 271 | #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame^] | 272 | assert( scratchAllocOut==1 ); |
| 273 | scratchAllocOut = 0; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 274 | #endif |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame^] | 275 | sqlite3Config.m.xFree(p); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | |
| 279 | /* |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame^] | 280 | ** Place holders for the page-cache memory allocator. |
| 281 | */ |
| 282 | void *sqlite3PageMalloc(int iSize){ |
| 283 | return sqlite3Malloc(iSize); |
| 284 | } |
| 285 | void sqlite3PageFree(void *pOld){ |
| 286 | sqlite3_free(pOld); |
| 287 | } |
| 288 | |
| 289 | /* |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 290 | ** Return the size of a memory allocation previously obtained from |
| 291 | ** sqlite3Malloc() or sqlite3_malloc(). |
| 292 | */ |
| 293 | int sqlite3MallocSize(void *p){ |
| 294 | return sqlite3Config.m.xSize(p); |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | ** Free memory previously obtained from sqlite3Malloc(). |
| 299 | */ |
| 300 | void sqlite3_free(void *p){ |
| 301 | if( p==0 ) return; |
| 302 | if( sqlite3Config.bMemstat ){ |
| 303 | sqlite3_mutex_enter(mem0.mutex); |
| 304 | mem0.nowUsed -= sqlite3MallocSize(p); |
| 305 | sqlite3Config.m.xFree(p); |
| 306 | sqlite3_mutex_leave(mem0.mutex); |
| 307 | }else{ |
| 308 | sqlite3Config.m.xFree(p); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | /* |
| 313 | ** Change the size of an existing memory allocation |
| 314 | */ |
| 315 | void *sqlite3Realloc(void *pOld, int nBytes){ |
| 316 | int nOld, nNew; |
| 317 | void *pNew; |
| 318 | if( pOld==0 ){ |
| 319 | return sqlite3Malloc(nBytes); |
| 320 | } |
| 321 | if( nBytes<=0 ){ |
| 322 | sqlite3_free(pOld); |
| 323 | return 0; |
| 324 | } |
| 325 | nOld = sqlite3MallocSize(pOld); |
| 326 | if( sqlite3Config.bMemstat ){ |
| 327 | sqlite3_mutex_enter(mem0.mutex); |
| 328 | if( nBytes>mem0.mxReq ) mem0.mxReq = nBytes; |
| 329 | nNew = sqlite3Config.m.xRoundup(nBytes); |
| 330 | if( nOld==nNew ){ |
| 331 | pNew = pOld; |
| 332 | }else{ |
| 333 | if( mem0.nowUsed+nNew-nOld>=mem0.alarmThreshold ){ |
| 334 | sqlite3MallocAlarm(nNew-nOld); |
| 335 | } |
| 336 | if( sqlite3FaultStep(SQLITE_FAULTINJECTOR_MALLOC) ){ |
| 337 | pNew = 0; |
| 338 | }else{ |
| 339 | pNew = sqlite3Config.m.xRealloc(pOld, nNew); |
| 340 | if( pNew==0 ){ |
| 341 | sqlite3MallocAlarm(nBytes); |
| 342 | pNew = sqlite3Config.m.xRealloc(pOld, nNew); |
| 343 | } |
| 344 | } |
| 345 | if( pNew ){ |
| 346 | mem0.nowUsed += nNew-nOld; |
| 347 | if( mem0.nowUsed>mem0.mxUsed ){ |
| 348 | mem0.mxUsed = mem0.nowUsed; |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | sqlite3_mutex_leave(mem0.mutex); |
| 353 | }else{ |
| 354 | pNew = sqlite3Config.m.xRealloc(pOld, nBytes); |
| 355 | } |
| 356 | return pNew; |
| 357 | } |
| 358 | |
| 359 | /* |
| 360 | ** The public interface to sqlite3Realloc. Make sure that the memory |
| 361 | ** subsystem is initialized prior to invoking sqliteRealloc. |
| 362 | */ |
| 363 | void *sqlite3_realloc(void *pOld, int n){ |
| 364 | #ifndef SQLITE_OMIT_AUTOINIT |
| 365 | if( sqlite3_initialize() ) return 0; |
| 366 | #endif |
| 367 | return sqlite3Realloc(pOld, n); |
| 368 | } |
| 369 | |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 370 | |
| 371 | /* |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 372 | ** Allocate and zero memory. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 373 | */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 374 | void *sqlite3MallocZero(int n){ |
| 375 | void *p = sqlite3Malloc(n); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 376 | if( p ){ |
| 377 | memset(p, 0, n); |
| 378 | } |
| 379 | return p; |
| 380 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 381 | |
| 382 | /* |
| 383 | ** Allocate and zero memory. If the allocation fails, make |
| 384 | ** the mallocFailed flag in the connection pointer. |
| 385 | */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 386 | void *sqlite3DbMallocZero(sqlite3 *db, int n){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 387 | void *p = sqlite3DbMallocRaw(db, n); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 388 | if( p ){ |
| 389 | memset(p, 0, n); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 390 | } |
| 391 | return p; |
| 392 | } |
| 393 | |
| 394 | /* |
| 395 | ** Allocate and zero memory. If the allocation fails, make |
| 396 | ** the mallocFailed flag in the connection pointer. |
| 397 | */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 398 | void *sqlite3DbMallocRaw(sqlite3 *db, int n){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 399 | void *p = 0; |
| 400 | if( !db || db->mallocFailed==0 ){ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 401 | p = sqlite3Malloc(n); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 402 | if( !p && db ){ |
| 403 | db->mallocFailed = 1; |
| 404 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 405 | } |
| 406 | return p; |
| 407 | } |
| 408 | |
danielk1977 | 26783a5 | 2007-08-29 14:06:22 +0000 | [diff] [blame] | 409 | /* |
| 410 | ** Resize the block of memory pointed to by p to n bytes. If the |
| 411 | ** resize fails, set the mallocFailed flag inthe connection object. |
| 412 | */ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 413 | void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){ |
| 414 | void *pNew = 0; |
| 415 | if( db->mallocFailed==0 ){ |
| 416 | pNew = sqlite3_realloc(p, n); |
| 417 | if( !pNew ){ |
| 418 | db->mallocFailed = 1; |
| 419 | } |
| 420 | } |
| 421 | return pNew; |
| 422 | } |
| 423 | |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 424 | /* |
| 425 | ** Attempt to reallocate p. If the reallocation fails, then free p |
| 426 | ** and set the mallocFailed flag in the database connection. |
| 427 | */ |
| 428 | void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 429 | void *pNew; |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 430 | pNew = sqlite3DbRealloc(db, p, n); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 431 | if( !pNew ){ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 432 | sqlite3_free(p); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 433 | } |
| 434 | return pNew; |
| 435 | } |
| 436 | |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 437 | /* |
| 438 | ** Make a copy of a string in memory obtained from sqliteMalloc(). These |
| 439 | ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This |
| 440 | ** is because when memory debugging is turned on, these two functions are |
| 441 | ** called via macros that record the current file and line number in the |
| 442 | ** ThreadData structure. |
| 443 | */ |
| 444 | char *sqlite3StrDup(const char *z){ |
| 445 | char *zNew; |
| 446 | int n; |
| 447 | if( z==0 ) return 0; |
| 448 | n = strlen(z)+1; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 449 | zNew = sqlite3Malloc(n); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 450 | if( zNew ) memcpy(zNew, z, n); |
| 451 | return zNew; |
| 452 | } |
| 453 | char *sqlite3StrNDup(const char *z, int n){ |
| 454 | char *zNew; |
| 455 | if( z==0 ) return 0; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 456 | zNew = sqlite3Malloc(n+1); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 457 | if( zNew ){ |
| 458 | memcpy(zNew, z, n); |
| 459 | zNew[n] = 0; |
| 460 | } |
| 461 | return zNew; |
| 462 | } |
| 463 | |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 464 | char *sqlite3DbStrDup(sqlite3 *db, const char *z){ |
| 465 | char *zNew = sqlite3StrDup(z); |
| 466 | if( z && !zNew ){ |
| 467 | db->mallocFailed = 1; |
| 468 | } |
| 469 | return zNew; |
| 470 | } |
| 471 | char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){ |
| 472 | char *zNew = sqlite3StrNDup(z, n); |
| 473 | if( z && !zNew ){ |
| 474 | db->mallocFailed = 1; |
| 475 | } |
| 476 | return zNew; |
| 477 | } |
| 478 | |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 479 | /* |
| 480 | ** Create a string from the 2nd and subsequent arguments (up to the |
| 481 | ** first NULL argument), store the string in memory obtained from |
| 482 | ** sqliteMalloc() and make the pointer indicated by the 1st argument |
| 483 | ** point to that string. The 1st argument must either be NULL or |
| 484 | ** point to memory obtained from sqliteMalloc(). |
| 485 | */ |
| 486 | void sqlite3SetString(char **pz, ...){ |
| 487 | va_list ap; |
| 488 | int nByte; |
| 489 | const char *z; |
| 490 | char *zResult; |
| 491 | |
| 492 | assert( pz!=0 ); |
| 493 | nByte = 1; |
| 494 | va_start(ap, pz); |
| 495 | while( (z = va_arg(ap, const char*))!=0 ){ |
| 496 | nByte += strlen(z); |
| 497 | } |
| 498 | va_end(ap); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 499 | sqlite3_free(*pz); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 500 | *pz = zResult = sqlite3Malloc(nByte); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 501 | if( zResult==0 ){ |
| 502 | return; |
| 503 | } |
| 504 | *zResult = 0; |
| 505 | va_start(ap, pz); |
| 506 | while( (z = va_arg(ap, const char*))!=0 ){ |
| 507 | int n = strlen(z); |
| 508 | memcpy(zResult, z, n); |
| 509 | zResult += n; |
| 510 | } |
| 511 | zResult[0] = 0; |
| 512 | va_end(ap); |
| 513 | } |
| 514 | |
| 515 | |
| 516 | /* |
| 517 | ** This function must be called before exiting any API function (i.e. |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 518 | ** returning control to the user) that has called sqlite3_malloc or |
| 519 | ** sqlite3_realloc. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 520 | ** |
| 521 | ** The returned value is normally a copy of the second argument to this |
| 522 | ** function. However, if a malloc() failure has occured since the previous |
| 523 | ** invocation SQLITE_NOMEM is returned instead. |
| 524 | ** |
| 525 | ** If the first argument, db, is not NULL and a malloc() error has occured, |
| 526 | ** then the connection error-code (the value returned by sqlite3_errcode()) |
| 527 | ** is set to SQLITE_NOMEM. |
| 528 | */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 529 | int sqlite3ApiExit(sqlite3* db, int rc){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 530 | /* If the db handle is not NULL, then we must hold the connection handle |
| 531 | ** mutex here. Otherwise the read (and possible write) of db->mallocFailed |
| 532 | ** is unsafe, as is the call to sqlite3Error(). |
| 533 | */ |
| 534 | assert( !db || sqlite3_mutex_held(db->mutex) ); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 535 | if( db && db->mallocFailed ){ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 536 | sqlite3Error(db, SQLITE_NOMEM, 0); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 537 | db->mallocFailed = 0; |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 538 | rc = SQLITE_NOMEM; |
| 539 | } |
| 540 | return rc & (db ? db->errMask : 0xff); |
| 541 | } |