blob: aaa58e2d10cc3a151c7c7741b64a91a54665945e [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.
21**
drh902b9ee2008-12-05 17:17:07 +000022** $Id: mem2.c,v 1.41 2008/12/05 17:17:08 drh Exp $
drh4c3645c2007-08-15 17:07:57 +000023*/
drh0d180202008-02-14 23:26:56 +000024#include "sqliteInt.h"
drh4c3645c2007-08-15 17:07:57 +000025
26/*
27** This version of the memory allocator is used only if the
drh0d180202008-02-14 23:26:56 +000028** SQLITE_MEMDEBUG macro is defined
drh4c3645c2007-08-15 17:07:57 +000029*/
drh0d180202008-02-14 23:26:56 +000030#ifdef SQLITE_MEMDEBUG
drh4c3645c2007-08-15 17:07:57 +000031
32/*
33** The backtrace functionality is only available with GLIBC
34*/
35#ifdef __GLIBC__
36 extern int backtrace(void**,int);
37 extern void backtrace_symbols_fd(void*const*,int,int);
38#else
danielk197744a376f2008-08-12 15:04:58 +000039# define backtrace(A,B) 1
drh4c3645c2007-08-15 17:07:57 +000040# define backtrace_symbols_fd(A,B,C)
41#endif
drh0d180202008-02-14 23:26:56 +000042#include <stdio.h>
drh4c3645c2007-08-15 17:07:57 +000043
drh4c3645c2007-08-15 17:07:57 +000044/*
45** Each memory allocation looks like this:
46**
drh4a50aac2007-08-23 02:47:53 +000047** ------------------------------------------------------------------------
48** | Title | backtrace pointers | MemBlockHdr | allocation | EndGuard |
49** ------------------------------------------------------------------------
drh4c3645c2007-08-15 17:07:57 +000050**
51** The application code sees only a pointer to the allocation. We have
52** to back up from the allocation pointer to find the MemBlockHdr. The
53** MemBlockHdr tells us the size of the allocation and the number of
54** backtrace pointers. There is also a guard word at the end of the
55** MemBlockHdr.
56*/
57struct MemBlockHdr {
drhfab69592008-04-10 14:57:24 +000058 i64 iSize; /* Size of this allocation */
drh4c3645c2007-08-15 17:07:57 +000059 struct MemBlockHdr *pNext, *pPrev; /* Linked list of all unfreed memory */
drh153c62c2007-08-24 03:51:33 +000060 char nBacktrace; /* Number of backtraces on this alloc */
61 char nBacktraceSlots; /* Available backtrace slots */
62 short nTitle; /* Bytes of title; includes '\0' */
63 int iForeGuard; /* Guard word for sanity */
drh4c3645c2007-08-15 17:07:57 +000064};
65
66/*
67** Guard words
68*/
69#define FOREGUARD 0x80F5E153
70#define REARGUARD 0xE4676B53
71
72/*
drhd2bb3272007-10-15 19:34:32 +000073** Number of malloc size increments to track.
74*/
drh9c7a60d2007-10-19 17:47:24 +000075#define NCSIZE 1000
drhd2bb3272007-10-15 19:34:32 +000076
77/*
drh0e6f1542007-08-15 20:41:28 +000078** All of the static variables used by this module are collected
79** into a single structure named "mem". This is to keep the
80** static variables organized and to reduce namespace pollution
81** when this module is combined with other in the amalgamation.
drh4c3645c2007-08-15 17:07:57 +000082*/
drh0e6f1542007-08-15 20:41:28 +000083static struct {
drh0e6f1542007-08-15 20:41:28 +000084
85 /*
86 ** Mutex to control access to the memory allocation subsystem.
87 */
88 sqlite3_mutex *mutex;
drhfec00ea2008-06-14 16:56:21 +000089
drh0e6f1542007-08-15 20:41:28 +000090 /*
91 ** Head and tail of a linked list of all outstanding allocations
92 */
93 struct MemBlockHdr *pFirst;
94 struct MemBlockHdr *pLast;
95
96 /*
97 ** The number of levels of backtrace to save in new allocations.
98 */
99 int nBacktrace;
danielk19776f332c12008-03-21 14:22:44 +0000100 void (*xBacktrace)(int, int, void **);
drh0e6f1542007-08-15 20:41:28 +0000101
102 /*
drh4a50aac2007-08-23 02:47:53 +0000103 ** Title text to insert in front of each block
104 */
105 int nTitle; /* Bytes of zTitle to save. Includes '\0' and padding */
106 char zTitle[100]; /* The title text */
107
drhd677b3d2007-08-20 22:48:41 +0000108 /*
109 ** sqlite3MallocDisallow() increments the following counter.
110 ** sqlite3MallocAllow() decrements it.
111 */
112 int disallow; /* Do not allow memory allocation */
drhd2bb3272007-10-15 19:34:32 +0000113
114 /*
115 ** Gather statistics on the sizes of memory allocations.
drh2abcd582008-07-24 23:34:07 +0000116 ** nAlloc[i] is the number of allocation attempts of i*8
drhd2bb3272007-10-15 19:34:32 +0000117 ** bytes. i==NCSIZE is the number of allocation attempts for
drh9c7a60d2007-10-19 17:47:24 +0000118 ** sizes more than NCSIZE*8 bytes.
drhd2bb3272007-10-15 19:34:32 +0000119 */
drh2abcd582008-07-24 23:34:07 +0000120 int nAlloc[NCSIZE]; /* Total number of allocations */
121 int nCurrent[NCSIZE]; /* Current number of allocations */
122 int mxCurrent[NCSIZE]; /* Highwater mark for nCurrent */
drhd2bb3272007-10-15 19:34:32 +0000123
drh153c62c2007-08-24 03:51:33 +0000124} mem;
drh0e6f1542007-08-15 20:41:28 +0000125
drh2abcd582008-07-24 23:34:07 +0000126
127/*
128** Adjust memory usage statistics
129*/
130static void adjustStats(int iSize, int increment){
131 int i = ((iSize+7)&~7)/8;
132 if( i>NCSIZE-1 ){
133 i = NCSIZE - 1;
134 }
135 if( increment>0 ){
136 mem.nAlloc[i]++;
137 mem.nCurrent[i]++;
138 if( mem.nCurrent[i]>mem.mxCurrent[i] ){
139 mem.mxCurrent[i] = mem.nCurrent[i];
140 }
141 }else{
142 mem.nCurrent[i]--;
143 assert( mem.nCurrent[i]>=0 );
144 }
145}
146
drh4c3645c2007-08-15 17:07:57 +0000147/*
148** Given an allocation, find the MemBlockHdr for that allocation.
149**
150** This routine checks the guards at either end of the allocation and
151** if they are incorrect it asserts.
152*/
153static struct MemBlockHdr *sqlite3MemsysGetHeader(void *pAllocation){
154 struct MemBlockHdr *p;
drh153c62c2007-08-24 03:51:33 +0000155 int *pInt;
danielk1977ce98bba2008-04-03 10:13:01 +0000156 u8 *pU8;
157 int nReserve;
drh4c3645c2007-08-15 17:07:57 +0000158
159 p = (struct MemBlockHdr*)pAllocation;
160 p--;
drh902b9ee2008-12-05 17:17:07 +0000161 assert( p->iForeGuard==(int)FOREGUARD );
drhfab69592008-04-10 14:57:24 +0000162 nReserve = (p->iSize+7)&~7;
drh153c62c2007-08-24 03:51:33 +0000163 pInt = (int*)pAllocation;
danielk1977ce98bba2008-04-03 10:13:01 +0000164 pU8 = (u8*)pAllocation;
drh902b9ee2008-12-05 17:17:07 +0000165 assert( pInt[nReserve/sizeof(int)]==(int)REARGUARD );
danielk1977ce98bba2008-04-03 10:13:01 +0000166 assert( (nReserve-0)<=p->iSize || pU8[nReserve-1]==0x65 );
167 assert( (nReserve-1)<=p->iSize || pU8[nReserve-2]==0x65 );
168 assert( (nReserve-2)<=p->iSize || pU8[nReserve-3]==0x65 );
drh4c3645c2007-08-15 17:07:57 +0000169 return p;
170}
171
172/*
danielk1977a7a8e142008-02-13 18:25:27 +0000173** Return the number of bytes currently allocated at address p.
174*/
drhfec00ea2008-06-14 16:56:21 +0000175static int sqlite3MemSize(void *p){
danielk1977a7a8e142008-02-13 18:25:27 +0000176 struct MemBlockHdr *pHdr;
177 if( !p ){
178 return 0;
179 }
180 pHdr = sqlite3MemsysGetHeader(p);
181 return pHdr->iSize;
182}
183
184/*
drhfec00ea2008-06-14 16:56:21 +0000185** Initialize the memory allocation subsystem.
drh40257ff2008-06-13 18:24:27 +0000186*/
drhfec00ea2008-06-14 16:56:21 +0000187static int sqlite3MemInit(void *NotUsed){
drh902b9ee2008-12-05 17:17:07 +0000188 UNUSED_PARAMETER(NotUsed);
danielk1977075c23a2008-09-01 18:34:20 +0000189 if( !sqlite3GlobalConfig.bMemstat ){
drh65bbf292008-06-19 01:03:17 +0000190 /* If memory status is enabled, then the malloc.c wrapper will already
191 ** hold the STATIC_MEM mutex when the routines here are invoked. */
192 mem.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
193 }
drh40257ff2008-06-13 18:24:27 +0000194 return SQLITE_OK;
195}
196
197/*
drhfec00ea2008-06-14 16:56:21 +0000198** Deinitialize the memory allocation subsystem.
199*/
200static void sqlite3MemShutdown(void *NotUsed){
drh902b9ee2008-12-05 17:17:07 +0000201 UNUSED_PARAMETER(NotUsed);
drhfec00ea2008-06-14 16:56:21 +0000202 mem.mutex = 0;
203}
204
205/*
206** Round up a request size to the next valid allocation size.
207*/
208static int sqlite3MemRoundup(int n){
209 return (n+7) & ~7;
210}
211
212/*
drh0e6f1542007-08-15 20:41:28 +0000213** Allocate nByte bytes of memory.
drh4c3645c2007-08-15 17:07:57 +0000214*/
drhfec00ea2008-06-14 16:56:21 +0000215static void *sqlite3MemMalloc(int nByte){
drh4c3645c2007-08-15 17:07:57 +0000216 struct MemBlockHdr *pHdr;
217 void **pBt;
drh4a50aac2007-08-23 02:47:53 +0000218 char *z;
drh153c62c2007-08-24 03:51:33 +0000219 int *pInt;
danielk1977ca0c8972007-09-01 09:02:53 +0000220 void *p = 0;
drh153c62c2007-08-24 03:51:33 +0000221 int totalSize;
drhfec00ea2008-06-14 16:56:21 +0000222 int nReserve;
223 sqlite3_mutex_enter(mem.mutex);
224 assert( mem.disallow==0 );
225 nReserve = (nByte+7)&~7;
drhfec00ea2008-06-14 16:56:21 +0000226 totalSize = nReserve + sizeof(*pHdr) + sizeof(int) +
227 mem.nBacktrace*sizeof(void*) + mem.nTitle;
228 p = malloc(totalSize);
229 if( p ){
230 z = p;
231 pBt = (void**)&z[mem.nTitle];
232 pHdr = (struct MemBlockHdr*)&pBt[mem.nBacktrace];
233 pHdr->pNext = 0;
234 pHdr->pPrev = mem.pLast;
235 if( mem.pLast ){
236 mem.pLast->pNext = pHdr;
237 }else{
238 mem.pFirst = pHdr;
239 }
240 mem.pLast = pHdr;
241 pHdr->iForeGuard = FOREGUARD;
242 pHdr->nBacktraceSlots = mem.nBacktrace;
243 pHdr->nTitle = mem.nTitle;
244 if( mem.nBacktrace ){
245 void *aAddr[40];
246 pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1;
247 memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*));
248 if( mem.xBacktrace ){
249 mem.xBacktrace(nByte, pHdr->nBacktrace-1, &aAddr[1]);
250 }
251 }else{
252 pHdr->nBacktrace = 0;
253 }
254 if( mem.nTitle ){
255 memcpy(z, mem.zTitle, mem.nTitle);
256 }
257 pHdr->iSize = nByte;
drh2abcd582008-07-24 23:34:07 +0000258 adjustStats(nByte, +1);
drhfec00ea2008-06-14 16:56:21 +0000259 pInt = (int*)&pHdr[1];
260 pInt[nReserve/sizeof(int)] = REARGUARD;
261 memset(pInt, 0x65, nReserve);
262 p = (void*)pInt;
263 }
264 sqlite3_mutex_leave(mem.mutex);
drh4c3645c2007-08-15 17:07:57 +0000265 return p;
266}
267
268/*
269** Free memory.
270*/
drhfec00ea2008-06-14 16:56:21 +0000271static void sqlite3MemFree(void *pPrior){
drh4c3645c2007-08-15 17:07:57 +0000272 struct MemBlockHdr *pHdr;
273 void **pBt;
drh4a50aac2007-08-23 02:47:53 +0000274 char *z;
danielk1977075c23a2008-09-01 18:34:20 +0000275 assert( sqlite3GlobalConfig.bMemstat || mem.mutex!=0 );
drh4c3645c2007-08-15 17:07:57 +0000276 pHdr = sqlite3MemsysGetHeader(pPrior);
277 pBt = (void**)pHdr;
278 pBt -= pHdr->nBacktraceSlots;
drh6bdec4a2007-08-16 19:40:16 +0000279 sqlite3_mutex_enter(mem.mutex);
drh4c3645c2007-08-15 17:07:57 +0000280 if( pHdr->pPrev ){
281 assert( pHdr->pPrev->pNext==pHdr );
282 pHdr->pPrev->pNext = pHdr->pNext;
283 }else{
drh0e6f1542007-08-15 20:41:28 +0000284 assert( mem.pFirst==pHdr );
285 mem.pFirst = pHdr->pNext;
drh4c3645c2007-08-15 17:07:57 +0000286 }
287 if( pHdr->pNext ){
288 assert( pHdr->pNext->pPrev==pHdr );
289 pHdr->pNext->pPrev = pHdr->pPrev;
290 }else{
drh0e6f1542007-08-15 20:41:28 +0000291 assert( mem.pLast==pHdr );
292 mem.pLast = pHdr->pPrev;
drh4c3645c2007-08-15 17:07:57 +0000293 }
drh4a50aac2007-08-23 02:47:53 +0000294 z = (char*)pBt;
295 z -= pHdr->nTitle;
drh2abcd582008-07-24 23:34:07 +0000296 adjustStats(pHdr->iSize, -1);
drh4a50aac2007-08-23 02:47:53 +0000297 memset(z, 0x2b, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
drh153c62c2007-08-24 03:51:33 +0000298 pHdr->iSize + sizeof(int) + pHdr->nTitle);
drh4a50aac2007-08-23 02:47:53 +0000299 free(z);
drh0e6f1542007-08-15 20:41:28 +0000300 sqlite3_mutex_leave(mem.mutex);
drh4c3645c2007-08-15 17:07:57 +0000301}
302
303/*
304** Change the size of an existing memory allocation.
305**
306** For this debugging implementation, we *always* make a copy of the
307** allocation into a new place in memory. In this way, if the
308** higher level code is using pointer to the old allocation, it is
309** much more likely to break and we are much more liking to find
310** the error.
311*/
drhfec00ea2008-06-14 16:56:21 +0000312static void *sqlite3MemRealloc(void *pPrior, int nByte){
drh4c3645c2007-08-15 17:07:57 +0000313 struct MemBlockHdr *pOldHdr;
314 void *pNew;
drhd677b3d2007-08-20 22:48:41 +0000315 assert( mem.disallow==0 );
drh4c3645c2007-08-15 17:07:57 +0000316 pOldHdr = sqlite3MemsysGetHeader(pPrior);
drhfec00ea2008-06-14 16:56:21 +0000317 pNew = sqlite3MemMalloc(nByte);
drh4c3645c2007-08-15 17:07:57 +0000318 if( pNew ){
319 memcpy(pNew, pPrior, nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize);
320 if( nByte>pOldHdr->iSize ){
321 memset(&((char*)pNew)[pOldHdr->iSize], 0x2b, nByte - pOldHdr->iSize);
322 }
drhfec00ea2008-06-14 16:56:21 +0000323 sqlite3MemFree(pPrior);
drh4c3645c2007-08-15 17:07:57 +0000324 }
325 return pNew;
326}
327
drhd1370b62008-10-28 18:58:20 +0000328/*
329** Populate the low-level memory allocation function pointers in
330** sqlite3GlobalConfig.m with pointers to the routines in this file.
331*/
332void sqlite3MemSetDefault(void){
drhfec00ea2008-06-14 16:56:21 +0000333 static const sqlite3_mem_methods defaultMethods = {
334 sqlite3MemMalloc,
335 sqlite3MemFree,
336 sqlite3MemRealloc,
337 sqlite3MemSize,
338 sqlite3MemRoundup,
339 sqlite3MemInit,
340 sqlite3MemShutdown,
341 0
342 };
drhd1370b62008-10-28 18:58:20 +0000343 sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
drhfec00ea2008-06-14 16:56:21 +0000344}
345
drh4c3645c2007-08-15 17:07:57 +0000346/*
347** Set the number of backtrace levels kept for each allocation.
danielk19776d2ab0e2008-06-17 17:21:18 +0000348** A value of zero turns off backtracing. The number is always rounded
drh4c3645c2007-08-15 17:07:57 +0000349** up to a multiple of 2.
350*/
drh49e4fd72008-02-19 15:15:15 +0000351void sqlite3MemdebugBacktrace(int depth){
drh4c3645c2007-08-15 17:07:57 +0000352 if( depth<0 ){ depth = 0; }
353 if( depth>20 ){ depth = 20; }
drh2f999a62007-08-15 19:16:43 +0000354 depth = (depth+1)&0xfe;
drh0e6f1542007-08-15 20:41:28 +0000355 mem.nBacktrace = depth;
drh4c3645c2007-08-15 17:07:57 +0000356}
357
danielk19776f332c12008-03-21 14:22:44 +0000358void sqlite3MemdebugBacktraceCallback(void (*xBacktrace)(int, int, void **)){
359 mem.xBacktrace = xBacktrace;
360}
361
drh4c3645c2007-08-15 17:07:57 +0000362/*
drh4a50aac2007-08-23 02:47:53 +0000363** Set the title string for subsequent allocations.
364*/
drh49e4fd72008-02-19 15:15:15 +0000365void sqlite3MemdebugSettitle(const char *zTitle){
drh902b9ee2008-12-05 17:17:07 +0000366 unsigned int n = strlen(zTitle) + 1;
drhfec00ea2008-06-14 16:56:21 +0000367 sqlite3_mutex_enter(mem.mutex);
drh4a50aac2007-08-23 02:47:53 +0000368 if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1;
369 memcpy(mem.zTitle, zTitle, n);
370 mem.zTitle[n] = 0;
drhfab69592008-04-10 14:57:24 +0000371 mem.nTitle = (n+7)&~7;
drh4a50aac2007-08-23 02:47:53 +0000372 sqlite3_mutex_leave(mem.mutex);
373}
374
danielk1977dbdc4d42008-03-28 07:42:53 +0000375void sqlite3MemdebugSync(){
376 struct MemBlockHdr *pHdr;
377 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
378 void **pBt = (void**)pHdr;
379 pBt -= pHdr->nBacktraceSlots;
380 mem.xBacktrace(pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]);
381 }
382}
383
drh4a50aac2007-08-23 02:47:53 +0000384/*
drh4c3645c2007-08-15 17:07:57 +0000385** Open the file indicated and write a log of all unfreed memory
386** allocations into that log.
387*/
drh49e4fd72008-02-19 15:15:15 +0000388void sqlite3MemdebugDump(const char *zFilename){
drh4c3645c2007-08-15 17:07:57 +0000389 FILE *out;
390 struct MemBlockHdr *pHdr;
391 void **pBt;
drhd2bb3272007-10-15 19:34:32 +0000392 int i;
drh4c3645c2007-08-15 17:07:57 +0000393 out = fopen(zFilename, "w");
394 if( out==0 ){
395 fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
396 zFilename);
397 return;
398 }
drh0e6f1542007-08-15 20:41:28 +0000399 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
drh4a50aac2007-08-23 02:47:53 +0000400 char *z = (char*)pHdr;
401 z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle;
drhfab69592008-04-10 14:57:24 +0000402 fprintf(out, "**** %lld bytes at %p from %s ****\n",
drhd5499d62007-08-24 04:15:00 +0000403 pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???");
drh4c3645c2007-08-15 17:07:57 +0000404 if( pHdr->nBacktrace ){
405 fflush(out);
406 pBt = (void**)pHdr;
407 pBt -= pHdr->nBacktraceSlots;
408 backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out));
409 fprintf(out, "\n");
410 }
411 }
drhd2bb3272007-10-15 19:34:32 +0000412 fprintf(out, "COUNTS:\n");
413 for(i=0; i<NCSIZE-1; i++){
drh2abcd582008-07-24 23:34:07 +0000414 if( mem.nAlloc[i] ){
415 fprintf(out, " %5d: %10d %10d %10d\n",
416 i*8, mem.nAlloc[i], mem.nCurrent[i], mem.mxCurrent[i]);
drhd2bb3272007-10-15 19:34:32 +0000417 }
418 }
drh2abcd582008-07-24 23:34:07 +0000419 if( mem.nAlloc[NCSIZE-1] ){
420 fprintf(out, " %5d: %10d %10d %10d\n",
421 NCSIZE*8-8, mem.nAlloc[NCSIZE-1],
422 mem.nCurrent[NCSIZE-1], mem.mxCurrent[NCSIZE-1]);
drhd2bb3272007-10-15 19:34:32 +0000423 }
drh4c3645c2007-08-15 17:07:57 +0000424 fclose(out);
425}
426
danielk1977a7a8e142008-02-13 18:25:27 +0000427/*
drhfec00ea2008-06-14 16:56:21 +0000428** Return the number of times sqlite3MemMalloc() has been called.
danielk1977a7a8e142008-02-13 18:25:27 +0000429*/
drh49e4fd72008-02-19 15:15:15 +0000430int sqlite3MemdebugMallocCount(){
danielk1977a7a8e142008-02-13 18:25:27 +0000431 int i;
432 int nTotal = 0;
433 for(i=0; i<NCSIZE; i++){
drh2abcd582008-07-24 23:34:07 +0000434 nTotal += mem.nAlloc[i];
danielk1977a7a8e142008-02-13 18:25:27 +0000435 }
436 return nTotal;
437}
438
drhd677b3d2007-08-20 22:48:41 +0000439
drh0d180202008-02-14 23:26:56 +0000440#endif /* SQLITE_MEMDEBUG */