drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 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 | ************************************************************************* |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 12 | ** |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 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 | ** |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 22 | ** $Id: mem2.c,v 1.41 2008/12/05 17:17:08 drh Exp $ |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 23 | */ |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 24 | #include "sqliteInt.h" |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 25 | |
| 26 | /* |
| 27 | ** This version of the memory allocator is used only if the |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 28 | ** SQLITE_MEMDEBUG macro is defined |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 29 | */ |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 30 | #ifdef SQLITE_MEMDEBUG |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 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 |
danielk1977 | 44a376f | 2008-08-12 15:04:58 +0000 | [diff] [blame] | 39 | # define backtrace(A,B) 1 |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 40 | # define backtrace_symbols_fd(A,B,C) |
| 41 | #endif |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 42 | #include <stdio.h> |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 43 | |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 44 | /* |
| 45 | ** Each memory allocation looks like this: |
| 46 | ** |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 47 | ** ------------------------------------------------------------------------ |
| 48 | ** | Title | backtrace pointers | MemBlockHdr | allocation | EndGuard | |
| 49 | ** ------------------------------------------------------------------------ |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 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 { |
drh | fab6959 | 2008-04-10 14:57:24 +0000 | [diff] [blame] | 58 | i64 iSize; /* Size of this allocation */ |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 59 | struct MemBlockHdr *pNext, *pPrev; /* Linked list of all unfreed memory */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 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 */ |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | /* |
| 67 | ** Guard words |
| 68 | */ |
| 69 | #define FOREGUARD 0x80F5E153 |
| 70 | #define REARGUARD 0xE4676B53 |
| 71 | |
| 72 | /* |
drh | d2bb327 | 2007-10-15 19:34:32 +0000 | [diff] [blame] | 73 | ** Number of malloc size increments to track. |
| 74 | */ |
drh | 9c7a60d | 2007-10-19 17:47:24 +0000 | [diff] [blame] | 75 | #define NCSIZE 1000 |
drh | d2bb327 | 2007-10-15 19:34:32 +0000 | [diff] [blame] | 76 | |
| 77 | /* |
drh | 0e6f154 | 2007-08-15 20:41:28 +0000 | [diff] [blame] | 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. |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 82 | */ |
drh | 0e6f154 | 2007-08-15 20:41:28 +0000 | [diff] [blame] | 83 | static struct { |
drh | 0e6f154 | 2007-08-15 20:41:28 +0000 | [diff] [blame] | 84 | |
| 85 | /* |
| 86 | ** Mutex to control access to the memory allocation subsystem. |
| 87 | */ |
| 88 | sqlite3_mutex *mutex; |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 89 | |
drh | 0e6f154 | 2007-08-15 20:41:28 +0000 | [diff] [blame] | 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; |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 100 | void (*xBacktrace)(int, int, void **); |
drh | 0e6f154 | 2007-08-15 20:41:28 +0000 | [diff] [blame] | 101 | |
| 102 | /* |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 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 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 108 | /* |
| 109 | ** sqlite3MallocDisallow() increments the following counter. |
| 110 | ** sqlite3MallocAllow() decrements it. |
| 111 | */ |
| 112 | int disallow; /* Do not allow memory allocation */ |
drh | d2bb327 | 2007-10-15 19:34:32 +0000 | [diff] [blame] | 113 | |
| 114 | /* |
| 115 | ** Gather statistics on the sizes of memory allocations. |
drh | 2abcd58 | 2008-07-24 23:34:07 +0000 | [diff] [blame] | 116 | ** nAlloc[i] is the number of allocation attempts of i*8 |
drh | d2bb327 | 2007-10-15 19:34:32 +0000 | [diff] [blame] | 117 | ** bytes. i==NCSIZE is the number of allocation attempts for |
drh | 9c7a60d | 2007-10-19 17:47:24 +0000 | [diff] [blame] | 118 | ** sizes more than NCSIZE*8 bytes. |
drh | d2bb327 | 2007-10-15 19:34:32 +0000 | [diff] [blame] | 119 | */ |
drh | 2abcd58 | 2008-07-24 23:34:07 +0000 | [diff] [blame] | 120 | int nAlloc[NCSIZE]; /* Total number of allocations */ |
| 121 | int nCurrent[NCSIZE]; /* Current number of allocations */ |
| 122 | int mxCurrent[NCSIZE]; /* Highwater mark for nCurrent */ |
drh | d2bb327 | 2007-10-15 19:34:32 +0000 | [diff] [blame] | 123 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 124 | } mem; |
drh | 0e6f154 | 2007-08-15 20:41:28 +0000 | [diff] [blame] | 125 | |
drh | 2abcd58 | 2008-07-24 23:34:07 +0000 | [diff] [blame] | 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 | |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 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; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 155 | int *pInt; |
danielk1977 | ce98bba | 2008-04-03 10:13:01 +0000 | [diff] [blame] | 156 | u8 *pU8; |
| 157 | int nReserve; |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 158 | |
| 159 | p = (struct MemBlockHdr*)pAllocation; |
| 160 | p--; |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 161 | assert( p->iForeGuard==(int)FOREGUARD ); |
drh | fab6959 | 2008-04-10 14:57:24 +0000 | [diff] [blame] | 162 | nReserve = (p->iSize+7)&~7; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 163 | pInt = (int*)pAllocation; |
danielk1977 | ce98bba | 2008-04-03 10:13:01 +0000 | [diff] [blame] | 164 | pU8 = (u8*)pAllocation; |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 165 | assert( pInt[nReserve/sizeof(int)]==(int)REARGUARD ); |
danielk1977 | ce98bba | 2008-04-03 10:13:01 +0000 | [diff] [blame] | 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 ); |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 169 | return p; |
| 170 | } |
| 171 | |
| 172 | /* |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 173 | ** Return the number of bytes currently allocated at address p. |
| 174 | */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 175 | static int sqlite3MemSize(void *p){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 176 | struct MemBlockHdr *pHdr; |
| 177 | if( !p ){ |
| 178 | return 0; |
| 179 | } |
| 180 | pHdr = sqlite3MemsysGetHeader(p); |
| 181 | return pHdr->iSize; |
| 182 | } |
| 183 | |
| 184 | /* |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 185 | ** Initialize the memory allocation subsystem. |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 186 | */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 187 | static int sqlite3MemInit(void *NotUsed){ |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 188 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 189 | if( !sqlite3GlobalConfig.bMemstat ){ |
drh | 65bbf29 | 2008-06-19 01:03:17 +0000 | [diff] [blame] | 190 | /* 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 | } |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 194 | return SQLITE_OK; |
| 195 | } |
| 196 | |
| 197 | /* |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 198 | ** Deinitialize the memory allocation subsystem. |
| 199 | */ |
| 200 | static void sqlite3MemShutdown(void *NotUsed){ |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 201 | UNUSED_PARAMETER(NotUsed); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 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 | /* |
drh | 0e6f154 | 2007-08-15 20:41:28 +0000 | [diff] [blame] | 213 | ** Allocate nByte bytes of memory. |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 214 | */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 215 | static void *sqlite3MemMalloc(int nByte){ |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 216 | struct MemBlockHdr *pHdr; |
| 217 | void **pBt; |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 218 | char *z; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 219 | int *pInt; |
danielk1977 | ca0c897 | 2007-09-01 09:02:53 +0000 | [diff] [blame] | 220 | void *p = 0; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 221 | int totalSize; |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 222 | int nReserve; |
| 223 | sqlite3_mutex_enter(mem.mutex); |
| 224 | assert( mem.disallow==0 ); |
| 225 | nReserve = (nByte+7)&~7; |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 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; |
drh | 2abcd58 | 2008-07-24 23:34:07 +0000 | [diff] [blame] | 258 | adjustStats(nByte, +1); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 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); |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 265 | return p; |
| 266 | } |
| 267 | |
| 268 | /* |
| 269 | ** Free memory. |
| 270 | */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 271 | static void sqlite3MemFree(void *pPrior){ |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 272 | struct MemBlockHdr *pHdr; |
| 273 | void **pBt; |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 274 | char *z; |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 275 | assert( sqlite3GlobalConfig.bMemstat || mem.mutex!=0 ); |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 276 | pHdr = sqlite3MemsysGetHeader(pPrior); |
| 277 | pBt = (void**)pHdr; |
| 278 | pBt -= pHdr->nBacktraceSlots; |
drh | 6bdec4a | 2007-08-16 19:40:16 +0000 | [diff] [blame] | 279 | sqlite3_mutex_enter(mem.mutex); |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 280 | if( pHdr->pPrev ){ |
| 281 | assert( pHdr->pPrev->pNext==pHdr ); |
| 282 | pHdr->pPrev->pNext = pHdr->pNext; |
| 283 | }else{ |
drh | 0e6f154 | 2007-08-15 20:41:28 +0000 | [diff] [blame] | 284 | assert( mem.pFirst==pHdr ); |
| 285 | mem.pFirst = pHdr->pNext; |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 286 | } |
| 287 | if( pHdr->pNext ){ |
| 288 | assert( pHdr->pNext->pPrev==pHdr ); |
| 289 | pHdr->pNext->pPrev = pHdr->pPrev; |
| 290 | }else{ |
drh | 0e6f154 | 2007-08-15 20:41:28 +0000 | [diff] [blame] | 291 | assert( mem.pLast==pHdr ); |
| 292 | mem.pLast = pHdr->pPrev; |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 293 | } |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 294 | z = (char*)pBt; |
| 295 | z -= pHdr->nTitle; |
drh | 2abcd58 | 2008-07-24 23:34:07 +0000 | [diff] [blame] | 296 | adjustStats(pHdr->iSize, -1); |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 297 | memset(z, 0x2b, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) + |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 298 | pHdr->iSize + sizeof(int) + pHdr->nTitle); |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 299 | free(z); |
drh | 0e6f154 | 2007-08-15 20:41:28 +0000 | [diff] [blame] | 300 | sqlite3_mutex_leave(mem.mutex); |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 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 | */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 312 | static void *sqlite3MemRealloc(void *pPrior, int nByte){ |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 313 | struct MemBlockHdr *pOldHdr; |
| 314 | void *pNew; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 315 | assert( mem.disallow==0 ); |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 316 | pOldHdr = sqlite3MemsysGetHeader(pPrior); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 317 | pNew = sqlite3MemMalloc(nByte); |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 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 | } |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 323 | sqlite3MemFree(pPrior); |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 324 | } |
| 325 | return pNew; |
| 326 | } |
| 327 | |
drh | d1370b6 | 2008-10-28 18:58:20 +0000 | [diff] [blame] | 328 | /* |
| 329 | ** Populate the low-level memory allocation function pointers in |
| 330 | ** sqlite3GlobalConfig.m with pointers to the routines in this file. |
| 331 | */ |
| 332 | void sqlite3MemSetDefault(void){ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 333 | static const sqlite3_mem_methods defaultMethods = { |
| 334 | sqlite3MemMalloc, |
| 335 | sqlite3MemFree, |
| 336 | sqlite3MemRealloc, |
| 337 | sqlite3MemSize, |
| 338 | sqlite3MemRoundup, |
| 339 | sqlite3MemInit, |
| 340 | sqlite3MemShutdown, |
| 341 | 0 |
| 342 | }; |
drh | d1370b6 | 2008-10-28 18:58:20 +0000 | [diff] [blame] | 343 | sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 344 | } |
| 345 | |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 346 | /* |
| 347 | ** Set the number of backtrace levels kept for each allocation. |
danielk1977 | 6d2ab0e | 2008-06-17 17:21:18 +0000 | [diff] [blame] | 348 | ** A value of zero turns off backtracing. The number is always rounded |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 349 | ** up to a multiple of 2. |
| 350 | */ |
drh | 49e4fd7 | 2008-02-19 15:15:15 +0000 | [diff] [blame] | 351 | void sqlite3MemdebugBacktrace(int depth){ |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 352 | if( depth<0 ){ depth = 0; } |
| 353 | if( depth>20 ){ depth = 20; } |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 354 | depth = (depth+1)&0xfe; |
drh | 0e6f154 | 2007-08-15 20:41:28 +0000 | [diff] [blame] | 355 | mem.nBacktrace = depth; |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 356 | } |
| 357 | |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 358 | void sqlite3MemdebugBacktraceCallback(void (*xBacktrace)(int, int, void **)){ |
| 359 | mem.xBacktrace = xBacktrace; |
| 360 | } |
| 361 | |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 362 | /* |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 363 | ** Set the title string for subsequent allocations. |
| 364 | */ |
drh | 49e4fd7 | 2008-02-19 15:15:15 +0000 | [diff] [blame] | 365 | void sqlite3MemdebugSettitle(const char *zTitle){ |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 366 | unsigned int n = strlen(zTitle) + 1; |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 367 | sqlite3_mutex_enter(mem.mutex); |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 368 | if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1; |
| 369 | memcpy(mem.zTitle, zTitle, n); |
| 370 | mem.zTitle[n] = 0; |
drh | fab6959 | 2008-04-10 14:57:24 +0000 | [diff] [blame] | 371 | mem.nTitle = (n+7)&~7; |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 372 | sqlite3_mutex_leave(mem.mutex); |
| 373 | } |
| 374 | |
danielk1977 | dbdc4d4 | 2008-03-28 07:42:53 +0000 | [diff] [blame] | 375 | void 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 | |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 384 | /* |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 385 | ** Open the file indicated and write a log of all unfreed memory |
| 386 | ** allocations into that log. |
| 387 | */ |
drh | 49e4fd7 | 2008-02-19 15:15:15 +0000 | [diff] [blame] | 388 | void sqlite3MemdebugDump(const char *zFilename){ |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 389 | FILE *out; |
| 390 | struct MemBlockHdr *pHdr; |
| 391 | void **pBt; |
drh | d2bb327 | 2007-10-15 19:34:32 +0000 | [diff] [blame] | 392 | int i; |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 393 | 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 | } |
drh | 0e6f154 | 2007-08-15 20:41:28 +0000 | [diff] [blame] | 399 | for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){ |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 400 | char *z = (char*)pHdr; |
| 401 | z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle; |
drh | fab6959 | 2008-04-10 14:57:24 +0000 | [diff] [blame] | 402 | fprintf(out, "**** %lld bytes at %p from %s ****\n", |
drh | d5499d6 | 2007-08-24 04:15:00 +0000 | [diff] [blame] | 403 | pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???"); |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 404 | 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 | } |
drh | d2bb327 | 2007-10-15 19:34:32 +0000 | [diff] [blame] | 412 | fprintf(out, "COUNTS:\n"); |
| 413 | for(i=0; i<NCSIZE-1; i++){ |
drh | 2abcd58 | 2008-07-24 23:34:07 +0000 | [diff] [blame] | 414 | if( mem.nAlloc[i] ){ |
| 415 | fprintf(out, " %5d: %10d %10d %10d\n", |
| 416 | i*8, mem.nAlloc[i], mem.nCurrent[i], mem.mxCurrent[i]); |
drh | d2bb327 | 2007-10-15 19:34:32 +0000 | [diff] [blame] | 417 | } |
| 418 | } |
drh | 2abcd58 | 2008-07-24 23:34:07 +0000 | [diff] [blame] | 419 | 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]); |
drh | d2bb327 | 2007-10-15 19:34:32 +0000 | [diff] [blame] | 423 | } |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 424 | fclose(out); |
| 425 | } |
| 426 | |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 427 | /* |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 428 | ** Return the number of times sqlite3MemMalloc() has been called. |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 429 | */ |
drh | 49e4fd7 | 2008-02-19 15:15:15 +0000 | [diff] [blame] | 430 | int sqlite3MemdebugMallocCount(){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 431 | int i; |
| 432 | int nTotal = 0; |
| 433 | for(i=0; i<NCSIZE; i++){ |
drh | 2abcd58 | 2008-07-24 23:34:07 +0000 | [diff] [blame] | 434 | nTotal += mem.nAlloc[i]; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 435 | } |
| 436 | return nTotal; |
| 437 | } |
| 438 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 439 | |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 440 | #endif /* SQLITE_MEMDEBUG */ |