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 | ************************************************************************* |
| 12 | ** Memory allocation functions used throughout sqlite. |
| 13 | ** |
| 14 | ** |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame^] | 15 | ** $Id: malloc.c,v 1.12 2007/08/29 12:31:26 danielk1977 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 |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 62 | return sqlite3PagerReleaseMemory(n); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 63 | #else |
| 64 | return SQLITE_OK; |
| 65 | #endif |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 66 | } |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 67 | |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 68 | |
| 69 | /* |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 70 | ** Allocate and zero memory. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 71 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 72 | void *sqlite3MallocZero(unsigned n){ |
| 73 | void *p = sqlite3_malloc(n); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 74 | if( p ){ |
| 75 | memset(p, 0, n); |
| 76 | } |
| 77 | return p; |
| 78 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 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){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame^] | 85 | void *p = sqlite3DbMallocRaw(db, n); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 86 | if( p ){ |
| 87 | memset(p, 0, n); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 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){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame^] | 97 | void *p = 0; |
| 98 | if( !db || db->mallocFailed==0 ){ |
| 99 | p = sqlite3_malloc(n); |
| 100 | if( !p && db ){ |
| 101 | db->mallocFailed = 1; |
| 102 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 103 | } |
| 104 | return p; |
| 105 | } |
| 106 | |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame^] | 107 | void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){ |
| 108 | void *pNew = 0; |
| 109 | if( db->mallocFailed==0 ){ |
| 110 | pNew = sqlite3_realloc(p, n); |
| 111 | if( !pNew ){ |
| 112 | db->mallocFailed = 1; |
| 113 | } |
| 114 | } |
| 115 | return pNew; |
| 116 | } |
| 117 | |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 118 | /* |
| 119 | ** Attempt to reallocate p. If the reallocation fails, then free p |
| 120 | ** and set the mallocFailed flag in the database connection. |
| 121 | */ |
| 122 | void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 123 | void *pNew; |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame^] | 124 | pNew = sqlite3DbRealloc(db, p, n); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 125 | if( !pNew ){ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 126 | sqlite3_free(p); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 127 | } |
| 128 | return pNew; |
| 129 | } |
| 130 | |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 131 | /* |
| 132 | ** Make a copy of a string in memory obtained from sqliteMalloc(). These |
| 133 | ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This |
| 134 | ** is because when memory debugging is turned on, these two functions are |
| 135 | ** called via macros that record the current file and line number in the |
| 136 | ** ThreadData structure. |
| 137 | */ |
| 138 | char *sqlite3StrDup(const char *z){ |
| 139 | char *zNew; |
| 140 | int n; |
| 141 | if( z==0 ) return 0; |
| 142 | n = strlen(z)+1; |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 143 | zNew = sqlite3_malloc(n); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 144 | if( zNew ) memcpy(zNew, z, n); |
| 145 | return zNew; |
| 146 | } |
| 147 | char *sqlite3StrNDup(const char *z, int n){ |
| 148 | char *zNew; |
| 149 | if( z==0 ) return 0; |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 150 | zNew = sqlite3_malloc(n+1); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 151 | if( zNew ){ |
| 152 | memcpy(zNew, z, n); |
| 153 | zNew[n] = 0; |
| 154 | } |
| 155 | return zNew; |
| 156 | } |
| 157 | |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 158 | char *sqlite3DbStrDup(sqlite3 *db, const char *z){ |
| 159 | char *zNew = sqlite3StrDup(z); |
| 160 | if( z && !zNew ){ |
| 161 | db->mallocFailed = 1; |
| 162 | } |
| 163 | return zNew; |
| 164 | } |
| 165 | char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){ |
| 166 | char *zNew = sqlite3StrNDup(z, n); |
| 167 | if( z && !zNew ){ |
| 168 | db->mallocFailed = 1; |
| 169 | } |
| 170 | return zNew; |
| 171 | } |
| 172 | |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 173 | /* |
| 174 | ** Create a string from the 2nd and subsequent arguments (up to the |
| 175 | ** first NULL argument), store the string in memory obtained from |
| 176 | ** sqliteMalloc() and make the pointer indicated by the 1st argument |
| 177 | ** point to that string. The 1st argument must either be NULL or |
| 178 | ** point to memory obtained from sqliteMalloc(). |
| 179 | */ |
| 180 | void sqlite3SetString(char **pz, ...){ |
| 181 | va_list ap; |
| 182 | int nByte; |
| 183 | const char *z; |
| 184 | char *zResult; |
| 185 | |
| 186 | assert( pz!=0 ); |
| 187 | nByte = 1; |
| 188 | va_start(ap, pz); |
| 189 | while( (z = va_arg(ap, const char*))!=0 ){ |
| 190 | nByte += strlen(z); |
| 191 | } |
| 192 | va_end(ap); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 193 | sqlite3_free(*pz); |
| 194 | *pz = zResult = sqlite3_malloc(nByte); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 195 | if( zResult==0 ){ |
| 196 | return; |
| 197 | } |
| 198 | *zResult = 0; |
| 199 | va_start(ap, pz); |
| 200 | while( (z = va_arg(ap, const char*))!=0 ){ |
| 201 | int n = strlen(z); |
| 202 | memcpy(zResult, z, n); |
| 203 | zResult += n; |
| 204 | } |
| 205 | zResult[0] = 0; |
| 206 | va_end(ap); |
| 207 | } |
| 208 | |
| 209 | |
| 210 | /* |
| 211 | ** This function must be called before exiting any API function (i.e. |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 212 | ** returning control to the user) that has called sqlite3_malloc or |
| 213 | ** sqlite3_realloc. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 214 | ** |
| 215 | ** The returned value is normally a copy of the second argument to this |
| 216 | ** function. However, if a malloc() failure has occured since the previous |
| 217 | ** invocation SQLITE_NOMEM is returned instead. |
| 218 | ** |
| 219 | ** If the first argument, db, is not NULL and a malloc() error has occured, |
| 220 | ** then the connection error-code (the value returned by sqlite3_errcode()) |
| 221 | ** is set to SQLITE_NOMEM. |
| 222 | */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 223 | int sqlite3ApiExit(sqlite3* db, int rc){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame^] | 224 | /* If the db handle is not NULL, then we must hold the connection handle |
| 225 | ** mutex here. Otherwise the read (and possible write) of db->mallocFailed |
| 226 | ** is unsafe, as is the call to sqlite3Error(). |
| 227 | */ |
| 228 | assert( !db || sqlite3_mutex_held(db->mutex) ); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 229 | if( db && db->mallocFailed ){ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 230 | sqlite3Error(db, SQLITE_NOMEM, 0); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 231 | db->mallocFailed = 0; |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 232 | rc = SQLITE_NOMEM; |
| 233 | } |
| 234 | return rc & (db ? db->errMask : 0xff); |
| 235 | } |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame^] | 236 | |