blob: 760e77ca2194bef6056eea450a2f560ccfc482e8 [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*************************************************************************
drh4c3645c2007-08-15 17:07:57 +000012**
drhfec00ea2008-06-14 16:56:21 +000013** This file contains low-level memory allocation drivers for when
14** SQLite will use the standard C-library malloc/realloc/free interface
15** to obtain the memory it needs while adding lots of additional debugging
16** information to each allocation in order to help detect and fix memory
17** leaks and memory usage errors.
18**
19** This file contains implementations of the low-level memory allocation
20** routines specified in the sqlite3_mem_methods object.
drh4c3645c2007-08-15 17:07:57 +000021*/
drh0d180202008-02-14 23:26:56 +000022#include "sqliteInt.h"
drh4c3645c2007-08-15 17:07:57 +000023
24/*
25** This version of the memory allocator is used only if the
drh0d180202008-02-14 23:26:56 +000026** SQLITE_MEMDEBUG macro is defined
drh4c3645c2007-08-15 17:07:57 +000027*/
drh0d180202008-02-14 23:26:56 +000028#ifdef SQLITE_MEMDEBUG
drh4c3645c2007-08-15 17:07:57 +000029
30/*
31** The backtrace functionality is only available with GLIBC
32*/
33#ifdef __GLIBC__
34 extern int backtrace(void**,int);
35 extern void backtrace_symbols_fd(void*const*,int,int);
36#else
danielk197744a376f2008-08-12 15:04:58 +000037# define backtrace(A,B) 1
drh4c3645c2007-08-15 17:07:57 +000038# define backtrace_symbols_fd(A,B,C)
39#endif
drh0d180202008-02-14 23:26:56 +000040#include <stdio.h>
drh4c3645c2007-08-15 17:07:57 +000041
drh4c3645c2007-08-15 17:07:57 +000042/*
43** Each memory allocation looks like this:
44**
drh4a50aac2007-08-23 02:47:53 +000045** ------------------------------------------------------------------------
46** | Title | backtrace pointers | MemBlockHdr | allocation | EndGuard |
47** ------------------------------------------------------------------------
drh4c3645c2007-08-15 17:07:57 +000048**
49** The application code sees only a pointer to the allocation. We have
50** to back up from the allocation pointer to find the MemBlockHdr. The
51** MemBlockHdr tells us the size of the allocation and the number of
52** backtrace pointers. There is also a guard word at the end of the
53** MemBlockHdr.
54*/
55struct MemBlockHdr {
drhfab69592008-04-10 14:57:24 +000056 i64 iSize; /* Size of this allocation */
drh4c3645c2007-08-15 17:07:57 +000057 struct MemBlockHdr *pNext, *pPrev; /* Linked list of all unfreed memory */
drh153c62c2007-08-24 03:51:33 +000058 char nBacktrace; /* Number of backtraces on this alloc */
59 char nBacktraceSlots; /* Available backtrace slots */
drh107b56e2010-03-12 16:32:53 +000060 u8 nTitle; /* Bytes of title; includes '\0' */
61 u8 eType; /* Allocation type code */
drh153c62c2007-08-24 03:51:33 +000062 int iForeGuard; /* Guard word for sanity */
drh4c3645c2007-08-15 17:07:57 +000063};
64
65/*
66** Guard words
67*/
68#define FOREGUARD 0x80F5E153
69#define REARGUARD 0xE4676B53
70
71/*
drhd2bb3272007-10-15 19:34:32 +000072** Number of malloc size increments to track.
73*/
drh9c7a60d2007-10-19 17:47:24 +000074#define NCSIZE 1000
drhd2bb3272007-10-15 19:34:32 +000075
76/*
drh0e6f1542007-08-15 20:41:28 +000077** All of the static variables used by this module are collected
78** into a single structure named "mem". This is to keep the
79** static variables organized and to reduce namespace pollution
80** when this module is combined with other in the amalgamation.
drh4c3645c2007-08-15 17:07:57 +000081*/
drh0e6f1542007-08-15 20:41:28 +000082static struct {
drh0e6f1542007-08-15 20:41:28 +000083
84 /*
85 ** Mutex to control access to the memory allocation subsystem.
86 */
87 sqlite3_mutex *mutex;
drhfec00ea2008-06-14 16:56:21 +000088
drh0e6f1542007-08-15 20:41:28 +000089 /*
90 ** Head and tail of a linked list of all outstanding allocations
91 */
92 struct MemBlockHdr *pFirst;
93 struct MemBlockHdr *pLast;
94
95 /*
96 ** The number of levels of backtrace to save in new allocations.
97 */
98 int nBacktrace;
danielk19776f332c12008-03-21 14:22:44 +000099 void (*xBacktrace)(int, int, void **);
drh0e6f1542007-08-15 20:41:28 +0000100
101 /*
drh4a50aac2007-08-23 02:47:53 +0000102 ** Title text to insert in front of each block
103 */
104 int nTitle; /* Bytes of zTitle to save. Includes '\0' and padding */
105 char zTitle[100]; /* The title text */
106
drhd677b3d2007-08-20 22:48:41 +0000107 /*
108 ** sqlite3MallocDisallow() increments the following counter.
109 ** sqlite3MallocAllow() decrements it.
110 */
111 int disallow; /* Do not allow memory allocation */
drhd2bb3272007-10-15 19:34:32 +0000112
113 /*
114 ** Gather statistics on the sizes of memory allocations.
drh2abcd582008-07-24 23:34:07 +0000115 ** nAlloc[i] is the number of allocation attempts of i*8
drhd2bb3272007-10-15 19:34:32 +0000116 ** bytes. i==NCSIZE is the number of allocation attempts for
drh9c7a60d2007-10-19 17:47:24 +0000117 ** sizes more than NCSIZE*8 bytes.
drhd2bb3272007-10-15 19:34:32 +0000118 */
drh2abcd582008-07-24 23:34:07 +0000119 int nAlloc[NCSIZE]; /* Total number of allocations */
120 int nCurrent[NCSIZE]; /* Current number of allocations */
121 int mxCurrent[NCSIZE]; /* Highwater mark for nCurrent */
drhd2bb3272007-10-15 19:34:32 +0000122
drh153c62c2007-08-24 03:51:33 +0000123} mem;
drh0e6f1542007-08-15 20:41:28 +0000124
drh2abcd582008-07-24 23:34:07 +0000125
126/*
127** Adjust memory usage statistics
128*/
129static void adjustStats(int iSize, int increment){
danielk1977bc739712009-03-23 04:33:32 +0000130 int i = ROUND8(iSize)/8;
drh2abcd582008-07-24 23:34:07 +0000131 if( i>NCSIZE-1 ){
132 i = NCSIZE - 1;
133 }
134 if( increment>0 ){
135 mem.nAlloc[i]++;
136 mem.nCurrent[i]++;
137 if( mem.nCurrent[i]>mem.mxCurrent[i] ){
138 mem.mxCurrent[i] = mem.nCurrent[i];
139 }
140 }else{
141 mem.nCurrent[i]--;
142 assert( mem.nCurrent[i]>=0 );
143 }
144}
145
drh4c3645c2007-08-15 17:07:57 +0000146/*
147** Given an allocation, find the MemBlockHdr for that allocation.
148**
149** This routine checks the guards at either end of the allocation and
150** if they are incorrect it asserts.
151*/
152static struct MemBlockHdr *sqlite3MemsysGetHeader(void *pAllocation){
153 struct MemBlockHdr *p;
drh153c62c2007-08-24 03:51:33 +0000154 int *pInt;
danielk1977ce98bba2008-04-03 10:13:01 +0000155 u8 *pU8;
156 int nReserve;
drh4c3645c2007-08-15 17:07:57 +0000157
158 p = (struct MemBlockHdr*)pAllocation;
159 p--;
drh902b9ee2008-12-05 17:17:07 +0000160 assert( p->iForeGuard==(int)FOREGUARD );
danielk1977bc739712009-03-23 04:33:32 +0000161 nReserve = ROUND8(p->iSize);
drh153c62c2007-08-24 03:51:33 +0000162 pInt = (int*)pAllocation;
danielk1977ce98bba2008-04-03 10:13:01 +0000163 pU8 = (u8*)pAllocation;
drh902b9ee2008-12-05 17:17:07 +0000164 assert( pInt[nReserve/sizeof(int)]==(int)REARGUARD );
shaned20010c2009-02-05 03:00:06 +0000165 /* This checks any of the "extra" bytes allocated due
166 ** to rounding up to an 8 byte boundary to ensure
167 ** they haven't been overwritten.
168 */
169 while( nReserve-- > p->iSize ) assert( pU8[nReserve]==0x65 );
drh4c3645c2007-08-15 17:07:57 +0000170 return p;
171}
172
173/*
danielk1977a7a8e142008-02-13 18:25:27 +0000174** Return the number of bytes currently allocated at address p.
175*/
drhfec00ea2008-06-14 16:56:21 +0000176static int sqlite3MemSize(void *p){
danielk1977a7a8e142008-02-13 18:25:27 +0000177 struct MemBlockHdr *pHdr;
178 if( !p ){
179 return 0;
180 }
181 pHdr = sqlite3MemsysGetHeader(p);
182 return pHdr->iSize;
183}
184
185/*
drhfec00ea2008-06-14 16:56:21 +0000186** Initialize the memory allocation subsystem.
drh40257ff2008-06-13 18:24:27 +0000187*/
drhfec00ea2008-06-14 16:56:21 +0000188static int sqlite3MemInit(void *NotUsed){
drh902b9ee2008-12-05 17:17:07 +0000189 UNUSED_PARAMETER(NotUsed);
shaned20010c2009-02-05 03:00:06 +0000190 assert( (sizeof(struct MemBlockHdr)&7) == 0 );
danielk1977075c23a2008-09-01 18:34:20 +0000191 if( !sqlite3GlobalConfig.bMemstat ){
drh65bbf292008-06-19 01:03:17 +0000192 /* If memory status is enabled, then the malloc.c wrapper will already
193 ** hold the STATIC_MEM mutex when the routines here are invoked. */
194 mem.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
195 }
drh40257ff2008-06-13 18:24:27 +0000196 return SQLITE_OK;
197}
198
199/*
drhfec00ea2008-06-14 16:56:21 +0000200** Deinitialize the memory allocation subsystem.
201*/
202static void sqlite3MemShutdown(void *NotUsed){
drh902b9ee2008-12-05 17:17:07 +0000203 UNUSED_PARAMETER(NotUsed);
drhfec00ea2008-06-14 16:56:21 +0000204 mem.mutex = 0;
205}
206
207/*
208** Round up a request size to the next valid allocation size.
209*/
210static int sqlite3MemRoundup(int n){
danielk1977bc739712009-03-23 04:33:32 +0000211 return ROUND8(n);
drhfec00ea2008-06-14 16:56:21 +0000212}
213
214/*
drh10f864e2010-01-05 03:30:15 +0000215** Fill a buffer with pseudo-random bytes. This is used to preset
216** the content of a new memory allocation to unpredictable values and
217** to clear the content of a freed allocation to unpredictable values.
218*/
219static void randomFill(char *pBuf, int nByte){
220 unsigned int x, y, r;
221 x = SQLITE_PTR_TO_INT(pBuf);
222 y = nByte | 1;
223 while( nByte >= 4 ){
224 x = (x>>1) ^ (-(x&1) & 0xd0000001);
225 y = y*1103515245 + 12345;
226 r = x ^ y;
227 *(int*)pBuf = r;
228 pBuf += 4;
229 nByte -= 4;
230 }
231 while( nByte-- > 0 ){
232 x = (x>>1) ^ (-(x&1) & 0xd0000001);
233 y = y*1103515245 + 12345;
234 r = x ^ y;
235 *(pBuf++) = r & 0xff;
236 }
237}
238
239/*
drh0e6f1542007-08-15 20:41:28 +0000240** Allocate nByte bytes of memory.
drh4c3645c2007-08-15 17:07:57 +0000241*/
drhfec00ea2008-06-14 16:56:21 +0000242static void *sqlite3MemMalloc(int nByte){
drh4c3645c2007-08-15 17:07:57 +0000243 struct MemBlockHdr *pHdr;
244 void **pBt;
drh4a50aac2007-08-23 02:47:53 +0000245 char *z;
drh153c62c2007-08-24 03:51:33 +0000246 int *pInt;
danielk1977ca0c8972007-09-01 09:02:53 +0000247 void *p = 0;
drh153c62c2007-08-24 03:51:33 +0000248 int totalSize;
drhfec00ea2008-06-14 16:56:21 +0000249 int nReserve;
250 sqlite3_mutex_enter(mem.mutex);
251 assert( mem.disallow==0 );
danielk1977bc739712009-03-23 04:33:32 +0000252 nReserve = ROUND8(nByte);
drhfec00ea2008-06-14 16:56:21 +0000253 totalSize = nReserve + sizeof(*pHdr) + sizeof(int) +
254 mem.nBacktrace*sizeof(void*) + mem.nTitle;
255 p = malloc(totalSize);
256 if( p ){
257 z = p;
258 pBt = (void**)&z[mem.nTitle];
259 pHdr = (struct MemBlockHdr*)&pBt[mem.nBacktrace];
260 pHdr->pNext = 0;
261 pHdr->pPrev = mem.pLast;
262 if( mem.pLast ){
263 mem.pLast->pNext = pHdr;
264 }else{
265 mem.pFirst = pHdr;
266 }
267 mem.pLast = pHdr;
268 pHdr->iForeGuard = FOREGUARD;
drh107b56e2010-03-12 16:32:53 +0000269 pHdr->eType = MEMTYPE_HEAP;
drhfec00ea2008-06-14 16:56:21 +0000270 pHdr->nBacktraceSlots = mem.nBacktrace;
271 pHdr->nTitle = mem.nTitle;
272 if( mem.nBacktrace ){
273 void *aAddr[40];
274 pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1;
275 memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*));
danielk19776ab3a2e2009-02-19 14:39:25 +0000276 assert(pBt[0]);
drhfec00ea2008-06-14 16:56:21 +0000277 if( mem.xBacktrace ){
278 mem.xBacktrace(nByte, pHdr->nBacktrace-1, &aAddr[1]);
279 }
280 }else{
281 pHdr->nBacktrace = 0;
282 }
283 if( mem.nTitle ){
284 memcpy(z, mem.zTitle, mem.nTitle);
285 }
286 pHdr->iSize = nByte;
drh2abcd582008-07-24 23:34:07 +0000287 adjustStats(nByte, +1);
drhfec00ea2008-06-14 16:56:21 +0000288 pInt = (int*)&pHdr[1];
289 pInt[nReserve/sizeof(int)] = REARGUARD;
drh10f864e2010-01-05 03:30:15 +0000290 randomFill((char*)pInt, nByte);
291 memset(((char*)pInt)+nByte, 0x65, nReserve-nByte);
drhfec00ea2008-06-14 16:56:21 +0000292 p = (void*)pInt;
293 }
294 sqlite3_mutex_leave(mem.mutex);
drh4c3645c2007-08-15 17:07:57 +0000295 return p;
296}
297
298/*
299** Free memory.
300*/
drhfec00ea2008-06-14 16:56:21 +0000301static void sqlite3MemFree(void *pPrior){
drh4c3645c2007-08-15 17:07:57 +0000302 struct MemBlockHdr *pHdr;
303 void **pBt;
drh4a50aac2007-08-23 02:47:53 +0000304 char *z;
dan3b4aae52010-03-08 15:17:53 +0000305 assert( sqlite3GlobalConfig.bMemstat || sqlite3GlobalConfig.bCoreMutex==0
306 || mem.mutex!=0 );
drh4c3645c2007-08-15 17:07:57 +0000307 pHdr = sqlite3MemsysGetHeader(pPrior);
308 pBt = (void**)pHdr;
309 pBt -= pHdr->nBacktraceSlots;
drh6bdec4a2007-08-16 19:40:16 +0000310 sqlite3_mutex_enter(mem.mutex);
drh4c3645c2007-08-15 17:07:57 +0000311 if( pHdr->pPrev ){
312 assert( pHdr->pPrev->pNext==pHdr );
313 pHdr->pPrev->pNext = pHdr->pNext;
314 }else{
drh0e6f1542007-08-15 20:41:28 +0000315 assert( mem.pFirst==pHdr );
316 mem.pFirst = pHdr->pNext;
drh4c3645c2007-08-15 17:07:57 +0000317 }
318 if( pHdr->pNext ){
319 assert( pHdr->pNext->pPrev==pHdr );
320 pHdr->pNext->pPrev = pHdr->pPrev;
321 }else{
drh0e6f1542007-08-15 20:41:28 +0000322 assert( mem.pLast==pHdr );
323 mem.pLast = pHdr->pPrev;
drh4c3645c2007-08-15 17:07:57 +0000324 }
drh4a50aac2007-08-23 02:47:53 +0000325 z = (char*)pBt;
326 z -= pHdr->nTitle;
drh2abcd582008-07-24 23:34:07 +0000327 adjustStats(pHdr->iSize, -1);
drh10f864e2010-01-05 03:30:15 +0000328 randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
329 pHdr->iSize + sizeof(int) + pHdr->nTitle);
drh4a50aac2007-08-23 02:47:53 +0000330 free(z);
drh0e6f1542007-08-15 20:41:28 +0000331 sqlite3_mutex_leave(mem.mutex);
drh4c3645c2007-08-15 17:07:57 +0000332}
333
334/*
335** Change the size of an existing memory allocation.
336**
337** For this debugging implementation, we *always* make a copy of the
338** allocation into a new place in memory. In this way, if the
339** higher level code is using pointer to the old allocation, it is
340** much more likely to break and we are much more liking to find
341** the error.
342*/
drhfec00ea2008-06-14 16:56:21 +0000343static void *sqlite3MemRealloc(void *pPrior, int nByte){
drh4c3645c2007-08-15 17:07:57 +0000344 struct MemBlockHdr *pOldHdr;
345 void *pNew;
drhd677b3d2007-08-20 22:48:41 +0000346 assert( mem.disallow==0 );
drh4c3645c2007-08-15 17:07:57 +0000347 pOldHdr = sqlite3MemsysGetHeader(pPrior);
drhfec00ea2008-06-14 16:56:21 +0000348 pNew = sqlite3MemMalloc(nByte);
drh4c3645c2007-08-15 17:07:57 +0000349 if( pNew ){
350 memcpy(pNew, pPrior, nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize);
351 if( nByte>pOldHdr->iSize ){
drh10f864e2010-01-05 03:30:15 +0000352 randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - pOldHdr->iSize);
drh4c3645c2007-08-15 17:07:57 +0000353 }
drhfec00ea2008-06-14 16:56:21 +0000354 sqlite3MemFree(pPrior);
drh4c3645c2007-08-15 17:07:57 +0000355 }
356 return pNew;
357}
358
drhd1370b62008-10-28 18:58:20 +0000359/*
360** Populate the low-level memory allocation function pointers in
361** sqlite3GlobalConfig.m with pointers to the routines in this file.
362*/
363void sqlite3MemSetDefault(void){
drhfec00ea2008-06-14 16:56:21 +0000364 static const sqlite3_mem_methods defaultMethods = {
365 sqlite3MemMalloc,
366 sqlite3MemFree,
367 sqlite3MemRealloc,
368 sqlite3MemSize,
369 sqlite3MemRoundup,
370 sqlite3MemInit,
371 sqlite3MemShutdown,
372 0
373 };
drhd1370b62008-10-28 18:58:20 +0000374 sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
drhfec00ea2008-06-14 16:56:21 +0000375}
376
drh4c3645c2007-08-15 17:07:57 +0000377/*
drh107b56e2010-03-12 16:32:53 +0000378** Set the "type" of an allocation.
379*/
380void sqlite3MemdebugSetType(void *p, u8 eType){
381 if( p ){
382 struct MemBlockHdr *pHdr;
383 pHdr = sqlite3MemsysGetHeader(p);
384 assert( pHdr->iForeGuard==FOREGUARD );
385 pHdr->eType = eType;
386 }
387}
388
389/*
390** Return TRUE if the mask of type in eType matches the type of the
391** allocation p. Also return true if p==NULL.
392**
393** This routine is designed for use within an assert() statement, to
394** verify the type of an allocation. For example:
395**
396** assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
397*/
398int sqlite3MemdebugHasType(void *p, u8 eType){
399 int rc = 1;
400 if( p ){
401 struct MemBlockHdr *pHdr;
402 pHdr = sqlite3MemsysGetHeader(p);
403 assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */
drh107b56e2010-03-12 16:32:53 +0000404 if( (pHdr->eType&eType)==0 ){
drh107b56e2010-03-12 16:32:53 +0000405 rc = 0;
406 }
407 }
408 return rc;
409}
410
drh1b0bfc62010-07-25 02:39:06 +0000411/*
412** Return TRUE if the mask of type in eType matches no bits of the type of the
413** allocation p. Also return true if p==NULL.
414**
415** This routine is designed for use within an assert() statement, to
416** verify the type of an allocation. For example:
417**
418** assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
419*/
420int sqlite3MemdebugNoType(void *p, u8 eType){
421 int rc = 1;
422 if( p ){
423 struct MemBlockHdr *pHdr;
424 pHdr = sqlite3MemsysGetHeader(p);
425 assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */
426 if( (pHdr->eType&eType)!=0 ){
427 rc = 0;
428 }
429 }
430 return rc;
431}
drh107b56e2010-03-12 16:32:53 +0000432
433/*
drh4c3645c2007-08-15 17:07:57 +0000434** Set the number of backtrace levels kept for each allocation.
danielk19776d2ab0e2008-06-17 17:21:18 +0000435** A value of zero turns off backtracing. The number is always rounded
drh4c3645c2007-08-15 17:07:57 +0000436** up to a multiple of 2.
437*/
drh49e4fd72008-02-19 15:15:15 +0000438void sqlite3MemdebugBacktrace(int depth){
drh4c3645c2007-08-15 17:07:57 +0000439 if( depth<0 ){ depth = 0; }
440 if( depth>20 ){ depth = 20; }
drh2f999a62007-08-15 19:16:43 +0000441 depth = (depth+1)&0xfe;
drh0e6f1542007-08-15 20:41:28 +0000442 mem.nBacktrace = depth;
drh4c3645c2007-08-15 17:07:57 +0000443}
444
danielk19776f332c12008-03-21 14:22:44 +0000445void sqlite3MemdebugBacktraceCallback(void (*xBacktrace)(int, int, void **)){
446 mem.xBacktrace = xBacktrace;
447}
448
drh4c3645c2007-08-15 17:07:57 +0000449/*
drh4a50aac2007-08-23 02:47:53 +0000450** Set the title string for subsequent allocations.
451*/
drh49e4fd72008-02-19 15:15:15 +0000452void sqlite3MemdebugSettitle(const char *zTitle){
drhea678832008-12-10 19:26:22 +0000453 unsigned int n = sqlite3Strlen30(zTitle) + 1;
drhfec00ea2008-06-14 16:56:21 +0000454 sqlite3_mutex_enter(mem.mutex);
drh4a50aac2007-08-23 02:47:53 +0000455 if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1;
456 memcpy(mem.zTitle, zTitle, n);
457 mem.zTitle[n] = 0;
danielk1977bc739712009-03-23 04:33:32 +0000458 mem.nTitle = ROUND8(n);
drh4a50aac2007-08-23 02:47:53 +0000459 sqlite3_mutex_leave(mem.mutex);
460}
461
danielk1977dbdc4d42008-03-28 07:42:53 +0000462void sqlite3MemdebugSync(){
463 struct MemBlockHdr *pHdr;
464 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
465 void **pBt = (void**)pHdr;
466 pBt -= pHdr->nBacktraceSlots;
467 mem.xBacktrace(pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]);
468 }
469}
470
drh4a50aac2007-08-23 02:47:53 +0000471/*
drh4c3645c2007-08-15 17:07:57 +0000472** Open the file indicated and write a log of all unfreed memory
473** allocations into that log.
474*/
drh49e4fd72008-02-19 15:15:15 +0000475void sqlite3MemdebugDump(const char *zFilename){
drh4c3645c2007-08-15 17:07:57 +0000476 FILE *out;
477 struct MemBlockHdr *pHdr;
478 void **pBt;
drhd2bb3272007-10-15 19:34:32 +0000479 int i;
drh4c3645c2007-08-15 17:07:57 +0000480 out = fopen(zFilename, "w");
481 if( out==0 ){
482 fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
483 zFilename);
484 return;
485 }
drh0e6f1542007-08-15 20:41:28 +0000486 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
drh4a50aac2007-08-23 02:47:53 +0000487 char *z = (char*)pHdr;
488 z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle;
drhfab69592008-04-10 14:57:24 +0000489 fprintf(out, "**** %lld bytes at %p from %s ****\n",
drhd5499d62007-08-24 04:15:00 +0000490 pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???");
drh4c3645c2007-08-15 17:07:57 +0000491 if( pHdr->nBacktrace ){
492 fflush(out);
493 pBt = (void**)pHdr;
494 pBt -= pHdr->nBacktraceSlots;
495 backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out));
496 fprintf(out, "\n");
497 }
498 }
drhd2bb3272007-10-15 19:34:32 +0000499 fprintf(out, "COUNTS:\n");
500 for(i=0; i<NCSIZE-1; i++){
drh2abcd582008-07-24 23:34:07 +0000501 if( mem.nAlloc[i] ){
502 fprintf(out, " %5d: %10d %10d %10d\n",
503 i*8, mem.nAlloc[i], mem.nCurrent[i], mem.mxCurrent[i]);
drhd2bb3272007-10-15 19:34:32 +0000504 }
505 }
drh2abcd582008-07-24 23:34:07 +0000506 if( mem.nAlloc[NCSIZE-1] ){
507 fprintf(out, " %5d: %10d %10d %10d\n",
508 NCSIZE*8-8, mem.nAlloc[NCSIZE-1],
509 mem.nCurrent[NCSIZE-1], mem.mxCurrent[NCSIZE-1]);
drhd2bb3272007-10-15 19:34:32 +0000510 }
drh4c3645c2007-08-15 17:07:57 +0000511 fclose(out);
512}
513
danielk1977a7a8e142008-02-13 18:25:27 +0000514/*
drhfec00ea2008-06-14 16:56:21 +0000515** Return the number of times sqlite3MemMalloc() has been called.
danielk1977a7a8e142008-02-13 18:25:27 +0000516*/
drh49e4fd72008-02-19 15:15:15 +0000517int sqlite3MemdebugMallocCount(){
danielk1977a7a8e142008-02-13 18:25:27 +0000518 int i;
519 int nTotal = 0;
520 for(i=0; i<NCSIZE; i++){
drh2abcd582008-07-24 23:34:07 +0000521 nTotal += mem.nAlloc[i];
danielk1977a7a8e142008-02-13 18:25:27 +0000522 }
523 return nTotal;
524}
525
drhd677b3d2007-08-20 22:48:41 +0000526
drh0d180202008-02-14 23:26:56 +0000527#endif /* SQLITE_MEMDEBUG */