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. |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 21 | */ |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 22 | #include "sqliteInt.h" |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 23 | |
| 24 | /* |
| 25 | ** This version of the memory allocator is used only if the |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 26 | ** SQLITE_MEMDEBUG macro is defined |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 27 | */ |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 28 | #ifdef SQLITE_MEMDEBUG |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 29 | |
| 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 |
danielk1977 | 44a376f | 2008-08-12 15:04:58 +0000 | [diff] [blame] | 37 | # define backtrace(A,B) 1 |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 38 | # define backtrace_symbols_fd(A,B,C) |
| 39 | #endif |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 40 | #include <stdio.h> |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 41 | |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 42 | /* |
| 43 | ** Each memory allocation looks like this: |
| 44 | ** |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 45 | ** ------------------------------------------------------------------------ |
| 46 | ** | Title | backtrace pointers | MemBlockHdr | allocation | EndGuard | |
| 47 | ** ------------------------------------------------------------------------ |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 48 | ** |
| 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 | */ |
| 55 | struct MemBlockHdr { |
drh | fab6959 | 2008-04-10 14:57:24 +0000 | [diff] [blame] | 56 | i64 iSize; /* Size of this allocation */ |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 57 | struct MemBlockHdr *pNext, *pPrev; /* Linked list of all unfreed memory */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 58 | char nBacktrace; /* Number of backtraces on this alloc */ |
| 59 | char nBacktraceSlots; /* Available backtrace slots */ |
drh | 107b56e | 2010-03-12 16:32:53 +0000 | [diff] [blame] | 60 | u8 nTitle; /* Bytes of title; includes '\0' */ |
| 61 | u8 eType; /* Allocation type code */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 62 | int iForeGuard; /* Guard word for sanity */ |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | /* |
| 66 | ** Guard words |
| 67 | */ |
| 68 | #define FOREGUARD 0x80F5E153 |
| 69 | #define REARGUARD 0xE4676B53 |
| 70 | |
| 71 | /* |
drh | d2bb327 | 2007-10-15 19:34:32 +0000 | [diff] [blame] | 72 | ** Number of malloc size increments to track. |
| 73 | */ |
drh | 9c7a60d | 2007-10-19 17:47:24 +0000 | [diff] [blame] | 74 | #define NCSIZE 1000 |
drh | d2bb327 | 2007-10-15 19:34:32 +0000 | [diff] [blame] | 75 | |
| 76 | /* |
drh | 0e6f154 | 2007-08-15 20:41:28 +0000 | [diff] [blame] | 77 | ** 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. |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 81 | */ |
drh | 0e6f154 | 2007-08-15 20:41:28 +0000 | [diff] [blame] | 82 | static struct { |
drh | 0e6f154 | 2007-08-15 20:41:28 +0000 | [diff] [blame] | 83 | |
| 84 | /* |
| 85 | ** Mutex to control access to the memory allocation subsystem. |
| 86 | */ |
| 87 | sqlite3_mutex *mutex; |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 88 | |
drh | 0e6f154 | 2007-08-15 20:41:28 +0000 | [diff] [blame] | 89 | /* |
| 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; |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 99 | void (*xBacktrace)(int, int, void **); |
drh | 0e6f154 | 2007-08-15 20:41:28 +0000 | [diff] [blame] | 100 | |
| 101 | /* |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 102 | ** 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 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 107 | /* |
| 108 | ** sqlite3MallocDisallow() increments the following counter. |
| 109 | ** sqlite3MallocAllow() decrements it. |
| 110 | */ |
| 111 | int disallow; /* Do not allow memory allocation */ |
drh | d2bb327 | 2007-10-15 19:34:32 +0000 | [diff] [blame] | 112 | |
| 113 | /* |
| 114 | ** Gather statistics on the sizes of memory allocations. |
drh | 2abcd58 | 2008-07-24 23:34:07 +0000 | [diff] [blame] | 115 | ** nAlloc[i] is the number of allocation attempts of i*8 |
drh | d2bb327 | 2007-10-15 19:34:32 +0000 | [diff] [blame] | 116 | ** bytes. i==NCSIZE is the number of allocation attempts for |
drh | 9c7a60d | 2007-10-19 17:47:24 +0000 | [diff] [blame] | 117 | ** sizes more than NCSIZE*8 bytes. |
drh | d2bb327 | 2007-10-15 19:34:32 +0000 | [diff] [blame] | 118 | */ |
drh | 2abcd58 | 2008-07-24 23:34:07 +0000 | [diff] [blame] | 119 | int nAlloc[NCSIZE]; /* Total number of allocations */ |
| 120 | int nCurrent[NCSIZE]; /* Current number of allocations */ |
| 121 | int mxCurrent[NCSIZE]; /* Highwater mark for nCurrent */ |
drh | d2bb327 | 2007-10-15 19:34:32 +0000 | [diff] [blame] | 122 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 123 | } mem; |
drh | 0e6f154 | 2007-08-15 20:41:28 +0000 | [diff] [blame] | 124 | |
drh | 2abcd58 | 2008-07-24 23:34:07 +0000 | [diff] [blame] | 125 | |
| 126 | /* |
| 127 | ** Adjust memory usage statistics |
| 128 | */ |
| 129 | static void adjustStats(int iSize, int increment){ |
danielk1977 | bc73971 | 2009-03-23 04:33:32 +0000 | [diff] [blame] | 130 | int i = ROUND8(iSize)/8; |
drh | 2abcd58 | 2008-07-24 23:34:07 +0000 | [diff] [blame] | 131 | 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 | |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 146 | /* |
| 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 | */ |
| 152 | static struct MemBlockHdr *sqlite3MemsysGetHeader(void *pAllocation){ |
| 153 | struct MemBlockHdr *p; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 154 | int *pInt; |
danielk1977 | ce98bba | 2008-04-03 10:13:01 +0000 | [diff] [blame] | 155 | u8 *pU8; |
| 156 | int nReserve; |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 157 | |
| 158 | p = (struct MemBlockHdr*)pAllocation; |
| 159 | p--; |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 160 | assert( p->iForeGuard==(int)FOREGUARD ); |
danielk1977 | bc73971 | 2009-03-23 04:33:32 +0000 | [diff] [blame] | 161 | nReserve = ROUND8(p->iSize); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 162 | pInt = (int*)pAllocation; |
danielk1977 | ce98bba | 2008-04-03 10:13:01 +0000 | [diff] [blame] | 163 | pU8 = (u8*)pAllocation; |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 164 | assert( pInt[nReserve/sizeof(int)]==(int)REARGUARD ); |
shane | d20010c | 2009-02-05 03:00:06 +0000 | [diff] [blame] | 165 | /* 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 ); |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 170 | return p; |
| 171 | } |
| 172 | |
| 173 | /* |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 174 | ** Return the number of bytes currently allocated at address p. |
| 175 | */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 176 | static int sqlite3MemSize(void *p){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 177 | struct MemBlockHdr *pHdr; |
| 178 | if( !p ){ |
| 179 | return 0; |
| 180 | } |
| 181 | pHdr = sqlite3MemsysGetHeader(p); |
| 182 | return pHdr->iSize; |
| 183 | } |
| 184 | |
| 185 | /* |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 186 | ** Initialize the memory allocation subsystem. |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 187 | */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 188 | static int sqlite3MemInit(void *NotUsed){ |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 189 | UNUSED_PARAMETER(NotUsed); |
shane | d20010c | 2009-02-05 03:00:06 +0000 | [diff] [blame] | 190 | assert( (sizeof(struct MemBlockHdr)&7) == 0 ); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 191 | if( !sqlite3GlobalConfig.bMemstat ){ |
drh | 65bbf29 | 2008-06-19 01:03:17 +0000 | [diff] [blame] | 192 | /* 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 | } |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 196 | return SQLITE_OK; |
| 197 | } |
| 198 | |
| 199 | /* |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 200 | ** Deinitialize the memory allocation subsystem. |
| 201 | */ |
| 202 | static void sqlite3MemShutdown(void *NotUsed){ |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 203 | UNUSED_PARAMETER(NotUsed); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 204 | mem.mutex = 0; |
| 205 | } |
| 206 | |
| 207 | /* |
| 208 | ** Round up a request size to the next valid allocation size. |
| 209 | */ |
| 210 | static int sqlite3MemRoundup(int n){ |
danielk1977 | bc73971 | 2009-03-23 04:33:32 +0000 | [diff] [blame] | 211 | return ROUND8(n); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /* |
drh | 10f864e | 2010-01-05 03:30:15 +0000 | [diff] [blame] | 215 | ** 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 | */ |
| 219 | static 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 | /* |
drh | 0e6f154 | 2007-08-15 20:41:28 +0000 | [diff] [blame] | 240 | ** Allocate nByte bytes of memory. |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 241 | */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 242 | static void *sqlite3MemMalloc(int nByte){ |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 243 | struct MemBlockHdr *pHdr; |
| 244 | void **pBt; |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 245 | char *z; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 246 | int *pInt; |
danielk1977 | ca0c897 | 2007-09-01 09:02:53 +0000 | [diff] [blame] | 247 | void *p = 0; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 248 | int totalSize; |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 249 | int nReserve; |
| 250 | sqlite3_mutex_enter(mem.mutex); |
| 251 | assert( mem.disallow==0 ); |
danielk1977 | bc73971 | 2009-03-23 04:33:32 +0000 | [diff] [blame] | 252 | nReserve = ROUND8(nByte); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 253 | 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; |
drh | 107b56e | 2010-03-12 16:32:53 +0000 | [diff] [blame] | 269 | pHdr->eType = MEMTYPE_HEAP; |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 270 | 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*)); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 276 | assert(pBt[0]); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 277 | 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; |
drh | 2abcd58 | 2008-07-24 23:34:07 +0000 | [diff] [blame] | 287 | adjustStats(nByte, +1); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 288 | pInt = (int*)&pHdr[1]; |
| 289 | pInt[nReserve/sizeof(int)] = REARGUARD; |
drh | 10f864e | 2010-01-05 03:30:15 +0000 | [diff] [blame] | 290 | randomFill((char*)pInt, nByte); |
| 291 | memset(((char*)pInt)+nByte, 0x65, nReserve-nByte); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 292 | p = (void*)pInt; |
| 293 | } |
| 294 | sqlite3_mutex_leave(mem.mutex); |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 295 | return p; |
| 296 | } |
| 297 | |
| 298 | /* |
| 299 | ** Free memory. |
| 300 | */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 301 | static void sqlite3MemFree(void *pPrior){ |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 302 | struct MemBlockHdr *pHdr; |
| 303 | void **pBt; |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 304 | char *z; |
dan | 3b4aae5 | 2010-03-08 15:17:53 +0000 | [diff] [blame] | 305 | assert( sqlite3GlobalConfig.bMemstat || sqlite3GlobalConfig.bCoreMutex==0 |
| 306 | || mem.mutex!=0 ); |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 307 | pHdr = sqlite3MemsysGetHeader(pPrior); |
| 308 | pBt = (void**)pHdr; |
| 309 | pBt -= pHdr->nBacktraceSlots; |
drh | 6bdec4a | 2007-08-16 19:40:16 +0000 | [diff] [blame] | 310 | sqlite3_mutex_enter(mem.mutex); |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 311 | if( pHdr->pPrev ){ |
| 312 | assert( pHdr->pPrev->pNext==pHdr ); |
| 313 | pHdr->pPrev->pNext = pHdr->pNext; |
| 314 | }else{ |
drh | 0e6f154 | 2007-08-15 20:41:28 +0000 | [diff] [blame] | 315 | assert( mem.pFirst==pHdr ); |
| 316 | mem.pFirst = pHdr->pNext; |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 317 | } |
| 318 | if( pHdr->pNext ){ |
| 319 | assert( pHdr->pNext->pPrev==pHdr ); |
| 320 | pHdr->pNext->pPrev = pHdr->pPrev; |
| 321 | }else{ |
drh | 0e6f154 | 2007-08-15 20:41:28 +0000 | [diff] [blame] | 322 | assert( mem.pLast==pHdr ); |
| 323 | mem.pLast = pHdr->pPrev; |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 324 | } |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 325 | z = (char*)pBt; |
| 326 | z -= pHdr->nTitle; |
drh | 2abcd58 | 2008-07-24 23:34:07 +0000 | [diff] [blame] | 327 | adjustStats(pHdr->iSize, -1); |
drh | 10f864e | 2010-01-05 03:30:15 +0000 | [diff] [blame] | 328 | randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) + |
| 329 | pHdr->iSize + sizeof(int) + pHdr->nTitle); |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 330 | free(z); |
drh | 0e6f154 | 2007-08-15 20:41:28 +0000 | [diff] [blame] | 331 | sqlite3_mutex_leave(mem.mutex); |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 332 | } |
| 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 | */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 343 | static void *sqlite3MemRealloc(void *pPrior, int nByte){ |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 344 | struct MemBlockHdr *pOldHdr; |
| 345 | void *pNew; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 346 | assert( mem.disallow==0 ); |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 347 | pOldHdr = sqlite3MemsysGetHeader(pPrior); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 348 | pNew = sqlite3MemMalloc(nByte); |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 349 | if( pNew ){ |
| 350 | memcpy(pNew, pPrior, nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize); |
| 351 | if( nByte>pOldHdr->iSize ){ |
drh | 10f864e | 2010-01-05 03:30:15 +0000 | [diff] [blame] | 352 | randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - pOldHdr->iSize); |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 353 | } |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 354 | sqlite3MemFree(pPrior); |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 355 | } |
| 356 | return pNew; |
| 357 | } |
| 358 | |
drh | d1370b6 | 2008-10-28 18:58:20 +0000 | [diff] [blame] | 359 | /* |
| 360 | ** Populate the low-level memory allocation function pointers in |
| 361 | ** sqlite3GlobalConfig.m with pointers to the routines in this file. |
| 362 | */ |
| 363 | void sqlite3MemSetDefault(void){ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 364 | static const sqlite3_mem_methods defaultMethods = { |
| 365 | sqlite3MemMalloc, |
| 366 | sqlite3MemFree, |
| 367 | sqlite3MemRealloc, |
| 368 | sqlite3MemSize, |
| 369 | sqlite3MemRoundup, |
| 370 | sqlite3MemInit, |
| 371 | sqlite3MemShutdown, |
| 372 | 0 |
| 373 | }; |
drh | d1370b6 | 2008-10-28 18:58:20 +0000 | [diff] [blame] | 374 | sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 375 | } |
| 376 | |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 377 | /* |
drh | 107b56e | 2010-03-12 16:32:53 +0000 | [diff] [blame] | 378 | ** Set the "type" of an allocation. |
| 379 | */ |
| 380 | void 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 | */ |
| 398 | int 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 */ |
drh | 107b56e | 2010-03-12 16:32:53 +0000 | [diff] [blame] | 404 | if( (pHdr->eType&eType)==0 ){ |
drh | 107b56e | 2010-03-12 16:32:53 +0000 | [diff] [blame] | 405 | rc = 0; |
| 406 | } |
| 407 | } |
| 408 | return rc; |
| 409 | } |
| 410 | |
drh | 1b0bfc6 | 2010-07-25 02:39:06 +0000 | [diff] [blame] | 411 | /* |
| 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 | */ |
| 420 | int 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 | } |
drh | 107b56e | 2010-03-12 16:32:53 +0000 | [diff] [blame] | 432 | |
| 433 | /* |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 434 | ** Set the number of backtrace levels kept for each allocation. |
danielk1977 | 6d2ab0e | 2008-06-17 17:21:18 +0000 | [diff] [blame] | 435 | ** A value of zero turns off backtracing. The number is always rounded |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 436 | ** up to a multiple of 2. |
| 437 | */ |
drh | 49e4fd7 | 2008-02-19 15:15:15 +0000 | [diff] [blame] | 438 | void sqlite3MemdebugBacktrace(int depth){ |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 439 | if( depth<0 ){ depth = 0; } |
| 440 | if( depth>20 ){ depth = 20; } |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 441 | depth = (depth+1)&0xfe; |
drh | 0e6f154 | 2007-08-15 20:41:28 +0000 | [diff] [blame] | 442 | mem.nBacktrace = depth; |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 443 | } |
| 444 | |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 445 | void sqlite3MemdebugBacktraceCallback(void (*xBacktrace)(int, int, void **)){ |
| 446 | mem.xBacktrace = xBacktrace; |
| 447 | } |
| 448 | |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 449 | /* |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 450 | ** Set the title string for subsequent allocations. |
| 451 | */ |
drh | 49e4fd7 | 2008-02-19 15:15:15 +0000 | [diff] [blame] | 452 | void sqlite3MemdebugSettitle(const char *zTitle){ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 453 | unsigned int n = sqlite3Strlen30(zTitle) + 1; |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 454 | sqlite3_mutex_enter(mem.mutex); |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 455 | if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1; |
| 456 | memcpy(mem.zTitle, zTitle, n); |
| 457 | mem.zTitle[n] = 0; |
danielk1977 | bc73971 | 2009-03-23 04:33:32 +0000 | [diff] [blame] | 458 | mem.nTitle = ROUND8(n); |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 459 | sqlite3_mutex_leave(mem.mutex); |
| 460 | } |
| 461 | |
danielk1977 | dbdc4d4 | 2008-03-28 07:42:53 +0000 | [diff] [blame] | 462 | void 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 | |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 471 | /* |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 472 | ** Open the file indicated and write a log of all unfreed memory |
| 473 | ** allocations into that log. |
| 474 | */ |
drh | 49e4fd7 | 2008-02-19 15:15:15 +0000 | [diff] [blame] | 475 | void sqlite3MemdebugDump(const char *zFilename){ |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 476 | FILE *out; |
| 477 | struct MemBlockHdr *pHdr; |
| 478 | void **pBt; |
drh | d2bb327 | 2007-10-15 19:34:32 +0000 | [diff] [blame] | 479 | int i; |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 480 | 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 | } |
drh | 0e6f154 | 2007-08-15 20:41:28 +0000 | [diff] [blame] | 486 | for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){ |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 487 | char *z = (char*)pHdr; |
| 488 | z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle; |
drh | fab6959 | 2008-04-10 14:57:24 +0000 | [diff] [blame] | 489 | fprintf(out, "**** %lld bytes at %p from %s ****\n", |
drh | d5499d6 | 2007-08-24 04:15:00 +0000 | [diff] [blame] | 490 | pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???"); |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 491 | 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 | } |
drh | d2bb327 | 2007-10-15 19:34:32 +0000 | [diff] [blame] | 499 | fprintf(out, "COUNTS:\n"); |
| 500 | for(i=0; i<NCSIZE-1; i++){ |
drh | 2abcd58 | 2008-07-24 23:34:07 +0000 | [diff] [blame] | 501 | if( mem.nAlloc[i] ){ |
| 502 | fprintf(out, " %5d: %10d %10d %10d\n", |
| 503 | i*8, mem.nAlloc[i], mem.nCurrent[i], mem.mxCurrent[i]); |
drh | d2bb327 | 2007-10-15 19:34:32 +0000 | [diff] [blame] | 504 | } |
| 505 | } |
drh | 2abcd58 | 2008-07-24 23:34:07 +0000 | [diff] [blame] | 506 | 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]); |
drh | d2bb327 | 2007-10-15 19:34:32 +0000 | [diff] [blame] | 510 | } |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 511 | fclose(out); |
| 512 | } |
| 513 | |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 514 | /* |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 515 | ** Return the number of times sqlite3MemMalloc() has been called. |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 516 | */ |
drh | 49e4fd7 | 2008-02-19 15:15:15 +0000 | [diff] [blame] | 517 | int sqlite3MemdebugMallocCount(){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 518 | int i; |
| 519 | int nTotal = 0; |
| 520 | for(i=0; i<NCSIZE; i++){ |
drh | 2abcd58 | 2008-07-24 23:34:07 +0000 | [diff] [blame] | 521 | nTotal += mem.nAlloc[i]; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 522 | } |
| 523 | return nTotal; |
| 524 | } |
| 525 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 526 | |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 527 | #endif /* SQLITE_MEMDEBUG */ |