xref: /sqlite-3.40.0/src/mem2.c (revision 2abcd58f)
1 /*
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 **
13 ** 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 **
22 ** $Id: mem2.c,v 1.36 2008/07/24 23:34:07 drh Exp $
23 */
24 #include "sqliteInt.h"
25 
26 /*
27 ** This version of the memory allocator is used only if the
28 ** SQLITE_MEMDEBUG macro is defined
29 */
30 #ifdef SQLITE_MEMDEBUG
31 
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
39 # define backtrace(A,B) 0
40 # define backtrace_symbols_fd(A,B,C)
41 #endif
42 #include <stdio.h>
43 
44 /*
45 ** Each memory allocation looks like this:
46 **
47 **  ------------------------------------------------------------------------
48 **  | Title |  backtrace pointers |  MemBlockHdr |  allocation |  EndGuard |
49 **  ------------------------------------------------------------------------
50 **
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 */
57 struct MemBlockHdr {
58   i64 iSize;                          /* Size of this allocation */
59   struct MemBlockHdr *pNext, *pPrev;  /* Linked list of all unfreed memory */
60   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 */
64 };
65 
66 /*
67 ** Guard words
68 */
69 #define FOREGUARD 0x80F5E153
70 #define REARGUARD 0xE4676B53
71 
72 /*
73 ** Number of malloc size increments to track.
74 */
75 #define NCSIZE  1000
76 
77 /*
78 ** 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.
82 */
83 static struct {
84 
85   /*
86   ** Mutex to control access to the memory allocation subsystem.
87   */
88   sqlite3_mutex *mutex;
89 
90   /*
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;
100   void (*xBacktrace)(int, int, void **);
101 
102   /*
103   ** 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 
108   /*
109   ** sqlite3MallocDisallow() increments the following counter.
110   ** sqlite3MallocAllow() decrements it.
111   */
112   int disallow; /* Do not allow memory allocation */
113 
114   /*
115   ** Gather statistics on the sizes of memory allocations.
116   ** nAlloc[i] is the number of allocation attempts of i*8
117   ** bytes.  i==NCSIZE is the number of allocation attempts for
118   ** sizes more than NCSIZE*8 bytes.
119   */
120   int nAlloc[NCSIZE];      /* Total number of allocations */
121   int nCurrent[NCSIZE];    /* Current number of allocations */
122   int mxCurrent[NCSIZE];   /* Highwater mark for nCurrent */
123 
124 } mem;
125 
126 
127 /*
128 ** Adjust memory usage statistics
129 */
130 static 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 
147 /*
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 */
153 static struct MemBlockHdr *sqlite3MemsysGetHeader(void *pAllocation){
154   struct MemBlockHdr *p;
155   int *pInt;
156   u8 *pU8;
157   int nReserve;
158 
159   p = (struct MemBlockHdr*)pAllocation;
160   p--;
161   assert( p->iForeGuard==FOREGUARD );
162   nReserve = (p->iSize+7)&~7;
163   pInt = (int*)pAllocation;
164   pU8 = (u8*)pAllocation;
165   assert( pInt[nReserve/sizeof(int)]==REARGUARD );
166   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 );
169   return p;
170 }
171 
172 /*
173 ** Return the number of bytes currently allocated at address p.
174 */
175 static int sqlite3MemSize(void *p){
176   struct MemBlockHdr *pHdr;
177   if( !p ){
178     return 0;
179   }
180   pHdr = sqlite3MemsysGetHeader(p);
181   return pHdr->iSize;
182 }
183 
184 /*
185 ** Initialize the memory allocation subsystem.
186 */
187 static int sqlite3MemInit(void *NotUsed){
188   if( !sqlite3Config.bMemstat ){
189     /* If memory status is enabled, then the malloc.c wrapper will already
190     ** hold the STATIC_MEM mutex when the routines here are invoked. */
191     mem.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
192   }
193   return SQLITE_OK;
194 }
195 
196 /*
197 ** Deinitialize the memory allocation subsystem.
198 */
199 static void sqlite3MemShutdown(void *NotUsed){
200   mem.mutex = 0;
201 }
202 
203 /*
204 ** Round up a request size to the next valid allocation size.
205 */
206 static int sqlite3MemRoundup(int n){
207   return (n+7) & ~7;
208 }
209 
210 /*
211 ** Allocate nByte bytes of memory.
212 */
213 static void *sqlite3MemMalloc(int nByte){
214   struct MemBlockHdr *pHdr;
215   void **pBt;
216   char *z;
217   int *pInt;
218   void *p = 0;
219   int totalSize;
220   int nReserve;
221   sqlite3_mutex_enter(mem.mutex);
222   assert( mem.disallow==0 );
223   nReserve = (nByte+7)&~7;
224   totalSize = nReserve + sizeof(*pHdr) + sizeof(int) +
225                mem.nBacktrace*sizeof(void*) + mem.nTitle;
226   p = malloc(totalSize);
227   if( p ){
228     z = p;
229     pBt = (void**)&z[mem.nTitle];
230     pHdr = (struct MemBlockHdr*)&pBt[mem.nBacktrace];
231     pHdr->pNext = 0;
232     pHdr->pPrev = mem.pLast;
233     if( mem.pLast ){
234       mem.pLast->pNext = pHdr;
235     }else{
236       mem.pFirst = pHdr;
237     }
238     mem.pLast = pHdr;
239     pHdr->iForeGuard = FOREGUARD;
240     pHdr->nBacktraceSlots = mem.nBacktrace;
241     pHdr->nTitle = mem.nTitle;
242     if( mem.nBacktrace ){
243       void *aAddr[40];
244       pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1;
245       memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*));
246       if( mem.xBacktrace ){
247         mem.xBacktrace(nByte, pHdr->nBacktrace-1, &aAddr[1]);
248       }
249     }else{
250       pHdr->nBacktrace = 0;
251     }
252     if( mem.nTitle ){
253       memcpy(z, mem.zTitle, mem.nTitle);
254     }
255     pHdr->iSize = nByte;
256     adjustStats(nByte, +1);
257     pInt = (int*)&pHdr[1];
258     pInt[nReserve/sizeof(int)] = REARGUARD;
259     memset(pInt, 0x65, nReserve);
260     p = (void*)pInt;
261   }
262   sqlite3_mutex_leave(mem.mutex);
263   return p;
264 }
265 
266 /*
267 ** Free memory.
268 */
269 static void sqlite3MemFree(void *pPrior){
270   struct MemBlockHdr *pHdr;
271   void **pBt;
272   char *z;
273   assert( sqlite3Config.bMemstat || mem.mutex!=0 );
274   pHdr = sqlite3MemsysGetHeader(pPrior);
275   pBt = (void**)pHdr;
276   pBt -= pHdr->nBacktraceSlots;
277   sqlite3_mutex_enter(mem.mutex);
278   if( pHdr->pPrev ){
279     assert( pHdr->pPrev->pNext==pHdr );
280     pHdr->pPrev->pNext = pHdr->pNext;
281   }else{
282     assert( mem.pFirst==pHdr );
283     mem.pFirst = pHdr->pNext;
284   }
285   if( pHdr->pNext ){
286     assert( pHdr->pNext->pPrev==pHdr );
287     pHdr->pNext->pPrev = pHdr->pPrev;
288   }else{
289     assert( mem.pLast==pHdr );
290     mem.pLast = pHdr->pPrev;
291   }
292   z = (char*)pBt;
293   z -= pHdr->nTitle;
294   adjustStats(pHdr->iSize, -1);
295   memset(z, 0x2b, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
296                   pHdr->iSize + sizeof(int) + pHdr->nTitle);
297   free(z);
298   sqlite3_mutex_leave(mem.mutex);
299 }
300 
301 /*
302 ** Change the size of an existing memory allocation.
303 **
304 ** For this debugging implementation, we *always* make a copy of the
305 ** allocation into a new place in memory.  In this way, if the
306 ** higher level code is using pointer to the old allocation, it is
307 ** much more likely to break and we are much more liking to find
308 ** the error.
309 */
310 static void *sqlite3MemRealloc(void *pPrior, int nByte){
311   struct MemBlockHdr *pOldHdr;
312   void *pNew;
313   assert( mem.disallow==0 );
314   pOldHdr = sqlite3MemsysGetHeader(pPrior);
315   pNew = sqlite3MemMalloc(nByte);
316   if( pNew ){
317     memcpy(pNew, pPrior, nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize);
318     if( nByte>pOldHdr->iSize ){
319       memset(&((char*)pNew)[pOldHdr->iSize], 0x2b, nByte - pOldHdr->iSize);
320     }
321     sqlite3MemFree(pPrior);
322   }
323   return pNew;
324 }
325 
326 
327 sqlite3_mem_methods *sqlite3MemGetDefault(void){
328   static const sqlite3_mem_methods defaultMethods = {
329      sqlite3MemMalloc,
330      sqlite3MemFree,
331      sqlite3MemRealloc,
332      sqlite3MemSize,
333      sqlite3MemRoundup,
334      sqlite3MemInit,
335      sqlite3MemShutdown,
336      0
337   };
338   return &defaultMethods;
339 }
340 
341 /*
342 ** Populate the low-level memory allocation function pointers in
343 ** sqlite3Config.m with pointers to the routines in this file.
344 */
345 void sqlite3MemSetDefault(void){
346   sqlite3_config(SQLITE_CONFIG_MALLOC, sqlite3MemGetDefault());
347 }
348 
349 /*
350 ** Set the number of backtrace levels kept for each allocation.
351 ** A value of zero turns off backtracing.  The number is always rounded
352 ** up to a multiple of 2.
353 */
354 void sqlite3MemdebugBacktrace(int depth){
355   if( depth<0 ){ depth = 0; }
356   if( depth>20 ){ depth = 20; }
357   depth = (depth+1)&0xfe;
358   mem.nBacktrace = depth;
359 }
360 
361 void sqlite3MemdebugBacktraceCallback(void (*xBacktrace)(int, int, void **)){
362   mem.xBacktrace = xBacktrace;
363 }
364 
365 /*
366 ** Set the title string for subsequent allocations.
367 */
368 void sqlite3MemdebugSettitle(const char *zTitle){
369   int n = strlen(zTitle) + 1;
370   sqlite3_mutex_enter(mem.mutex);
371   if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1;
372   memcpy(mem.zTitle, zTitle, n);
373   mem.zTitle[n] = 0;
374   mem.nTitle = (n+7)&~7;
375   sqlite3_mutex_leave(mem.mutex);
376 }
377 
378 void sqlite3MemdebugSync(){
379   struct MemBlockHdr *pHdr;
380   for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
381     void **pBt = (void**)pHdr;
382     pBt -= pHdr->nBacktraceSlots;
383     mem.xBacktrace(pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]);
384   }
385 }
386 
387 /*
388 ** Open the file indicated and write a log of all unfreed memory
389 ** allocations into that log.
390 */
391 void sqlite3MemdebugDump(const char *zFilename){
392   FILE *out;
393   struct MemBlockHdr *pHdr;
394   void **pBt;
395   int i;
396   out = fopen(zFilename, "w");
397   if( out==0 ){
398     fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
399                     zFilename);
400     return;
401   }
402   for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
403     char *z = (char*)pHdr;
404     z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle;
405     fprintf(out, "**** %lld bytes at %p from %s ****\n",
406             pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???");
407     if( pHdr->nBacktrace ){
408       fflush(out);
409       pBt = (void**)pHdr;
410       pBt -= pHdr->nBacktraceSlots;
411       backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out));
412       fprintf(out, "\n");
413     }
414   }
415   fprintf(out, "COUNTS:\n");
416   for(i=0; i<NCSIZE-1; i++){
417     if( mem.nAlloc[i] ){
418       fprintf(out, "   %5d: %10d %10d %10d\n",
419             i*8, mem.nAlloc[i], mem.nCurrent[i], mem.mxCurrent[i]);
420     }
421   }
422   if( mem.nAlloc[NCSIZE-1] ){
423     fprintf(out, "   %5d: %10d %10d %10d\n",
424              NCSIZE*8-8, mem.nAlloc[NCSIZE-1],
425              mem.nCurrent[NCSIZE-1], mem.mxCurrent[NCSIZE-1]);
426   }
427   fclose(out);
428 }
429 
430 /*
431 ** Return the number of times sqlite3MemMalloc() has been called.
432 */
433 int sqlite3MemdebugMallocCount(){
434   int i;
435   int nTotal = 0;
436   for(i=0; i<NCSIZE; i++){
437     nTotal += mem.nAlloc[i];
438   }
439   return nTotal;
440 }
441 
442 
443 #endif /* SQLITE_MEMDEBUG */
444