blob: 5916ec251ec96317efed0deb17bf239b54f66a27 [file] [log] [blame]
drha3152892007-05-05 11:48:52 +00001/*
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*************************************************************************
drhfec00ea2008-06-14 16:56:21 +000012**
drha3152892007-05-05 11:48:52 +000013** Memory allocation functions used throughout sqlite.
14**
drhfec00ea2008-06-14 16:56:21 +000015** $Id: malloc.c,v 1.16 2008/06/14 16:56:22 drh Exp $
drha3152892007-05-05 11:48:52 +000016*/
17#include "sqliteInt.h"
drha3152892007-05-05 11:48:52 +000018#include <stdarg.h>
19#include <ctype.h>
20
21/*
drhb21c8cd2007-08-21 19:33:56 +000022** This routine runs when the memory allocator sees that the
23** total memory allocation is about to exceed the soft heap
24** limit.
25*/
26static void softHeapLimitEnforcer(
27 void *NotUsed,
drh153c62c2007-08-24 03:51:33 +000028 sqlite3_int64 inUse,
29 int allocSize
drhb21c8cd2007-08-21 19:33:56 +000030){
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.
drha3152892007-05-05 11:48:52 +000037*/
38void sqlite3_soft_heap_limit(int n){
drhb21c8cd2007-08-21 19:33:56 +000039 sqlite3_uint64 iLimit;
40 int overage;
41 if( n<0 ){
42 iLimit = 0;
43 }else{
44 iLimit = n;
drha3152892007-05-05 11:48:52 +000045 }
drhb21c8cd2007-08-21 19:33:56 +000046 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 }
drha3152892007-05-05 11:48:52 +000055}
56
57/*
58** Release memory held by SQLite instances created by the current thread.
59*/
60int sqlite3_release_memory(int n){
drh86f8c192007-08-22 00:39:19 +000061#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
danielk1977dfb316d2008-03-26 18:34:43 +000062 int nRet = sqlite3VdbeReleaseMemory(n);
63 nRet += sqlite3PagerReleaseMemory(n-nRet);
64 return nRet;
danielk19771e536952007-08-16 10:09:01 +000065#else
66 return SQLITE_OK;
67#endif
drha3152892007-05-05 11:48:52 +000068}
drha3152892007-05-05 11:48:52 +000069
drhfec00ea2008-06-14 16:56:21 +000070/*
71** State information local to the memory allocation subsystem.
72*/
73static struct {
74 sqlite3_mutex *mutex; /* Mutex to serialize access */
75
76 /*
77 ** The alarm callback and its arguments. The mem0.mutex lock will
78 ** be held while the callback is running. Recursive calls into
79 ** the memory subsystem are allowed, but no new callbacks will be
80 ** issued. The alarmBusy variable is set to prevent recursive
81 ** callbacks.
82 */
83 sqlite3_int64 alarmThreshold;
84 void (*alarmCallback)(void*, sqlite3_int64,int);
85 void *alarmArg;
86 int alarmBusy;
87
88 /*
89 ** Performance statistics
90 */
91 sqlite3_int64 nowUsed; /* Main memory currently in use */
92 sqlite3_int64 mxUsed; /* Highwater mark for nowUsed */
93 int mxReq; /* maximum request size for main or page-cache mem */
94} mem0;
95
96/*
97** Initialize the memory allocation subsystem.
98*/
99int sqlite3MallocInit(void){
100 if( sqlite3Config.m.xMalloc==0 ){
101 sqlite3MemSetDefault();
102 }
103 memset(&mem0, 0, sizeof(mem0));
104 if( sqlite3Config.bMemstat && sqlite3Config.bCoreMutex ){
105 mem0.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM);
106 }
107 return sqlite3Config.m.xInit(sqlite3Config.m.pAppData);
108}
109
110/*
111** Deinitialize the memory allocation subsystem.
112*/
113void sqlite3MallocEnd(void){
114 sqlite3Config.m.xShutdown(sqlite3Config.m.pAppData);
115}
116
117/*
118** Return the amount of memory currently checked out.
119*/
120sqlite3_int64 sqlite3_memory_used(void){
121 sqlite3_int64 n;
122 sqlite3_mutex_enter(mem0.mutex);
123 n = mem0.nowUsed;
124 sqlite3_mutex_leave(mem0.mutex);
125 return n;
126}
127
128/*
129** Return the maximum amount of memory that has ever been
130** checked out since either the beginning of this process
131** or since the most recent reset.
132*/
133sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
134 sqlite3_int64 n;
135 sqlite3_mutex_enter(mem0.mutex);
136 n = mem0.mxUsed;
137 if( resetFlag ){
138 mem0.mxUsed = mem0.nowUsed;
139 }
140 sqlite3_mutex_leave(mem0.mutex);
141 return n;
142}
143
144/*
145** Change the alarm callback
146*/
147int sqlite3_memory_alarm(
148 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
149 void *pArg,
150 sqlite3_int64 iThreshold
151){
152 sqlite3_mutex_enter(mem0.mutex);
153 mem0.alarmCallback = xCallback;
154 mem0.alarmArg = pArg;
155 mem0.alarmThreshold = iThreshold;
156 sqlite3_mutex_leave(mem0.mutex);
157 return SQLITE_OK;
158}
159
160/*
161** Trigger the alarm
162*/
163static void sqlite3MallocAlarm(int nByte){
164 void (*xCallback)(void*,sqlite3_int64,int);
165 sqlite3_int64 nowUsed;
166 void *pArg;
167 if( mem0.alarmCallback==0 || mem0.alarmBusy ) return;
168 mem0.alarmBusy = 1;
169 xCallback = mem0.alarmCallback;
170 nowUsed = mem0.nowUsed;
171 pArg = mem0.alarmArg;
172 sqlite3_mutex_leave(mem0.mutex);
173 xCallback(pArg, nowUsed, nByte);
174 sqlite3_mutex_enter(mem0.mutex);
175 mem0.alarmBusy = 0;
176}
177
178
179/*
180** Allocate memory. This routine is like sqlite3_malloc() except that it
181** assumes the memory subsystem has already been initialized.
182*/
183void *sqlite3Malloc(int n){
184 void *p;
185 int nFull;
186 if( n<=0 ){
187 return 0;
188 }else if( sqlite3Config.bMemstat ){
189 nFull = sqlite3Config.m.xRoundup(n);
190 sqlite3_mutex_enter(mem0.mutex);
191 if( n>mem0.mxReq ) mem0.mxReq = n;
192 if( mem0.alarmCallback!=0 && mem0.nowUsed+nFull>=mem0.alarmThreshold ){
193 sqlite3MallocAlarm(nFull);
194 }
195 if( sqlite3FaultStep(SQLITE_FAULTINJECTOR_MALLOC) ){
196 p = 0;
197 }else{
198 p = sqlite3Config.m.xMalloc(nFull);
199 if( p==0 ){
200 sqlite3MallocAlarm(nFull);
201 p = malloc(nFull);
202 }
203 }
204 if( p ){
205 mem0.nowUsed += nFull;
206 if( mem0.nowUsed>mem0.mxUsed ){
207 mem0.mxUsed = mem0.nowUsed;
208 }
209 }
210 sqlite3_mutex_leave(mem0.mutex);
211 }else{
212 p = sqlite3Config.m.xMalloc(n);
213 }
214 return p;
215}
216
217/*
218** This version of the memory allocation is for use by the application.
219** First make sure the memory subsystem is initialized, then do the
220** allocation.
221*/
222void *sqlite3_malloc(int n){
223#ifndef SQLITE_OMIT_AUTOINIT
224 if( sqlite3_initialize() ) return 0;
225#endif
226 return sqlite3Malloc(n);
227}
228
229/*
230** Return the size of a memory allocation previously obtained from
231** sqlite3Malloc() or sqlite3_malloc().
232*/
233int sqlite3MallocSize(void *p){
234 return sqlite3Config.m.xSize(p);
235}
236
237/*
238** Free memory previously obtained from sqlite3Malloc().
239*/
240void sqlite3_free(void *p){
241 if( p==0 ) return;
242 if( sqlite3Config.bMemstat ){
243 sqlite3_mutex_enter(mem0.mutex);
244 mem0.nowUsed -= sqlite3MallocSize(p);
245 sqlite3Config.m.xFree(p);
246 sqlite3_mutex_leave(mem0.mutex);
247 }else{
248 sqlite3Config.m.xFree(p);
249 }
250}
251
252/*
253** Change the size of an existing memory allocation
254*/
255void *sqlite3Realloc(void *pOld, int nBytes){
256 int nOld, nNew;
257 void *pNew;
258 if( pOld==0 ){
259 return sqlite3Malloc(nBytes);
260 }
261 if( nBytes<=0 ){
262 sqlite3_free(pOld);
263 return 0;
264 }
265 nOld = sqlite3MallocSize(pOld);
266 if( sqlite3Config.bMemstat ){
267 sqlite3_mutex_enter(mem0.mutex);
268 if( nBytes>mem0.mxReq ) mem0.mxReq = nBytes;
269 nNew = sqlite3Config.m.xRoundup(nBytes);
270 if( nOld==nNew ){
271 pNew = pOld;
272 }else{
273 if( mem0.nowUsed+nNew-nOld>=mem0.alarmThreshold ){
274 sqlite3MallocAlarm(nNew-nOld);
275 }
276 if( sqlite3FaultStep(SQLITE_FAULTINJECTOR_MALLOC) ){
277 pNew = 0;
278 }else{
279 pNew = sqlite3Config.m.xRealloc(pOld, nNew);
280 if( pNew==0 ){
281 sqlite3MallocAlarm(nBytes);
282 pNew = sqlite3Config.m.xRealloc(pOld, nNew);
283 }
284 }
285 if( pNew ){
286 mem0.nowUsed += nNew-nOld;
287 if( mem0.nowUsed>mem0.mxUsed ){
288 mem0.mxUsed = mem0.nowUsed;
289 }
290 }
291 }
292 sqlite3_mutex_leave(mem0.mutex);
293 }else{
294 pNew = sqlite3Config.m.xRealloc(pOld, nBytes);
295 }
296 return pNew;
297}
298
299/*
300** The public interface to sqlite3Realloc. Make sure that the memory
301** subsystem is initialized prior to invoking sqliteRealloc.
302*/
303void *sqlite3_realloc(void *pOld, int n){
304#ifndef SQLITE_OMIT_AUTOINIT
305 if( sqlite3_initialize() ) return 0;
306#endif
307 return sqlite3Realloc(pOld, n);
308}
309
drha3152892007-05-05 11:48:52 +0000310
311/*
drh17435752007-08-16 04:30:38 +0000312** Allocate and zero memory.
drha3152892007-05-05 11:48:52 +0000313*/
drhfec00ea2008-06-14 16:56:21 +0000314void *sqlite3MallocZero(int n){
315 void *p = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000316 if( p ){
317 memset(p, 0, n);
318 }
319 return p;
320}
drh17435752007-08-16 04:30:38 +0000321
322/*
323** Allocate and zero memory. If the allocation fails, make
324** the mallocFailed flag in the connection pointer.
325*/
drhfec00ea2008-06-14 16:56:21 +0000326void *sqlite3DbMallocZero(sqlite3 *db, int n){
danielk1977a1644fd2007-08-29 12:31:25 +0000327 void *p = sqlite3DbMallocRaw(db, n);
drh17435752007-08-16 04:30:38 +0000328 if( p ){
329 memset(p, 0, n);
drh17435752007-08-16 04:30:38 +0000330 }
331 return p;
332}
333
334/*
335** Allocate and zero memory. If the allocation fails, make
336** the mallocFailed flag in the connection pointer.
337*/
drhfec00ea2008-06-14 16:56:21 +0000338void *sqlite3DbMallocRaw(sqlite3 *db, int n){
danielk1977a1644fd2007-08-29 12:31:25 +0000339 void *p = 0;
340 if( !db || db->mallocFailed==0 ){
drhfec00ea2008-06-14 16:56:21 +0000341 p = sqlite3Malloc(n);
danielk1977a1644fd2007-08-29 12:31:25 +0000342 if( !p && db ){
343 db->mallocFailed = 1;
344 }
drh17435752007-08-16 04:30:38 +0000345 }
346 return p;
347}
348
danielk197726783a52007-08-29 14:06:22 +0000349/*
350** Resize the block of memory pointed to by p to n bytes. If the
351** resize fails, set the mallocFailed flag inthe connection object.
352*/
danielk1977a1644fd2007-08-29 12:31:25 +0000353void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
354 void *pNew = 0;
355 if( db->mallocFailed==0 ){
356 pNew = sqlite3_realloc(p, n);
357 if( !pNew ){
358 db->mallocFailed = 1;
359 }
360 }
361 return pNew;
362}
363
drh17435752007-08-16 04:30:38 +0000364/*
365** Attempt to reallocate p. If the reallocation fails, then free p
366** and set the mallocFailed flag in the database connection.
367*/
368void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
drha3152892007-05-05 11:48:52 +0000369 void *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000370 pNew = sqlite3DbRealloc(db, p, n);
drha3152892007-05-05 11:48:52 +0000371 if( !pNew ){
danielk19771e536952007-08-16 10:09:01 +0000372 sqlite3_free(p);
drha3152892007-05-05 11:48:52 +0000373 }
374 return pNew;
375}
376
drha3152892007-05-05 11:48:52 +0000377/*
378** Make a copy of a string in memory obtained from sqliteMalloc(). These
379** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
380** is because when memory debugging is turned on, these two functions are
381** called via macros that record the current file and line number in the
382** ThreadData structure.
383*/
384char *sqlite3StrDup(const char *z){
385 char *zNew;
386 int n;
387 if( z==0 ) return 0;
388 n = strlen(z)+1;
danielk19771e536952007-08-16 10:09:01 +0000389 zNew = sqlite3_malloc(n);
drha3152892007-05-05 11:48:52 +0000390 if( zNew ) memcpy(zNew, z, n);
391 return zNew;
392}
393char *sqlite3StrNDup(const char *z, int n){
394 char *zNew;
395 if( z==0 ) return 0;
danielk19771e536952007-08-16 10:09:01 +0000396 zNew = sqlite3_malloc(n+1);
drha3152892007-05-05 11:48:52 +0000397 if( zNew ){
398 memcpy(zNew, z, n);
399 zNew[n] = 0;
400 }
401 return zNew;
402}
403
danielk19771e536952007-08-16 10:09:01 +0000404char *sqlite3DbStrDup(sqlite3 *db, const char *z){
405 char *zNew = sqlite3StrDup(z);
406 if( z && !zNew ){
407 db->mallocFailed = 1;
408 }
409 return zNew;
410}
411char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
412 char *zNew = sqlite3StrNDup(z, n);
413 if( z && !zNew ){
414 db->mallocFailed = 1;
415 }
416 return zNew;
417}
418
drha3152892007-05-05 11:48:52 +0000419/*
420** Create a string from the 2nd and subsequent arguments (up to the
421** first NULL argument), store the string in memory obtained from
422** sqliteMalloc() and make the pointer indicated by the 1st argument
423** point to that string. The 1st argument must either be NULL or
424** point to memory obtained from sqliteMalloc().
425*/
426void sqlite3SetString(char **pz, ...){
427 va_list ap;
428 int nByte;
429 const char *z;
430 char *zResult;
431
432 assert( pz!=0 );
433 nByte = 1;
434 va_start(ap, pz);
435 while( (z = va_arg(ap, const char*))!=0 ){
436 nByte += strlen(z);
437 }
438 va_end(ap);
danielk19771e536952007-08-16 10:09:01 +0000439 sqlite3_free(*pz);
440 *pz = zResult = sqlite3_malloc(nByte);
drha3152892007-05-05 11:48:52 +0000441 if( zResult==0 ){
442 return;
443 }
444 *zResult = 0;
445 va_start(ap, pz);
446 while( (z = va_arg(ap, const char*))!=0 ){
447 int n = strlen(z);
448 memcpy(zResult, z, n);
449 zResult += n;
450 }
451 zResult[0] = 0;
452 va_end(ap);
453}
454
455
456/*
457** This function must be called before exiting any API function (i.e.
drh17435752007-08-16 04:30:38 +0000458** returning control to the user) that has called sqlite3_malloc or
459** sqlite3_realloc.
drha3152892007-05-05 11:48:52 +0000460**
461** The returned value is normally a copy of the second argument to this
462** function. However, if a malloc() failure has occured since the previous
463** invocation SQLITE_NOMEM is returned instead.
464**
465** If the first argument, db, is not NULL and a malloc() error has occured,
466** then the connection error-code (the value returned by sqlite3_errcode())
467** is set to SQLITE_NOMEM.
468*/
drha3152892007-05-05 11:48:52 +0000469int sqlite3ApiExit(sqlite3* db, int rc){
danielk1977a1644fd2007-08-29 12:31:25 +0000470 /* If the db handle is not NULL, then we must hold the connection handle
471 ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
472 ** is unsafe, as is the call to sqlite3Error().
473 */
474 assert( !db || sqlite3_mutex_held(db->mutex) );
danielk19771e536952007-08-16 10:09:01 +0000475 if( db && db->mallocFailed ){
drha3152892007-05-05 11:48:52 +0000476 sqlite3Error(db, SQLITE_NOMEM, 0);
drh17435752007-08-16 04:30:38 +0000477 db->mallocFailed = 0;
drha3152892007-05-05 11:48:52 +0000478 rc = SQLITE_NOMEM;
479 }
480 return rc & (db ? db->errMask : 0xff);
481}