ds@1952: /* ds@1952: ** 2001 September 15 ds@1952: ** ds@1952: ** The author disclaims copyright to this source code. In place of ds@1952: ** a legal notice, here is a blessing: ds@1952: ** ds@1952: ** May you do good and not evil. ds@1952: ** May you find forgiveness for yourself and forgive others. ds@1952: ** May you share freely, never taking more than you give. ds@1952: ** ds@1952: ************************************************************************* ds@1952: ** Memory allocation functions used throughout sqlite. ds@1952: ** ds@1952: ** ds@1952: ** $Id$ ds@1952: */ ds@1952: #include "sqliteInt.h" ds@1952: #include ds@1952: #include ds@1952: ds@1952: /* ds@1952: ** This routine runs when the memory allocator sees that the ds@1952: ** total memory allocation is about to exceed the soft heap ds@1952: ** limit. ds@1952: */ ds@1952: static void softHeapLimitEnforcer( ds@1952: void *NotUsed, ds@1952: sqlite3_int64 inUse, ds@1952: int allocSize ds@1952: ){ ds@1952: sqlite3_release_memory(allocSize); ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Set the soft heap-size limit for the current thread. Passing a ds@1952: ** zero or negative value indicates no limit. ds@1952: */ ds@1952: void sqlite3_soft_heap_limit(int n){ ds@1952: sqlite3_uint64 iLimit; ds@1952: int overage; ds@1952: if( n<0 ){ ds@1952: iLimit = 0; ds@1952: }else{ ds@1952: iLimit = n; ds@1952: } ds@1952: if( iLimit>0 ){ ds@1952: sqlite3_memory_alarm(softHeapLimitEnforcer, 0, iLimit); ds@1952: }else{ ds@1952: sqlite3_memory_alarm(0, 0, 0); ds@1952: } ds@1952: overage = sqlite3_memory_used() - n; ds@1952: if( overage>0 ){ ds@1952: sqlite3_release_memory(overage); ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Release memory held by SQLite instances created by the current thread. ds@1952: */ ds@1952: int sqlite3_release_memory(int n){ ds@1952: #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT ds@1952: return sqlite3PagerReleaseMemory(n); ds@1952: #else ds@1952: return SQLITE_OK; ds@1952: #endif ds@1952: } ds@1952: ds@1952: ds@1952: /* ds@1952: ** Allocate and zero memory. ds@1952: */ ds@1952: void *sqlite3MallocZero(unsigned n){ ds@1952: void *p = sqlite3_malloc(n); ds@1952: if( p ){ ds@1952: memset(p, 0, n); ds@1952: } ds@1952: return p; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Allocate and zero memory. If the allocation fails, make ds@1952: ** the mallocFailed flag in the connection pointer. ds@1952: */ ds@1952: void *sqlite3DbMallocZero(sqlite3 *db, unsigned n){ ds@1952: void *p = sqlite3DbMallocRaw(db, n); ds@1952: if( p ){ ds@1952: memset(p, 0, n); ds@1952: } ds@1952: return p; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Allocate and zero memory. If the allocation fails, make ds@1952: ** the mallocFailed flag in the connection pointer. ds@1952: */ ds@1952: void *sqlite3DbMallocRaw(sqlite3 *db, unsigned n){ ds@1952: void *p = 0; ds@1952: if( !db || db->mallocFailed==0 ){ ds@1952: p = sqlite3_malloc(n); ds@1952: if( !p && db ){ ds@1952: db->mallocFailed = 1; ds@1952: } ds@1952: } ds@1952: return p; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Resize the block of memory pointed to by p to n bytes. If the ds@1952: ** resize fails, set the mallocFailed flag inthe connection object. ds@1952: */ ds@1952: void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){ ds@1952: void *pNew = 0; ds@1952: if( db->mallocFailed==0 ){ ds@1952: pNew = sqlite3_realloc(p, n); ds@1952: if( !pNew ){ ds@1952: db->mallocFailed = 1; ds@1952: } ds@1952: } ds@1952: return pNew; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Attempt to reallocate p. If the reallocation fails, then free p ds@1952: ** and set the mallocFailed flag in the database connection. ds@1952: */ ds@1952: void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){ ds@1952: void *pNew; ds@1952: pNew = sqlite3DbRealloc(db, p, n); ds@1952: if( !pNew ){ ds@1952: sqlite3_free(p); ds@1952: } ds@1952: return pNew; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Make a copy of a string in memory obtained from sqliteMalloc(). These ds@1952: ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This ds@1952: ** is because when memory debugging is turned on, these two functions are ds@1952: ** called via macros that record the current file and line number in the ds@1952: ** ThreadData structure. ds@1952: */ ds@1952: char *sqlite3StrDup(const char *z){ ds@1952: char *zNew; ds@1952: int n; ds@1952: if( z==0 ) return 0; ds@1952: n = strlen(z)+1; ds@1952: zNew = sqlite3_malloc(n); ds@1952: if( zNew ) memcpy(zNew, z, n); ds@1952: return zNew; ds@1952: } ds@1952: char *sqlite3StrNDup(const char *z, int n){ ds@1952: char *zNew; ds@1952: if( z==0 ) return 0; ds@1952: zNew = sqlite3_malloc(n+1); ds@1952: if( zNew ){ ds@1952: memcpy(zNew, z, n); ds@1952: zNew[n] = 0; ds@1952: } ds@1952: return zNew; ds@1952: } ds@1952: ds@1952: char *sqlite3DbStrDup(sqlite3 *db, const char *z){ ds@1952: char *zNew = sqlite3StrDup(z); ds@1952: if( z && !zNew ){ ds@1952: db->mallocFailed = 1; ds@1952: } ds@1952: return zNew; ds@1952: } ds@1952: char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){ ds@1952: char *zNew = sqlite3StrNDup(z, n); ds@1952: if( z && !zNew ){ ds@1952: db->mallocFailed = 1; ds@1952: } ds@1952: return zNew; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Create a string from the 2nd and subsequent arguments (up to the ds@1952: ** first NULL argument), store the string in memory obtained from ds@1952: ** sqliteMalloc() and make the pointer indicated by the 1st argument ds@1952: ** point to that string. The 1st argument must either be NULL or ds@1952: ** point to memory obtained from sqliteMalloc(). ds@1952: */ ds@1952: void sqlite3SetString(char **pz, ...){ ds@1952: va_list ap; ds@1952: int nByte; ds@1952: const char *z; ds@1952: char *zResult; ds@1952: ds@1952: assert( pz!=0 ); ds@1952: nByte = 1; ds@1952: va_start(ap, pz); ds@1952: while( (z = va_arg(ap, const char*))!=0 ){ ds@1952: nByte += strlen(z); ds@1952: } ds@1952: va_end(ap); ds@1952: sqlite3_free(*pz); ds@1952: *pz = zResult = sqlite3_malloc(nByte); ds@1952: if( zResult==0 ){ ds@1952: return; ds@1952: } ds@1952: *zResult = 0; ds@1952: va_start(ap, pz); ds@1952: while( (z = va_arg(ap, const char*))!=0 ){ ds@1952: int n = strlen(z); ds@1952: memcpy(zResult, z, n); ds@1952: zResult += n; ds@1952: } ds@1952: zResult[0] = 0; ds@1952: va_end(ap); ds@1952: } ds@1952: ds@1952: ds@1952: /* ds@1952: ** This function must be called before exiting any API function (i.e. ds@1952: ** returning control to the user) that has called sqlite3_malloc or ds@1952: ** sqlite3_realloc. ds@1952: ** ds@1952: ** The returned value is normally a copy of the second argument to this ds@1952: ** function. However, if a malloc() failure has occured since the previous ds@1952: ** invocation SQLITE_NOMEM is returned instead. ds@1952: ** ds@1952: ** If the first argument, db, is not NULL and a malloc() error has occured, ds@1952: ** then the connection error-code (the value returned by sqlite3_errcode()) ds@1952: ** is set to SQLITE_NOMEM. ds@1952: */ ds@1952: int sqlite3ApiExit(sqlite3* db, int rc){ ds@1952: /* If the db handle is not NULL, then we must hold the connection handle ds@1952: ** mutex here. Otherwise the read (and possible write) of db->mallocFailed ds@1952: ** is unsafe, as is the call to sqlite3Error(). ds@1952: */ ds@1952: assert( !db || sqlite3_mutex_held(db->mutex) ); ds@1952: if( db && db->mallocFailed ){ ds@1952: sqlite3Error(db, SQLITE_NOMEM, 0); ds@1952: db->mallocFailed = 0; ds@1952: rc = SQLITE_NOMEM; ds@1952: } ds@1952: return rc & (db ? db->errMask : 0xff); ds@1952: } ds@1952: