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 | ** |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame^] | 15 | ** $Id: malloc.c,v 1.5 2007/08/16 04:30:40 drh Exp $ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 16 | */ |
| 17 | #include "sqliteInt.h" |
| 18 | #include "os.h" |
| 19 | #include <stdarg.h> |
| 20 | #include <ctype.h> |
| 21 | |
| 22 | /* |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 23 | ** Set the soft heap-size limit for the current thread. Passing a negative |
| 24 | ** value indicates no limit. |
| 25 | */ |
| 26 | void sqlite3_soft_heap_limit(int n){ |
| 27 | ThreadData *pTd = sqlite3ThreadData(); |
| 28 | if( pTd ){ |
| 29 | pTd->nSoftHeapLimit = n; |
| 30 | } |
| 31 | sqlite3ReleaseThreadData(); |
| 32 | } |
| 33 | |
| 34 | /* |
| 35 | ** Release memory held by SQLite instances created by the current thread. |
| 36 | */ |
| 37 | int sqlite3_release_memory(int n){ |
| 38 | return sqlite3PagerReleaseMemory(n); |
| 39 | } |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 40 | |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 41 | |
| 42 | /* |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame^] | 43 | ** Allocate and zero memory. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 44 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame^] | 45 | void *sqlite3MallocZero(unsigned n){ |
| 46 | void *p = sqlite3_malloc(n); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 47 | if( p ){ |
| 48 | memset(p, 0, n); |
| 49 | } |
| 50 | return p; |
| 51 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame^] | 52 | |
| 53 | /* |
| 54 | ** Allocate and zero memory. If the allocation fails, make |
| 55 | ** the mallocFailed flag in the connection pointer. |
| 56 | */ |
| 57 | void *sqlite3DbMallocZero(sqlite3 *db, unsigned n){ |
| 58 | void *p = sqlite3_malloc(n); |
| 59 | if( p ){ |
| 60 | memset(p, 0, n); |
| 61 | }else{ |
| 62 | db->mallocFailed = 1; |
| 63 | } |
| 64 | return p; |
| 65 | } |
| 66 | |
| 67 | /* |
| 68 | ** Allocate and zero memory. If the allocation fails, make |
| 69 | ** the mallocFailed flag in the connection pointer. |
| 70 | */ |
| 71 | void *sqlite3DbMallocRaw(sqlite3 *db, unsigned n){ |
| 72 | void *p = sqlite3_malloc(n); |
| 73 | if( !p ){ |
| 74 | db->mallocFailed = 1; |
| 75 | } |
| 76 | return p; |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | ** Attempt to reallocate p. If the reallocation fails, then free p |
| 81 | ** and set the mallocFailed flag in the database connection. |
| 82 | */ |
| 83 | void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 84 | void *pNew; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame^] | 85 | pNew = sqlite3_realloc(p, n); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 86 | if( !pNew ){ |
| 87 | sqlite3FreeX(p); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame^] | 88 | db->mallocFailed = 1; |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 89 | } |
| 90 | return pNew; |
| 91 | } |
| 92 | |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 93 | |
| 94 | /* |
| 95 | ** Make a copy of a string in memory obtained from sqliteMalloc(). These |
| 96 | ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This |
| 97 | ** is because when memory debugging is turned on, these two functions are |
| 98 | ** called via macros that record the current file and line number in the |
| 99 | ** ThreadData structure. |
| 100 | */ |
| 101 | char *sqlite3StrDup(const char *z){ |
| 102 | char *zNew; |
| 103 | int n; |
| 104 | if( z==0 ) return 0; |
| 105 | n = strlen(z)+1; |
| 106 | zNew = sqlite3MallocRaw(n, 1); |
| 107 | if( zNew ) memcpy(zNew, z, n); |
| 108 | return zNew; |
| 109 | } |
| 110 | char *sqlite3StrNDup(const char *z, int n){ |
| 111 | char *zNew; |
| 112 | if( z==0 ) return 0; |
| 113 | zNew = sqlite3MallocRaw(n+1, 1); |
| 114 | if( zNew ){ |
| 115 | memcpy(zNew, z, n); |
| 116 | zNew[n] = 0; |
| 117 | } |
| 118 | return zNew; |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | ** Create a string from the 2nd and subsequent arguments (up to the |
| 123 | ** first NULL argument), store the string in memory obtained from |
| 124 | ** sqliteMalloc() and make the pointer indicated by the 1st argument |
| 125 | ** point to that string. The 1st argument must either be NULL or |
| 126 | ** point to memory obtained from sqliteMalloc(). |
| 127 | */ |
| 128 | void sqlite3SetString(char **pz, ...){ |
| 129 | va_list ap; |
| 130 | int nByte; |
| 131 | const char *z; |
| 132 | char *zResult; |
| 133 | |
| 134 | assert( pz!=0 ); |
| 135 | nByte = 1; |
| 136 | va_start(ap, pz); |
| 137 | while( (z = va_arg(ap, const char*))!=0 ){ |
| 138 | nByte += strlen(z); |
| 139 | } |
| 140 | va_end(ap); |
| 141 | sqliteFree(*pz); |
| 142 | *pz = zResult = sqliteMallocRaw( nByte ); |
| 143 | if( zResult==0 ){ |
| 144 | return; |
| 145 | } |
| 146 | *zResult = 0; |
| 147 | va_start(ap, pz); |
| 148 | while( (z = va_arg(ap, const char*))!=0 ){ |
| 149 | int n = strlen(z); |
| 150 | memcpy(zResult, z, n); |
| 151 | zResult += n; |
| 152 | } |
| 153 | zResult[0] = 0; |
| 154 | va_end(ap); |
| 155 | } |
| 156 | |
| 157 | |
| 158 | /* |
| 159 | ** This function must be called before exiting any API function (i.e. |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame^] | 160 | ** returning control to the user) that has called sqlite3_malloc or |
| 161 | ** sqlite3_realloc. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 162 | ** |
| 163 | ** The returned value is normally a copy of the second argument to this |
| 164 | ** function. However, if a malloc() failure has occured since the previous |
| 165 | ** invocation SQLITE_NOMEM is returned instead. |
| 166 | ** |
| 167 | ** If the first argument, db, is not NULL and a malloc() error has occured, |
| 168 | ** then the connection error-code (the value returned by sqlite3_errcode()) |
| 169 | ** is set to SQLITE_NOMEM. |
| 170 | */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 171 | int sqlite3ApiExit(sqlite3* db, int rc){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame^] | 172 | if( db->mallocFailed ){ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 173 | sqlite3Error(db, SQLITE_NOMEM, 0); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame^] | 174 | db->mallocFailed = 0; |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 175 | rc = SQLITE_NOMEM; |
| 176 | } |
| 177 | return rc & (db ? db->errMask : 0xff); |
| 178 | } |
| 179 | |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 180 | #ifdef SQLITE_MEMDEBUG |
| 181 | /* |
| 182 | ** This function sets a flag in the thread-specific-data structure that will |
| 183 | ** cause an assert to fail if sqliteMalloc() or sqliteRealloc() is called. |
| 184 | */ |
| 185 | void sqlite3MallocDisallow(){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame^] | 186 | #if 0 |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 187 | assert( sqlite3_mallocDisallowed>=0 ); |
| 188 | sqlite3_mallocDisallowed++; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame^] | 189 | #endif |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | /* |
| 193 | ** This function clears the flag set in the thread-specific-data structure set |
| 194 | ** by sqlite3MallocDisallow(). |
| 195 | */ |
| 196 | void sqlite3MallocAllow(){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame^] | 197 | #if 0 |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 198 | assert( sqlite3_mallocDisallowed>0 ); |
| 199 | sqlite3_mallocDisallowed--; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame^] | 200 | #endif |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 201 | } |
| 202 | #endif |