drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2007 October 14 |
| 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 | ** This file contains the C functions that implement a memory |
| 13 | ** allocation subsystem for use by SQLite. |
| 14 | ** |
| 15 | ** This version of the memory allocation subsystem omits all |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 16 | ** use of malloc(). The SQLite user supplies a block of memory |
| 17 | ** before calling sqlite3_initialize() from which allocations |
| 18 | ** are made and returned by the xMalloc() and xRealloc() |
| 19 | ** implementations. Once sqlite3_initialize() has been called, |
| 20 | ** the amount of memory available to SQLite is fixed and cannot |
| 21 | ** be changed. |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 22 | ** |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 23 | ** This version of the memory allocation subsystem is included |
| 24 | ** in the build only if SQLITE_ENABLE_MEMSYS5 is defined. |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 25 | ** |
danielk1977 | 6b39c2e | 2008-06-25 14:57:53 +0000 | [diff] [blame] | 26 | ** $Id: mem5.c,v 1.8 2008/06/25 14:57:54 danielk1977 Exp $ |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 27 | */ |
| 28 | #include "sqliteInt.h" |
| 29 | |
| 30 | /* |
| 31 | ** This version of the memory allocator is used only when |
| 32 | ** SQLITE_POW2_MEMORY_SIZE is defined. |
| 33 | */ |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 34 | #ifdef SQLITE_ENABLE_MEMSYS5 |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 35 | |
| 36 | /* |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 37 | ** Log2 of the minimum size of an allocation. For example, if |
| 38 | ** 4 then all allocations will be rounded up to at least 16 bytes. |
| 39 | ** If 5 then all allocations will be rounded up to at least 32 bytes. |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 40 | */ |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 41 | #ifndef SQLITE_POW2_LOGMIN |
| 42 | # define SQLITE_POW2_LOGMIN 6 |
| 43 | #endif |
| 44 | #define POW2_MIN (1<<SQLITE_POW2_LOGMIN) |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 45 | |
| 46 | /* |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 47 | ** Log2 of the maximum size of an allocation. |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 48 | */ |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 49 | #ifndef SQLITE_POW2_LOGMAX |
| 50 | # define SQLITE_POW2_LOGMAX 18 |
| 51 | #endif |
| 52 | #define POW2_MAX (((unsigned int)1)<<SQLITE_POW2_LOGMAX) |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 53 | |
| 54 | /* |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 55 | ** Number of distinct allocation sizes. |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 56 | */ |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 57 | #define NSIZE (SQLITE_POW2_LOGMAX - SQLITE_POW2_LOGMIN + 1) |
| 58 | |
| 59 | /* |
| 60 | ** A minimum allocation is an instance of the following structure. |
| 61 | ** Larger allocations are an array of these structures where the |
| 62 | ** size of the array is a power of 2. |
| 63 | */ |
| 64 | typedef struct Mem5Block Mem5Block; |
| 65 | struct Mem5Block { |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 66 | union { |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 67 | char aData[POW2_MIN]; |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 68 | struct { |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 69 | int next; /* Index in mem5.aPool[] of next free chunk */ |
| 70 | int prev; /* Index in mem5.aPool[] of previous free chunk */ |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 71 | } list; |
| 72 | } u; |
| 73 | }; |
| 74 | |
| 75 | /* |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 76 | ** The size in blocks of an POW2_MAX allocation |
| 77 | */ |
| 78 | #define SZ_MAX (1<<(NSIZE-1)) |
| 79 | |
| 80 | /* |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 81 | ** Masks used for mem5.aCtrl[] elements. |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 82 | */ |
| 83 | #define CTRL_LOGSIZE 0x1f /* Log2 Size of this block relative to POW2_MIN */ |
| 84 | #define CTRL_FREE 0x20 /* True if not checked out */ |
| 85 | |
| 86 | /* |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 87 | ** All of the static variables used by this module are collected |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 88 | ** into a single structure named "mem5". This is to keep the |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 89 | ** static variables organized and to reduce namespace pollution |
| 90 | ** when this module is combined with other in the amalgamation. |
| 91 | */ |
| 92 | static struct { |
| 93 | /* |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 94 | ** The alarm callback and its arguments. The mem5.mutex lock will |
drh | eee4c8c | 2008-02-18 22:24:57 +0000 | [diff] [blame] | 95 | ** be held while the callback is running. Recursive calls into |
| 96 | ** the memory subsystem are allowed, but no new callbacks will be |
| 97 | ** issued. The alarmBusy variable is set to prevent recursive |
| 98 | ** callbacks. |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 99 | */ |
drh | eee4c8c | 2008-02-18 22:24:57 +0000 | [diff] [blame] | 100 | sqlite3_int64 alarmThreshold; |
| 101 | void (*alarmCallback)(void*, sqlite3_int64,int); |
| 102 | void *alarmArg; |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 103 | int alarmBusy; |
| 104 | |
| 105 | /* |
| 106 | ** Mutex to control access to the memory allocation subsystem. |
| 107 | */ |
| 108 | sqlite3_mutex *mutex; |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 109 | |
| 110 | /* |
| 111 | ** Performance statistics |
| 112 | */ |
| 113 | u64 nAlloc; /* Total number of calls to malloc */ |
| 114 | u64 totalAlloc; /* Total of all malloc calls - includes internal frag */ |
| 115 | u64 totalExcess; /* Total internal fragmentation */ |
| 116 | u32 currentOut; /* Current checkout, including internal fragmentation */ |
| 117 | u32 currentCount; /* Current number of distinct checkouts */ |
| 118 | u32 maxOut; /* Maximum instantaneous currentOut */ |
| 119 | u32 maxCount; /* Maximum instantaneous currentCount */ |
| 120 | u32 maxRequest; /* Largest allocation (exclusive of internal frag) */ |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 121 | |
| 122 | /* |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 123 | ** Lists of free blocks of various sizes. |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 124 | */ |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 125 | int aiFreelist[NSIZE]; |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 126 | |
| 127 | /* |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 128 | ** Space for tracking which blocks are checked out and the size |
| 129 | ** of each block. One byte per block. |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 130 | */ |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 131 | u8 *aCtrl; |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 132 | |
| 133 | /* |
| 134 | ** Memory available for allocation |
| 135 | */ |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 136 | int nBlock; |
| 137 | Mem5Block *aPool; |
| 138 | } mem5; |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 139 | |
| 140 | /* |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 141 | ** Unlink the chunk at mem5.aPool[i] from list it is currently |
| 142 | ** on. It should be found on mem5.aiFreelist[iLogsize]. |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 143 | */ |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 144 | static void memsys5Unlink(int i, int iLogsize){ |
| 145 | int next, prev; |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 146 | assert( i>=0 && i<mem5.nBlock ); |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 147 | assert( iLogsize>=0 && iLogsize<NSIZE ); |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 148 | assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize ); |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 149 | |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 150 | next = mem5.aPool[i].u.list.next; |
| 151 | prev = mem5.aPool[i].u.list.prev; |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 152 | if( prev<0 ){ |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 153 | mem5.aiFreelist[iLogsize] = next; |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 154 | }else{ |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 155 | mem5.aPool[prev].u.list.next = next; |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 156 | } |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 157 | if( next>=0 ){ |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 158 | mem5.aPool[next].u.list.prev = prev; |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 159 | } |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /* |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 163 | ** Link the chunk at mem5.aPool[i] so that is on the iLogsize |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 164 | ** free list. |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 165 | */ |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 166 | static void memsys5Link(int i, int iLogsize){ |
| 167 | int x; |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 168 | assert( sqlite3_mutex_held(mem5.mutex) ); |
| 169 | assert( i>=0 && i<mem5.nBlock ); |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 170 | assert( iLogsize>=0 && iLogsize<NSIZE ); |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 171 | assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize ); |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 172 | |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 173 | mem5.aPool[i].u.list.next = x = mem5.aiFreelist[iLogsize]; |
| 174 | mem5.aPool[i].u.list.prev = -1; |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 175 | if( x>=0 ){ |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 176 | assert( x<mem5.nBlock ); |
| 177 | mem5.aPool[x].u.list.prev = i; |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 178 | } |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 179 | mem5.aiFreelist[iLogsize] = i; |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /* |
danielk1977 | 6b39c2e | 2008-06-25 14:57:53 +0000 | [diff] [blame] | 183 | ** If the STATIC_MEM mutex is not already held, obtain it now. The mutex |
| 184 | ** will already be held (obtained by code in malloc.c) if |
| 185 | ** sqlite3Config.bMemStat is true. |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 186 | */ |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 187 | static void memsys5Enter(void){ |
danielk1977 | 6b39c2e | 2008-06-25 14:57:53 +0000 | [diff] [blame] | 188 | if( sqlite3Config.bMemstat==0 && mem5.mutex==0 ){ |
| 189 | mem5.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); |
| 190 | } |
| 191 | sqlite3_mutex_enter(mem5.mutex); |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 192 | } |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 193 | static void memsys5Leave(void){ |
danielk1977 | 6b39c2e | 2008-06-25 14:57:53 +0000 | [diff] [blame] | 194 | sqlite3_mutex_leave(mem5.mutex); |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /* |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 198 | ** Return the size of an outstanding allocation, in bytes. The |
| 199 | ** size returned omits the 8-byte header overhead. This only |
| 200 | ** works for chunks that are currently checked out. |
| 201 | */ |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 202 | static int memsys5Size(void *p){ |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 203 | int iSize = 0; |
| 204 | if( p ){ |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 205 | int i = ((Mem5Block*)p) - mem5.aPool; |
| 206 | assert( i>=0 && i<mem5.nBlock ); |
| 207 | iSize = 1 << ((mem5.aCtrl[i]&CTRL_LOGSIZE) + SQLITE_POW2_LOGMIN); |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 208 | } |
| 209 | return iSize; |
| 210 | } |
| 211 | |
| 212 | /* |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 213 | ** Find the first entry on the freelist iLogsize. Unlink that |
| 214 | ** entry and return its index. |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 215 | */ |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 216 | static int memsys5UnlinkFirst(int iLogsize){ |
| 217 | int i; |
| 218 | int iFirst; |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 219 | |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 220 | assert( iLogsize>=0 && iLogsize<NSIZE ); |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 221 | i = iFirst = mem5.aiFreelist[iLogsize]; |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 222 | assert( iFirst>=0 ); |
| 223 | while( i>0 ){ |
| 224 | if( i<iFirst ) iFirst = i; |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 225 | i = mem5.aPool[i].u.list.next; |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 226 | } |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 227 | memsys5Unlink(iFirst, iLogsize); |
| 228 | return iFirst; |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /* |
| 232 | ** Return a block of memory of at least nBytes in size. |
| 233 | ** Return NULL if unable. |
| 234 | */ |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 235 | static void *memsys5MallocUnsafe(int nByte){ |
| 236 | int i; /* Index of a mem5.aPool[] slot */ |
| 237 | int iBin; /* Index into mem5.aiFreelist[] */ |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 238 | int iFullSz; /* Size of allocation rounded up to power of 2 */ |
| 239 | int iLogsize; /* Log2 of iFullSz/POW2_MIN */ |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 240 | |
drh | eee4c8c | 2008-02-18 22:24:57 +0000 | [diff] [blame] | 241 | /* Keep track of the maximum allocation request. Even unfulfilled |
| 242 | ** requests are counted */ |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 243 | if( nByte>mem5.maxRequest ){ |
| 244 | mem5.maxRequest = nByte; |
drh | eee4c8c | 2008-02-18 22:24:57 +0000 | [diff] [blame] | 245 | } |
| 246 | |
drh | eee4c8c | 2008-02-18 22:24:57 +0000 | [diff] [blame] | 247 | /* Round nByte up to the next valid power of two */ |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 248 | if( nByte>POW2_MAX ) return 0; |
| 249 | for(iFullSz=POW2_MIN, iLogsize=0; iFullSz<nByte; iFullSz *= 2, iLogsize++){} |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 250 | |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 251 | /* Make sure mem5.aiFreelist[iLogsize] contains at least one free |
drh | eee4c8c | 2008-02-18 22:24:57 +0000 | [diff] [blame] | 252 | ** block. If not, then split a block of the next larger power of |
| 253 | ** two in order to create a new free block of size iLogsize. |
| 254 | */ |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 255 | for(iBin=iLogsize; mem5.aiFreelist[iBin]<0 && iBin<NSIZE; iBin++){} |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 256 | if( iBin>=NSIZE ) return 0; |
| 257 | i = memsys5UnlinkFirst(iBin); |
| 258 | while( iBin>iLogsize ){ |
| 259 | int newSize; |
| 260 | |
| 261 | iBin--; |
| 262 | newSize = 1 << iBin; |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 263 | mem5.aCtrl[i+newSize] = CTRL_FREE | iBin; |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 264 | memsys5Link(i+newSize, iBin); |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 265 | } |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 266 | mem5.aCtrl[i] = iLogsize; |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 267 | |
drh | eee4c8c | 2008-02-18 22:24:57 +0000 | [diff] [blame] | 268 | /* Update allocator performance statistics. */ |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 269 | mem5.nAlloc++; |
| 270 | mem5.totalAlloc += iFullSz; |
| 271 | mem5.totalExcess += iFullSz - nByte; |
| 272 | mem5.currentCount++; |
| 273 | mem5.currentOut += iFullSz; |
| 274 | if( mem5.maxCount<mem5.currentCount ) mem5.maxCount = mem5.currentCount; |
| 275 | if( mem5.maxOut<mem5.currentOut ) mem5.maxOut = mem5.currentOut; |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 276 | |
drh | eee4c8c | 2008-02-18 22:24:57 +0000 | [diff] [blame] | 277 | /* Return a pointer to the allocated memory. */ |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 278 | return (void*)&mem5.aPool[i]; |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | /* |
| 282 | ** Free an outstanding memory allocation. |
| 283 | */ |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 284 | static void memsys5FreeUnsafe(void *pOld){ |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 285 | u32 size, iLogsize; |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 286 | int i; |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 287 | |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 288 | i = ((Mem5Block*)pOld) - mem5.aPool; |
| 289 | assert( i>=0 && i<mem5.nBlock ); |
| 290 | assert( (mem5.aCtrl[i] & CTRL_FREE)==0 ); |
| 291 | iLogsize = mem5.aCtrl[i] & CTRL_LOGSIZE; |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 292 | size = 1<<iLogsize; |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 293 | assert( i+size-1<mem5.nBlock ); |
| 294 | mem5.aCtrl[i] |= CTRL_FREE; |
| 295 | mem5.aCtrl[i+size-1] |= CTRL_FREE; |
| 296 | assert( mem5.currentCount>0 ); |
| 297 | assert( mem5.currentOut>=0 ); |
| 298 | mem5.currentCount--; |
| 299 | mem5.currentOut -= size*POW2_MIN; |
| 300 | assert( mem5.currentOut>0 || mem5.currentCount==0 ); |
| 301 | assert( mem5.currentCount>0 || mem5.currentOut==0 ); |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 302 | |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 303 | mem5.aCtrl[i] = CTRL_FREE | iLogsize; |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 304 | while( iLogsize<NSIZE-1 ){ |
| 305 | int iBuddy; |
| 306 | |
| 307 | if( (i>>iLogsize) & 1 ){ |
| 308 | iBuddy = i - size; |
| 309 | }else{ |
| 310 | iBuddy = i + size; |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 311 | } |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 312 | assert( iBuddy>=0 && iBuddy<mem5.nBlock ); |
| 313 | if( mem5.aCtrl[iBuddy]!=(CTRL_FREE | iLogsize) ) break; |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 314 | memsys5Unlink(iBuddy, iLogsize); |
| 315 | iLogsize++; |
| 316 | if( iBuddy<i ){ |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 317 | mem5.aCtrl[iBuddy] = CTRL_FREE | iLogsize; |
| 318 | mem5.aCtrl[i] = 0; |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 319 | i = iBuddy; |
| 320 | }else{ |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 321 | mem5.aCtrl[i] = CTRL_FREE | iLogsize; |
| 322 | mem5.aCtrl[iBuddy] = 0; |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 323 | } |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 324 | size *= 2; |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 325 | } |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 326 | memsys5Link(i, iLogsize); |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | /* |
| 330 | ** Allocate nBytes of memory |
| 331 | */ |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 332 | static void *memsys5Malloc(int nBytes){ |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 333 | sqlite3_int64 *p = 0; |
| 334 | if( nBytes>0 ){ |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 335 | memsys5Enter(); |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 336 | p = memsys5MallocUnsafe(nBytes); |
| 337 | memsys5Leave(); |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 338 | } |
| 339 | return (void*)p; |
| 340 | } |
| 341 | |
| 342 | /* |
| 343 | ** Free memory. |
| 344 | */ |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 345 | static void memsys5Free(void *pPrior){ |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 346 | if( pPrior==0 ){ |
| 347 | return; |
| 348 | } |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 349 | memsys5Enter(); |
| 350 | memsys5FreeUnsafe(pPrior); |
| 351 | memsys5Leave(); |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /* |
| 355 | ** Change the size of an existing memory allocation |
| 356 | */ |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 357 | static void *memsys5Realloc(void *pPrior, int nBytes){ |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 358 | int nOld; |
| 359 | void *p; |
| 360 | if( pPrior==0 ){ |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 361 | return memsys5Malloc(nBytes); |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 362 | } |
| 363 | if( nBytes<=0 ){ |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 364 | memsys5Free(pPrior); |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 365 | return 0; |
| 366 | } |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 367 | nOld = memsys5Size(pPrior); |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 368 | if( nBytes<=nOld ){ |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 369 | return pPrior; |
| 370 | } |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 371 | memsys5Enter(); |
| 372 | p = memsys5MallocUnsafe(nBytes); |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 373 | if( p ){ |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 374 | memcpy(p, pPrior, nOld); |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 375 | memsys5FreeUnsafe(pPrior); |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 376 | } |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 377 | memsys5Leave(); |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 378 | return p; |
| 379 | } |
| 380 | |
| 381 | /* |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 382 | ** Round up a request size to the next valid allocation size. |
| 383 | */ |
| 384 | static int memsys5Roundup(int n){ |
| 385 | int iFullSz; |
| 386 | for(iFullSz=POW2_MIN; iFullSz<n; iFullSz *= 2); |
| 387 | return iFullSz; |
| 388 | } |
| 389 | |
| 390 | /* |
| 391 | ** Initialize this module. |
| 392 | */ |
| 393 | static int memsys5Init(void *NotUsed){ |
| 394 | return SQLITE_OK; |
| 395 | } |
| 396 | |
| 397 | /* |
| 398 | ** Deinitialize this module. |
| 399 | */ |
| 400 | static void memsys5Shutdown(void *NotUsed){ |
| 401 | return; |
| 402 | } |
| 403 | |
| 404 | /* |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 405 | ** Open the file indicated and write a log of all unfreed memory |
| 406 | ** allocations into that log. |
| 407 | */ |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 408 | void sqlite3Memsys5Dump(const char *zFilename){ |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 409 | #ifdef SQLITE_DEBUG |
| 410 | FILE *out; |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 411 | int i, j, n; |
| 412 | |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 413 | if( zFilename==0 || zFilename[0]==0 ){ |
| 414 | out = stdout; |
| 415 | }else{ |
| 416 | out = fopen(zFilename, "w"); |
| 417 | if( out==0 ){ |
| 418 | fprintf(stderr, "** Unable to output memory debug output log: %s **\n", |
| 419 | zFilename); |
| 420 | return; |
| 421 | } |
| 422 | } |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 423 | memsys5Enter(); |
| 424 | for(i=0; i<NSIZE; i++){ |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 425 | for(n=0, j=mem5.aiFreelist[i]; j>=0; j = mem5.aPool[j].u.list.next, n++){} |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 426 | fprintf(out, "freelist items of size %d: %d\n", POW2_MIN << i, n); |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 427 | } |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 428 | fprintf(out, "mem5.nAlloc = %llu\n", mem5.nAlloc); |
| 429 | fprintf(out, "mem5.totalAlloc = %llu\n", mem5.totalAlloc); |
| 430 | fprintf(out, "mem5.totalExcess = %llu\n", mem5.totalExcess); |
| 431 | fprintf(out, "mem5.currentOut = %u\n", mem5.currentOut); |
| 432 | fprintf(out, "mem5.currentCount = %u\n", mem5.currentCount); |
| 433 | fprintf(out, "mem5.maxOut = %u\n", mem5.maxOut); |
| 434 | fprintf(out, "mem5.maxCount = %u\n", mem5.maxCount); |
| 435 | fprintf(out, "mem5.maxRequest = %u\n", mem5.maxRequest); |
| 436 | memsys5Leave(); |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 437 | if( out==stdout ){ |
| 438 | fflush(stdout); |
| 439 | }else{ |
| 440 | fclose(out); |
| 441 | } |
| 442 | #endif |
| 443 | } |
| 444 | |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 445 | /* |
| 446 | ** This routine is the only routine in this file with external |
| 447 | ** linkage. |
| 448 | ** |
| 449 | ** Populate the low-level memory allocation function pointers in |
| 450 | ** sqlite3Config.m with pointers to the routines in this file. The |
| 451 | ** arguments specify the block of memory to manage. |
| 452 | ** |
| 453 | ** This routine is only called by sqlite3_config(), and therefore |
| 454 | ** is not required to be threadsafe (it is not). |
| 455 | */ |
| 456 | void sqlite3MemSetMemsys5(u8 *zByte, int nByte){ |
| 457 | static const sqlite3_mem_methods memsys5Methods = { |
| 458 | memsys5Malloc, |
| 459 | memsys5Free, |
| 460 | memsys5Realloc, |
| 461 | memsys5Size, |
| 462 | memsys5Roundup, |
| 463 | memsys5Init, |
| 464 | memsys5Shutdown, |
| 465 | 0 |
| 466 | }; |
| 467 | int i; |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 468 | |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 469 | mem5.nBlock = (nByte / (sizeof(Mem5Block)+sizeof(u8))); |
| 470 | mem5.nBlock -= (mem5.nBlock%SZ_MAX); |
| 471 | mem5.aPool = (Mem5Block *)zByte; |
| 472 | mem5.aCtrl = (u8 *)&mem5.aPool[mem5.nBlock]; |
| 473 | |
| 474 | assert( sizeof(Mem5Block)==POW2_MIN ); |
| 475 | assert( mem5.nBlock>=SZ_MAX ); |
| 476 | assert( (mem5.nBlock%SZ_MAX)==0 ); |
| 477 | |
| 478 | for(i=0; i<NSIZE; i++) mem5.aiFreelist[i] = -1; |
| 479 | for(i=0; i<=mem5.nBlock-SZ_MAX; i += SZ_MAX){ |
| 480 | mem5.aCtrl[i] = (NSIZE-1) | CTRL_FREE; |
| 481 | memsys5Link(i, NSIZE-1); |
| 482 | } |
| 483 | |
| 484 | /* Configure the functions to call to allocate memory. */ |
| 485 | sqlite3_config(SQLITE_CONFIG_MALLOC, &memsys5Methods); |
| 486 | } |
| 487 | |
| 488 | #endif /* SQLITE_ENABLE_MEMSYS5 */ |