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 ************************************************************************* 12 ** Memory allocation functions used throughout sqlite. 13 ** 14 ** 15 ** $Id: malloc.c,v 1.14 2007/10/20 16:36:31 drh Exp $ 16 */ 17 #include "sqliteInt.h" 18 #include <stdarg.h> 19 #include <ctype.h> 20 21 /* 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, 28 sqlite3_int64 inUse, 29 int allocSize 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. 37 */ 38 void sqlite3_soft_heap_limit(int n){ 39 sqlite3_uint64 iLimit; 40 int overage; 41 if( n<0 ){ 42 iLimit = 0; 43 }else{ 44 iLimit = n; 45 } 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 } 55 } 56 57 /* 58 ** Release memory held by SQLite instances created by the current thread. 59 */ 60 int sqlite3_release_memory(int n){ 61 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT 62 return sqlite3PagerReleaseMemory(n); 63 #else 64 return SQLITE_OK; 65 #endif 66 } 67 68 69 /* 70 ** Allocate and zero memory. 71 */ 72 void *sqlite3MallocZero(unsigned n){ 73 void *p = sqlite3_malloc(n); 74 if( p ){ 75 memset(p, 0, n); 76 } 77 return p; 78 } 79 80 /* 81 ** Allocate and zero memory. If the allocation fails, make 82 ** the mallocFailed flag in the connection pointer. 83 */ 84 void *sqlite3DbMallocZero(sqlite3 *db, unsigned n){ 85 void *p = sqlite3DbMallocRaw(db, n); 86 if( p ){ 87 memset(p, 0, n); 88 } 89 return p; 90 } 91 92 /* 93 ** Allocate and zero memory. If the allocation fails, make 94 ** the mallocFailed flag in the connection pointer. 95 */ 96 void *sqlite3DbMallocRaw(sqlite3 *db, unsigned n){ 97 void *p = 0; 98 if( !db || db->mallocFailed==0 ){ 99 p = sqlite3_malloc(n); 100 if( !p && db ){ 101 db->mallocFailed = 1; 102 } 103 } 104 return p; 105 } 106 107 /* 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. 110 */ 111 void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){ 112 void *pNew = 0; 113 if( db->mallocFailed==0 ){ 114 pNew = sqlite3_realloc(p, n); 115 if( !pNew ){ 116 db->mallocFailed = 1; 117 } 118 } 119 return pNew; 120 } 121 122 /* 123 ** Attempt to reallocate p. If the reallocation fails, then free p 124 ** and set the mallocFailed flag in the database connection. 125 */ 126 void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){ 127 void *pNew; 128 pNew = sqlite3DbRealloc(db, p, n); 129 if( !pNew ){ 130 sqlite3_free(p); 131 } 132 return pNew; 133 } 134 135 /* 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. 141 */ 142 char *sqlite3StrDup(const char *z){ 143 char *zNew; 144 int n; 145 if( z==0 ) return 0; 146 n = strlen(z)+1; 147 zNew = sqlite3_malloc(n); 148 if( zNew ) memcpy(zNew, z, n); 149 return zNew; 150 } 151 char *sqlite3StrNDup(const char *z, int n){ 152 char *zNew; 153 if( z==0 ) return 0; 154 zNew = sqlite3_malloc(n+1); 155 if( zNew ){ 156 memcpy(zNew, z, n); 157 zNew[n] = 0; 158 } 159 return zNew; 160 } 161 162 char *sqlite3DbStrDup(sqlite3 *db, const char *z){ 163 char *zNew = sqlite3StrDup(z); 164 if( z && !zNew ){ 165 db->mallocFailed = 1; 166 } 167 return zNew; 168 } 169 char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){ 170 char *zNew = sqlite3StrNDup(z, n); 171 if( z && !zNew ){ 172 db->mallocFailed = 1; 173 } 174 return zNew; 175 } 176 177 /* 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(). 183 */ 184 void sqlite3SetString(char **pz, ...){ 185 va_list ap; 186 int nByte; 187 const char *z; 188 char *zResult; 189 190 assert( pz!=0 ); 191 nByte = 1; 192 va_start(ap, pz); 193 while( (z = va_arg(ap, const char*))!=0 ){ 194 nByte += strlen(z); 195 } 196 va_end(ap); 197 sqlite3_free(*pz); 198 *pz = zResult = sqlite3_malloc(nByte); 199 if( zResult==0 ){ 200 return; 201 } 202 *zResult = 0; 203 va_start(ap, pz); 204 while( (z = va_arg(ap, const char*))!=0 ){ 205 int n = strlen(z); 206 memcpy(zResult, z, n); 207 zResult += n; 208 } 209 zResult[0] = 0; 210 va_end(ap); 211 } 212 213 214 /* 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 217 ** sqlite3_realloc. 218 ** 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. 222 ** 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. 226 */ 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(). 231 */ 232 assert( !db || sqlite3_mutex_held(db->mutex) ); 233 if( db && db->mallocFailed ){ 234 sqlite3Error(db, SQLITE_NOMEM, 0); 235 db->mallocFailed = 0; 236 rc = SQLITE_NOMEM; 237 } 238 return rc & (db ? db->errMask : 0xff); 239 } 240