xref: /sqlite-3.40.0/src/malloc.c (revision b21c8cd4)
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 *************************************************************************
12a3152895Sdrh ** Memory allocation functions used throughout sqlite.
13a3152895Sdrh **
14a3152895Sdrh **
15*b21c8cd4Sdrh ** $Id: malloc.c,v 1.8 2007/08/21 19:33:56 drh Exp $
16a3152895Sdrh */
17a3152895Sdrh #include "sqliteInt.h"
18a3152895Sdrh #include <stdarg.h>
19a3152895Sdrh #include <ctype.h>
20a3152895Sdrh 
21a3152895Sdrh /*
22*b21c8cd4Sdrh ** This routine runs when the memory allocator sees that the
23*b21c8cd4Sdrh ** total memory allocation is about to exceed the soft heap
24*b21c8cd4Sdrh ** limit.
25*b21c8cd4Sdrh */
26*b21c8cd4Sdrh static void softHeapLimitEnforcer(
27*b21c8cd4Sdrh   void *NotUsed,
28*b21c8cd4Sdrh   sqlite3_uint64 inUse,
29*b21c8cd4Sdrh   unsigned int allocSize
30*b21c8cd4Sdrh ){
31*b21c8cd4Sdrh   sqlite3_release_memory(allocSize);
32*b21c8cd4Sdrh }
33*b21c8cd4Sdrh 
34*b21c8cd4Sdrh /*
35*b21c8cd4Sdrh ** Set the soft heap-size limit for the current thread. Passing a
36*b21c8cd4Sdrh ** zero or negative value indicates no limit.
37a3152895Sdrh */
38a3152895Sdrh void sqlite3_soft_heap_limit(int n){
39*b21c8cd4Sdrh   sqlite3_uint64 iLimit;
40*b21c8cd4Sdrh   int overage;
41*b21c8cd4Sdrh   if( n<0 ){
42*b21c8cd4Sdrh     iLimit = 0;
43*b21c8cd4Sdrh   }else{
44*b21c8cd4Sdrh     iLimit = n;
45a3152895Sdrh   }
46*b21c8cd4Sdrh   if( iLimit>0 ){
47*b21c8cd4Sdrh     sqlite3_memory_alarm(softHeapLimitEnforcer, 0, iLimit);
48*b21c8cd4Sdrh   }else{
49*b21c8cd4Sdrh     sqlite3_memory_alarm(0, 0, 0);
50*b21c8cd4Sdrh   }
51*b21c8cd4Sdrh   overage = sqlite3_memory_used() - n;
52*b21c8cd4Sdrh   if( overage>0 ){
53*b21c8cd4Sdrh     sqlite3_release_memory(overage);
54*b21c8cd4Sdrh   }
55a3152895Sdrh }
56a3152895Sdrh 
57a3152895Sdrh /*
58a3152895Sdrh ** Release memory held by SQLite instances created by the current thread.
59a3152895Sdrh */
60a3152895Sdrh int sqlite3_release_memory(int n){
611e536953Sdanielk1977 #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO)
62a3152895Sdrh   return sqlite3PagerReleaseMemory(n);
631e536953Sdanielk1977 #else
641e536953Sdanielk1977   return SQLITE_OK;
651e536953Sdanielk1977 #endif
66a3152895Sdrh }
67a3152895Sdrh 
68a3152895Sdrh 
69a3152895Sdrh /*
7017435752Sdrh ** Allocate and zero memory.
71a3152895Sdrh */
7217435752Sdrh void *sqlite3MallocZero(unsigned n){
7317435752Sdrh   void *p = sqlite3_malloc(n);
74a3152895Sdrh   if( p ){
75a3152895Sdrh     memset(p, 0, n);
76a3152895Sdrh   }
77a3152895Sdrh   return p;
78a3152895Sdrh }
7917435752Sdrh 
8017435752Sdrh /*
8117435752Sdrh ** Allocate and zero memory.  If the allocation fails, make
8217435752Sdrh ** the mallocFailed flag in the connection pointer.
8317435752Sdrh */
8417435752Sdrh void *sqlite3DbMallocZero(sqlite3 *db, unsigned n){
8517435752Sdrh   void *p = sqlite3_malloc(n);
8617435752Sdrh   if( p ){
8717435752Sdrh     memset(p, 0, n);
88*b21c8cd4Sdrh   }else if( db ){
8917435752Sdrh     db->mallocFailed = 1;
9017435752Sdrh   }
9117435752Sdrh   return p;
9217435752Sdrh }
9317435752Sdrh 
9417435752Sdrh /*
9517435752Sdrh ** Allocate and zero memory.  If the allocation fails, make
9617435752Sdrh ** the mallocFailed flag in the connection pointer.
9717435752Sdrh */
9817435752Sdrh void *sqlite3DbMallocRaw(sqlite3 *db, unsigned n){
9917435752Sdrh   void *p = sqlite3_malloc(n);
10017435752Sdrh   if( !p ){
10117435752Sdrh     db->mallocFailed = 1;
10217435752Sdrh   }
10317435752Sdrh   return p;
10417435752Sdrh }
10517435752Sdrh 
10617435752Sdrh /*
10717435752Sdrh ** Attempt to reallocate p.  If the reallocation fails, then free p
10817435752Sdrh ** and set the mallocFailed flag in the database connection.
10917435752Sdrh */
11017435752Sdrh void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
111a3152895Sdrh   void *pNew;
11217435752Sdrh   pNew = sqlite3_realloc(p, n);
113a3152895Sdrh   if( !pNew ){
1141e536953Sdanielk1977     sqlite3_free(p);
11517435752Sdrh     db->mallocFailed = 1;
116a3152895Sdrh   }
117a3152895Sdrh   return pNew;
118a3152895Sdrh }
119a3152895Sdrh 
120a3152895Sdrh 
121a3152895Sdrh /*
122a3152895Sdrh ** Make a copy of a string in memory obtained from sqliteMalloc(). These
123a3152895Sdrh ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
124a3152895Sdrh ** is because when memory debugging is turned on, these two functions are
125a3152895Sdrh ** called via macros that record the current file and line number in the
126a3152895Sdrh ** ThreadData structure.
127a3152895Sdrh */
128a3152895Sdrh char *sqlite3StrDup(const char *z){
129a3152895Sdrh   char *zNew;
130a3152895Sdrh   int n;
131a3152895Sdrh   if( z==0 ) return 0;
132a3152895Sdrh   n = strlen(z)+1;
1331e536953Sdanielk1977   zNew = sqlite3_malloc(n);
134a3152895Sdrh   if( zNew ) memcpy(zNew, z, n);
135a3152895Sdrh   return zNew;
136a3152895Sdrh }
137a3152895Sdrh char *sqlite3StrNDup(const char *z, int n){
138a3152895Sdrh   char *zNew;
139a3152895Sdrh   if( z==0 ) return 0;
1401e536953Sdanielk1977   zNew = sqlite3_malloc(n+1);
141a3152895Sdrh   if( zNew ){
142a3152895Sdrh     memcpy(zNew, z, n);
143a3152895Sdrh     zNew[n] = 0;
144a3152895Sdrh   }
145a3152895Sdrh   return zNew;
146a3152895Sdrh }
147a3152895Sdrh 
1481e536953Sdanielk1977 char *sqlite3DbStrDup(sqlite3 *db, const char *z){
1491e536953Sdanielk1977   char *zNew = sqlite3StrDup(z);
1501e536953Sdanielk1977   if( z && !zNew ){
1511e536953Sdanielk1977     db->mallocFailed = 1;
1521e536953Sdanielk1977   }
1531e536953Sdanielk1977   return zNew;
1541e536953Sdanielk1977 }
1551e536953Sdanielk1977 char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
1561e536953Sdanielk1977   char *zNew = sqlite3StrNDup(z, n);
1571e536953Sdanielk1977   if( z && !zNew ){
1581e536953Sdanielk1977     db->mallocFailed = 1;
1591e536953Sdanielk1977   }
1601e536953Sdanielk1977   return zNew;
1611e536953Sdanielk1977 }
1621e536953Sdanielk1977 
163a3152895Sdrh /*
164a3152895Sdrh ** Create a string from the 2nd and subsequent arguments (up to the
165a3152895Sdrh ** first NULL argument), store the string in memory obtained from
166a3152895Sdrh ** sqliteMalloc() and make the pointer indicated by the 1st argument
167a3152895Sdrh ** point to that string.  The 1st argument must either be NULL or
168a3152895Sdrh ** point to memory obtained from sqliteMalloc().
169a3152895Sdrh */
170a3152895Sdrh void sqlite3SetString(char **pz, ...){
171a3152895Sdrh   va_list ap;
172a3152895Sdrh   int nByte;
173a3152895Sdrh   const char *z;
174a3152895Sdrh   char *zResult;
175a3152895Sdrh 
176a3152895Sdrh   assert( pz!=0 );
177a3152895Sdrh   nByte = 1;
178a3152895Sdrh   va_start(ap, pz);
179a3152895Sdrh   while( (z = va_arg(ap, const char*))!=0 ){
180a3152895Sdrh     nByte += strlen(z);
181a3152895Sdrh   }
182a3152895Sdrh   va_end(ap);
1831e536953Sdanielk1977   sqlite3_free(*pz);
1841e536953Sdanielk1977   *pz = zResult = sqlite3_malloc(nByte);
185a3152895Sdrh   if( zResult==0 ){
186a3152895Sdrh     return;
187a3152895Sdrh   }
188a3152895Sdrh   *zResult = 0;
189a3152895Sdrh   va_start(ap, pz);
190a3152895Sdrh   while( (z = va_arg(ap, const char*))!=0 ){
191a3152895Sdrh     int n = strlen(z);
192a3152895Sdrh     memcpy(zResult, z, n);
193a3152895Sdrh     zResult += n;
194a3152895Sdrh   }
195a3152895Sdrh   zResult[0] = 0;
196a3152895Sdrh   va_end(ap);
197a3152895Sdrh }
198a3152895Sdrh 
199a3152895Sdrh 
200a3152895Sdrh /*
201a3152895Sdrh ** This function must be called before exiting any API function (i.e.
20217435752Sdrh ** returning control to the user) that has called sqlite3_malloc or
20317435752Sdrh ** sqlite3_realloc.
204a3152895Sdrh **
205a3152895Sdrh ** The returned value is normally a copy of the second argument to this
206a3152895Sdrh ** function. However, if a malloc() failure has occured since the previous
207a3152895Sdrh ** invocation SQLITE_NOMEM is returned instead.
208a3152895Sdrh **
209a3152895Sdrh ** If the first argument, db, is not NULL and a malloc() error has occured,
210a3152895Sdrh ** then the connection error-code (the value returned by sqlite3_errcode())
211a3152895Sdrh ** is set to SQLITE_NOMEM.
212a3152895Sdrh */
213a3152895Sdrh int sqlite3ApiExit(sqlite3* db, int rc){
2141e536953Sdanielk1977   if( db && db->mallocFailed ){
215a3152895Sdrh     sqlite3Error(db, SQLITE_NOMEM, 0);
21617435752Sdrh     db->mallocFailed = 0;
217a3152895Sdrh     rc = SQLITE_NOMEM;
218a3152895Sdrh   }
219a3152895Sdrh   return rc & (db ? db->errMask : 0xff);
220a3152895Sdrh }
221