blob: 3c567c9c0da132c439b78ded0f557eb1c11e6581 [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**
drhe5ae5732008-06-15 02:51:47 +000015** $Id: malloc.c,v 1.17 2008/06/15 02:51:48 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 */
drhe5ae5732008-06-15 02:51:47 +000093 int mxReq; /* Max request size for ordinary mallocs */
94 int mxTempReq; /* Max request size for xTemp mallocs */
drhfec00ea2008-06-14 16:56:21 +000095} mem0;
96
97/*
98** Initialize the memory allocation subsystem.
99*/
100int sqlite3MallocInit(void){
101 if( sqlite3Config.m.xMalloc==0 ){
102 sqlite3MemSetDefault();
103 }
104 memset(&mem0, 0, sizeof(mem0));
105 if( sqlite3Config.bMemstat && sqlite3Config.bCoreMutex ){
106 mem0.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM);
107 }
108 return sqlite3Config.m.xInit(sqlite3Config.m.pAppData);
109}
110
111/*
112** Deinitialize the memory allocation subsystem.
113*/
114void sqlite3MallocEnd(void){
115 sqlite3Config.m.xShutdown(sqlite3Config.m.pAppData);
116}
117
118/*
119** Return the amount of memory currently checked out.
120*/
121sqlite3_int64 sqlite3_memory_used(void){
122 sqlite3_int64 n;
123 sqlite3_mutex_enter(mem0.mutex);
124 n = mem0.nowUsed;
125 sqlite3_mutex_leave(mem0.mutex);
126 return n;
127}
128
129/*
130** Return the maximum amount of memory that has ever been
131** checked out since either the beginning of this process
132** or since the most recent reset.
133*/
134sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
135 sqlite3_int64 n;
136 sqlite3_mutex_enter(mem0.mutex);
137 n = mem0.mxUsed;
138 if( resetFlag ){
139 mem0.mxUsed = mem0.nowUsed;
140 }
141 sqlite3_mutex_leave(mem0.mutex);
142 return n;
143}
144
145/*
146** Change the alarm callback
147*/
148int sqlite3_memory_alarm(
149 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
150 void *pArg,
151 sqlite3_int64 iThreshold
152){
153 sqlite3_mutex_enter(mem0.mutex);
154 mem0.alarmCallback = xCallback;
155 mem0.alarmArg = pArg;
156 mem0.alarmThreshold = iThreshold;
157 sqlite3_mutex_leave(mem0.mutex);
158 return SQLITE_OK;
159}
160
161/*
162** Trigger the alarm
163*/
164static void sqlite3MallocAlarm(int nByte){
165 void (*xCallback)(void*,sqlite3_int64,int);
166 sqlite3_int64 nowUsed;
167 void *pArg;
168 if( mem0.alarmCallback==0 || mem0.alarmBusy ) return;
169 mem0.alarmBusy = 1;
170 xCallback = mem0.alarmCallback;
171 nowUsed = mem0.nowUsed;
172 pArg = mem0.alarmArg;
173 sqlite3_mutex_leave(mem0.mutex);
174 xCallback(pArg, nowUsed, nByte);
175 sqlite3_mutex_enter(mem0.mutex);
176 mem0.alarmBusy = 0;
177}
178
179
180/*
181** Allocate memory. This routine is like sqlite3_malloc() except that it
182** assumes the memory subsystem has already been initialized.
183*/
184void *sqlite3Malloc(int n){
185 void *p;
186 int nFull;
187 if( n<=0 ){
188 return 0;
189 }else if( sqlite3Config.bMemstat ){
190 nFull = sqlite3Config.m.xRoundup(n);
191 sqlite3_mutex_enter(mem0.mutex);
192 if( n>mem0.mxReq ) mem0.mxReq = n;
193 if( mem0.alarmCallback!=0 && mem0.nowUsed+nFull>=mem0.alarmThreshold ){
194 sqlite3MallocAlarm(nFull);
195 }
196 if( sqlite3FaultStep(SQLITE_FAULTINJECTOR_MALLOC) ){
197 p = 0;
198 }else{
199 p = sqlite3Config.m.xMalloc(nFull);
200 if( p==0 ){
201 sqlite3MallocAlarm(nFull);
202 p = malloc(nFull);
203 }
204 }
205 if( p ){
206 mem0.nowUsed += nFull;
207 if( mem0.nowUsed>mem0.mxUsed ){
208 mem0.mxUsed = mem0.nowUsed;
209 }
210 }
211 sqlite3_mutex_leave(mem0.mutex);
212 }else{
213 p = sqlite3Config.m.xMalloc(n);
214 }
215 return p;
216}
217
218/*
219** This version of the memory allocation is for use by the application.
220** First make sure the memory subsystem is initialized, then do the
221** allocation.
222*/
223void *sqlite3_malloc(int n){
224#ifndef SQLITE_OMIT_AUTOINIT
225 if( sqlite3_initialize() ) return 0;
226#endif
227 return sqlite3Malloc(n);
228}
229
230/*
drhe5ae5732008-06-15 02:51:47 +0000231** Each thread may only have a single outstanding allocation from
232** xTempMalloc(). We verify this constraint in the single-threaded
233** case by setting tempAllocOut to 1 when an allocation
234** is outstanding clearing it when the allocation is freed.
235*/
236#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
237static int tempAllocOut = 0;
238#endif
239
240
241/*
242** Allocate memory that is to be used and released right away.
243** This routine is similar to alloca() in that it is not intended
244** for situations where the memory might be held long-term. This
245** routine is intended to get memory to old large transient data
246** structures that would not normally fit on the stack of an
247** embedded processor.
248*/
249void *sqlite3TempMalloc(int n){
250 void *p;
251 assert( n>0 );
252 if( sqlite3FaultStep(SQLITE_FAULTINJECTOR_MALLOC) ){
253 return 0;
254 }
255#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
256 assert( tempAllocOut==0 );
257 tempAllocOut = 1;
258#endif
259 if( sqlite3Config.bMemstat ){
260 sqlite3_mutex_enter(mem0.mutex);
261 if( n>mem0.mxTempReq ) mem0.mxTempReq = n;
262 p = sqlite3Config.m.xTempMalloc(n);
263 sqlite3_mutex_leave(mem0.mutex);
264 }else{
265 p = sqlite3Config.m.xTempMalloc(n);
266 }
267 return p;
268}
269void sqlite3TempFree(void *p){
270 if( p ){
271#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
272 assert( tempAllocOut==1 );
273 tempAllocOut = 0;
274#endif
275 sqlite3Config.m.xTempFree(p);
276 }
277}
278
279/*
drhfec00ea2008-06-14 16:56:21 +0000280** Return the size of a memory allocation previously obtained from
281** sqlite3Malloc() or sqlite3_malloc().
282*/
283int sqlite3MallocSize(void *p){
284 return sqlite3Config.m.xSize(p);
285}
286
287/*
288** Free memory previously obtained from sqlite3Malloc().
289*/
290void sqlite3_free(void *p){
291 if( p==0 ) return;
292 if( sqlite3Config.bMemstat ){
293 sqlite3_mutex_enter(mem0.mutex);
294 mem0.nowUsed -= sqlite3MallocSize(p);
295 sqlite3Config.m.xFree(p);
296 sqlite3_mutex_leave(mem0.mutex);
297 }else{
298 sqlite3Config.m.xFree(p);
299 }
300}
301
302/*
303** Change the size of an existing memory allocation
304*/
305void *sqlite3Realloc(void *pOld, int nBytes){
306 int nOld, nNew;
307 void *pNew;
308 if( pOld==0 ){
309 return sqlite3Malloc(nBytes);
310 }
311 if( nBytes<=0 ){
312 sqlite3_free(pOld);
313 return 0;
314 }
315 nOld = sqlite3MallocSize(pOld);
316 if( sqlite3Config.bMemstat ){
317 sqlite3_mutex_enter(mem0.mutex);
318 if( nBytes>mem0.mxReq ) mem0.mxReq = nBytes;
319 nNew = sqlite3Config.m.xRoundup(nBytes);
320 if( nOld==nNew ){
321 pNew = pOld;
322 }else{
323 if( mem0.nowUsed+nNew-nOld>=mem0.alarmThreshold ){
324 sqlite3MallocAlarm(nNew-nOld);
325 }
326 if( sqlite3FaultStep(SQLITE_FAULTINJECTOR_MALLOC) ){
327 pNew = 0;
328 }else{
329 pNew = sqlite3Config.m.xRealloc(pOld, nNew);
330 if( pNew==0 ){
331 sqlite3MallocAlarm(nBytes);
332 pNew = sqlite3Config.m.xRealloc(pOld, nNew);
333 }
334 }
335 if( pNew ){
336 mem0.nowUsed += nNew-nOld;
337 if( mem0.nowUsed>mem0.mxUsed ){
338 mem0.mxUsed = mem0.nowUsed;
339 }
340 }
341 }
342 sqlite3_mutex_leave(mem0.mutex);
343 }else{
344 pNew = sqlite3Config.m.xRealloc(pOld, nBytes);
345 }
346 return pNew;
347}
348
349/*
350** The public interface to sqlite3Realloc. Make sure that the memory
351** subsystem is initialized prior to invoking sqliteRealloc.
352*/
353void *sqlite3_realloc(void *pOld, int n){
354#ifndef SQLITE_OMIT_AUTOINIT
355 if( sqlite3_initialize() ) return 0;
356#endif
357 return sqlite3Realloc(pOld, n);
358}
359
drha3152892007-05-05 11:48:52 +0000360
361/*
drh17435752007-08-16 04:30:38 +0000362** Allocate and zero memory.
drha3152892007-05-05 11:48:52 +0000363*/
drhfec00ea2008-06-14 16:56:21 +0000364void *sqlite3MallocZero(int n){
365 void *p = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000366 if( p ){
367 memset(p, 0, n);
368 }
369 return p;
370}
drh17435752007-08-16 04:30:38 +0000371
372/*
373** Allocate and zero memory. If the allocation fails, make
374** the mallocFailed flag in the connection pointer.
375*/
drhfec00ea2008-06-14 16:56:21 +0000376void *sqlite3DbMallocZero(sqlite3 *db, int n){
danielk1977a1644fd2007-08-29 12:31:25 +0000377 void *p = sqlite3DbMallocRaw(db, n);
drh17435752007-08-16 04:30:38 +0000378 if( p ){
379 memset(p, 0, n);
drh17435752007-08-16 04:30:38 +0000380 }
381 return p;
382}
383
384/*
385** Allocate and zero memory. If the allocation fails, make
386** the mallocFailed flag in the connection pointer.
387*/
drhfec00ea2008-06-14 16:56:21 +0000388void *sqlite3DbMallocRaw(sqlite3 *db, int n){
danielk1977a1644fd2007-08-29 12:31:25 +0000389 void *p = 0;
390 if( !db || db->mallocFailed==0 ){
drhfec00ea2008-06-14 16:56:21 +0000391 p = sqlite3Malloc(n);
danielk1977a1644fd2007-08-29 12:31:25 +0000392 if( !p && db ){
393 db->mallocFailed = 1;
394 }
drh17435752007-08-16 04:30:38 +0000395 }
396 return p;
397}
398
danielk197726783a52007-08-29 14:06:22 +0000399/*
400** Resize the block of memory pointed to by p to n bytes. If the
401** resize fails, set the mallocFailed flag inthe connection object.
402*/
danielk1977a1644fd2007-08-29 12:31:25 +0000403void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
404 void *pNew = 0;
405 if( db->mallocFailed==0 ){
406 pNew = sqlite3_realloc(p, n);
407 if( !pNew ){
408 db->mallocFailed = 1;
409 }
410 }
411 return pNew;
412}
413
drh17435752007-08-16 04:30:38 +0000414/*
415** Attempt to reallocate p. If the reallocation fails, then free p
416** and set the mallocFailed flag in the database connection.
417*/
418void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
drha3152892007-05-05 11:48:52 +0000419 void *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000420 pNew = sqlite3DbRealloc(db, p, n);
drha3152892007-05-05 11:48:52 +0000421 if( !pNew ){
danielk19771e536952007-08-16 10:09:01 +0000422 sqlite3_free(p);
drha3152892007-05-05 11:48:52 +0000423 }
424 return pNew;
425}
426
drha3152892007-05-05 11:48:52 +0000427/*
428** Make a copy of a string in memory obtained from sqliteMalloc(). These
429** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
430** is because when memory debugging is turned on, these two functions are
431** called via macros that record the current file and line number in the
432** ThreadData structure.
433*/
434char *sqlite3StrDup(const char *z){
435 char *zNew;
436 int n;
437 if( z==0 ) return 0;
438 n = strlen(z)+1;
drhe5ae5732008-06-15 02:51:47 +0000439 zNew = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000440 if( zNew ) memcpy(zNew, z, n);
441 return zNew;
442}
443char *sqlite3StrNDup(const char *z, int n){
444 char *zNew;
445 if( z==0 ) return 0;
drhe5ae5732008-06-15 02:51:47 +0000446 zNew = sqlite3Malloc(n+1);
drha3152892007-05-05 11:48:52 +0000447 if( zNew ){
448 memcpy(zNew, z, n);
449 zNew[n] = 0;
450 }
451 return zNew;
452}
453
danielk19771e536952007-08-16 10:09:01 +0000454char *sqlite3DbStrDup(sqlite3 *db, const char *z){
455 char *zNew = sqlite3StrDup(z);
456 if( z && !zNew ){
457 db->mallocFailed = 1;
458 }
459 return zNew;
460}
461char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
462 char *zNew = sqlite3StrNDup(z, n);
463 if( z && !zNew ){
464 db->mallocFailed = 1;
465 }
466 return zNew;
467}
468
drha3152892007-05-05 11:48:52 +0000469/*
470** Create a string from the 2nd and subsequent arguments (up to the
471** first NULL argument), store the string in memory obtained from
472** sqliteMalloc() and make the pointer indicated by the 1st argument
473** point to that string. The 1st argument must either be NULL or
474** point to memory obtained from sqliteMalloc().
475*/
476void sqlite3SetString(char **pz, ...){
477 va_list ap;
478 int nByte;
479 const char *z;
480 char *zResult;
481
482 assert( pz!=0 );
483 nByte = 1;
484 va_start(ap, pz);
485 while( (z = va_arg(ap, const char*))!=0 ){
486 nByte += strlen(z);
487 }
488 va_end(ap);
danielk19771e536952007-08-16 10:09:01 +0000489 sqlite3_free(*pz);
drhe5ae5732008-06-15 02:51:47 +0000490 *pz = zResult = sqlite3Malloc(nByte);
drha3152892007-05-05 11:48:52 +0000491 if( zResult==0 ){
492 return;
493 }
494 *zResult = 0;
495 va_start(ap, pz);
496 while( (z = va_arg(ap, const char*))!=0 ){
497 int n = strlen(z);
498 memcpy(zResult, z, n);
499 zResult += n;
500 }
501 zResult[0] = 0;
502 va_end(ap);
503}
504
505
506/*
507** This function must be called before exiting any API function (i.e.
drh17435752007-08-16 04:30:38 +0000508** returning control to the user) that has called sqlite3_malloc or
509** sqlite3_realloc.
drha3152892007-05-05 11:48:52 +0000510**
511** The returned value is normally a copy of the second argument to this
512** function. However, if a malloc() failure has occured since the previous
513** invocation SQLITE_NOMEM is returned instead.
514**
515** If the first argument, db, is not NULL and a malloc() error has occured,
516** then the connection error-code (the value returned by sqlite3_errcode())
517** is set to SQLITE_NOMEM.
518*/
drha3152892007-05-05 11:48:52 +0000519int sqlite3ApiExit(sqlite3* db, int rc){
danielk1977a1644fd2007-08-29 12:31:25 +0000520 /* If the db handle is not NULL, then we must hold the connection handle
521 ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
522 ** is unsafe, as is the call to sqlite3Error().
523 */
524 assert( !db || sqlite3_mutex_held(db->mutex) );
danielk19771e536952007-08-16 10:09:01 +0000525 if( db && db->mallocFailed ){
drha3152892007-05-05 11:48:52 +0000526 sqlite3Error(db, SQLITE_NOMEM, 0);
drh17435752007-08-16 04:30:38 +0000527 db->mallocFailed = 0;
drha3152892007-05-05 11:48:52 +0000528 rc = SQLITE_NOMEM;
529 }
530 return rc & (db ? db->errMask : 0xff);
531}