|
1 /* |
|
2 ** 2007 August 15 |
|
3 ** |
|
4 ** Portions Copyright (c) 2008 Nokia Corporation and/or its subsidiaries. All rights reserved. |
|
5 ** |
|
6 ** The author disclaims copyright to this source code. In place of |
|
7 ** a legal notice, here is a blessing: |
|
8 ** |
|
9 ** May you do good and not evil. |
|
10 ** May you find forgiveness for yourself and forgive others. |
|
11 ** May you share freely, never taking more than you give. |
|
12 ** |
|
13 ************************************************************************* |
|
14 ** |
|
15 ** This file contains low-level memory allocation drivers for when |
|
16 ** SQLite will use the standard C-library malloc/realloc/free interface |
|
17 ** to obtain the memory it needs while adding lots of additional debugging |
|
18 ** information to each allocation in order to help detect and fix memory |
|
19 ** leaks and memory usage errors. |
|
20 ** |
|
21 ** This file contains implementations of the low-level memory allocation |
|
22 ** routines specified in the sqlite3_mem_methods object. |
|
23 ** |
|
24 ** $Id: mem2.c,v 1.39 2008/09/01 18:34:20 danielk1977 Exp $ |
|
25 */ |
|
26 #include "sqliteInt.h" |
|
27 |
|
28 /* |
|
29 ** This version of the memory allocator is used only if the |
|
30 ** SQLITE_MEMDEBUG macro is defined |
|
31 */ |
|
32 #ifdef SQLITE_MEMDEBUG |
|
33 |
|
34 /* |
|
35 ** The backtrace functionality is only available with GLIBC |
|
36 */ |
|
37 #ifdef __GLIBC__ |
|
38 extern int backtrace(void**,int); |
|
39 extern void backtrace_symbols_fd(void*const*,int,int); |
|
40 #else |
|
41 # define backtrace(A,B) 1 |
|
42 # define backtrace_symbols_fd(A,B,C) |
|
43 #endif |
|
44 #include <stdio.h> |
|
45 |
|
46 /* |
|
47 ** Each memory allocation looks like this: |
|
48 ** |
|
49 ** ------------------------------------------------------------------------ |
|
50 ** | Title | backtrace pointers | MemBlockHdr | allocation | EndGuard | |
|
51 ** ------------------------------------------------------------------------ |
|
52 ** |
|
53 ** The application code sees only a pointer to the allocation. We have |
|
54 ** to back up from the allocation pointer to find the MemBlockHdr. The |
|
55 ** MemBlockHdr tells us the size of the allocation and the number of |
|
56 ** backtrace pointers. There is also a guard word at the end of the |
|
57 ** MemBlockHdr. |
|
58 */ |
|
59 struct MemBlockHdr { |
|
60 i64 iSize; /* Size of this allocation */ |
|
61 struct MemBlockHdr *pNext, *pPrev; /* Linked list of all unfreed memory */ |
|
62 char nBacktrace; /* Number of backtraces on this alloc */ |
|
63 char nBacktraceSlots; /* Available backtrace slots */ |
|
64 short nTitle; /* Bytes of title; includes '\0' */ |
|
65 int iForeGuard; /* Guard word for sanity */ |
|
66 }; |
|
67 |
|
68 /* |
|
69 ** Guard words |
|
70 */ |
|
71 #define FOREGUARD 0x80F5E153 |
|
72 #define REARGUARD 0xE4676B53 |
|
73 |
|
74 /* |
|
75 ** Number of malloc size increments to track. |
|
76 */ |
|
77 #define NCSIZE 1000 |
|
78 |
|
79 /* |
|
80 ** All of the static variables used by this module are collected |
|
81 ** into a single structure named "mem". This is to keep the |
|
82 ** static variables organized and to reduce namespace pollution |
|
83 ** when this module is combined with other in the amalgamation. |
|
84 */ |
|
85 static struct { |
|
86 |
|
87 /* |
|
88 ** Mutex to control access to the memory allocation subsystem. |
|
89 */ |
|
90 sqlite3_mutex *mutex; |
|
91 |
|
92 /* |
|
93 ** Head and tail of a linked list of all outstanding allocations |
|
94 */ |
|
95 struct MemBlockHdr *pFirst; |
|
96 struct MemBlockHdr *pLast; |
|
97 |
|
98 /* |
|
99 ** The number of levels of backtrace to save in new allocations. |
|
100 */ |
|
101 int nBacktrace; |
|
102 void (*xBacktrace)(int, int, void **); |
|
103 |
|
104 /* |
|
105 ** Title text to insert in front of each block |
|
106 */ |
|
107 int nTitle; /* Bytes of zTitle to save. Includes '\0' and padding */ |
|
108 char zTitle[100]; /* The title text */ |
|
109 |
|
110 /* |
|
111 ** sqlite3MallocDisallow() increments the following counter. |
|
112 ** sqlite3MallocAllow() decrements it. |
|
113 */ |
|
114 int disallow; /* Do not allow memory allocation */ |
|
115 |
|
116 /* |
|
117 ** Gather statistics on the sizes of memory allocations. |
|
118 ** nAlloc[i] is the number of allocation attempts of i*8 |
|
119 ** bytes. i==NCSIZE is the number of allocation attempts for |
|
120 ** sizes more than NCSIZE*8 bytes. |
|
121 */ |
|
122 int nAlloc[NCSIZE]; /* Total number of allocations */ |
|
123 int nCurrent[NCSIZE]; /* Current number of allocations */ |
|
124 int mxCurrent[NCSIZE]; /* Highwater mark for nCurrent */ |
|
125 |
|
126 } mem; |
|
127 |
|
128 |
|
129 /* |
|
130 ** Adjust memory usage statistics |
|
131 */ |
|
132 static void adjustStats(int iSize, int increment){ |
|
133 int i = ((iSize+7)&~7)/8; |
|
134 if( i>NCSIZE-1 ){ |
|
135 i = NCSIZE - 1; |
|
136 } |
|
137 if( increment>0 ){ |
|
138 mem.nAlloc[i]++; |
|
139 mem.nCurrent[i]++; |
|
140 if( mem.nCurrent[i]>mem.mxCurrent[i] ){ |
|
141 mem.mxCurrent[i] = mem.nCurrent[i]; |
|
142 } |
|
143 }else{ |
|
144 mem.nCurrent[i]--; |
|
145 assert( mem.nCurrent[i]>=0 ); |
|
146 } |
|
147 } |
|
148 |
|
149 /* |
|
150 ** Given an allocation, find the MemBlockHdr for that allocation. |
|
151 ** |
|
152 ** This routine checks the guards at either end of the allocation and |
|
153 ** if they are incorrect it asserts. |
|
154 */ |
|
155 static struct MemBlockHdr *sqlite3MemsysGetHeader(void *pAllocation){ |
|
156 struct MemBlockHdr *p; |
|
157 int *pInt; |
|
158 u8 *pU8; |
|
159 int nReserve; |
|
160 |
|
161 p = (struct MemBlockHdr*)pAllocation; |
|
162 p--; |
|
163 assert( p->iForeGuard==FOREGUARD ); |
|
164 nReserve = (p->iSize+7)&~7; |
|
165 pInt = (int*)pAllocation; |
|
166 pU8 = (u8*)pAllocation; |
|
167 assert( pInt[nReserve/sizeof(int)]==REARGUARD ); |
|
168 assert( (nReserve-0)<=p->iSize || pU8[nReserve-1]==0x65 ); |
|
169 assert( (nReserve-1)<=p->iSize || pU8[nReserve-2]==0x65 ); |
|
170 assert( (nReserve-2)<=p->iSize || pU8[nReserve-3]==0x65 ); |
|
171 return p; |
|
172 } |
|
173 |
|
174 /* |
|
175 ** Return the number of bytes currently allocated at address p. |
|
176 */ |
|
177 static int sqlite3MemSize(void *p){ |
|
178 struct MemBlockHdr *pHdr; |
|
179 if( !p ){ |
|
180 return 0; |
|
181 } |
|
182 pHdr = sqlite3MemsysGetHeader(p); |
|
183 return pHdr->iSize; |
|
184 } |
|
185 |
|
186 /* |
|
187 ** Initialize the memory allocation subsystem. |
|
188 */ |
|
189 static int sqlite3MemInit(void *NotUsed){ |
|
190 if( !sqlite3GlobalConfig.bMemstat ){ |
|
191 /* If memory status is enabled, then the malloc.c wrapper will already |
|
192 ** hold the STATIC_MEM mutex when the routines here are invoked. */ |
|
193 mem.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); |
|
194 } |
|
195 return SQLITE_OK; |
|
196 } |
|
197 |
|
198 /* |
|
199 ** Deinitialize the memory allocation subsystem. |
|
200 */ |
|
201 static void sqlite3MemShutdown(void *NotUsed){ |
|
202 mem.mutex = 0; |
|
203 } |
|
204 |
|
205 /* |
|
206 ** Round up a request size to the next valid allocation size. |
|
207 */ |
|
208 static int sqlite3MemRoundup(int n){ |
|
209 return (n+7) & ~7; |
|
210 } |
|
211 |
|
212 /* |
|
213 ** Allocate nByte bytes of memory. |
|
214 */ |
|
215 static void *sqlite3MemMalloc(int nByte){ |
|
216 struct MemBlockHdr *pHdr; |
|
217 void **pBt; |
|
218 char *z; |
|
219 int *pInt; |
|
220 void *p = 0; |
|
221 int totalSize; |
|
222 int nReserve; |
|
223 sqlite3_mutex_enter(mem.mutex); |
|
224 assert( mem.disallow==0 ); |
|
225 nReserve = (nByte+7)&~7; |
|
226 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; |
|
258 adjustStats(nByte, +1); |
|
259 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); |
|
265 return p; |
|
266 } |
|
267 |
|
268 /* |
|
269 ** Free memory. |
|
270 */ |
|
271 static void sqlite3MemFree(void *pPrior){ |
|
272 struct MemBlockHdr *pHdr; |
|
273 void **pBt; |
|
274 char *z; |
|
275 assert( sqlite3GlobalConfig.bMemstat || mem.mutex!=0 ); |
|
276 pHdr = sqlite3MemsysGetHeader(pPrior); |
|
277 pBt = (void**)pHdr; |
|
278 pBt -= pHdr->nBacktraceSlots; |
|
279 sqlite3_mutex_enter(mem.mutex); |
|
280 if( pHdr->pPrev ){ |
|
281 assert( pHdr->pPrev->pNext==pHdr ); |
|
282 pHdr->pPrev->pNext = pHdr->pNext; |
|
283 }else{ |
|
284 assert( mem.pFirst==pHdr ); |
|
285 mem.pFirst = pHdr->pNext; |
|
286 } |
|
287 if( pHdr->pNext ){ |
|
288 assert( pHdr->pNext->pPrev==pHdr ); |
|
289 pHdr->pNext->pPrev = pHdr->pPrev; |
|
290 }else{ |
|
291 assert( mem.pLast==pHdr ); |
|
292 mem.pLast = pHdr->pPrev; |
|
293 } |
|
294 z = (char*)pBt; |
|
295 z -= pHdr->nTitle; |
|
296 adjustStats(pHdr->iSize, -1); |
|
297 memset(z, 0x2b, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) + |
|
298 pHdr->iSize + sizeof(int) + pHdr->nTitle); |
|
299 free(z); |
|
300 sqlite3_mutex_leave(mem.mutex); |
|
301 } |
|
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 */ |
|
312 static void *sqlite3MemRealloc(void *pPrior, int nByte){ |
|
313 struct MemBlockHdr *pOldHdr; |
|
314 void *pNew; |
|
315 assert( mem.disallow==0 ); |
|
316 pOldHdr = sqlite3MemsysGetHeader(pPrior); |
|
317 pNew = sqlite3MemMalloc(nByte); |
|
318 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 } |
|
323 sqlite3MemFree(pPrior); |
|
324 } |
|
325 return pNew; |
|
326 } |
|
327 |
|
328 |
|
329 const sqlite3_mem_methods *sqlite3MemGetDefault(void){ |
|
330 static const sqlite3_mem_methods defaultMethods = { |
|
331 sqlite3MemMalloc, |
|
332 sqlite3MemFree, |
|
333 sqlite3MemRealloc, |
|
334 sqlite3MemSize, |
|
335 sqlite3MemRoundup, |
|
336 sqlite3MemInit, |
|
337 sqlite3MemShutdown, |
|
338 0 |
|
339 }; |
|
340 return &defaultMethods; |
|
341 } |
|
342 |
|
343 /* |
|
344 ** Populate the low-level memory allocation function pointers in |
|
345 ** sqlite3GlobalConfig.m with pointers to the routines in this file. |
|
346 */ |
|
347 void sqlite3MemSetDefault(void){ |
|
348 sqlite3_config(SQLITE_CONFIG_MALLOC, sqlite3MemGetDefault()); |
|
349 } |
|
350 |
|
351 /* |
|
352 ** Set the number of backtrace levels kept for each allocation. |
|
353 ** A value of zero turns off backtracing. The number is always rounded |
|
354 ** up to a multiple of 2. |
|
355 */ |
|
356 void sqlite3MemdebugBacktrace(int depth){ |
|
357 if( depth<0 ){ depth = 0; } |
|
358 if( depth>20 ){ depth = 20; } |
|
359 depth = (depth+1)&0xfe; |
|
360 mem.nBacktrace = depth; |
|
361 } |
|
362 |
|
363 void sqlite3MemdebugBacktraceCallback(void (*xBacktrace)(int, int, void **)){ |
|
364 mem.xBacktrace = xBacktrace; |
|
365 } |
|
366 |
|
367 /* |
|
368 ** Set the title string for subsequent allocations. |
|
369 */ |
|
370 void sqlite3MemdebugSettitle(const char *zTitle){ |
|
371 int n = strlen(zTitle) + 1; |
|
372 sqlite3_mutex_enter(mem.mutex); |
|
373 if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1; |
|
374 memcpy(mem.zTitle, zTitle, n); |
|
375 mem.zTitle[n] = 0; |
|
376 mem.nTitle = (n+7)&~7; |
|
377 sqlite3_mutex_leave(mem.mutex); |
|
378 } |
|
379 |
|
380 void sqlite3MemdebugSync(){ |
|
381 struct MemBlockHdr *pHdr; |
|
382 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){ |
|
383 void **pBt = (void**)pHdr; |
|
384 pBt -= pHdr->nBacktraceSlots; |
|
385 mem.xBacktrace(pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]); |
|
386 } |
|
387 } |
|
388 |
|
389 /* |
|
390 ** Open the file indicated and write a log of all unfreed memory |
|
391 ** allocations into that log. |
|
392 */ |
|
393 void sqlite3MemdebugDump(const char *zFilename){ |
|
394 FILE *out; |
|
395 struct MemBlockHdr *pHdr; |
|
396 void **pBt; |
|
397 int i; |
|
398 char buf[300]; |
|
399 int memLeakFile = strstr(zFilename, "memleak.txt") != 0; |
|
400 out = fopen(zFilename, "w"); |
|
401 if( out==0 ){ |
|
402 sprintf(buf, "** Unable to output memory debug output log: %s **\n", zFilename); |
|
403 fprintf(stderr, buf); |
|
404 return; |
|
405 } |
|
406 sprintf(buf, "\n**** memory dump begin ****\n"); |
|
407 if(memLeakFile) |
|
408 printf(buf); |
|
409 fprintf(out, buf); |
|
410 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){ |
|
411 char *z = (char*)pHdr; |
|
412 z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle; |
|
413 sprintf(buf, "**** %lld bytes at %p from %s ****\n", pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???"); |
|
414 if(memLeakFile) |
|
415 printf(buf); |
|
416 fprintf(out, buf); |
|
417 if( pHdr->nBacktrace ){ |
|
418 fflush(out); |
|
419 pBt = (void**)pHdr; |
|
420 pBt -= pHdr->nBacktraceSlots; |
|
421 backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out)); |
|
422 fprintf(out, "\n"); |
|
423 } |
|
424 } |
|
425 sprintf(buf, "COUNTS:\n"); |
|
426 if(memLeakFile) |
|
427 printf(buf); |
|
428 fprintf(out, buf); |
|
429 for(i=0; i<NCSIZE-1; i++){ |
|
430 if( mem.nAlloc[i] ){ |
|
431 sprintf(buf, " %5d: %10d %10d %10d\n", i*8, mem.nAlloc[i], mem.nCurrent[i], mem.mxCurrent[i]); |
|
432 if(memLeakFile) |
|
433 printf(buf); |
|
434 fprintf(out, buf); |
|
435 } |
|
436 } |
|
437 if( mem.nAlloc[NCSIZE-1] ){ |
|
438 sprintf(buf, " %5d: %10d %10d %10d\n", NCSIZE*8-8, mem.nAlloc[NCSIZE-1], mem.nCurrent[NCSIZE-1], mem.mxCurrent[NCSIZE-1]); |
|
439 if(memLeakFile) |
|
440 printf(buf); |
|
441 fprintf(out, buf); |
|
442 } |
|
443 sprintf(buf, "**** memory dump end ****\n"); |
|
444 if(memLeakFile) |
|
445 printf(buf); |
|
446 fprintf(out, buf); |
|
447 fclose(out); |
|
448 } |
|
449 |
|
450 /* |
|
451 ** Return the number of times sqlite3MemMalloc() has been called. |
|
452 */ |
|
453 int sqlite3MemdebugMallocCount(){ |
|
454 int i; |
|
455 int nTotal = 0; |
|
456 for(i=0; i<NCSIZE; i++){ |
|
457 nTotal += mem.nAlloc[i]; |
|
458 } |
|
459 return nTotal; |
|
460 } |
|
461 |
|
462 |
|
463 #endif /* SQLITE_MEMDEBUG */ |