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