blob: bacc713f2476307d14b69e24b2cf841735c249a2 [file] [log] [blame]
drh4c3645c2007-08-15 17:07:57 +00001/*
2** 2007 August 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** This file contains the C functions that implement a memory
13** allocation subsystem for use by SQLite.
14**
danielk1977dbdc4d42008-03-28 07:42:53 +000015** $Id: mem2.c,v 1.24 2008/03/28 07:42:54 danielk1977 Exp $
drh4c3645c2007-08-15 17:07:57 +000016*/
drh0d180202008-02-14 23:26:56 +000017#include "sqliteInt.h"
drh4c3645c2007-08-15 17:07:57 +000018
19/*
20** This version of the memory allocator is used only if the
drh0d180202008-02-14 23:26:56 +000021** SQLITE_MEMDEBUG macro is defined
drh4c3645c2007-08-15 17:07:57 +000022*/
drh0d180202008-02-14 23:26:56 +000023#ifdef SQLITE_MEMDEBUG
drh4c3645c2007-08-15 17:07:57 +000024
25/*
26** The backtrace functionality is only available with GLIBC
27*/
28#ifdef __GLIBC__
29 extern int backtrace(void**,int);
30 extern void backtrace_symbols_fd(void*const*,int,int);
31#else
32# define backtrace(A,B) 0
33# define backtrace_symbols_fd(A,B,C)
34#endif
drh0d180202008-02-14 23:26:56 +000035#include <stdio.h>
drh4c3645c2007-08-15 17:07:57 +000036
drh4c3645c2007-08-15 17:07:57 +000037/*
38** Each memory allocation looks like this:
39**
drh4a50aac2007-08-23 02:47:53 +000040** ------------------------------------------------------------------------
41** | Title | backtrace pointers | MemBlockHdr | allocation | EndGuard |
42** ------------------------------------------------------------------------
drh4c3645c2007-08-15 17:07:57 +000043**
44** The application code sees only a pointer to the allocation. We have
45** to back up from the allocation pointer to find the MemBlockHdr. The
46** MemBlockHdr tells us the size of the allocation and the number of
47** backtrace pointers. There is also a guard word at the end of the
48** MemBlockHdr.
49*/
50struct MemBlockHdr {
51 struct MemBlockHdr *pNext, *pPrev; /* Linked list of all unfreed memory */
drh153c62c2007-08-24 03:51:33 +000052 int iSize; /* Size of this allocation */
53 char nBacktrace; /* Number of backtraces on this alloc */
54 char nBacktraceSlots; /* Available backtrace slots */
55 short nTitle; /* Bytes of title; includes '\0' */
56 int iForeGuard; /* Guard word for sanity */
drh4c3645c2007-08-15 17:07:57 +000057};
58
59/*
60** Guard words
61*/
62#define FOREGUARD 0x80F5E153
63#define REARGUARD 0xE4676B53
64
65/*
drhd2bb3272007-10-15 19:34:32 +000066** Number of malloc size increments to track.
67*/
drh9c7a60d2007-10-19 17:47:24 +000068#define NCSIZE 1000
drhd2bb3272007-10-15 19:34:32 +000069
70/*
drh0e6f1542007-08-15 20:41:28 +000071** All of the static variables used by this module are collected
72** into a single structure named "mem". This is to keep the
73** static variables organized and to reduce namespace pollution
74** when this module is combined with other in the amalgamation.
drh4c3645c2007-08-15 17:07:57 +000075*/
drh0e6f1542007-08-15 20:41:28 +000076static struct {
77 /*
78 ** The alarm callback and its arguments. The mem.mutex lock will
79 ** be held while the callback is running. Recursive calls into
80 ** the memory subsystem are allowed, but no new callbacks will be
81 ** issued. The alarmBusy variable is set to prevent recursive
82 ** callbacks.
83 */
drh153c62c2007-08-24 03:51:33 +000084 sqlite3_int64 alarmThreshold;
85 void (*alarmCallback)(void*, sqlite3_int64, int);
drh0e6f1542007-08-15 20:41:28 +000086 void *alarmArg;
87 int alarmBusy;
88
89 /*
90 ** Mutex to control access to the memory allocation subsystem.
91 */
92 sqlite3_mutex *mutex;
93
94 /*
95 ** Current allocation and high-water mark.
96 */
drh153c62c2007-08-24 03:51:33 +000097 sqlite3_int64 nowUsed;
98 sqlite3_int64 mxUsed;
drh0e6f1542007-08-15 20:41:28 +000099
100 /*
101 ** Head and tail of a linked list of all outstanding allocations
102 */
103 struct MemBlockHdr *pFirst;
104 struct MemBlockHdr *pLast;
105
106 /*
107 ** The number of levels of backtrace to save in new allocations.
108 */
109 int nBacktrace;
danielk19776f332c12008-03-21 14:22:44 +0000110 void (*xBacktrace)(int, int, void **);
drh0e6f1542007-08-15 20:41:28 +0000111
112 /*
drh4a50aac2007-08-23 02:47:53 +0000113 ** Title text to insert in front of each block
114 */
115 int nTitle; /* Bytes of zTitle to save. Includes '\0' and padding */
116 char zTitle[100]; /* The title text */
117
drhd677b3d2007-08-20 22:48:41 +0000118 /*
119 ** sqlite3MallocDisallow() increments the following counter.
120 ** sqlite3MallocAllow() decrements it.
121 */
122 int disallow; /* Do not allow memory allocation */
drhd2bb3272007-10-15 19:34:32 +0000123
124 /*
125 ** Gather statistics on the sizes of memory allocations.
drh9c7a60d2007-10-19 17:47:24 +0000126 ** sizeCnt[i] is the number of allocation attempts of i*8
drhd2bb3272007-10-15 19:34:32 +0000127 ** bytes. i==NCSIZE is the number of allocation attempts for
drh9c7a60d2007-10-19 17:47:24 +0000128 ** sizes more than NCSIZE*8 bytes.
drhd2bb3272007-10-15 19:34:32 +0000129 */
130 int sizeCnt[NCSIZE];
131
drh153c62c2007-08-24 03:51:33 +0000132} mem;
drh0e6f1542007-08-15 20:41:28 +0000133
drh4c3645c2007-08-15 17:07:57 +0000134
135/*
danielk1977ca0c8972007-09-01 09:02:53 +0000136** Enter the mutex mem.mutex. Allocate it if it is not already allocated.
drh4c3645c2007-08-15 17:07:57 +0000137*/
danielk1977ca0c8972007-09-01 09:02:53 +0000138static void enterMem(void){
drh0e6f1542007-08-15 20:41:28 +0000139 if( mem.mutex==0 ){
drh6bdec4a2007-08-16 19:40:16 +0000140 mem.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM);
drh0e6f1542007-08-15 20:41:28 +0000141 }
drh6bdec4a2007-08-16 19:40:16 +0000142 sqlite3_mutex_enter(mem.mutex);
danielk1977ca0c8972007-09-01 09:02:53 +0000143}
144
145/*
146** Return the amount of memory currently checked out.
147*/
148sqlite3_int64 sqlite3_memory_used(void){
149 sqlite3_int64 n;
150 enterMem();
drh0e6f1542007-08-15 20:41:28 +0000151 n = mem.nowUsed;
152 sqlite3_mutex_leave(mem.mutex);
153 return n;
154}
155
156/*
157** Return the maximum amount of memory that has ever been
158** checked out since either the beginning of this process
159** or since the most recent reset.
160*/
drh153c62c2007-08-24 03:51:33 +0000161sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
162 sqlite3_int64 n;
danielk1977ca0c8972007-09-01 09:02:53 +0000163 enterMem();
drh0e6f1542007-08-15 20:41:28 +0000164 n = mem.mxUsed;
165 if( resetFlag ){
166 mem.mxUsed = mem.nowUsed;
167 }
168 sqlite3_mutex_leave(mem.mutex);
169 return n;
170}
171
172/*
173** Change the alarm callback
174*/
175int sqlite3_memory_alarm(
drh153c62c2007-08-24 03:51:33 +0000176 void(*xCallback)(void *pArg, sqlite3_int64 used, int N),
drh0e6f1542007-08-15 20:41:28 +0000177 void *pArg,
drh153c62c2007-08-24 03:51:33 +0000178 sqlite3_int64 iThreshold
drh0e6f1542007-08-15 20:41:28 +0000179){
danielk1977ca0c8972007-09-01 09:02:53 +0000180 enterMem();
drh0e6f1542007-08-15 20:41:28 +0000181 mem.alarmCallback = xCallback;
182 mem.alarmArg = pArg;
183 mem.alarmThreshold = iThreshold;
184 sqlite3_mutex_leave(mem.mutex);
185 return SQLITE_OK;
186}
187
188/*
189** Trigger the alarm
190*/
drh153c62c2007-08-24 03:51:33 +0000191static void sqlite3MemsysAlarm(int nByte){
192 void (*xCallback)(void*,sqlite3_int64,int);
193 sqlite3_int64 nowUsed;
drh6bdec4a2007-08-16 19:40:16 +0000194 void *pArg;
drh0e6f1542007-08-15 20:41:28 +0000195 if( mem.alarmCallback==0 || mem.alarmBusy ) return;
196 mem.alarmBusy = 1;
drh6bdec4a2007-08-16 19:40:16 +0000197 xCallback = mem.alarmCallback;
198 nowUsed = mem.nowUsed;
199 pArg = mem.alarmArg;
200 sqlite3_mutex_leave(mem.mutex);
201 xCallback(pArg, nowUsed, nByte);
202 sqlite3_mutex_enter(mem.mutex);
drh0e6f1542007-08-15 20:41:28 +0000203 mem.alarmBusy = 0;
204}
drh4c3645c2007-08-15 17:07:57 +0000205
206/*
207** Given an allocation, find the MemBlockHdr for that allocation.
208**
209** This routine checks the guards at either end of the allocation and
210** if they are incorrect it asserts.
211*/
212static struct MemBlockHdr *sqlite3MemsysGetHeader(void *pAllocation){
213 struct MemBlockHdr *p;
drh153c62c2007-08-24 03:51:33 +0000214 int *pInt;
drh4c3645c2007-08-15 17:07:57 +0000215
216 p = (struct MemBlockHdr*)pAllocation;
217 p--;
218 assert( p->iForeGuard==FOREGUARD );
219 assert( (p->iSize & 3)==0 );
drh153c62c2007-08-24 03:51:33 +0000220 pInt = (int*)pAllocation;
221 assert( pInt[p->iSize/sizeof(int)]==REARGUARD );
drh4c3645c2007-08-15 17:07:57 +0000222 return p;
223}
224
225/*
danielk1977a7a8e142008-02-13 18:25:27 +0000226** Return the number of bytes currently allocated at address p.
227*/
228int sqlite3MallocSize(void *p){
229 struct MemBlockHdr *pHdr;
230 if( !p ){
231 return 0;
232 }
233 pHdr = sqlite3MemsysGetHeader(p);
234 return pHdr->iSize;
235}
236
237/*
drh0e6f1542007-08-15 20:41:28 +0000238** Allocate nByte bytes of memory.
drh4c3645c2007-08-15 17:07:57 +0000239*/
drhf3a65f72007-08-22 20:18:21 +0000240void *sqlite3_malloc(int nByte){
drh4c3645c2007-08-15 17:07:57 +0000241 struct MemBlockHdr *pHdr;
242 void **pBt;
drh4a50aac2007-08-23 02:47:53 +0000243 char *z;
drh153c62c2007-08-24 03:51:33 +0000244 int *pInt;
danielk1977ca0c8972007-09-01 09:02:53 +0000245 void *p = 0;
drh153c62c2007-08-24 03:51:33 +0000246 int totalSize;
drh4c3645c2007-08-15 17:07:57 +0000247
danielk1977ca0c8972007-09-01 09:02:53 +0000248 if( nByte>0 ){
249 enterMem();
250 assert( mem.disallow==0 );
251 if( mem.alarmCallback!=0 && mem.nowUsed+nByte>=mem.alarmThreshold ){
drh0e6f1542007-08-15 20:41:28 +0000252 sqlite3MemsysAlarm(nByte);
danielk1977ca0c8972007-09-01 09:02:53 +0000253 }
254 nByte = (nByte+3)&~3;
drhd2bb3272007-10-15 19:34:32 +0000255 if( nByte/8>NCSIZE-1 ){
256 mem.sizeCnt[NCSIZE-1]++;
257 }else{
258 mem.sizeCnt[nByte/8]++;
259 }
danielk1977ca0c8972007-09-01 09:02:53 +0000260 totalSize = nByte + sizeof(*pHdr) + sizeof(int) +
261 mem.nBacktrace*sizeof(void*) + mem.nTitle;
drh643167f2008-01-22 21:30:53 +0000262 if( sqlite3FaultStep(SQLITE_FAULTINJECTOR_MALLOC) ){
263 p = 0;
danielk1977ca0c8972007-09-01 09:02:53 +0000264 }else{
drh0e6f1542007-08-15 20:41:28 +0000265 p = malloc(totalSize);
danielk1977ca0c8972007-09-01 09:02:53 +0000266 if( p==0 ){
267 sqlite3MemsysAlarm(nByte);
268 p = malloc(totalSize);
269 }
drh0e6f1542007-08-15 20:41:28 +0000270 }
danielk1977ca0c8972007-09-01 09:02:53 +0000271 if( p ){
272 z = p;
273 pBt = (void**)&z[mem.nTitle];
274 pHdr = (struct MemBlockHdr*)&pBt[mem.nBacktrace];
275 pHdr->pNext = 0;
276 pHdr->pPrev = mem.pLast;
277 if( mem.pLast ){
278 mem.pLast->pNext = pHdr;
279 }else{
280 mem.pFirst = pHdr;
281 }
282 mem.pLast = pHdr;
283 pHdr->iForeGuard = FOREGUARD;
284 pHdr->nBacktraceSlots = mem.nBacktrace;
285 pHdr->nTitle = mem.nTitle;
286 if( mem.nBacktrace ){
287 void *aAddr[40];
288 pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1;
289 memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*));
danielk19776f332c12008-03-21 14:22:44 +0000290 if( mem.xBacktrace ){
291 mem.xBacktrace(nByte, pHdr->nBacktrace-1, &aAddr[1]);
292 }
danielk1977ca0c8972007-09-01 09:02:53 +0000293 }else{
294 pHdr->nBacktrace = 0;
295 }
296 if( mem.nTitle ){
297 memcpy(z, mem.zTitle, mem.nTitle);
298 }
299 pHdr->iSize = nByte;
300 pInt = (int*)&pHdr[1];
301 pInt[nByte/sizeof(int)] = REARGUARD;
302 memset(pInt, 0x65, nByte);
303 mem.nowUsed += nByte;
304 if( mem.nowUsed>mem.mxUsed ){
305 mem.mxUsed = mem.nowUsed;
306 }
307 p = (void*)pInt;
308 }
309 sqlite3_mutex_leave(mem.mutex);
drh4c3645c2007-08-15 17:07:57 +0000310 }
drh4c3645c2007-08-15 17:07:57 +0000311 return p;
312}
313
314/*
315** Free memory.
316*/
317void sqlite3_free(void *pPrior){
318 struct MemBlockHdr *pHdr;
319 void **pBt;
drh4a50aac2007-08-23 02:47:53 +0000320 char *z;
drh4c3645c2007-08-15 17:07:57 +0000321 if( pPrior==0 ){
322 return;
323 }
drh0e6f1542007-08-15 20:41:28 +0000324 assert( mem.mutex!=0 );
drh4c3645c2007-08-15 17:07:57 +0000325 pHdr = sqlite3MemsysGetHeader(pPrior);
326 pBt = (void**)pHdr;
327 pBt -= pHdr->nBacktraceSlots;
drh6bdec4a2007-08-16 19:40:16 +0000328 sqlite3_mutex_enter(mem.mutex);
drh0e6f1542007-08-15 20:41:28 +0000329 mem.nowUsed -= pHdr->iSize;
drh4c3645c2007-08-15 17:07:57 +0000330 if( pHdr->pPrev ){
331 assert( pHdr->pPrev->pNext==pHdr );
332 pHdr->pPrev->pNext = pHdr->pNext;
333 }else{
drh0e6f1542007-08-15 20:41:28 +0000334 assert( mem.pFirst==pHdr );
335 mem.pFirst = pHdr->pNext;
drh4c3645c2007-08-15 17:07:57 +0000336 }
337 if( pHdr->pNext ){
338 assert( pHdr->pNext->pPrev==pHdr );
339 pHdr->pNext->pPrev = pHdr->pPrev;
340 }else{
drh0e6f1542007-08-15 20:41:28 +0000341 assert( mem.pLast==pHdr );
342 mem.pLast = pHdr->pPrev;
drh4c3645c2007-08-15 17:07:57 +0000343 }
drh4a50aac2007-08-23 02:47:53 +0000344 z = (char*)pBt;
345 z -= pHdr->nTitle;
346 memset(z, 0x2b, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
drh153c62c2007-08-24 03:51:33 +0000347 pHdr->iSize + sizeof(int) + pHdr->nTitle);
drh4a50aac2007-08-23 02:47:53 +0000348 free(z);
drh0e6f1542007-08-15 20:41:28 +0000349 sqlite3_mutex_leave(mem.mutex);
drh4c3645c2007-08-15 17:07:57 +0000350}
351
352/*
353** Change the size of an existing memory allocation.
354**
355** For this debugging implementation, we *always* make a copy of the
356** allocation into a new place in memory. In this way, if the
357** higher level code is using pointer to the old allocation, it is
358** much more likely to break and we are much more liking to find
359** the error.
360*/
drhf3a65f72007-08-22 20:18:21 +0000361void *sqlite3_realloc(void *pPrior, int nByte){
drh4c3645c2007-08-15 17:07:57 +0000362 struct MemBlockHdr *pOldHdr;
363 void *pNew;
drh4c3645c2007-08-15 17:07:57 +0000364 if( pPrior==0 ){
365 return sqlite3_malloc(nByte);
366 }
drhf3a65f72007-08-22 20:18:21 +0000367 if( nByte<=0 ){
drh4c3645c2007-08-15 17:07:57 +0000368 sqlite3_free(pPrior);
drh2f999a62007-08-15 19:16:43 +0000369 return 0;
drh4c3645c2007-08-15 17:07:57 +0000370 }
drhd677b3d2007-08-20 22:48:41 +0000371 assert( mem.disallow==0 );
drh4c3645c2007-08-15 17:07:57 +0000372 pOldHdr = sqlite3MemsysGetHeader(pPrior);
373 pNew = sqlite3_malloc(nByte);
374 if( pNew ){
375 memcpy(pNew, pPrior, nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize);
376 if( nByte>pOldHdr->iSize ){
377 memset(&((char*)pNew)[pOldHdr->iSize], 0x2b, nByte - pOldHdr->iSize);
378 }
379 sqlite3_free(pPrior);
380 }
381 return pNew;
382}
383
384/*
385** Set the number of backtrace levels kept for each allocation.
386** A value of zero turns of backtracing. The number is always rounded
387** up to a multiple of 2.
388*/
drh49e4fd72008-02-19 15:15:15 +0000389void sqlite3MemdebugBacktrace(int depth){
drh4c3645c2007-08-15 17:07:57 +0000390 if( depth<0 ){ depth = 0; }
391 if( depth>20 ){ depth = 20; }
drh2f999a62007-08-15 19:16:43 +0000392 depth = (depth+1)&0xfe;
drh0e6f1542007-08-15 20:41:28 +0000393 mem.nBacktrace = depth;
drh4c3645c2007-08-15 17:07:57 +0000394}
395
danielk19776f332c12008-03-21 14:22:44 +0000396void sqlite3MemdebugBacktraceCallback(void (*xBacktrace)(int, int, void **)){
397 mem.xBacktrace = xBacktrace;
398}
399
drh4c3645c2007-08-15 17:07:57 +0000400/*
drh4a50aac2007-08-23 02:47:53 +0000401** Set the title string for subsequent allocations.
402*/
drh49e4fd72008-02-19 15:15:15 +0000403void sqlite3MemdebugSettitle(const char *zTitle){
drh4a50aac2007-08-23 02:47:53 +0000404 int n = strlen(zTitle) + 1;
danielk1977ca0c8972007-09-01 09:02:53 +0000405 enterMem();
drh4a50aac2007-08-23 02:47:53 +0000406 if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1;
407 memcpy(mem.zTitle, zTitle, n);
408 mem.zTitle[n] = 0;
409 mem.nTitle = (n+3)&~3;
410 sqlite3_mutex_leave(mem.mutex);
411}
412
danielk1977dbdc4d42008-03-28 07:42:53 +0000413void sqlite3MemdebugSync(){
414 struct MemBlockHdr *pHdr;
415 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
416 void **pBt = (void**)pHdr;
417 pBt -= pHdr->nBacktraceSlots;
418 mem.xBacktrace(pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]);
419 }
420}
421
drh4a50aac2007-08-23 02:47:53 +0000422/*
drh4c3645c2007-08-15 17:07:57 +0000423** Open the file indicated and write a log of all unfreed memory
424** allocations into that log.
425*/
drh49e4fd72008-02-19 15:15:15 +0000426void sqlite3MemdebugDump(const char *zFilename){
drh4c3645c2007-08-15 17:07:57 +0000427 FILE *out;
428 struct MemBlockHdr *pHdr;
429 void **pBt;
drhd2bb3272007-10-15 19:34:32 +0000430 int i;
drh4c3645c2007-08-15 17:07:57 +0000431 out = fopen(zFilename, "w");
432 if( out==0 ){
433 fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
434 zFilename);
435 return;
436 }
drh0e6f1542007-08-15 20:41:28 +0000437 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
drh4a50aac2007-08-23 02:47:53 +0000438 char *z = (char*)pHdr;
439 z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle;
drhd5499d62007-08-24 04:15:00 +0000440 fprintf(out, "**** %d bytes at %p from %s ****\n",
441 pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???");
drh4c3645c2007-08-15 17:07:57 +0000442 if( pHdr->nBacktrace ){
443 fflush(out);
444 pBt = (void**)pHdr;
445 pBt -= pHdr->nBacktraceSlots;
446 backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out));
447 fprintf(out, "\n");
448 }
449 }
drhd2bb3272007-10-15 19:34:32 +0000450 fprintf(out, "COUNTS:\n");
451 for(i=0; i<NCSIZE-1; i++){
452 if( mem.sizeCnt[i] ){
453 fprintf(out, " %3d: %d\n", i*8+8, mem.sizeCnt[i]);
454 }
455 }
456 if( mem.sizeCnt[NCSIZE-1] ){
457 fprintf(out, " >%3d: %d\n", NCSIZE*8, mem.sizeCnt[NCSIZE-1]);
458 }
drh4c3645c2007-08-15 17:07:57 +0000459 fclose(out);
460}
461
danielk1977a7a8e142008-02-13 18:25:27 +0000462/*
463** Return the number of times sqlite3_malloc() has been called.
464*/
drh49e4fd72008-02-19 15:15:15 +0000465int sqlite3MemdebugMallocCount(){
danielk1977a7a8e142008-02-13 18:25:27 +0000466 int i;
467 int nTotal = 0;
468 for(i=0; i<NCSIZE; i++){
469 nTotal += mem.sizeCnt[i];
470 }
471 return nTotal;
472}
473
drhd677b3d2007-08-20 22:48:41 +0000474
drh0d180202008-02-14 23:26:56 +0000475#endif /* SQLITE_MEMDEBUG */