blob: c8a5025ea12052864da044b694f9fb349125b7cf [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**
drh86f8c192007-08-22 00:39:19 +000015** $Id: malloc.c,v 1.9 2007/08/22 00:39:20 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,
28 sqlite3_uint64 inUse,
29 unsigned int allocSize
30){
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
drha3152892007-05-05 11:48:52 +000062 return sqlite3PagerReleaseMemory(n);
danielk19771e536952007-08-16 10:09:01 +000063#else
64 return SQLITE_OK;
65#endif
drha3152892007-05-05 11:48:52 +000066}
drha3152892007-05-05 11:48:52 +000067
drha3152892007-05-05 11:48:52 +000068
69/*
drh17435752007-08-16 04:30:38 +000070** Allocate and zero memory.
drha3152892007-05-05 11:48:52 +000071*/
drh17435752007-08-16 04:30:38 +000072void *sqlite3MallocZero(unsigned n){
73 void *p = sqlite3_malloc(n);
drha3152892007-05-05 11:48:52 +000074 if( p ){
75 memset(p, 0, n);
76 }
77 return p;
78}
drh17435752007-08-16 04:30:38 +000079
80/*
81** Allocate and zero memory. If the allocation fails, make
82** the mallocFailed flag in the connection pointer.
83*/
84void *sqlite3DbMallocZero(sqlite3 *db, unsigned n){
85 void *p = sqlite3_malloc(n);
86 if( p ){
87 memset(p, 0, n);
drhb21c8cd2007-08-21 19:33:56 +000088 }else if( db ){
drh17435752007-08-16 04:30:38 +000089 db->mallocFailed = 1;
90 }
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){
99 void *p = sqlite3_malloc(n);
100 if( !p ){
101 db->mallocFailed = 1;
102 }
103 return p;
104}
105
106/*
107** Attempt to reallocate p. If the reallocation fails, then free p
108** and set the mallocFailed flag in the database connection.
109*/
110void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
drha3152892007-05-05 11:48:52 +0000111 void *pNew;
drh17435752007-08-16 04:30:38 +0000112 pNew = sqlite3_realloc(p, n);
drha3152892007-05-05 11:48:52 +0000113 if( !pNew ){
danielk19771e536952007-08-16 10:09:01 +0000114 sqlite3_free(p);
drh17435752007-08-16 04:30:38 +0000115 db->mallocFailed = 1;
drha3152892007-05-05 11:48:52 +0000116 }
117 return pNew;
118}
119
drha3152892007-05-05 11:48:52 +0000120
121/*
122** Make a copy of a string in memory obtained from sqliteMalloc(). These
123** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
124** is because when memory debugging is turned on, these two functions are
125** called via macros that record the current file and line number in the
126** ThreadData structure.
127*/
128char *sqlite3StrDup(const char *z){
129 char *zNew;
130 int n;
131 if( z==0 ) return 0;
132 n = strlen(z)+1;
danielk19771e536952007-08-16 10:09:01 +0000133 zNew = sqlite3_malloc(n);
drha3152892007-05-05 11:48:52 +0000134 if( zNew ) memcpy(zNew, z, n);
135 return zNew;
136}
137char *sqlite3StrNDup(const char *z, int n){
138 char *zNew;
139 if( z==0 ) return 0;
danielk19771e536952007-08-16 10:09:01 +0000140 zNew = sqlite3_malloc(n+1);
drha3152892007-05-05 11:48:52 +0000141 if( zNew ){
142 memcpy(zNew, z, n);
143 zNew[n] = 0;
144 }
145 return zNew;
146}
147
danielk19771e536952007-08-16 10:09:01 +0000148char *sqlite3DbStrDup(sqlite3 *db, const char *z){
149 char *zNew = sqlite3StrDup(z);
150 if( z && !zNew ){
151 db->mallocFailed = 1;
152 }
153 return zNew;
154}
155char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
156 char *zNew = sqlite3StrNDup(z, n);
157 if( z && !zNew ){
158 db->mallocFailed = 1;
159 }
160 return zNew;
161}
162
drha3152892007-05-05 11:48:52 +0000163/*
164** Create a string from the 2nd and subsequent arguments (up to the
165** first NULL argument), store the string in memory obtained from
166** sqliteMalloc() and make the pointer indicated by the 1st argument
167** point to that string. The 1st argument must either be NULL or
168** point to memory obtained from sqliteMalloc().
169*/
170void sqlite3SetString(char **pz, ...){
171 va_list ap;
172 int nByte;
173 const char *z;
174 char *zResult;
175
176 assert( pz!=0 );
177 nByte = 1;
178 va_start(ap, pz);
179 while( (z = va_arg(ap, const char*))!=0 ){
180 nByte += strlen(z);
181 }
182 va_end(ap);
danielk19771e536952007-08-16 10:09:01 +0000183 sqlite3_free(*pz);
184 *pz = zResult = sqlite3_malloc(nByte);
drha3152892007-05-05 11:48:52 +0000185 if( zResult==0 ){
186 return;
187 }
188 *zResult = 0;
189 va_start(ap, pz);
190 while( (z = va_arg(ap, const char*))!=0 ){
191 int n = strlen(z);
192 memcpy(zResult, z, n);
193 zResult += n;
194 }
195 zResult[0] = 0;
196 va_end(ap);
197}
198
199
200/*
201** This function must be called before exiting any API function (i.e.
drh17435752007-08-16 04:30:38 +0000202** returning control to the user) that has called sqlite3_malloc or
203** sqlite3_realloc.
drha3152892007-05-05 11:48:52 +0000204**
205** The returned value is normally a copy of the second argument to this
206** function. However, if a malloc() failure has occured since the previous
207** invocation SQLITE_NOMEM is returned instead.
208**
209** If the first argument, db, is not NULL and a malloc() error has occured,
210** then the connection error-code (the value returned by sqlite3_errcode())
211** is set to SQLITE_NOMEM.
212*/
drha3152892007-05-05 11:48:52 +0000213int sqlite3ApiExit(sqlite3* db, int rc){
danielk19771e536952007-08-16 10:09:01 +0000214 if( db && db->mallocFailed ){
drha3152892007-05-05 11:48:52 +0000215 sqlite3Error(db, SQLITE_NOMEM, 0);
drh17435752007-08-16 04:30:38 +0000216 db->mallocFailed = 0;
drha3152892007-05-05 11:48:52 +0000217 rc = SQLITE_NOMEM;
218 }
219 return rc & (db ? db->errMask : 0xff);
220}