4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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.
11 *************************************************************************
12 ** Memory allocation functions used throughout sqlite.
17 #include "sqliteInt.h"
22 ** This routine runs when the memory allocator sees that the
23 ** total memory allocation is about to exceed the soft heap
26 static void softHeapLimitEnforcer(
31 sqlite3_release_memory(allocSize);
35 ** Set the soft heap-size limit for the current thread. Passing a
36 ** zero or negative value indicates no limit.
38 void sqlite3_soft_heap_limit(int n){
39 sqlite3_uint64 iLimit;
47 sqlite3_memory_alarm(softHeapLimitEnforcer, 0, iLimit);
49 sqlite3_memory_alarm(0, 0, 0);
51 overage = sqlite3_memory_used() - n;
53 sqlite3_release_memory(overage);
58 ** Release memory held by SQLite instances created by the current thread.
60 int sqlite3_release_memory(int n){
61 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
62 return sqlite3PagerReleaseMemory(n);
70 ** Allocate and zero memory.
72 void *sqlite3MallocZero(unsigned n){
73 void *p = sqlite3_malloc(n);
81 ** Allocate and zero memory. If the allocation fails, make
82 ** the mallocFailed flag in the connection pointer.
84 void *sqlite3DbMallocZero(sqlite3 *db, unsigned n){
85 void *p = sqlite3DbMallocRaw(db, n);
93 ** Allocate and zero memory. If the allocation fails, make
94 ** the mallocFailed flag in the connection pointer.
96 void *sqlite3DbMallocRaw(sqlite3 *db, unsigned n){
98 if( !db || db->mallocFailed==0 ){
99 p = sqlite3_malloc(n);
101 db->mallocFailed = 1;
108 ** Resize the block of memory pointed to by p to n bytes. If the
109 ** resize fails, set the mallocFailed flag inthe connection object.
111 void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
113 if( db->mallocFailed==0 ){
114 pNew = sqlite3_realloc(p, n);
116 db->mallocFailed = 1;
123 ** Attempt to reallocate p. If the reallocation fails, then free p
124 ** and set the mallocFailed flag in the database connection.
126 void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
128 pNew = sqlite3DbRealloc(db, p, n);
136 ** Make a copy of a string in memory obtained from sqliteMalloc(). These
137 ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
138 ** is because when memory debugging is turned on, these two functions are
139 ** called via macros that record the current file and line number in the
140 ** ThreadData structure.
142 char *sqlite3StrDup(const char *z){
147 zNew = sqlite3_malloc(n);
148 if( zNew ) memcpy(zNew, z, n);
151 char *sqlite3StrNDup(const char *z, int n){
154 zNew = sqlite3_malloc(n+1);
162 char *sqlite3DbStrDup(sqlite3 *db, const char *z){
163 char *zNew = sqlite3StrDup(z);
165 db->mallocFailed = 1;
169 char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
170 char *zNew = sqlite3StrNDup(z, n);
172 db->mallocFailed = 1;
178 ** Create a string from the 2nd and subsequent arguments (up to the
179 ** first NULL argument), store the string in memory obtained from
180 ** sqliteMalloc() and make the pointer indicated by the 1st argument
181 ** point to that string. The 1st argument must either be NULL or
182 ** point to memory obtained from sqliteMalloc().
184 void sqlite3SetString(char **pz, ...){
193 while( (z = va_arg(ap, const char*))!=0 ){
198 *pz = zResult = sqlite3_malloc(nByte);
204 while( (z = va_arg(ap, const char*))!=0 ){
206 memcpy(zResult, z, n);
215 ** This function must be called before exiting any API function (i.e.
216 ** returning control to the user) that has called sqlite3_malloc or
219 ** The returned value is normally a copy of the second argument to this
220 ** function. However, if a malloc() failure has occured since the previous
221 ** invocation SQLITE_NOMEM is returned instead.
223 ** If the first argument, db, is not NULL and a malloc() error has occured,
224 ** then the connection error-code (the value returned by sqlite3_errcode())
225 ** is set to SQLITE_NOMEM.
227 int sqlite3ApiExit(sqlite3* db, int rc){
228 /* If the db handle is not NULL, then we must hold the connection handle
229 ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
230 ** is unsafe, as is the call to sqlite3Error().
232 assert( !db || sqlite3_mutex_held(db->mutex) );
233 if( db && db->mallocFailed ){
234 sqlite3Error(db, SQLITE_NOMEM, 0);
235 db->mallocFailed = 0;
238 return rc & (db ? db->errMask : 0xff);