xref: /sqlite-3.40.0/src/malloc.c (revision e5ae5735)
1a3152895Sdrh /*
2a3152895Sdrh ** 2001 September 15
3a3152895Sdrh **
4a3152895Sdrh ** The author disclaims copyright to this source code.  In place of
5a3152895Sdrh ** a legal notice, here is a blessing:
6a3152895Sdrh **
7a3152895Sdrh **    May you do good and not evil.
8a3152895Sdrh **    May you find forgiveness for yourself and forgive others.
9a3152895Sdrh **    May you share freely, never taking more than you give.
10a3152895Sdrh **
11a3152895Sdrh *************************************************************************
12fec00eabSdrh **
13a3152895Sdrh ** Memory allocation functions used throughout sqlite.
14a3152895Sdrh **
15*e5ae5735Sdrh ** $Id: malloc.c,v 1.17 2008/06/15 02:51:48 drh Exp $
16a3152895Sdrh */
17a3152895Sdrh #include "sqliteInt.h"
18a3152895Sdrh #include <stdarg.h>
19a3152895Sdrh #include <ctype.h>
20a3152895Sdrh 
21a3152895Sdrh /*
22b21c8cd4Sdrh ** This routine runs when the memory allocator sees that the
23b21c8cd4Sdrh ** total memory allocation is about to exceed the soft heap
24b21c8cd4Sdrh ** limit.
25b21c8cd4Sdrh */
26b21c8cd4Sdrh static void softHeapLimitEnforcer(
27b21c8cd4Sdrh   void *NotUsed,
28153c62c4Sdrh   sqlite3_int64 inUse,
29153c62c4Sdrh   int allocSize
30b21c8cd4Sdrh ){
31b21c8cd4Sdrh   sqlite3_release_memory(allocSize);
32b21c8cd4Sdrh }
33b21c8cd4Sdrh 
34b21c8cd4Sdrh /*
35b21c8cd4Sdrh ** Set the soft heap-size limit for the current thread. Passing a
36b21c8cd4Sdrh ** zero or negative value indicates no limit.
37a3152895Sdrh */
38a3152895Sdrh void sqlite3_soft_heap_limit(int n){
39b21c8cd4Sdrh   sqlite3_uint64 iLimit;
40b21c8cd4Sdrh   int overage;
41b21c8cd4Sdrh   if( n<0 ){
42b21c8cd4Sdrh     iLimit = 0;
43b21c8cd4Sdrh   }else{
44b21c8cd4Sdrh     iLimit = n;
45a3152895Sdrh   }
46b21c8cd4Sdrh   if( iLimit>0 ){
47b21c8cd4Sdrh     sqlite3_memory_alarm(softHeapLimitEnforcer, 0, iLimit);
48b21c8cd4Sdrh   }else{
49b21c8cd4Sdrh     sqlite3_memory_alarm(0, 0, 0);
50b21c8cd4Sdrh   }
51b21c8cd4Sdrh   overage = sqlite3_memory_used() - n;
52b21c8cd4Sdrh   if( overage>0 ){
53b21c8cd4Sdrh     sqlite3_release_memory(overage);
54b21c8cd4Sdrh   }
55a3152895Sdrh }
56a3152895Sdrh 
57a3152895Sdrh /*
58a3152895Sdrh ** Release memory held by SQLite instances created by the current thread.
59a3152895Sdrh */
60a3152895Sdrh int sqlite3_release_memory(int n){
6186f8c197Sdrh #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
62dfb316d4Sdanielk1977   int nRet = sqlite3VdbeReleaseMemory(n);
63dfb316d4Sdanielk1977   nRet += sqlite3PagerReleaseMemory(n-nRet);
64dfb316d4Sdanielk1977   return nRet;
651e536953Sdanielk1977 #else
661e536953Sdanielk1977   return SQLITE_OK;
671e536953Sdanielk1977 #endif
68a3152895Sdrh }
69a3152895Sdrh 
70fec00eabSdrh /*
71fec00eabSdrh ** State information local to the memory allocation subsystem.
72fec00eabSdrh */
73fec00eabSdrh static struct {
74fec00eabSdrh   sqlite3_mutex *mutex;         /* Mutex to serialize access */
75fec00eabSdrh 
76fec00eabSdrh   /*
77fec00eabSdrh   ** The alarm callback and its arguments.  The mem0.mutex lock will
78fec00eabSdrh   ** be held while the callback is running.  Recursive calls into
79fec00eabSdrh   ** the memory subsystem are allowed, but no new callbacks will be
80fec00eabSdrh   ** issued.  The alarmBusy variable is set to prevent recursive
81fec00eabSdrh   ** callbacks.
82fec00eabSdrh   */
83fec00eabSdrh   sqlite3_int64 alarmThreshold;
84fec00eabSdrh   void (*alarmCallback)(void*, sqlite3_int64,int);
85fec00eabSdrh   void *alarmArg;
86fec00eabSdrh   int alarmBusy;
87fec00eabSdrh 
88fec00eabSdrh   /*
89fec00eabSdrh   ** Performance statistics
90fec00eabSdrh   */
91fec00eabSdrh   sqlite3_int64 nowUsed;  /* Main memory currently in use */
92fec00eabSdrh   sqlite3_int64 mxUsed;   /* Highwater mark for nowUsed */
93*e5ae5735Sdrh   int mxReq;              /* Max request size for ordinary mallocs */
94*e5ae5735Sdrh   int mxTempReq;          /* Max request size for xTemp mallocs */
95fec00eabSdrh } mem0;
96fec00eabSdrh 
97fec00eabSdrh /*
98fec00eabSdrh ** Initialize the memory allocation subsystem.
99fec00eabSdrh */
100fec00eabSdrh int sqlite3MallocInit(void){
101fec00eabSdrh   if( sqlite3Config.m.xMalloc==0 ){
102fec00eabSdrh     sqlite3MemSetDefault();
103fec00eabSdrh   }
104fec00eabSdrh   memset(&mem0, 0, sizeof(mem0));
105fec00eabSdrh   if( sqlite3Config.bMemstat && sqlite3Config.bCoreMutex ){
106fec00eabSdrh     mem0.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM);
107fec00eabSdrh   }
108fec00eabSdrh   return sqlite3Config.m.xInit(sqlite3Config.m.pAppData);
109fec00eabSdrh }
110fec00eabSdrh 
111fec00eabSdrh /*
112fec00eabSdrh ** Deinitialize the memory allocation subsystem.
113fec00eabSdrh */
114fec00eabSdrh void sqlite3MallocEnd(void){
115fec00eabSdrh    sqlite3Config.m.xShutdown(sqlite3Config.m.pAppData);
116fec00eabSdrh }
117fec00eabSdrh 
118fec00eabSdrh /*
119fec00eabSdrh ** Return the amount of memory currently checked out.
120fec00eabSdrh */
121fec00eabSdrh sqlite3_int64 sqlite3_memory_used(void){
122fec00eabSdrh   sqlite3_int64 n;
123fec00eabSdrh   sqlite3_mutex_enter(mem0.mutex);
124fec00eabSdrh   n = mem0.nowUsed;
125fec00eabSdrh   sqlite3_mutex_leave(mem0.mutex);
126fec00eabSdrh   return n;
127fec00eabSdrh }
128fec00eabSdrh 
129fec00eabSdrh /*
130fec00eabSdrh ** Return the maximum amount of memory that has ever been
131fec00eabSdrh ** checked out since either the beginning of this process
132fec00eabSdrh ** or since the most recent reset.
133fec00eabSdrh */
134fec00eabSdrh sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
135fec00eabSdrh   sqlite3_int64 n;
136fec00eabSdrh   sqlite3_mutex_enter(mem0.mutex);
137fec00eabSdrh   n = mem0.mxUsed;
138fec00eabSdrh   if( resetFlag ){
139fec00eabSdrh     mem0.mxUsed = mem0.nowUsed;
140fec00eabSdrh   }
141fec00eabSdrh   sqlite3_mutex_leave(mem0.mutex);
142fec00eabSdrh   return n;
143fec00eabSdrh }
144fec00eabSdrh 
145fec00eabSdrh /*
146fec00eabSdrh ** Change the alarm callback
147fec00eabSdrh */
148fec00eabSdrh int sqlite3_memory_alarm(
149fec00eabSdrh   void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
150fec00eabSdrh   void *pArg,
151fec00eabSdrh   sqlite3_int64 iThreshold
152fec00eabSdrh ){
153fec00eabSdrh   sqlite3_mutex_enter(mem0.mutex);
154fec00eabSdrh   mem0.alarmCallback = xCallback;
155fec00eabSdrh   mem0.alarmArg = pArg;
156fec00eabSdrh   mem0.alarmThreshold = iThreshold;
157fec00eabSdrh   sqlite3_mutex_leave(mem0.mutex);
158fec00eabSdrh   return SQLITE_OK;
159fec00eabSdrh }
160fec00eabSdrh 
161fec00eabSdrh /*
162fec00eabSdrh ** Trigger the alarm
163fec00eabSdrh */
164fec00eabSdrh static void sqlite3MallocAlarm(int nByte){
165fec00eabSdrh   void (*xCallback)(void*,sqlite3_int64,int);
166fec00eabSdrh   sqlite3_int64 nowUsed;
167fec00eabSdrh   void *pArg;
168fec00eabSdrh   if( mem0.alarmCallback==0 || mem0.alarmBusy  ) return;
169fec00eabSdrh   mem0.alarmBusy = 1;
170fec00eabSdrh   xCallback = mem0.alarmCallback;
171fec00eabSdrh   nowUsed = mem0.nowUsed;
172fec00eabSdrh   pArg = mem0.alarmArg;
173fec00eabSdrh   sqlite3_mutex_leave(mem0.mutex);
174fec00eabSdrh   xCallback(pArg, nowUsed, nByte);
175fec00eabSdrh   sqlite3_mutex_enter(mem0.mutex);
176fec00eabSdrh   mem0.alarmBusy = 0;
177fec00eabSdrh }
178fec00eabSdrh 
179fec00eabSdrh 
180fec00eabSdrh /*
181fec00eabSdrh ** Allocate memory.  This routine is like sqlite3_malloc() except that it
182fec00eabSdrh ** assumes the memory subsystem has already been initialized.
183fec00eabSdrh */
184fec00eabSdrh void *sqlite3Malloc(int n){
185fec00eabSdrh   void *p;
186fec00eabSdrh   int nFull;
187fec00eabSdrh   if( n<=0 ){
188fec00eabSdrh     return 0;
189fec00eabSdrh   }else if( sqlite3Config.bMemstat ){
190fec00eabSdrh     nFull = sqlite3Config.m.xRoundup(n);
191fec00eabSdrh     sqlite3_mutex_enter(mem0.mutex);
192fec00eabSdrh     if( n>mem0.mxReq ) mem0.mxReq = n;
193fec00eabSdrh     if( mem0.alarmCallback!=0 && mem0.nowUsed+nFull>=mem0.alarmThreshold ){
194fec00eabSdrh       sqlite3MallocAlarm(nFull);
195fec00eabSdrh     }
196fec00eabSdrh     if( sqlite3FaultStep(SQLITE_FAULTINJECTOR_MALLOC) ){
197fec00eabSdrh       p = 0;
198fec00eabSdrh     }else{
199fec00eabSdrh       p = sqlite3Config.m.xMalloc(nFull);
200fec00eabSdrh       if( p==0 ){
201fec00eabSdrh         sqlite3MallocAlarm(nFull);
202fec00eabSdrh         p = malloc(nFull);
203fec00eabSdrh       }
204fec00eabSdrh     }
205fec00eabSdrh     if( p ){
206fec00eabSdrh       mem0.nowUsed += nFull;
207fec00eabSdrh       if( mem0.nowUsed>mem0.mxUsed ){
208fec00eabSdrh         mem0.mxUsed = mem0.nowUsed;
209fec00eabSdrh       }
210fec00eabSdrh     }
211fec00eabSdrh     sqlite3_mutex_leave(mem0.mutex);
212fec00eabSdrh   }else{
213fec00eabSdrh     p = sqlite3Config.m.xMalloc(n);
214fec00eabSdrh   }
215fec00eabSdrh   return p;
216fec00eabSdrh }
217fec00eabSdrh 
218fec00eabSdrh /*
219fec00eabSdrh ** This version of the memory allocation is for use by the application.
220fec00eabSdrh ** First make sure the memory subsystem is initialized, then do the
221fec00eabSdrh ** allocation.
222fec00eabSdrh */
223fec00eabSdrh void *sqlite3_malloc(int n){
224fec00eabSdrh #ifndef SQLITE_OMIT_AUTOINIT
225fec00eabSdrh   if( sqlite3_initialize() ) return 0;
226fec00eabSdrh #endif
227fec00eabSdrh   return sqlite3Malloc(n);
228fec00eabSdrh }
229fec00eabSdrh 
230fec00eabSdrh /*
231*e5ae5735Sdrh ** Each thread may only have a single outstanding allocation from
232*e5ae5735Sdrh ** xTempMalloc().  We verify this constraint in the single-threaded
233*e5ae5735Sdrh ** case by setting tempAllocOut to 1 when an allocation
234*e5ae5735Sdrh ** is outstanding clearing it when the allocation is freed.
235*e5ae5735Sdrh */
236*e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
237*e5ae5735Sdrh static int tempAllocOut = 0;
238*e5ae5735Sdrh #endif
239*e5ae5735Sdrh 
240*e5ae5735Sdrh 
241*e5ae5735Sdrh /*
242*e5ae5735Sdrh ** Allocate memory that is to be used and released right away.
243*e5ae5735Sdrh ** This routine is similar to alloca() in that it is not intended
244*e5ae5735Sdrh ** for situations where the memory might be held long-term.  This
245*e5ae5735Sdrh ** routine is intended to get memory to old large transient data
246*e5ae5735Sdrh ** structures that would not normally fit on the stack of an
247*e5ae5735Sdrh ** embedded processor.
248*e5ae5735Sdrh */
249*e5ae5735Sdrh void *sqlite3TempMalloc(int n){
250*e5ae5735Sdrh   void *p;
251*e5ae5735Sdrh   assert( n>0 );
252*e5ae5735Sdrh   if( sqlite3FaultStep(SQLITE_FAULTINJECTOR_MALLOC) ){
253*e5ae5735Sdrh     return 0;
254*e5ae5735Sdrh   }
255*e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
256*e5ae5735Sdrh   assert( tempAllocOut==0 );
257*e5ae5735Sdrh   tempAllocOut = 1;
258*e5ae5735Sdrh #endif
259*e5ae5735Sdrh   if( sqlite3Config.bMemstat ){
260*e5ae5735Sdrh     sqlite3_mutex_enter(mem0.mutex);
261*e5ae5735Sdrh     if( n>mem0.mxTempReq ) mem0.mxTempReq = n;
262*e5ae5735Sdrh     p = sqlite3Config.m.xTempMalloc(n);
263*e5ae5735Sdrh     sqlite3_mutex_leave(mem0.mutex);
264*e5ae5735Sdrh   }else{
265*e5ae5735Sdrh     p = sqlite3Config.m.xTempMalloc(n);
266*e5ae5735Sdrh   }
267*e5ae5735Sdrh   return p;
268*e5ae5735Sdrh }
269*e5ae5735Sdrh void sqlite3TempFree(void *p){
270*e5ae5735Sdrh   if( p ){
271*e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
272*e5ae5735Sdrh     assert( tempAllocOut==1 );
273*e5ae5735Sdrh     tempAllocOut = 0;
274*e5ae5735Sdrh #endif
275*e5ae5735Sdrh     sqlite3Config.m.xTempFree(p);
276*e5ae5735Sdrh   }
277*e5ae5735Sdrh }
278*e5ae5735Sdrh 
279*e5ae5735Sdrh /*
280fec00eabSdrh ** Return the size of a memory allocation previously obtained from
281fec00eabSdrh ** sqlite3Malloc() or sqlite3_malloc().
282fec00eabSdrh */
283fec00eabSdrh int sqlite3MallocSize(void *p){
284fec00eabSdrh   return sqlite3Config.m.xSize(p);
285fec00eabSdrh }
286fec00eabSdrh 
287fec00eabSdrh /*
288fec00eabSdrh ** Free memory previously obtained from sqlite3Malloc().
289fec00eabSdrh */
290fec00eabSdrh void sqlite3_free(void *p){
291fec00eabSdrh   if( p==0 ) return;
292fec00eabSdrh   if( sqlite3Config.bMemstat ){
293fec00eabSdrh     sqlite3_mutex_enter(mem0.mutex);
294fec00eabSdrh     mem0.nowUsed -= sqlite3MallocSize(p);
295fec00eabSdrh     sqlite3Config.m.xFree(p);
296fec00eabSdrh     sqlite3_mutex_leave(mem0.mutex);
297fec00eabSdrh   }else{
298fec00eabSdrh     sqlite3Config.m.xFree(p);
299fec00eabSdrh   }
300fec00eabSdrh }
301fec00eabSdrh 
302fec00eabSdrh /*
303fec00eabSdrh ** Change the size of an existing memory allocation
304fec00eabSdrh */
305fec00eabSdrh void *sqlite3Realloc(void *pOld, int nBytes){
306fec00eabSdrh   int nOld, nNew;
307fec00eabSdrh   void *pNew;
308fec00eabSdrh   if( pOld==0 ){
309fec00eabSdrh     return sqlite3Malloc(nBytes);
310fec00eabSdrh   }
311fec00eabSdrh   if( nBytes<=0 ){
312fec00eabSdrh     sqlite3_free(pOld);
313fec00eabSdrh     return 0;
314fec00eabSdrh   }
315fec00eabSdrh   nOld = sqlite3MallocSize(pOld);
316fec00eabSdrh   if( sqlite3Config.bMemstat ){
317fec00eabSdrh     sqlite3_mutex_enter(mem0.mutex);
318fec00eabSdrh     if( nBytes>mem0.mxReq ) mem0.mxReq = nBytes;
319fec00eabSdrh     nNew = sqlite3Config.m.xRoundup(nBytes);
320fec00eabSdrh     if( nOld==nNew ){
321fec00eabSdrh       pNew = pOld;
322fec00eabSdrh     }else{
323fec00eabSdrh       if( mem0.nowUsed+nNew-nOld>=mem0.alarmThreshold ){
324fec00eabSdrh         sqlite3MallocAlarm(nNew-nOld);
325fec00eabSdrh       }
326fec00eabSdrh       if( sqlite3FaultStep(SQLITE_FAULTINJECTOR_MALLOC) ){
327fec00eabSdrh         pNew = 0;
328fec00eabSdrh       }else{
329fec00eabSdrh         pNew = sqlite3Config.m.xRealloc(pOld, nNew);
330fec00eabSdrh         if( pNew==0 ){
331fec00eabSdrh           sqlite3MallocAlarm(nBytes);
332fec00eabSdrh           pNew = sqlite3Config.m.xRealloc(pOld, nNew);
333fec00eabSdrh         }
334fec00eabSdrh       }
335fec00eabSdrh       if( pNew ){
336fec00eabSdrh         mem0.nowUsed += nNew-nOld;
337fec00eabSdrh         if( mem0.nowUsed>mem0.mxUsed ){
338fec00eabSdrh           mem0.mxUsed = mem0.nowUsed;
339fec00eabSdrh         }
340fec00eabSdrh       }
341fec00eabSdrh     }
342fec00eabSdrh     sqlite3_mutex_leave(mem0.mutex);
343fec00eabSdrh   }else{
344fec00eabSdrh     pNew = sqlite3Config.m.xRealloc(pOld, nBytes);
345fec00eabSdrh   }
346fec00eabSdrh   return pNew;
347fec00eabSdrh }
348fec00eabSdrh 
349fec00eabSdrh /*
350fec00eabSdrh ** The public interface to sqlite3Realloc.  Make sure that the memory
351fec00eabSdrh ** subsystem is initialized prior to invoking sqliteRealloc.
352fec00eabSdrh */
353fec00eabSdrh void *sqlite3_realloc(void *pOld, int n){
354fec00eabSdrh #ifndef SQLITE_OMIT_AUTOINIT
355fec00eabSdrh   if( sqlite3_initialize() ) return 0;
356fec00eabSdrh #endif
357fec00eabSdrh   return sqlite3Realloc(pOld, n);
358fec00eabSdrh }
359fec00eabSdrh 
360a3152895Sdrh 
361a3152895Sdrh /*
36217435752Sdrh ** Allocate and zero memory.
363a3152895Sdrh */
364fec00eabSdrh void *sqlite3MallocZero(int n){
365fec00eabSdrh   void *p = sqlite3Malloc(n);
366a3152895Sdrh   if( p ){
367a3152895Sdrh     memset(p, 0, n);
368a3152895Sdrh   }
369a3152895Sdrh   return p;
370a3152895Sdrh }
37117435752Sdrh 
37217435752Sdrh /*
37317435752Sdrh ** Allocate and zero memory.  If the allocation fails, make
37417435752Sdrh ** the mallocFailed flag in the connection pointer.
37517435752Sdrh */
376fec00eabSdrh void *sqlite3DbMallocZero(sqlite3 *db, int n){
377a1644fd8Sdanielk1977   void *p = sqlite3DbMallocRaw(db, n);
37817435752Sdrh   if( p ){
37917435752Sdrh     memset(p, 0, n);
38017435752Sdrh   }
38117435752Sdrh   return p;
38217435752Sdrh }
38317435752Sdrh 
38417435752Sdrh /*
38517435752Sdrh ** Allocate and zero memory.  If the allocation fails, make
38617435752Sdrh ** the mallocFailed flag in the connection pointer.
38717435752Sdrh */
388fec00eabSdrh void *sqlite3DbMallocRaw(sqlite3 *db, int n){
389a1644fd8Sdanielk1977   void *p = 0;
390a1644fd8Sdanielk1977   if( !db || db->mallocFailed==0 ){
391fec00eabSdrh     p = sqlite3Malloc(n);
392f3a65f7eSdrh     if( !p && db ){
39317435752Sdrh       db->mallocFailed = 1;
39417435752Sdrh     }
395a1644fd8Sdanielk1977   }
39617435752Sdrh   return p;
39717435752Sdrh }
39817435752Sdrh 
39926783a58Sdanielk1977 /*
40026783a58Sdanielk1977 ** Resize the block of memory pointed to by p to n bytes. If the
40126783a58Sdanielk1977 ** resize fails, set the mallocFailed flag inthe connection object.
40226783a58Sdanielk1977 */
403a1644fd8Sdanielk1977 void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
404a1644fd8Sdanielk1977   void *pNew = 0;
405a1644fd8Sdanielk1977   if( db->mallocFailed==0 ){
406a1644fd8Sdanielk1977     pNew = sqlite3_realloc(p, n);
407a1644fd8Sdanielk1977     if( !pNew ){
408a1644fd8Sdanielk1977       db->mallocFailed = 1;
409a1644fd8Sdanielk1977     }
410a1644fd8Sdanielk1977   }
411a1644fd8Sdanielk1977   return pNew;
412a1644fd8Sdanielk1977 }
413a1644fd8Sdanielk1977 
41417435752Sdrh /*
41517435752Sdrh ** Attempt to reallocate p.  If the reallocation fails, then free p
41617435752Sdrh ** and set the mallocFailed flag in the database connection.
41717435752Sdrh */
41817435752Sdrh void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
419a3152895Sdrh   void *pNew;
420a1644fd8Sdanielk1977   pNew = sqlite3DbRealloc(db, p, n);
421a3152895Sdrh   if( !pNew ){
4221e536953Sdanielk1977     sqlite3_free(p);
423a3152895Sdrh   }
424a3152895Sdrh   return pNew;
425a3152895Sdrh }
426a3152895Sdrh 
427a3152895Sdrh /*
428a3152895Sdrh ** Make a copy of a string in memory obtained from sqliteMalloc(). These
429a3152895Sdrh ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
430a3152895Sdrh ** is because when memory debugging is turned on, these two functions are
431a3152895Sdrh ** called via macros that record the current file and line number in the
432a3152895Sdrh ** ThreadData structure.
433a3152895Sdrh */
434a3152895Sdrh char *sqlite3StrDup(const char *z){
435a3152895Sdrh   char *zNew;
436a3152895Sdrh   int n;
437a3152895Sdrh   if( z==0 ) return 0;
438a3152895Sdrh   n = strlen(z)+1;
439*e5ae5735Sdrh   zNew = sqlite3Malloc(n);
440a3152895Sdrh   if( zNew ) memcpy(zNew, z, n);
441a3152895Sdrh   return zNew;
442a3152895Sdrh }
443a3152895Sdrh char *sqlite3StrNDup(const char *z, int n){
444a3152895Sdrh   char *zNew;
445a3152895Sdrh   if( z==0 ) return 0;
446*e5ae5735Sdrh   zNew = sqlite3Malloc(n+1);
447a3152895Sdrh   if( zNew ){
448a3152895Sdrh     memcpy(zNew, z, n);
449a3152895Sdrh     zNew[n] = 0;
450a3152895Sdrh   }
451a3152895Sdrh   return zNew;
452a3152895Sdrh }
453a3152895Sdrh 
4541e536953Sdanielk1977 char *sqlite3DbStrDup(sqlite3 *db, const char *z){
4551e536953Sdanielk1977   char *zNew = sqlite3StrDup(z);
4561e536953Sdanielk1977   if( z && !zNew ){
4571e536953Sdanielk1977     db->mallocFailed = 1;
4581e536953Sdanielk1977   }
4591e536953Sdanielk1977   return zNew;
4601e536953Sdanielk1977 }
4611e536953Sdanielk1977 char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
4621e536953Sdanielk1977   char *zNew = sqlite3StrNDup(z, n);
4631e536953Sdanielk1977   if( z && !zNew ){
4641e536953Sdanielk1977     db->mallocFailed = 1;
4651e536953Sdanielk1977   }
4661e536953Sdanielk1977   return zNew;
4671e536953Sdanielk1977 }
4681e536953Sdanielk1977 
469a3152895Sdrh /*
470a3152895Sdrh ** Create a string from the 2nd and subsequent arguments (up to the
471a3152895Sdrh ** first NULL argument), store the string in memory obtained from
472a3152895Sdrh ** sqliteMalloc() and make the pointer indicated by the 1st argument
473a3152895Sdrh ** point to that string.  The 1st argument must either be NULL or
474a3152895Sdrh ** point to memory obtained from sqliteMalloc().
475a3152895Sdrh */
476a3152895Sdrh void sqlite3SetString(char **pz, ...){
477a3152895Sdrh   va_list ap;
478a3152895Sdrh   int nByte;
479a3152895Sdrh   const char *z;
480a3152895Sdrh   char *zResult;
481a3152895Sdrh 
482a3152895Sdrh   assert( pz!=0 );
483a3152895Sdrh   nByte = 1;
484a3152895Sdrh   va_start(ap, pz);
485a3152895Sdrh   while( (z = va_arg(ap, const char*))!=0 ){
486a3152895Sdrh     nByte += strlen(z);
487a3152895Sdrh   }
488a3152895Sdrh   va_end(ap);
4891e536953Sdanielk1977   sqlite3_free(*pz);
490*e5ae5735Sdrh   *pz = zResult = sqlite3Malloc(nByte);
491a3152895Sdrh   if( zResult==0 ){
492a3152895Sdrh     return;
493a3152895Sdrh   }
494a3152895Sdrh   *zResult = 0;
495a3152895Sdrh   va_start(ap, pz);
496a3152895Sdrh   while( (z = va_arg(ap, const char*))!=0 ){
497a3152895Sdrh     int n = strlen(z);
498a3152895Sdrh     memcpy(zResult, z, n);
499a3152895Sdrh     zResult += n;
500a3152895Sdrh   }
501a3152895Sdrh   zResult[0] = 0;
502a3152895Sdrh   va_end(ap);
503a3152895Sdrh }
504a3152895Sdrh 
505a3152895Sdrh 
506a3152895Sdrh /*
507a3152895Sdrh ** This function must be called before exiting any API function (i.e.
50817435752Sdrh ** returning control to the user) that has called sqlite3_malloc or
50917435752Sdrh ** sqlite3_realloc.
510a3152895Sdrh **
511a3152895Sdrh ** The returned value is normally a copy of the second argument to this
512a3152895Sdrh ** function. However, if a malloc() failure has occured since the previous
513a3152895Sdrh ** invocation SQLITE_NOMEM is returned instead.
514a3152895Sdrh **
515a3152895Sdrh ** If the first argument, db, is not NULL and a malloc() error has occured,
516a3152895Sdrh ** then the connection error-code (the value returned by sqlite3_errcode())
517a3152895Sdrh ** is set to SQLITE_NOMEM.
518a3152895Sdrh */
519a3152895Sdrh int sqlite3ApiExit(sqlite3* db, int rc){
520a1644fd8Sdanielk1977   /* If the db handle is not NULL, then we must hold the connection handle
521a1644fd8Sdanielk1977   ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
522a1644fd8Sdanielk1977   ** is unsafe, as is the call to sqlite3Error().
523a1644fd8Sdanielk1977   */
524a1644fd8Sdanielk1977   assert( !db || sqlite3_mutex_held(db->mutex) );
5251e536953Sdanielk1977   if( db && db->mallocFailed ){
526a3152895Sdrh     sqlite3Error(db, SQLITE_NOMEM, 0);
52717435752Sdrh     db->mallocFailed = 0;
528a3152895Sdrh     rc = SQLITE_NOMEM;
529a3152895Sdrh   }
530a3152895Sdrh   return rc & (db ? db->errMask : 0xff);
531a3152895Sdrh }
532