|
ds@1952
|
1 |
/* |
|
ds@1952
|
2 |
** 2001 September 15 |
|
ds@1952
|
3 |
** |
|
ds@1952
|
4 |
** The author disclaims copyright to this source code. In place of |
|
ds@1952
|
5 |
** a legal notice, here is a blessing: |
|
ds@1952
|
6 |
** |
|
ds@1952
|
7 |
** May you do good and not evil. |
|
ds@1952
|
8 |
** May you find forgiveness for yourself and forgive others. |
|
ds@1952
|
9 |
** May you share freely, never taking more than you give. |
|
ds@1952
|
10 |
** |
|
ds@1952
|
11 |
************************************************************************* |
|
ds@1952
|
12 |
** Memory allocation functions used throughout sqlite. |
|
ds@1952
|
13 |
** |
|
ds@1952
|
14 |
** |
|
ds@1952
|
15 |
** $Id$ |
|
ds@1952
|
16 |
*/ |
|
ds@1952
|
17 |
#include "sqliteInt.h" |
|
ds@1952
|
18 |
#include <stdarg.h> |
|
ds@1952
|
19 |
#include <ctype.h> |
|
ds@1952
|
20 |
|
|
ds@1952
|
21 |
/* |
|
ds@1952
|
22 |
** This routine runs when the memory allocator sees that the |
|
ds@1952
|
23 |
** total memory allocation is about to exceed the soft heap |
|
ds@1952
|
24 |
** limit. |
|
ds@1952
|
25 |
*/ |
|
ds@1952
|
26 |
static void softHeapLimitEnforcer( |
|
ds@1952
|
27 |
void *NotUsed, |
|
ds@1952
|
28 |
sqlite3_int64 inUse, |
|
ds@1952
|
29 |
int allocSize |
|
ds@1952
|
30 |
){ |
|
ds@1952
|
31 |
sqlite3_release_memory(allocSize); |
|
ds@1952
|
32 |
} |
|
ds@1952
|
33 |
|
|
ds@1952
|
34 |
/* |
|
ds@1952
|
35 |
** Set the soft heap-size limit for the current thread. Passing a |
|
ds@1952
|
36 |
** zero or negative value indicates no limit. |
|
ds@1952
|
37 |
*/ |
|
ds@1952
|
38 |
void sqlite3_soft_heap_limit(int n){ |
|
ds@1952
|
39 |
sqlite3_uint64 iLimit; |
|
ds@1952
|
40 |
int overage; |
|
ds@1952
|
41 |
if( n<0 ){ |
|
ds@1952
|
42 |
iLimit = 0; |
|
ds@1952
|
43 |
}else{ |
|
ds@1952
|
44 |
iLimit = n; |
|
ds@1952
|
45 |
} |
|
ds@1952
|
46 |
if( iLimit>0 ){ |
|
ds@1952
|
47 |
sqlite3_memory_alarm(softHeapLimitEnforcer, 0, iLimit); |
|
ds@1952
|
48 |
}else{ |
|
ds@1952
|
49 |
sqlite3_memory_alarm(0, 0, 0); |
|
ds@1952
|
50 |
} |
|
ds@1952
|
51 |
overage = sqlite3_memory_used() - n; |
|
ds@1952
|
52 |
if( overage>0 ){ |
|
ds@1952
|
53 |
sqlite3_release_memory(overage); |
|
ds@1952
|
54 |
} |
|
ds@1952
|
55 |
} |
|
ds@1952
|
56 |
|
|
ds@1952
|
57 |
/* |
|
ds@1952
|
58 |
** Release memory held by SQLite instances created by the current thread. |
|
ds@1952
|
59 |
*/ |
|
ds@1952
|
60 |
int sqlite3_release_memory(int n){ |
|
ds@1952
|
61 |
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
|
ds@1952
|
62 |
return sqlite3PagerReleaseMemory(n); |
|
ds@1952
|
63 |
#else |
|
ds@1952
|
64 |
return SQLITE_OK; |
|
ds@1952
|
65 |
#endif |
|
ds@1952
|
66 |
} |
|
ds@1952
|
67 |
|
|
ds@1952
|
68 |
|
|
ds@1952
|
69 |
/* |
|
ds@1952
|
70 |
** Allocate and zero memory. |
|
ds@1952
|
71 |
*/ |
|
ds@1952
|
72 |
void *sqlite3MallocZero(unsigned n){ |
|
ds@1952
|
73 |
void *p = sqlite3_malloc(n); |
|
ds@1952
|
74 |
if( p ){ |
|
ds@1952
|
75 |
memset(p, 0, n); |
|
ds@1952
|
76 |
} |
|
ds@1952
|
77 |
return p; |
|
ds@1952
|
78 |
} |
|
ds@1952
|
79 |
|
|
ds@1952
|
80 |
/* |
|
ds@1952
|
81 |
** Allocate and zero memory. If the allocation fails, make |
|
ds@1952
|
82 |
** the mallocFailed flag in the connection pointer. |
|
ds@1952
|
83 |
*/ |
|
ds@1952
|
84 |
void *sqlite3DbMallocZero(sqlite3 *db, unsigned n){ |
|
ds@1952
|
85 |
void *p = sqlite3DbMallocRaw(db, n); |
|
ds@1952
|
86 |
if( p ){ |
|
ds@1952
|
87 |
memset(p, 0, n); |
|
ds@1952
|
88 |
} |
|
ds@1952
|
89 |
return p; |
|
ds@1952
|
90 |
} |
|
ds@1952
|
91 |
|
|
ds@1952
|
92 |
/* |
|
ds@1952
|
93 |
** Allocate and zero memory. If the allocation fails, make |
|
ds@1952
|
94 |
** the mallocFailed flag in the connection pointer. |
|
ds@1952
|
95 |
*/ |
|
ds@1952
|
96 |
void *sqlite3DbMallocRaw(sqlite3 *db, unsigned n){ |
|
ds@1952
|
97 |
void *p = 0; |
|
ds@1952
|
98 |
if( !db || db->mallocFailed==0 ){ |
|
ds@1952
|
99 |
p = sqlite3_malloc(n); |
|
ds@1952
|
100 |
if( !p && db ){ |
|
ds@1952
|
101 |
db->mallocFailed = 1; |
|
ds@1952
|
102 |
} |
|
ds@1952
|
103 |
} |
|
ds@1952
|
104 |
return p; |
|
ds@1952
|
105 |
} |
|
ds@1952
|
106 |
|
|
ds@1952
|
107 |
/* |
|
ds@1952
|
108 |
** Resize the block of memory pointed to by p to n bytes. If the |
|
ds@1952
|
109 |
** resize fails, set the mallocFailed flag inthe connection object. |
|
ds@1952
|
110 |
*/ |
|
ds@1952
|
111 |
void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){ |
|
ds@1952
|
112 |
void *pNew = 0; |
|
ds@1952
|
113 |
if( db->mallocFailed==0 ){ |
|
ds@1952
|
114 |
pNew = sqlite3_realloc(p, n); |
|
ds@1952
|
115 |
if( !pNew ){ |
|
ds@1952
|
116 |
db->mallocFailed = 1; |
|
ds@1952
|
117 |
} |
|
ds@1952
|
118 |
} |
|
ds@1952
|
119 |
return pNew; |
|
ds@1952
|
120 |
} |
|
ds@1952
|
121 |
|
|
ds@1952
|
122 |
/* |
|
ds@1952
|
123 |
** Attempt to reallocate p. If the reallocation fails, then free p |
|
ds@1952
|
124 |
** and set the mallocFailed flag in the database connection. |
|
ds@1952
|
125 |
*/ |
|
ds@1952
|
126 |
void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){ |
|
ds@1952
|
127 |
void *pNew; |
|
ds@1952
|
128 |
pNew = sqlite3DbRealloc(db, p, n); |
|
ds@1952
|
129 |
if( !pNew ){ |
|
ds@1952
|
130 |
sqlite3_free(p); |
|
ds@1952
|
131 |
} |
|
ds@1952
|
132 |
return pNew; |
|
ds@1952
|
133 |
} |
|
ds@1952
|
134 |
|
|
ds@1952
|
135 |
/* |
|
ds@1952
|
136 |
** Make a copy of a string in memory obtained from sqliteMalloc(). These |
|
ds@1952
|
137 |
** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This |
|
ds@1952
|
138 |
** is because when memory debugging is turned on, these two functions are |
|
ds@1952
|
139 |
** called via macros that record the current file and line number in the |
|
ds@1952
|
140 |
** ThreadData structure. |
|
ds@1952
|
141 |
*/ |
|
ds@1952
|
142 |
char *sqlite3StrDup(const char *z){ |
|
ds@1952
|
143 |
char *zNew; |
|
ds@1952
|
144 |
int n; |
|
ds@1952
|
145 |
if( z==0 ) return 0; |
|
ds@1952
|
146 |
n = strlen(z)+1; |
|
ds@1952
|
147 |
zNew = sqlite3_malloc(n); |
|
ds@1952
|
148 |
if( zNew ) memcpy(zNew, z, n); |
|
ds@1952
|
149 |
return zNew; |
|
ds@1952
|
150 |
} |
|
ds@1952
|
151 |
char *sqlite3StrNDup(const char *z, int n){ |
|
ds@1952
|
152 |
char *zNew; |
|
ds@1952
|
153 |
if( z==0 ) return 0; |
|
ds@1952
|
154 |
zNew = sqlite3_malloc(n+1); |
|
ds@1952
|
155 |
if( zNew ){ |
|
ds@1952
|
156 |
memcpy(zNew, z, n); |
|
ds@1952
|
157 |
zNew[n] = 0; |
|
ds@1952
|
158 |
} |
|
ds@1952
|
159 |
return zNew; |
|
ds@1952
|
160 |
} |
|
ds@1952
|
161 |
|
|
ds@1952
|
162 |
char *sqlite3DbStrDup(sqlite3 *db, const char *z){ |
|
ds@1952
|
163 |
char *zNew = sqlite3StrDup(z); |
|
ds@1952
|
164 |
if( z && !zNew ){ |
|
ds@1952
|
165 |
db->mallocFailed = 1; |
|
ds@1952
|
166 |
} |
|
ds@1952
|
167 |
return zNew; |
|
ds@1952
|
168 |
} |
|
ds@1952
|
169 |
char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){ |
|
ds@1952
|
170 |
char *zNew = sqlite3StrNDup(z, n); |
|
ds@1952
|
171 |
if( z && !zNew ){ |
|
ds@1952
|
172 |
db->mallocFailed = 1; |
|
ds@1952
|
173 |
} |
|
ds@1952
|
174 |
return zNew; |
|
ds@1952
|
175 |
} |
|
ds@1952
|
176 |
|
|
ds@1952
|
177 |
/* |
|
ds@1952
|
178 |
** Create a string from the 2nd and subsequent arguments (up to the |
|
ds@1952
|
179 |
** first NULL argument), store the string in memory obtained from |
|
ds@1952
|
180 |
** sqliteMalloc() and make the pointer indicated by the 1st argument |
|
ds@1952
|
181 |
** point to that string. The 1st argument must either be NULL or |
|
ds@1952
|
182 |
** point to memory obtained from sqliteMalloc(). |
|
ds@1952
|
183 |
*/ |
|
ds@1952
|
184 |
void sqlite3SetString(char **pz, ...){ |
|
ds@1952
|
185 |
va_list ap; |
|
ds@1952
|
186 |
int nByte; |
|
ds@1952
|
187 |
const char *z; |
|
ds@1952
|
188 |
char *zResult; |
|
ds@1952
|
189 |
|
|
ds@1952
|
190 |
assert( pz!=0 ); |
|
ds@1952
|
191 |
nByte = 1; |
|
ds@1952
|
192 |
va_start(ap, pz); |
|
ds@1952
|
193 |
while( (z = va_arg(ap, const char*))!=0 ){ |
|
ds@1952
|
194 |
nByte += strlen(z); |
|
ds@1952
|
195 |
} |
|
ds@1952
|
196 |
va_end(ap); |
|
ds@1952
|
197 |
sqlite3_free(*pz); |
|
ds@1952
|
198 |
*pz = zResult = sqlite3_malloc(nByte); |
|
ds@1952
|
199 |
if( zResult==0 ){ |
|
ds@1952
|
200 |
return; |
|
ds@1952
|
201 |
} |
|
ds@1952
|
202 |
*zResult = 0; |
|
ds@1952
|
203 |
va_start(ap, pz); |
|
ds@1952
|
204 |
while( (z = va_arg(ap, const char*))!=0 ){ |
|
ds@1952
|
205 |
int n = strlen(z); |
|
ds@1952
|
206 |
memcpy(zResult, z, n); |
|
ds@1952
|
207 |
zResult += n; |
|
ds@1952
|
208 |
} |
|
ds@1952
|
209 |
zResult[0] = 0; |
|
ds@1952
|
210 |
va_end(ap); |
|
ds@1952
|
211 |
} |
|
ds@1952
|
212 |
|
|
ds@1952
|
213 |
|
|
ds@1952
|
214 |
/* |
|
ds@1952
|
215 |
** This function must be called before exiting any API function (i.e. |
|
ds@1952
|
216 |
** returning control to the user) that has called sqlite3_malloc or |
|
ds@1952
|
217 |
** sqlite3_realloc. |
|
ds@1952
|
218 |
** |
|
ds@1952
|
219 |
** The returned value is normally a copy of the second argument to this |
|
ds@1952
|
220 |
** function. However, if a malloc() failure has occured since the previous |
|
ds@1952
|
221 |
** invocation SQLITE_NOMEM is returned instead. |
|
ds@1952
|
222 |
** |
|
ds@1952
|
223 |
** If the first argument, db, is not NULL and a malloc() error has occured, |
|
ds@1952
|
224 |
** then the connection error-code (the value returned by sqlite3_errcode()) |
|
ds@1952
|
225 |
** is set to SQLITE_NOMEM. |
|
ds@1952
|
226 |
*/ |
|
ds@1952
|
227 |
int sqlite3ApiExit(sqlite3* db, int rc){ |
|
ds@1952
|
228 |
/* If the db handle is not NULL, then we must hold the connection handle |
|
ds@1952
|
229 |
** mutex here. Otherwise the read (and possible write) of db->mallocFailed |
|
ds@1952
|
230 |
** is unsafe, as is the call to sqlite3Error(). |
|
ds@1952
|
231 |
*/ |
|
ds@1952
|
232 |
assert( !db || sqlite3_mutex_held(db->mutex) ); |
|
ds@1952
|
233 |
if( db && db->mallocFailed ){ |
|
ds@1952
|
234 |
sqlite3Error(db, SQLITE_NOMEM, 0); |
|
ds@1952
|
235 |
db->mallocFailed = 0; |
|
ds@1952
|
236 |
rc = SQLITE_NOMEM; |
|
ds@1952
|
237 |
} |
|
ds@1952
|
238 |
return rc & (db ? db->errMask : 0xff); |
|
ds@1952
|
239 |
} |
|
ds@1952
|
240 |
|