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 | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame^] | 15 | ** $Id: malloc.c,v 1.16 2008/06/14 16:56:22 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 */ |
| 93 | int mxReq; /* maximum request size for main or page-cache mem */ |
| 94 | } mem0; |
| 95 | |
| 96 | /* |
| 97 | ** Initialize the memory allocation subsystem. |
| 98 | */ |
| 99 | int sqlite3MallocInit(void){ |
| 100 | if( sqlite3Config.m.xMalloc==0 ){ |
| 101 | sqlite3MemSetDefault(); |
| 102 | } |
| 103 | memset(&mem0, 0, sizeof(mem0)); |
| 104 | if( sqlite3Config.bMemstat && sqlite3Config.bCoreMutex ){ |
| 105 | mem0.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM); |
| 106 | } |
| 107 | return sqlite3Config.m.xInit(sqlite3Config.m.pAppData); |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | ** Deinitialize the memory allocation subsystem. |
| 112 | */ |
| 113 | void sqlite3MallocEnd(void){ |
| 114 | sqlite3Config.m.xShutdown(sqlite3Config.m.pAppData); |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | ** Return the amount of memory currently checked out. |
| 119 | */ |
| 120 | sqlite3_int64 sqlite3_memory_used(void){ |
| 121 | sqlite3_int64 n; |
| 122 | sqlite3_mutex_enter(mem0.mutex); |
| 123 | n = mem0.nowUsed; |
| 124 | sqlite3_mutex_leave(mem0.mutex); |
| 125 | return n; |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | ** Return the maximum amount of memory that has ever been |
| 130 | ** checked out since either the beginning of this process |
| 131 | ** or since the most recent reset. |
| 132 | */ |
| 133 | sqlite3_int64 sqlite3_memory_highwater(int resetFlag){ |
| 134 | sqlite3_int64 n; |
| 135 | sqlite3_mutex_enter(mem0.mutex); |
| 136 | n = mem0.mxUsed; |
| 137 | if( resetFlag ){ |
| 138 | mem0.mxUsed = mem0.nowUsed; |
| 139 | } |
| 140 | sqlite3_mutex_leave(mem0.mutex); |
| 141 | return n; |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | ** Change the alarm callback |
| 146 | */ |
| 147 | int sqlite3_memory_alarm( |
| 148 | void(*xCallback)(void *pArg, sqlite3_int64 used,int N), |
| 149 | void *pArg, |
| 150 | sqlite3_int64 iThreshold |
| 151 | ){ |
| 152 | sqlite3_mutex_enter(mem0.mutex); |
| 153 | mem0.alarmCallback = xCallback; |
| 154 | mem0.alarmArg = pArg; |
| 155 | mem0.alarmThreshold = iThreshold; |
| 156 | sqlite3_mutex_leave(mem0.mutex); |
| 157 | return SQLITE_OK; |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | ** Trigger the alarm |
| 162 | */ |
| 163 | static void sqlite3MallocAlarm(int nByte){ |
| 164 | void (*xCallback)(void*,sqlite3_int64,int); |
| 165 | sqlite3_int64 nowUsed; |
| 166 | void *pArg; |
| 167 | if( mem0.alarmCallback==0 || mem0.alarmBusy ) return; |
| 168 | mem0.alarmBusy = 1; |
| 169 | xCallback = mem0.alarmCallback; |
| 170 | nowUsed = mem0.nowUsed; |
| 171 | pArg = mem0.alarmArg; |
| 172 | sqlite3_mutex_leave(mem0.mutex); |
| 173 | xCallback(pArg, nowUsed, nByte); |
| 174 | sqlite3_mutex_enter(mem0.mutex); |
| 175 | mem0.alarmBusy = 0; |
| 176 | } |
| 177 | |
| 178 | |
| 179 | /* |
| 180 | ** Allocate memory. This routine is like sqlite3_malloc() except that it |
| 181 | ** assumes the memory subsystem has already been initialized. |
| 182 | */ |
| 183 | void *sqlite3Malloc(int n){ |
| 184 | void *p; |
| 185 | int nFull; |
| 186 | if( n<=0 ){ |
| 187 | return 0; |
| 188 | }else if( sqlite3Config.bMemstat ){ |
| 189 | nFull = sqlite3Config.m.xRoundup(n); |
| 190 | sqlite3_mutex_enter(mem0.mutex); |
| 191 | if( n>mem0.mxReq ) mem0.mxReq = n; |
| 192 | if( mem0.alarmCallback!=0 && mem0.nowUsed+nFull>=mem0.alarmThreshold ){ |
| 193 | sqlite3MallocAlarm(nFull); |
| 194 | } |
| 195 | if( sqlite3FaultStep(SQLITE_FAULTINJECTOR_MALLOC) ){ |
| 196 | p = 0; |
| 197 | }else{ |
| 198 | p = sqlite3Config.m.xMalloc(nFull); |
| 199 | if( p==0 ){ |
| 200 | sqlite3MallocAlarm(nFull); |
| 201 | p = malloc(nFull); |
| 202 | } |
| 203 | } |
| 204 | if( p ){ |
| 205 | mem0.nowUsed += nFull; |
| 206 | if( mem0.nowUsed>mem0.mxUsed ){ |
| 207 | mem0.mxUsed = mem0.nowUsed; |
| 208 | } |
| 209 | } |
| 210 | sqlite3_mutex_leave(mem0.mutex); |
| 211 | }else{ |
| 212 | p = sqlite3Config.m.xMalloc(n); |
| 213 | } |
| 214 | return p; |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | ** This version of the memory allocation is for use by the application. |
| 219 | ** First make sure the memory subsystem is initialized, then do the |
| 220 | ** allocation. |
| 221 | */ |
| 222 | void *sqlite3_malloc(int n){ |
| 223 | #ifndef SQLITE_OMIT_AUTOINIT |
| 224 | if( sqlite3_initialize() ) return 0; |
| 225 | #endif |
| 226 | return sqlite3Malloc(n); |
| 227 | } |
| 228 | |
| 229 | /* |
| 230 | ** Return the size of a memory allocation previously obtained from |
| 231 | ** sqlite3Malloc() or sqlite3_malloc(). |
| 232 | */ |
| 233 | int sqlite3MallocSize(void *p){ |
| 234 | return sqlite3Config.m.xSize(p); |
| 235 | } |
| 236 | |
| 237 | /* |
| 238 | ** Free memory previously obtained from sqlite3Malloc(). |
| 239 | */ |
| 240 | void sqlite3_free(void *p){ |
| 241 | if( p==0 ) return; |
| 242 | if( sqlite3Config.bMemstat ){ |
| 243 | sqlite3_mutex_enter(mem0.mutex); |
| 244 | mem0.nowUsed -= sqlite3MallocSize(p); |
| 245 | sqlite3Config.m.xFree(p); |
| 246 | sqlite3_mutex_leave(mem0.mutex); |
| 247 | }else{ |
| 248 | sqlite3Config.m.xFree(p); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | /* |
| 253 | ** Change the size of an existing memory allocation |
| 254 | */ |
| 255 | void *sqlite3Realloc(void *pOld, int nBytes){ |
| 256 | int nOld, nNew; |
| 257 | void *pNew; |
| 258 | if( pOld==0 ){ |
| 259 | return sqlite3Malloc(nBytes); |
| 260 | } |
| 261 | if( nBytes<=0 ){ |
| 262 | sqlite3_free(pOld); |
| 263 | return 0; |
| 264 | } |
| 265 | nOld = sqlite3MallocSize(pOld); |
| 266 | if( sqlite3Config.bMemstat ){ |
| 267 | sqlite3_mutex_enter(mem0.mutex); |
| 268 | if( nBytes>mem0.mxReq ) mem0.mxReq = nBytes; |
| 269 | nNew = sqlite3Config.m.xRoundup(nBytes); |
| 270 | if( nOld==nNew ){ |
| 271 | pNew = pOld; |
| 272 | }else{ |
| 273 | if( mem0.nowUsed+nNew-nOld>=mem0.alarmThreshold ){ |
| 274 | sqlite3MallocAlarm(nNew-nOld); |
| 275 | } |
| 276 | if( sqlite3FaultStep(SQLITE_FAULTINJECTOR_MALLOC) ){ |
| 277 | pNew = 0; |
| 278 | }else{ |
| 279 | pNew = sqlite3Config.m.xRealloc(pOld, nNew); |
| 280 | if( pNew==0 ){ |
| 281 | sqlite3MallocAlarm(nBytes); |
| 282 | pNew = sqlite3Config.m.xRealloc(pOld, nNew); |
| 283 | } |
| 284 | } |
| 285 | if( pNew ){ |
| 286 | mem0.nowUsed += nNew-nOld; |
| 287 | if( mem0.nowUsed>mem0.mxUsed ){ |
| 288 | mem0.mxUsed = mem0.nowUsed; |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | sqlite3_mutex_leave(mem0.mutex); |
| 293 | }else{ |
| 294 | pNew = sqlite3Config.m.xRealloc(pOld, nBytes); |
| 295 | } |
| 296 | return pNew; |
| 297 | } |
| 298 | |
| 299 | /* |
| 300 | ** The public interface to sqlite3Realloc. Make sure that the memory |
| 301 | ** subsystem is initialized prior to invoking sqliteRealloc. |
| 302 | */ |
| 303 | void *sqlite3_realloc(void *pOld, int n){ |
| 304 | #ifndef SQLITE_OMIT_AUTOINIT |
| 305 | if( sqlite3_initialize() ) return 0; |
| 306 | #endif |
| 307 | return sqlite3Realloc(pOld, n); |
| 308 | } |
| 309 | |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 310 | |
| 311 | /* |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 312 | ** Allocate and zero memory. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 313 | */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame^] | 314 | void *sqlite3MallocZero(int n){ |
| 315 | void *p = sqlite3Malloc(n); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 316 | if( p ){ |
| 317 | memset(p, 0, n); |
| 318 | } |
| 319 | return p; |
| 320 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 321 | |
| 322 | /* |
| 323 | ** Allocate and zero memory. If the allocation fails, make |
| 324 | ** the mallocFailed flag in the connection pointer. |
| 325 | */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame^] | 326 | void *sqlite3DbMallocZero(sqlite3 *db, int n){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 327 | void *p = sqlite3DbMallocRaw(db, n); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 328 | if( p ){ |
| 329 | memset(p, 0, n); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 330 | } |
| 331 | return p; |
| 332 | } |
| 333 | |
| 334 | /* |
| 335 | ** Allocate and zero memory. If the allocation fails, make |
| 336 | ** the mallocFailed flag in the connection pointer. |
| 337 | */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame^] | 338 | void *sqlite3DbMallocRaw(sqlite3 *db, int n){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 339 | void *p = 0; |
| 340 | if( !db || db->mallocFailed==0 ){ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame^] | 341 | p = sqlite3Malloc(n); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 342 | if( !p && db ){ |
| 343 | db->mallocFailed = 1; |
| 344 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 345 | } |
| 346 | return p; |
| 347 | } |
| 348 | |
danielk1977 | 26783a5 | 2007-08-29 14:06:22 +0000 | [diff] [blame] | 349 | /* |
| 350 | ** Resize the block of memory pointed to by p to n bytes. If the |
| 351 | ** resize fails, set the mallocFailed flag inthe connection object. |
| 352 | */ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 353 | void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){ |
| 354 | void *pNew = 0; |
| 355 | if( db->mallocFailed==0 ){ |
| 356 | pNew = sqlite3_realloc(p, n); |
| 357 | if( !pNew ){ |
| 358 | db->mallocFailed = 1; |
| 359 | } |
| 360 | } |
| 361 | return pNew; |
| 362 | } |
| 363 | |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 364 | /* |
| 365 | ** Attempt to reallocate p. If the reallocation fails, then free p |
| 366 | ** and set the mallocFailed flag in the database connection. |
| 367 | */ |
| 368 | void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 369 | void *pNew; |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 370 | pNew = sqlite3DbRealloc(db, p, n); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 371 | if( !pNew ){ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 372 | sqlite3_free(p); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 373 | } |
| 374 | return pNew; |
| 375 | } |
| 376 | |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 377 | /* |
| 378 | ** Make a copy of a string in memory obtained from sqliteMalloc(). These |
| 379 | ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This |
| 380 | ** is because when memory debugging is turned on, these two functions are |
| 381 | ** called via macros that record the current file and line number in the |
| 382 | ** ThreadData structure. |
| 383 | */ |
| 384 | char *sqlite3StrDup(const char *z){ |
| 385 | char *zNew; |
| 386 | int n; |
| 387 | if( z==0 ) return 0; |
| 388 | n = strlen(z)+1; |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 389 | zNew = sqlite3_malloc(n); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 390 | if( zNew ) memcpy(zNew, z, n); |
| 391 | return zNew; |
| 392 | } |
| 393 | char *sqlite3StrNDup(const char *z, int n){ |
| 394 | char *zNew; |
| 395 | if( z==0 ) return 0; |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 396 | zNew = sqlite3_malloc(n+1); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 397 | if( zNew ){ |
| 398 | memcpy(zNew, z, n); |
| 399 | zNew[n] = 0; |
| 400 | } |
| 401 | return zNew; |
| 402 | } |
| 403 | |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 404 | char *sqlite3DbStrDup(sqlite3 *db, const char *z){ |
| 405 | char *zNew = sqlite3StrDup(z); |
| 406 | if( z && !zNew ){ |
| 407 | db->mallocFailed = 1; |
| 408 | } |
| 409 | return zNew; |
| 410 | } |
| 411 | char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){ |
| 412 | char *zNew = sqlite3StrNDup(z, n); |
| 413 | if( z && !zNew ){ |
| 414 | db->mallocFailed = 1; |
| 415 | } |
| 416 | return zNew; |
| 417 | } |
| 418 | |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 419 | /* |
| 420 | ** Create a string from the 2nd and subsequent arguments (up to the |
| 421 | ** first NULL argument), store the string in memory obtained from |
| 422 | ** sqliteMalloc() and make the pointer indicated by the 1st argument |
| 423 | ** point to that string. The 1st argument must either be NULL or |
| 424 | ** point to memory obtained from sqliteMalloc(). |
| 425 | */ |
| 426 | void sqlite3SetString(char **pz, ...){ |
| 427 | va_list ap; |
| 428 | int nByte; |
| 429 | const char *z; |
| 430 | char *zResult; |
| 431 | |
| 432 | assert( pz!=0 ); |
| 433 | nByte = 1; |
| 434 | va_start(ap, pz); |
| 435 | while( (z = va_arg(ap, const char*))!=0 ){ |
| 436 | nByte += strlen(z); |
| 437 | } |
| 438 | va_end(ap); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 439 | sqlite3_free(*pz); |
| 440 | *pz = zResult = sqlite3_malloc(nByte); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 441 | if( zResult==0 ){ |
| 442 | return; |
| 443 | } |
| 444 | *zResult = 0; |
| 445 | va_start(ap, pz); |
| 446 | while( (z = va_arg(ap, const char*))!=0 ){ |
| 447 | int n = strlen(z); |
| 448 | memcpy(zResult, z, n); |
| 449 | zResult += n; |
| 450 | } |
| 451 | zResult[0] = 0; |
| 452 | va_end(ap); |
| 453 | } |
| 454 | |
| 455 | |
| 456 | /* |
| 457 | ** This function must be called before exiting any API function (i.e. |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 458 | ** returning control to the user) that has called sqlite3_malloc or |
| 459 | ** sqlite3_realloc. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 460 | ** |
| 461 | ** The returned value is normally a copy of the second argument to this |
| 462 | ** function. However, if a malloc() failure has occured since the previous |
| 463 | ** invocation SQLITE_NOMEM is returned instead. |
| 464 | ** |
| 465 | ** If the first argument, db, is not NULL and a malloc() error has occured, |
| 466 | ** then the connection error-code (the value returned by sqlite3_errcode()) |
| 467 | ** is set to SQLITE_NOMEM. |
| 468 | */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 469 | int sqlite3ApiExit(sqlite3* db, int rc){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 470 | /* If the db handle is not NULL, then we must hold the connection handle |
| 471 | ** mutex here. Otherwise the read (and possible write) of db->mallocFailed |
| 472 | ** is unsafe, as is the call to sqlite3Error(). |
| 473 | */ |
| 474 | assert( !db || sqlite3_mutex_held(db->mutex) ); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 475 | if( db && db->mallocFailed ){ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 476 | sqlite3Error(db, SQLITE_NOMEM, 0); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 477 | db->mallocFailed = 0; |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 478 | rc = SQLITE_NOMEM; |
| 479 | } |
| 480 | return rc & (db ? db->errMask : 0xff); |
| 481 | } |