blob: cdfd6a9eae225a45df47e0f09a90324e50fca141 [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**
danielk197759f8c082008-06-18 17:09:10 +000015** $Id: malloc.c,v 1.19 2008/06/18 17:09:10 danielk1977 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 */
drhfacf0302008-06-17 15:12:00 +000094 int mxScratchReq; /* 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));
danielk197759f8c082008-06-18 17:09:10 +0000105 if( sqlite3Config.bMemstat ){
106 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
drhfec00ea2008-06-14 16:56:21 +0000107 }
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
drhfacf0302008-06-17 15:12:00 +0000232** xScratchMalloc(). We verify this constraint in the single-threaded
233** case by setting scratchAllocOut to 1 when an allocation
drhe5ae5732008-06-15 02:51:47 +0000234** is outstanding clearing it when the allocation is freed.
235*/
236#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhfacf0302008-06-17 15:12:00 +0000237static int scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000238#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*/
drhfacf0302008-06-17 15:12:00 +0000249void *sqlite3ScratchMalloc(int n){
drhe5ae5732008-06-15 02:51:47 +0000250 void *p;
251 assert( n>0 );
252 if( sqlite3FaultStep(SQLITE_FAULTINJECTOR_MALLOC) ){
253 return 0;
254 }
255#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhfacf0302008-06-17 15:12:00 +0000256 assert( scratchAllocOut==0 );
257 scratchAllocOut = 1;
drhe5ae5732008-06-15 02:51:47 +0000258#endif
259 if( sqlite3Config.bMemstat ){
260 sqlite3_mutex_enter(mem0.mutex);
drhfacf0302008-06-17 15:12:00 +0000261 if( n>mem0.mxScratchReq ) mem0.mxScratchReq = n;
262 p = sqlite3Config.m.xMalloc(n);
drhe5ae5732008-06-15 02:51:47 +0000263 sqlite3_mutex_leave(mem0.mutex);
264 }else{
drhfacf0302008-06-17 15:12:00 +0000265 p = sqlite3Config.m.xMalloc(n);
drhe5ae5732008-06-15 02:51:47 +0000266 }
267 return p;
268}
drhfacf0302008-06-17 15:12:00 +0000269void sqlite3ScratchFree(void *p){
drhe5ae5732008-06-15 02:51:47 +0000270 if( p ){
271#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhfacf0302008-06-17 15:12:00 +0000272 assert( scratchAllocOut==1 );
273 scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000274#endif
drhfacf0302008-06-17 15:12:00 +0000275 sqlite3Config.m.xFree(p);
drhe5ae5732008-06-15 02:51:47 +0000276 }
277}
278
279/*
drhfacf0302008-06-17 15:12:00 +0000280** Place holders for the page-cache memory allocator.
281*/
282void *sqlite3PageMalloc(int iSize){
283 return sqlite3Malloc(iSize);
284}
285void sqlite3PageFree(void *pOld){
286 sqlite3_free(pOld);
287}
288
289/*
drhfec00ea2008-06-14 16:56:21 +0000290** Return the size of a memory allocation previously obtained from
291** sqlite3Malloc() or sqlite3_malloc().
292*/
293int sqlite3MallocSize(void *p){
294 return sqlite3Config.m.xSize(p);
295}
296
297/*
298** Free memory previously obtained from sqlite3Malloc().
299*/
300void sqlite3_free(void *p){
301 if( p==0 ) return;
302 if( sqlite3Config.bMemstat ){
303 sqlite3_mutex_enter(mem0.mutex);
304 mem0.nowUsed -= sqlite3MallocSize(p);
305 sqlite3Config.m.xFree(p);
306 sqlite3_mutex_leave(mem0.mutex);
307 }else{
308 sqlite3Config.m.xFree(p);
309 }
310}
311
312/*
313** Change the size of an existing memory allocation
314*/
315void *sqlite3Realloc(void *pOld, int nBytes){
316 int nOld, nNew;
317 void *pNew;
318 if( pOld==0 ){
319 return sqlite3Malloc(nBytes);
320 }
321 if( nBytes<=0 ){
322 sqlite3_free(pOld);
323 return 0;
324 }
325 nOld = sqlite3MallocSize(pOld);
326 if( sqlite3Config.bMemstat ){
327 sqlite3_mutex_enter(mem0.mutex);
328 if( nBytes>mem0.mxReq ) mem0.mxReq = nBytes;
329 nNew = sqlite3Config.m.xRoundup(nBytes);
330 if( nOld==nNew ){
331 pNew = pOld;
332 }else{
333 if( mem0.nowUsed+nNew-nOld>=mem0.alarmThreshold ){
334 sqlite3MallocAlarm(nNew-nOld);
335 }
336 if( sqlite3FaultStep(SQLITE_FAULTINJECTOR_MALLOC) ){
337 pNew = 0;
338 }else{
339 pNew = sqlite3Config.m.xRealloc(pOld, nNew);
340 if( pNew==0 ){
341 sqlite3MallocAlarm(nBytes);
342 pNew = sqlite3Config.m.xRealloc(pOld, nNew);
343 }
344 }
345 if( pNew ){
346 mem0.nowUsed += nNew-nOld;
347 if( mem0.nowUsed>mem0.mxUsed ){
348 mem0.mxUsed = mem0.nowUsed;
349 }
350 }
351 }
352 sqlite3_mutex_leave(mem0.mutex);
353 }else{
354 pNew = sqlite3Config.m.xRealloc(pOld, nBytes);
355 }
356 return pNew;
357}
358
359/*
360** The public interface to sqlite3Realloc. Make sure that the memory
361** subsystem is initialized prior to invoking sqliteRealloc.
362*/
363void *sqlite3_realloc(void *pOld, int n){
364#ifndef SQLITE_OMIT_AUTOINIT
365 if( sqlite3_initialize() ) return 0;
366#endif
367 return sqlite3Realloc(pOld, n);
368}
369
drha3152892007-05-05 11:48:52 +0000370
371/*
drh17435752007-08-16 04:30:38 +0000372** Allocate and zero memory.
drha3152892007-05-05 11:48:52 +0000373*/
drhfec00ea2008-06-14 16:56:21 +0000374void *sqlite3MallocZero(int n){
375 void *p = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000376 if( p ){
377 memset(p, 0, n);
378 }
379 return p;
380}
drh17435752007-08-16 04:30:38 +0000381
382/*
383** Allocate and zero memory. If the allocation fails, make
384** the mallocFailed flag in the connection pointer.
385*/
drhfec00ea2008-06-14 16:56:21 +0000386void *sqlite3DbMallocZero(sqlite3 *db, int n){
danielk1977a1644fd2007-08-29 12:31:25 +0000387 void *p = sqlite3DbMallocRaw(db, n);
drh17435752007-08-16 04:30:38 +0000388 if( p ){
389 memset(p, 0, n);
drh17435752007-08-16 04:30:38 +0000390 }
391 return p;
392}
393
394/*
395** Allocate and zero memory. If the allocation fails, make
396** the mallocFailed flag in the connection pointer.
397*/
drhfec00ea2008-06-14 16:56:21 +0000398void *sqlite3DbMallocRaw(sqlite3 *db, int n){
danielk1977a1644fd2007-08-29 12:31:25 +0000399 void *p = 0;
400 if( !db || db->mallocFailed==0 ){
drhfec00ea2008-06-14 16:56:21 +0000401 p = sqlite3Malloc(n);
danielk1977a1644fd2007-08-29 12:31:25 +0000402 if( !p && db ){
403 db->mallocFailed = 1;
404 }
drh17435752007-08-16 04:30:38 +0000405 }
406 return p;
407}
408
danielk197726783a52007-08-29 14:06:22 +0000409/*
410** Resize the block of memory pointed to by p to n bytes. If the
411** resize fails, set the mallocFailed flag inthe connection object.
412*/
danielk1977a1644fd2007-08-29 12:31:25 +0000413void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
414 void *pNew = 0;
415 if( db->mallocFailed==0 ){
416 pNew = sqlite3_realloc(p, n);
417 if( !pNew ){
418 db->mallocFailed = 1;
419 }
420 }
421 return pNew;
422}
423
drh17435752007-08-16 04:30:38 +0000424/*
425** Attempt to reallocate p. If the reallocation fails, then free p
426** and set the mallocFailed flag in the database connection.
427*/
428void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
drha3152892007-05-05 11:48:52 +0000429 void *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000430 pNew = sqlite3DbRealloc(db, p, n);
drha3152892007-05-05 11:48:52 +0000431 if( !pNew ){
danielk19771e536952007-08-16 10:09:01 +0000432 sqlite3_free(p);
drha3152892007-05-05 11:48:52 +0000433 }
434 return pNew;
435}
436
drha3152892007-05-05 11:48:52 +0000437/*
438** Make a copy of a string in memory obtained from sqliteMalloc(). These
439** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
440** is because when memory debugging is turned on, these two functions are
441** called via macros that record the current file and line number in the
442** ThreadData structure.
443*/
444char *sqlite3StrDup(const char *z){
445 char *zNew;
446 int n;
447 if( z==0 ) return 0;
448 n = strlen(z)+1;
drhe5ae5732008-06-15 02:51:47 +0000449 zNew = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000450 if( zNew ) memcpy(zNew, z, n);
451 return zNew;
452}
453char *sqlite3StrNDup(const char *z, int n){
454 char *zNew;
455 if( z==0 ) return 0;
drhe5ae5732008-06-15 02:51:47 +0000456 zNew = sqlite3Malloc(n+1);
drha3152892007-05-05 11:48:52 +0000457 if( zNew ){
458 memcpy(zNew, z, n);
459 zNew[n] = 0;
460 }
461 return zNew;
462}
463
danielk19771e536952007-08-16 10:09:01 +0000464char *sqlite3DbStrDup(sqlite3 *db, const char *z){
465 char *zNew = sqlite3StrDup(z);
466 if( z && !zNew ){
467 db->mallocFailed = 1;
468 }
469 return zNew;
470}
471char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
472 char *zNew = sqlite3StrNDup(z, n);
473 if( z && !zNew ){
474 db->mallocFailed = 1;
475 }
476 return zNew;
477}
478
drha3152892007-05-05 11:48:52 +0000479/*
480** Create a string from the 2nd and subsequent arguments (up to the
481** first NULL argument), store the string in memory obtained from
482** sqliteMalloc() and make the pointer indicated by the 1st argument
483** point to that string. The 1st argument must either be NULL or
484** point to memory obtained from sqliteMalloc().
485*/
486void sqlite3SetString(char **pz, ...){
487 va_list ap;
488 int nByte;
489 const char *z;
490 char *zResult;
491
492 assert( pz!=0 );
493 nByte = 1;
494 va_start(ap, pz);
495 while( (z = va_arg(ap, const char*))!=0 ){
496 nByte += strlen(z);
497 }
498 va_end(ap);
danielk19771e536952007-08-16 10:09:01 +0000499 sqlite3_free(*pz);
drhe5ae5732008-06-15 02:51:47 +0000500 *pz = zResult = sqlite3Malloc(nByte);
drha3152892007-05-05 11:48:52 +0000501 if( zResult==0 ){
502 return;
503 }
504 *zResult = 0;
505 va_start(ap, pz);
506 while( (z = va_arg(ap, const char*))!=0 ){
507 int n = strlen(z);
508 memcpy(zResult, z, n);
509 zResult += n;
510 }
511 zResult[0] = 0;
512 va_end(ap);
513}
514
515
516/*
517** This function must be called before exiting any API function (i.e.
drh17435752007-08-16 04:30:38 +0000518** returning control to the user) that has called sqlite3_malloc or
519** sqlite3_realloc.
drha3152892007-05-05 11:48:52 +0000520**
521** The returned value is normally a copy of the second argument to this
522** function. However, if a malloc() failure has occured since the previous
523** invocation SQLITE_NOMEM is returned instead.
524**
525** If the first argument, db, is not NULL and a malloc() error has occured,
526** then the connection error-code (the value returned by sqlite3_errcode())
527** is set to SQLITE_NOMEM.
528*/
drha3152892007-05-05 11:48:52 +0000529int sqlite3ApiExit(sqlite3* db, int rc){
danielk1977a1644fd2007-08-29 12:31:25 +0000530 /* If the db handle is not NULL, then we must hold the connection handle
531 ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
532 ** is unsafe, as is the call to sqlite3Error().
533 */
534 assert( !db || sqlite3_mutex_held(db->mutex) );
danielk19771e536952007-08-16 10:09:01 +0000535 if( db && db->mallocFailed ){
drha3152892007-05-05 11:48:52 +0000536 sqlite3Error(db, SQLITE_NOMEM, 0);
drh17435752007-08-16 04:30:38 +0000537 db->mallocFailed = 0;
drha3152892007-05-05 11:48:52 +0000538 rc = SQLITE_NOMEM;
539 }
540 return rc & (db ? db->errMask : 0xff);
541}