blob: adafe3daf9ce3ddd7f64e938fe737814101ae85c [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**
danielk19771e536952007-08-16 10:09:01 +000015** $Id: malloc.c,v 1.6 2007/08/16 10:09:03 danielk1977 Exp $
drha3152892007-05-05 11:48:52 +000016*/
17#include "sqliteInt.h"
18#include "os.h"
19#include <stdarg.h>
20#include <ctype.h>
21
22/*
drha3152892007-05-05 11:48:52 +000023** Set the soft heap-size limit for the current thread. Passing a negative
24** value indicates no limit.
25*/
26void sqlite3_soft_heap_limit(int n){
danielk19771e536952007-08-16 10:09:01 +000027#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drha3152892007-05-05 11:48:52 +000028 ThreadData *pTd = sqlite3ThreadData();
29 if( pTd ){
30 pTd->nSoftHeapLimit = n;
31 }
32 sqlite3ReleaseThreadData();
danielk19771e536952007-08-16 10:09:01 +000033#endif
drha3152892007-05-05 11:48:52 +000034}
35
36/*
37** Release memory held by SQLite instances created by the current thread.
38*/
39int sqlite3_release_memory(int n){
danielk19771e536952007-08-16 10:09:01 +000040#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO)
drha3152892007-05-05 11:48:52 +000041 return sqlite3PagerReleaseMemory(n);
danielk19771e536952007-08-16 10:09:01 +000042#else
43 return SQLITE_OK;
44#endif
drha3152892007-05-05 11:48:52 +000045}
drha3152892007-05-05 11:48:52 +000046
drha3152892007-05-05 11:48:52 +000047
48/*
drh17435752007-08-16 04:30:38 +000049** Allocate and zero memory.
drha3152892007-05-05 11:48:52 +000050*/
drh17435752007-08-16 04:30:38 +000051void *sqlite3MallocZero(unsigned n){
52 void *p = sqlite3_malloc(n);
drha3152892007-05-05 11:48:52 +000053 if( p ){
54 memset(p, 0, n);
55 }
56 return p;
57}
drh17435752007-08-16 04:30:38 +000058
59/*
60** Allocate and zero memory. If the allocation fails, make
61** the mallocFailed flag in the connection pointer.
62*/
63void *sqlite3DbMallocZero(sqlite3 *db, unsigned n){
64 void *p = sqlite3_malloc(n);
65 if( p ){
66 memset(p, 0, n);
67 }else{
68 db->mallocFailed = 1;
69 }
70 return p;
71}
72
73/*
74** Allocate and zero memory. If the allocation fails, make
75** the mallocFailed flag in the connection pointer.
76*/
77void *sqlite3DbMallocRaw(sqlite3 *db, unsigned n){
78 void *p = sqlite3_malloc(n);
79 if( !p ){
80 db->mallocFailed = 1;
81 }
82 return p;
83}
84
85/*
86** Attempt to reallocate p. If the reallocation fails, then free p
87** and set the mallocFailed flag in the database connection.
88*/
89void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
drha3152892007-05-05 11:48:52 +000090 void *pNew;
drh17435752007-08-16 04:30:38 +000091 pNew = sqlite3_realloc(p, n);
drha3152892007-05-05 11:48:52 +000092 if( !pNew ){
danielk19771e536952007-08-16 10:09:01 +000093 sqlite3_free(p);
drh17435752007-08-16 04:30:38 +000094 db->mallocFailed = 1;
drha3152892007-05-05 11:48:52 +000095 }
96 return pNew;
97}
98
drha3152892007-05-05 11:48:52 +000099
100/*
101** Make a copy of a string in memory obtained from sqliteMalloc(). These
102** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
103** is because when memory debugging is turned on, these two functions are
104** called via macros that record the current file and line number in the
105** ThreadData structure.
106*/
107char *sqlite3StrDup(const char *z){
108 char *zNew;
109 int n;
110 if( z==0 ) return 0;
111 n = strlen(z)+1;
danielk19771e536952007-08-16 10:09:01 +0000112 zNew = sqlite3_malloc(n);
drha3152892007-05-05 11:48:52 +0000113 if( zNew ) memcpy(zNew, z, n);
114 return zNew;
115}
116char *sqlite3StrNDup(const char *z, int n){
117 char *zNew;
118 if( z==0 ) return 0;
danielk19771e536952007-08-16 10:09:01 +0000119 zNew = sqlite3_malloc(n+1);
drha3152892007-05-05 11:48:52 +0000120 if( zNew ){
121 memcpy(zNew, z, n);
122 zNew[n] = 0;
123 }
124 return zNew;
125}
126
danielk19771e536952007-08-16 10:09:01 +0000127char *sqlite3DbStrDup(sqlite3 *db, const char *z){
128 char *zNew = sqlite3StrDup(z);
129 if( z && !zNew ){
130 db->mallocFailed = 1;
131 }
132 return zNew;
133}
134char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
135 char *zNew = sqlite3StrNDup(z, n);
136 if( z && !zNew ){
137 db->mallocFailed = 1;
138 }
139 return zNew;
140}
141
drha3152892007-05-05 11:48:52 +0000142/*
143** Create a string from the 2nd and subsequent arguments (up to the
144** first NULL argument), store the string in memory obtained from
145** sqliteMalloc() and make the pointer indicated by the 1st argument
146** point to that string. The 1st argument must either be NULL or
147** point to memory obtained from sqliteMalloc().
148*/
149void sqlite3SetString(char **pz, ...){
150 va_list ap;
151 int nByte;
152 const char *z;
153 char *zResult;
154
155 assert( pz!=0 );
156 nByte = 1;
157 va_start(ap, pz);
158 while( (z = va_arg(ap, const char*))!=0 ){
159 nByte += strlen(z);
160 }
161 va_end(ap);
danielk19771e536952007-08-16 10:09:01 +0000162 sqlite3_free(*pz);
163 *pz = zResult = sqlite3_malloc(nByte);
drha3152892007-05-05 11:48:52 +0000164 if( zResult==0 ){
165 return;
166 }
167 *zResult = 0;
168 va_start(ap, pz);
169 while( (z = va_arg(ap, const char*))!=0 ){
170 int n = strlen(z);
171 memcpy(zResult, z, n);
172 zResult += n;
173 }
174 zResult[0] = 0;
175 va_end(ap);
176}
177
178
179/*
180** This function must be called before exiting any API function (i.e.
drh17435752007-08-16 04:30:38 +0000181** returning control to the user) that has called sqlite3_malloc or
182** sqlite3_realloc.
drha3152892007-05-05 11:48:52 +0000183**
184** The returned value is normally a copy of the second argument to this
185** function. However, if a malloc() failure has occured since the previous
186** invocation SQLITE_NOMEM is returned instead.
187**
188** If the first argument, db, is not NULL and a malloc() error has occured,
189** then the connection error-code (the value returned by sqlite3_errcode())
190** is set to SQLITE_NOMEM.
191*/
drha3152892007-05-05 11:48:52 +0000192int sqlite3ApiExit(sqlite3* db, int rc){
danielk19771e536952007-08-16 10:09:01 +0000193 if( db && db->mallocFailed ){
drha3152892007-05-05 11:48:52 +0000194 sqlite3Error(db, SQLITE_NOMEM, 0);
drh17435752007-08-16 04:30:38 +0000195 db->mallocFailed = 0;
drha3152892007-05-05 11:48:52 +0000196 rc = SQLITE_NOMEM;
197 }
198 return rc & (db ? db->errMask : 0xff);
199}
200
drha3152892007-05-05 11:48:52 +0000201#ifdef SQLITE_MEMDEBUG
202/*
203** This function sets a flag in the thread-specific-data structure that will
204** cause an assert to fail if sqliteMalloc() or sqliteRealloc() is called.
205*/
danielk19771e536952007-08-16 10:09:01 +0000206#if 0
drha3152892007-05-05 11:48:52 +0000207void sqlite3MallocDisallow(){
drh17435752007-08-16 04:30:38 +0000208#if 0
drha3152892007-05-05 11:48:52 +0000209 assert( sqlite3_mallocDisallowed>=0 );
210 sqlite3_mallocDisallowed++;
drh17435752007-08-16 04:30:38 +0000211#endif
drha3152892007-05-05 11:48:52 +0000212}
213
214/*
215** This function clears the flag set in the thread-specific-data structure set
216** by sqlite3MallocDisallow().
217*/
218void sqlite3MallocAllow(){
drh17435752007-08-16 04:30:38 +0000219#if 0
drha3152892007-05-05 11:48:52 +0000220 assert( sqlite3_mallocDisallowed>0 );
221 sqlite3_mallocDisallowed--;
drh17435752007-08-16 04:30:38 +0000222#endif
drha3152892007-05-05 11:48:52 +0000223}
224#endif
danielk19771e536952007-08-16 10:09:01 +0000225#endif