| OLD | NEW |
| 1 /* | 1 /* |
| 2 ** 2007 October 14 | 2 ** 2007 October 14 |
| 3 ** | 3 ** |
| 4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
| 5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
| 6 ** | 6 ** |
| 7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
| 8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
| 9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
| 10 ** | 10 ** |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 ** be changed. | 21 ** be changed. |
| 22 ** | 22 ** |
| 23 ** This version of the memory allocation subsystem is included | 23 ** This version of the memory allocation subsystem is included |
| 24 ** in the build only if SQLITE_ENABLE_MEMSYS5 is defined. | 24 ** in the build only if SQLITE_ENABLE_MEMSYS5 is defined. |
| 25 ** | 25 ** |
| 26 ** This memory allocator uses the following algorithm: | 26 ** This memory allocator uses the following algorithm: |
| 27 ** | 27 ** |
| 28 ** 1. All memory allocations sizes are rounded up to a power of 2. | 28 ** 1. All memory allocations sizes are rounded up to a power of 2. |
| 29 ** | 29 ** |
| 30 ** 2. If two adjacent free blocks are the halves of a larger block, | 30 ** 2. If two adjacent free blocks are the halves of a larger block, |
| 31 ** then the two blocks are coalesed into the single larger block. | 31 ** then the two blocks are coalesced into the single larger block. |
| 32 ** | 32 ** |
| 33 ** 3. New memory is allocated from the first available free block. | 33 ** 3. New memory is allocated from the first available free block. |
| 34 ** | 34 ** |
| 35 ** This algorithm is described in: J. M. Robson. "Bounds for Some Functions | 35 ** This algorithm is described in: J. M. Robson. "Bounds for Some Functions |
| 36 ** Concerning Dynamic Storage Allocation". Journal of the Association for | 36 ** Concerning Dynamic Storage Allocation". Journal of the Association for |
| 37 ** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499. | 37 ** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499. |
| 38 ** | 38 ** |
| 39 ** Let n be the size of the largest allocation divided by the minimum | 39 ** Let n be the size of the largest allocation divided by the minimum |
| 40 ** allocation size (after rounding all sizes up to a power of 2.) Let M | 40 ** allocation size (after rounding all sizes up to a power of 2.) Let M |
| 41 ** be the maximum amount of memory ever outstanding at one time. Let | 41 ** be the maximum amount of memory ever outstanding at one time. Let |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 | 123 |
| 124 /* | 124 /* |
| 125 ** Space for tracking which blocks are checked out and the size | 125 ** Space for tracking which blocks are checked out and the size |
| 126 ** of each block. One byte per block. | 126 ** of each block. One byte per block. |
| 127 */ | 127 */ |
| 128 u8 *aCtrl; | 128 u8 *aCtrl; |
| 129 | 129 |
| 130 } mem5; | 130 } mem5; |
| 131 | 131 |
| 132 /* | 132 /* |
| 133 ** Access the static variable through a macro for SQLITE_OMIT_WSD | 133 ** Access the static variable through a macro for SQLITE_OMIT_WSD. |
| 134 */ | 134 */ |
| 135 #define mem5 GLOBAL(struct Mem5Global, mem5) | 135 #define mem5 GLOBAL(struct Mem5Global, mem5) |
| 136 | 136 |
| 137 /* | 137 /* |
| 138 ** Assuming mem5.zPool is divided up into an array of Mem5Link | 138 ** Assuming mem5.zPool is divided up into an array of Mem5Link |
| 139 ** structures, return a pointer to the idx-th such lik. | 139 ** structures, return a pointer to the idx-th such link. |
| 140 */ | 140 */ |
| 141 #define MEM5LINK(idx) ((Mem5Link *)(&mem5.zPool[(idx)*mem5.szAtom])) | 141 #define MEM5LINK(idx) ((Mem5Link *)(&mem5.zPool[(idx)*mem5.szAtom])) |
| 142 | 142 |
| 143 /* | 143 /* |
| 144 ** Unlink the chunk at mem5.aPool[i] from list it is currently | 144 ** Unlink the chunk at mem5.aPool[i] from list it is currently |
| 145 ** on. It should be found on mem5.aiFreelist[iLogsize]. | 145 ** on. It should be found on mem5.aiFreelist[iLogsize]. |
| 146 */ | 146 */ |
| 147 static void memsys5Unlink(int i, int iLogsize){ | 147 static void memsys5Unlink(int i, int iLogsize){ |
| 148 int next, prev; | 148 int next, prev; |
| 149 assert( i>=0 && i<mem5.nBlock ); | 149 assert( i>=0 && i<mem5.nBlock ); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 } | 195 } |
| 196 | 196 |
| 197 /* | 197 /* |
| 198 ** Return the size of an outstanding allocation, in bytes. The | 198 ** Return the size of an outstanding allocation, in bytes. The |
| 199 ** size returned omits the 8-byte header overhead. This only | 199 ** size returned omits the 8-byte header overhead. This only |
| 200 ** works for chunks that are currently checked out. | 200 ** works for chunks that are currently checked out. |
| 201 */ | 201 */ |
| 202 static int memsys5Size(void *p){ | 202 static int memsys5Size(void *p){ |
| 203 int iSize = 0; | 203 int iSize = 0; |
| 204 if( p ){ | 204 if( p ){ |
| 205 int i = ((u8 *)p-mem5.zPool)/mem5.szAtom; | 205 int i = (int)(((u8 *)p-mem5.zPool)/mem5.szAtom); |
| 206 assert( i>=0 && i<mem5.nBlock ); | 206 assert( i>=0 && i<mem5.nBlock ); |
| 207 iSize = mem5.szAtom * (1 << (mem5.aCtrl[i]&CTRL_LOGSIZE)); | 207 iSize = mem5.szAtom * (1 << (mem5.aCtrl[i]&CTRL_LOGSIZE)); |
| 208 } | 208 } |
| 209 return iSize; | 209 return iSize; |
| 210 } | 210 } |
| 211 | 211 |
| 212 /* | 212 /* |
| 213 ** Find the first entry on the freelist iLogsize. Unlink that | |
| 214 ** entry and return its index. | |
| 215 */ | |
| 216 static int memsys5UnlinkFirst(int iLogsize){ | |
| 217 int i; | |
| 218 int iFirst; | |
| 219 | |
| 220 assert( iLogsize>=0 && iLogsize<=LOGMAX ); | |
| 221 i = iFirst = mem5.aiFreelist[iLogsize]; | |
| 222 assert( iFirst>=0 ); | |
| 223 while( i>0 ){ | |
| 224 if( i<iFirst ) iFirst = i; | |
| 225 i = MEM5LINK(i)->next; | |
| 226 } | |
| 227 memsys5Unlink(iFirst, iLogsize); | |
| 228 return iFirst; | |
| 229 } | |
| 230 | |
| 231 /* | |
| 232 ** Return a block of memory of at least nBytes in size. | 213 ** Return a block of memory of at least nBytes in size. |
| 233 ** Return NULL if unable. Return NULL if nBytes==0. | 214 ** Return NULL if unable. Return NULL if nBytes==0. |
| 234 ** | 215 ** |
| 235 ** The caller guarantees that nByte positive. | 216 ** The caller guarantees that nByte is positive. |
| 236 ** | 217 ** |
| 237 ** The caller has obtained a mutex prior to invoking this | 218 ** The caller has obtained a mutex prior to invoking this |
| 238 ** routine so there is never any chance that two or more | 219 ** routine so there is never any chance that two or more |
| 239 ** threads can be in this routine at the same time. | 220 ** threads can be in this routine at the same time. |
| 240 */ | 221 */ |
| 241 static void *memsys5MallocUnsafe(int nByte){ | 222 static void *memsys5MallocUnsafe(int nByte){ |
| 242 int i; /* Index of a mem5.aPool[] slot */ | 223 int i; /* Index of a mem5.aPool[] slot */ |
| 243 int iBin; /* Index into mem5.aiFreelist[] */ | 224 int iBin; /* Index into mem5.aiFreelist[] */ |
| 244 int iFullSz; /* Size of allocation rounded up to power of 2 */ | 225 int iFullSz; /* Size of allocation rounded up to power of 2 */ |
| 245 int iLogsize; /* Log2 of iFullSz/POW2_MIN */ | 226 int iLogsize; /* Log2 of iFullSz/POW2_MIN */ |
| (...skipping 14 matching lines...) Expand all Loading... |
| 260 return 0; | 241 return 0; |
| 261 } | 242 } |
| 262 | 243 |
| 263 /* Round nByte up to the next valid power of two */ | 244 /* Round nByte up to the next valid power of two */ |
| 264 for(iFullSz=mem5.szAtom, iLogsize=0; iFullSz<nByte; iFullSz *= 2, iLogsize++){
} | 245 for(iFullSz=mem5.szAtom, iLogsize=0; iFullSz<nByte; iFullSz *= 2, iLogsize++){
} |
| 265 | 246 |
| 266 /* Make sure mem5.aiFreelist[iLogsize] contains at least one free | 247 /* Make sure mem5.aiFreelist[iLogsize] contains at least one free |
| 267 ** block. If not, then split a block of the next larger power of | 248 ** block. If not, then split a block of the next larger power of |
| 268 ** two in order to create a new free block of size iLogsize. | 249 ** two in order to create a new free block of size iLogsize. |
| 269 */ | 250 */ |
| 270 for(iBin=iLogsize; mem5.aiFreelist[iBin]<0 && iBin<=LOGMAX; iBin++){} | 251 for(iBin=iLogsize; iBin<=LOGMAX && mem5.aiFreelist[iBin]<0; iBin++){} |
| 271 if( iBin>LOGMAX ){ | 252 if( iBin>LOGMAX ){ |
| 272 testcase( sqlite3GlobalConfig.xLog!=0 ); | 253 testcase( sqlite3GlobalConfig.xLog!=0 ); |
| 273 sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes", nByte); | 254 sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes", nByte); |
| 274 return 0; | 255 return 0; |
| 275 } | 256 } |
| 276 i = memsys5UnlinkFirst(iBin); | 257 i = mem5.aiFreelist[iBin]; |
| 258 memsys5Unlink(i, iBin); |
| 277 while( iBin>iLogsize ){ | 259 while( iBin>iLogsize ){ |
| 278 int newSize; | 260 int newSize; |
| 279 | 261 |
| 280 iBin--; | 262 iBin--; |
| 281 newSize = 1 << iBin; | 263 newSize = 1 << iBin; |
| 282 mem5.aCtrl[i+newSize] = CTRL_FREE | iBin; | 264 mem5.aCtrl[i+newSize] = CTRL_FREE | iBin; |
| 283 memsys5Link(i+newSize, iBin); | 265 memsys5Link(i+newSize, iBin); |
| 284 } | 266 } |
| 285 mem5.aCtrl[i] = iLogsize; | 267 mem5.aCtrl[i] = iLogsize; |
| 286 | 268 |
| 287 /* Update allocator performance statistics. */ | 269 /* Update allocator performance statistics. */ |
| 288 mem5.nAlloc++; | 270 mem5.nAlloc++; |
| 289 mem5.totalAlloc += iFullSz; | 271 mem5.totalAlloc += iFullSz; |
| 290 mem5.totalExcess += iFullSz - nByte; | 272 mem5.totalExcess += iFullSz - nByte; |
| 291 mem5.currentCount++; | 273 mem5.currentCount++; |
| 292 mem5.currentOut += iFullSz; | 274 mem5.currentOut += iFullSz; |
| 293 if( mem5.maxCount<mem5.currentCount ) mem5.maxCount = mem5.currentCount; | 275 if( mem5.maxCount<mem5.currentCount ) mem5.maxCount = mem5.currentCount; |
| 294 if( mem5.maxOut<mem5.currentOut ) mem5.maxOut = mem5.currentOut; | 276 if( mem5.maxOut<mem5.currentOut ) mem5.maxOut = mem5.currentOut; |
| 295 | 277 |
| 278 #ifdef SQLITE_DEBUG |
| 279 /* Make sure the allocated memory does not assume that it is set to zero |
| 280 ** or retains a value from a previous allocation */ |
| 281 memset(&mem5.zPool[i*mem5.szAtom], 0xAA, iFullSz); |
| 282 #endif |
| 283 |
| 296 /* Return a pointer to the allocated memory. */ | 284 /* Return a pointer to the allocated memory. */ |
| 297 return (void*)&mem5.zPool[i*mem5.szAtom]; | 285 return (void*)&mem5.zPool[i*mem5.szAtom]; |
| 298 } | 286 } |
| 299 | 287 |
| 300 /* | 288 /* |
| 301 ** Free an outstanding memory allocation. | 289 ** Free an outstanding memory allocation. |
| 302 */ | 290 */ |
| 303 static void memsys5FreeUnsafe(void *pOld){ | 291 static void memsys5FreeUnsafe(void *pOld){ |
| 304 u32 size, iLogsize; | 292 u32 size, iLogsize; |
| 305 int iBlock; | 293 int iBlock; |
| 306 | 294 |
| 307 /* Set iBlock to the index of the block pointed to by pOld in | 295 /* Set iBlock to the index of the block pointed to by pOld in |
| 308 ** the array of mem5.szAtom byte blocks pointed to by mem5.zPool. | 296 ** the array of mem5.szAtom byte blocks pointed to by mem5.zPool. |
| 309 */ | 297 */ |
| 310 iBlock = ((u8 *)pOld-mem5.zPool)/mem5.szAtom; | 298 iBlock = (int)(((u8 *)pOld-mem5.zPool)/mem5.szAtom); |
| 311 | 299 |
| 312 /* Check that the pointer pOld points to a valid, non-free block. */ | 300 /* Check that the pointer pOld points to a valid, non-free block. */ |
| 313 assert( iBlock>=0 && iBlock<mem5.nBlock ); | 301 assert( iBlock>=0 && iBlock<mem5.nBlock ); |
| 314 assert( ((u8 *)pOld-mem5.zPool)%mem5.szAtom==0 ); | 302 assert( ((u8 *)pOld-mem5.zPool)%mem5.szAtom==0 ); |
| 315 assert( (mem5.aCtrl[iBlock] & CTRL_FREE)==0 ); | 303 assert( (mem5.aCtrl[iBlock] & CTRL_FREE)==0 ); |
| 316 | 304 |
| 317 iLogsize = mem5.aCtrl[iBlock] & CTRL_LOGSIZE; | 305 iLogsize = mem5.aCtrl[iBlock] & CTRL_LOGSIZE; |
| 318 size = 1<<iLogsize; | 306 size = 1<<iLogsize; |
| 319 assert( iBlock+size-1<(u32)mem5.nBlock ); | 307 assert( iBlock+size-1<(u32)mem5.nBlock ); |
| 320 | 308 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 343 if( iBuddy<iBlock ){ | 331 if( iBuddy<iBlock ){ |
| 344 mem5.aCtrl[iBuddy] = CTRL_FREE | iLogsize; | 332 mem5.aCtrl[iBuddy] = CTRL_FREE | iLogsize; |
| 345 mem5.aCtrl[iBlock] = 0; | 333 mem5.aCtrl[iBlock] = 0; |
| 346 iBlock = iBuddy; | 334 iBlock = iBuddy; |
| 347 }else{ | 335 }else{ |
| 348 mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize; | 336 mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize; |
| 349 mem5.aCtrl[iBuddy] = 0; | 337 mem5.aCtrl[iBuddy] = 0; |
| 350 } | 338 } |
| 351 size *= 2; | 339 size *= 2; |
| 352 } | 340 } |
| 341 |
| 342 #ifdef SQLITE_DEBUG |
| 343 /* Overwrite freed memory with the 0x55 bit pattern to verify that it is |
| 344 ** not used after being freed */ |
| 345 memset(&mem5.zPool[iBlock*mem5.szAtom], 0x55, size); |
| 346 #endif |
| 347 |
| 353 memsys5Link(iBlock, iLogsize); | 348 memsys5Link(iBlock, iLogsize); |
| 354 } | 349 } |
| 355 | 350 |
| 356 /* | 351 /* |
| 357 ** Allocate nBytes of memory | 352 ** Allocate nBytes of memory. |
| 358 */ | 353 */ |
| 359 static void *memsys5Malloc(int nBytes){ | 354 static void *memsys5Malloc(int nBytes){ |
| 360 sqlite3_int64 *p = 0; | 355 sqlite3_int64 *p = 0; |
| 361 if( nBytes>0 ){ | 356 if( nBytes>0 ){ |
| 362 memsys5Enter(); | 357 memsys5Enter(); |
| 363 p = memsys5MallocUnsafe(nBytes); | 358 p = memsys5MallocUnsafe(nBytes); |
| 364 memsys5Leave(); | 359 memsys5Leave(); |
| 365 } | 360 } |
| 366 return (void*)p; | 361 return (void*)p; |
| 367 } | 362 } |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 572 memsys5Size, | 567 memsys5Size, |
| 573 memsys5Roundup, | 568 memsys5Roundup, |
| 574 memsys5Init, | 569 memsys5Init, |
| 575 memsys5Shutdown, | 570 memsys5Shutdown, |
| 576 0 | 571 0 |
| 577 }; | 572 }; |
| 578 return &memsys5Methods; | 573 return &memsys5Methods; |
| 579 } | 574 } |
| 580 | 575 |
| 581 #endif /* SQLITE_ENABLE_MEMSYS5 */ | 576 #endif /* SQLITE_ENABLE_MEMSYS5 */ |
| OLD | NEW |