blob: e0a678eaee7ae571523d978d45410e0bbe3ae917 [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**
drh17435752007-08-16 04:30:38 +000015** $Id: malloc.c,v 1.5 2007/08/16 04:30:40 drh 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){
27 ThreadData *pTd = sqlite3ThreadData();
28 if( pTd ){
29 pTd->nSoftHeapLimit = n;
30 }
31 sqlite3ReleaseThreadData();
32}
33
34/*
35** Release memory held by SQLite instances created by the current thread.
36*/
37int sqlite3_release_memory(int n){
38 return sqlite3PagerReleaseMemory(n);
39}
drha3152892007-05-05 11:48:52 +000040
drha3152892007-05-05 11:48:52 +000041
42/*
drh17435752007-08-16 04:30:38 +000043** Allocate and zero memory.
drha3152892007-05-05 11:48:52 +000044*/
drh17435752007-08-16 04:30:38 +000045void *sqlite3MallocZero(unsigned n){
46 void *p = sqlite3_malloc(n);
drha3152892007-05-05 11:48:52 +000047 if( p ){
48 memset(p, 0, n);
49 }
50 return p;
51}
drh17435752007-08-16 04:30:38 +000052
53/*
54** Allocate and zero memory. If the allocation fails, make
55** the mallocFailed flag in the connection pointer.
56*/
57void *sqlite3DbMallocZero(sqlite3 *db, unsigned n){
58 void *p = sqlite3_malloc(n);
59 if( p ){
60 memset(p, 0, n);
61 }else{
62 db->mallocFailed = 1;
63 }
64 return p;
65}
66
67/*
68** Allocate and zero memory. If the allocation fails, make
69** the mallocFailed flag in the connection pointer.
70*/
71void *sqlite3DbMallocRaw(sqlite3 *db, unsigned n){
72 void *p = sqlite3_malloc(n);
73 if( !p ){
74 db->mallocFailed = 1;
75 }
76 return p;
77}
78
79/*
80** Attempt to reallocate p. If the reallocation fails, then free p
81** and set the mallocFailed flag in the database connection.
82*/
83void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
drha3152892007-05-05 11:48:52 +000084 void *pNew;
drh17435752007-08-16 04:30:38 +000085 pNew = sqlite3_realloc(p, n);
drha3152892007-05-05 11:48:52 +000086 if( !pNew ){
87 sqlite3FreeX(p);
drh17435752007-08-16 04:30:38 +000088 db->mallocFailed = 1;
drha3152892007-05-05 11:48:52 +000089 }
90 return pNew;
91}
92
drha3152892007-05-05 11:48:52 +000093
94/*
95** Make a copy of a string in memory obtained from sqliteMalloc(). These
96** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
97** is because when memory debugging is turned on, these two functions are
98** called via macros that record the current file and line number in the
99** ThreadData structure.
100*/
101char *sqlite3StrDup(const char *z){
102 char *zNew;
103 int n;
104 if( z==0 ) return 0;
105 n = strlen(z)+1;
106 zNew = sqlite3MallocRaw(n, 1);
107 if( zNew ) memcpy(zNew, z, n);
108 return zNew;
109}
110char *sqlite3StrNDup(const char *z, int n){
111 char *zNew;
112 if( z==0 ) return 0;
113 zNew = sqlite3MallocRaw(n+1, 1);
114 if( zNew ){
115 memcpy(zNew, z, n);
116 zNew[n] = 0;
117 }
118 return zNew;
119}
120
121/*
122** Create a string from the 2nd and subsequent arguments (up to the
123** first NULL argument), store the string in memory obtained from
124** sqliteMalloc() and make the pointer indicated by the 1st argument
125** point to that string. The 1st argument must either be NULL or
126** point to memory obtained from sqliteMalloc().
127*/
128void sqlite3SetString(char **pz, ...){
129 va_list ap;
130 int nByte;
131 const char *z;
132 char *zResult;
133
134 assert( pz!=0 );
135 nByte = 1;
136 va_start(ap, pz);
137 while( (z = va_arg(ap, const char*))!=0 ){
138 nByte += strlen(z);
139 }
140 va_end(ap);
141 sqliteFree(*pz);
142 *pz = zResult = sqliteMallocRaw( nByte );
143 if( zResult==0 ){
144 return;
145 }
146 *zResult = 0;
147 va_start(ap, pz);
148 while( (z = va_arg(ap, const char*))!=0 ){
149 int n = strlen(z);
150 memcpy(zResult, z, n);
151 zResult += n;
152 }
153 zResult[0] = 0;
154 va_end(ap);
155}
156
157
158/*
159** This function must be called before exiting any API function (i.e.
drh17435752007-08-16 04:30:38 +0000160** returning control to the user) that has called sqlite3_malloc or
161** sqlite3_realloc.
drha3152892007-05-05 11:48:52 +0000162**
163** The returned value is normally a copy of the second argument to this
164** function. However, if a malloc() failure has occured since the previous
165** invocation SQLITE_NOMEM is returned instead.
166**
167** If the first argument, db, is not NULL and a malloc() error has occured,
168** then the connection error-code (the value returned by sqlite3_errcode())
169** is set to SQLITE_NOMEM.
170*/
drha3152892007-05-05 11:48:52 +0000171int sqlite3ApiExit(sqlite3* db, int rc){
drh17435752007-08-16 04:30:38 +0000172 if( db->mallocFailed ){
drha3152892007-05-05 11:48:52 +0000173 sqlite3Error(db, SQLITE_NOMEM, 0);
drh17435752007-08-16 04:30:38 +0000174 db->mallocFailed = 0;
drha3152892007-05-05 11:48:52 +0000175 rc = SQLITE_NOMEM;
176 }
177 return rc & (db ? db->errMask : 0xff);
178}
179
drha3152892007-05-05 11:48:52 +0000180#ifdef SQLITE_MEMDEBUG
181/*
182** This function sets a flag in the thread-specific-data structure that will
183** cause an assert to fail if sqliteMalloc() or sqliteRealloc() is called.
184*/
185void sqlite3MallocDisallow(){
drh17435752007-08-16 04:30:38 +0000186#if 0
drha3152892007-05-05 11:48:52 +0000187 assert( sqlite3_mallocDisallowed>=0 );
188 sqlite3_mallocDisallowed++;
drh17435752007-08-16 04:30:38 +0000189#endif
drha3152892007-05-05 11:48:52 +0000190}
191
192/*
193** This function clears the flag set in the thread-specific-data structure set
194** by sqlite3MallocDisallow().
195*/
196void sqlite3MallocAllow(){
drh17435752007-08-16 04:30:38 +0000197#if 0
drha3152892007-05-05 11:48:52 +0000198 assert( sqlite3_mallocDisallowed>0 );
199 sqlite3_mallocDisallowed--;
drh17435752007-08-16 04:30:38 +0000200#endif
drha3152892007-05-05 11:48:52 +0000201}
202#endif