blob: 55d5c76dee0b9a8f441ea41aac61ccdabf6de534 [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*************************************************************************
12** Memory allocation functions used throughout sqlite.
13**
14**
danielk1977dfb316d2008-03-26 18:34:43 +000015** $Id: malloc.c,v 1.15 2008/03/26 18:34:43 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
drha3152892007-05-05 11:48:52 +000070
71/*
drh17435752007-08-16 04:30:38 +000072** Allocate and zero memory.
drha3152892007-05-05 11:48:52 +000073*/
drh17435752007-08-16 04:30:38 +000074void *sqlite3MallocZero(unsigned n){
75 void *p = sqlite3_malloc(n);
drha3152892007-05-05 11:48:52 +000076 if( p ){
77 memset(p, 0, n);
78 }
79 return p;
80}
drh17435752007-08-16 04:30:38 +000081
82/*
83** Allocate and zero memory. If the allocation fails, make
84** the mallocFailed flag in the connection pointer.
85*/
86void *sqlite3DbMallocZero(sqlite3 *db, unsigned n){
danielk1977a1644fd2007-08-29 12:31:25 +000087 void *p = sqlite3DbMallocRaw(db, n);
drh17435752007-08-16 04:30:38 +000088 if( p ){
89 memset(p, 0, n);
drh17435752007-08-16 04:30:38 +000090 }
91 return p;
92}
93
94/*
95** Allocate and zero memory. If the allocation fails, make
96** the mallocFailed flag in the connection pointer.
97*/
98void *sqlite3DbMallocRaw(sqlite3 *db, unsigned n){
danielk1977a1644fd2007-08-29 12:31:25 +000099 void *p = 0;
100 if( !db || db->mallocFailed==0 ){
101 p = sqlite3_malloc(n);
102 if( !p && db ){
103 db->mallocFailed = 1;
104 }
drh17435752007-08-16 04:30:38 +0000105 }
106 return p;
107}
108
danielk197726783a52007-08-29 14:06:22 +0000109/*
110** Resize the block of memory pointed to by p to n bytes. If the
111** resize fails, set the mallocFailed flag inthe connection object.
112*/
danielk1977a1644fd2007-08-29 12:31:25 +0000113void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
114 void *pNew = 0;
115 if( db->mallocFailed==0 ){
116 pNew = sqlite3_realloc(p, n);
117 if( !pNew ){
118 db->mallocFailed = 1;
119 }
120 }
121 return pNew;
122}
123
drh17435752007-08-16 04:30:38 +0000124/*
125** Attempt to reallocate p. If the reallocation fails, then free p
126** and set the mallocFailed flag in the database connection.
127*/
128void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
drha3152892007-05-05 11:48:52 +0000129 void *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000130 pNew = sqlite3DbRealloc(db, p, n);
drha3152892007-05-05 11:48:52 +0000131 if( !pNew ){
danielk19771e536952007-08-16 10:09:01 +0000132 sqlite3_free(p);
drha3152892007-05-05 11:48:52 +0000133 }
134 return pNew;
135}
136
drha3152892007-05-05 11:48:52 +0000137/*
138** Make a copy of a string in memory obtained from sqliteMalloc(). These
139** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
140** is because when memory debugging is turned on, these two functions are
141** called via macros that record the current file and line number in the
142** ThreadData structure.
143*/
144char *sqlite3StrDup(const char *z){
145 char *zNew;
146 int n;
147 if( z==0 ) return 0;
148 n = strlen(z)+1;
danielk19771e536952007-08-16 10:09:01 +0000149 zNew = sqlite3_malloc(n);
drha3152892007-05-05 11:48:52 +0000150 if( zNew ) memcpy(zNew, z, n);
151 return zNew;
152}
153char *sqlite3StrNDup(const char *z, int n){
154 char *zNew;
155 if( z==0 ) return 0;
danielk19771e536952007-08-16 10:09:01 +0000156 zNew = sqlite3_malloc(n+1);
drha3152892007-05-05 11:48:52 +0000157 if( zNew ){
158 memcpy(zNew, z, n);
159 zNew[n] = 0;
160 }
161 return zNew;
162}
163
danielk19771e536952007-08-16 10:09:01 +0000164char *sqlite3DbStrDup(sqlite3 *db, const char *z){
165 char *zNew = sqlite3StrDup(z);
166 if( z && !zNew ){
167 db->mallocFailed = 1;
168 }
169 return zNew;
170}
171char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
172 char *zNew = sqlite3StrNDup(z, n);
173 if( z && !zNew ){
174 db->mallocFailed = 1;
175 }
176 return zNew;
177}
178
drha3152892007-05-05 11:48:52 +0000179/*
180** Create a string from the 2nd and subsequent arguments (up to the
181** first NULL argument), store the string in memory obtained from
182** sqliteMalloc() and make the pointer indicated by the 1st argument
183** point to that string. The 1st argument must either be NULL or
184** point to memory obtained from sqliteMalloc().
185*/
186void sqlite3SetString(char **pz, ...){
187 va_list ap;
188 int nByte;
189 const char *z;
190 char *zResult;
191
192 assert( pz!=0 );
193 nByte = 1;
194 va_start(ap, pz);
195 while( (z = va_arg(ap, const char*))!=0 ){
196 nByte += strlen(z);
197 }
198 va_end(ap);
danielk19771e536952007-08-16 10:09:01 +0000199 sqlite3_free(*pz);
200 *pz = zResult = sqlite3_malloc(nByte);
drha3152892007-05-05 11:48:52 +0000201 if( zResult==0 ){
202 return;
203 }
204 *zResult = 0;
205 va_start(ap, pz);
206 while( (z = va_arg(ap, const char*))!=0 ){
207 int n = strlen(z);
208 memcpy(zResult, z, n);
209 zResult += n;
210 }
211 zResult[0] = 0;
212 va_end(ap);
213}
214
215
216/*
217** This function must be called before exiting any API function (i.e.
drh17435752007-08-16 04:30:38 +0000218** returning control to the user) that has called sqlite3_malloc or
219** sqlite3_realloc.
drha3152892007-05-05 11:48:52 +0000220**
221** The returned value is normally a copy of the second argument to this
222** function. However, if a malloc() failure has occured since the previous
223** invocation SQLITE_NOMEM is returned instead.
224**
225** If the first argument, db, is not NULL and a malloc() error has occured,
226** then the connection error-code (the value returned by sqlite3_errcode())
227** is set to SQLITE_NOMEM.
228*/
drha3152892007-05-05 11:48:52 +0000229int sqlite3ApiExit(sqlite3* db, int rc){
danielk1977a1644fd2007-08-29 12:31:25 +0000230 /* If the db handle is not NULL, then we must hold the connection handle
231 ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
232 ** is unsafe, as is the call to sqlite3Error().
233 */
234 assert( !db || sqlite3_mutex_held(db->mutex) );
danielk19771e536952007-08-16 10:09:01 +0000235 if( db && db->mallocFailed ){
drha3152892007-05-05 11:48:52 +0000236 sqlite3Error(db, SQLITE_NOMEM, 0);
drh17435752007-08-16 04:30:38 +0000237 db->mallocFailed = 0;
drha3152892007-05-05 11:48:52 +0000238 rc = SQLITE_NOMEM;
239 }
240 return rc & (db ? db->errMask : 0xff);
241}