Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Change mem6.c to use the malloc() and free() functions directly, instead of going via another sqlite3_mem_methods structure. (CVS 5474) |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
cfa65e23df8b6f33884f533492b84dd1 |
User & Date: | danielk1977 2008-07-25 09:24:13.000 |
Context
2008-07-25
| ||
10:40 | Speed up the xFree() method of the mem6.c allocator by storing the offset from the pointer to the start of its chunk in a header field. (CVS 5475) (check-in: 0de54891d4 user: danielk1977 tags: trunk) | |
09:24 | Change mem6.c to use the malloc() and free() functions directly, instead of going via another sqlite3_mem_methods structure. (CVS 5474) (check-in: cfa65e23df user: danielk1977 tags: trunk) | |
08:49 | Updates to mem6.c allocator. (CVS 5473) (check-in: 43a4cae2ac user: danielk1977 tags: trunk) | |
Changes
Changes to src/mem6.c.
1 2 3 4 5 6 7 8 9 10 11 12 13 | /* ** 2008 July 24 ** ** The author disclaims copyright to this source code. In place of ** a legal notice, here is a blessing: ** ** May you do good and not evil. ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* ** ** This file contains an alternative memory allocation system for SQLite. | | | | | | | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | /* ** 2008 July 24 ** ** The author disclaims copyright to this source code. In place of ** a legal notice, here is a blessing: ** ** May you do good and not evil. ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* ** ** This file contains an alternative memory allocation system for SQLite. ** This system is implemented as a wrapper around the system provided ** by the operating system - vanilla malloc(), realloc() and free(). ** ** This system differentiates between requests for "small" allocations ** (by default those of 128 bytes or less) and "large" allocations (all ** others). The 256 byte threshhold is configurable at runtime. ** ** All requests for large allocations are passed through to the ** default system. ** ** Requests for small allocations are met by allocating space within ** one or more larger "chunks" of memory obtained from the default ** memory allocation system. Chunks of memory are usually 64KB or ** larger. The algorithm used to manage space within each chunk is ** the same as that used by mem5.c. ** ** This strategy is designed to prevent the default memory allocation ** system (usually the system malloc) from suffering from heap ** fragmentation. On some systems, heap fragmentation can cause a ** significant real-time slowdown. ** ** $Id: mem6.c,v 1.4 2008/07/25 09:24:13 danielk1977 Exp $ */ #ifdef SQLITE_ENABLE_MEMSYS6 #include "sqliteInt.h" /* |
︙ | ︙ | |||
306 307 308 309 310 311 312 | assert((iOffset+nAlloc)>pChunk->nBlock); } return pChunk; } struct Mem6Global { | < | | < < < | | 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 | assert((iOffset+nAlloc)>pChunk->nBlock); } return pChunk; } struct Mem6Global { int nMinAlloc; /* Minimum allowed allocation size */ int nThreshold; /* Allocs larger than this go to malloc() */ sqlite3_mutex *mutex; Mem6Chunk *pChunk; /* Singly linked list of all memory chunks */ } mem6; static void mem6Enter(void){ sqlite3_mutex_enter(mem6.mutex); } static void mem6Leave(void){ sqlite3_mutex_leave(mem6.mutex); } /* ** Based on the number and size of the currently allocated chunks, return ** the size of the next chunk to allocate, in bytes. */ static int nextChunkSize(void){ int iTotal = MIN_CHUNKSIZE; Mem6Chunk *p; for(p=mem6.pChunk; p; p=p->pNext){ iTotal = iTotal*2; } return iTotal; } /* ** The argument is a pointer that may or may not have been allocated from ** one of the Mem6Chunk objects managed within mem6. If it is, return |
︙ | ︙ | |||
357 358 359 360 361 362 363 | return 0; } static void freeChunk(Mem6Chunk *pChunk){ Mem6Chunk **pp = &mem6.pChunk; for( pp=&mem6.pChunk; *pp!=pChunk; pp = &(*pp)->pNext ); *pp = (*pp)->pNext; | | > | | | | | > | | < | | | | < | | > | | 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 | return 0; } static void freeChunk(Mem6Chunk *pChunk){ Mem6Chunk **pp = &mem6.pChunk; for( pp=&mem6.pChunk; *pp!=pChunk; pp = &(*pp)->pNext ); *pp = (*pp)->pNext; free(pChunk); } static void *memsys6Malloc(int nByte){ Mem6Chunk *pChunk; void *p = 0; int nTotal = nByte+8; mem6Enter(); if( nTotal>mem6.nThreshold ){ p = malloc(nTotal); }else{ for(pChunk=mem6.pChunk; !p && pChunk; pChunk=pChunk->pNext){ p = chunkMalloc(pChunk, nTotal); } if( !p ){ int iSize = nextChunkSize(); p = malloc(iSize); if( p ){ pChunk = chunkInit((u8 *)p, iSize, mem6.nMinAlloc); pChunk->pNext = mem6.pChunk; mem6.pChunk = pChunk; p = chunkMalloc(pChunk, nTotal); assert(p); } } } mem6Leave(); ((sqlite3_int64 *)p)[0] = nByte; return &((sqlite3_int64 *)p)[1]; } static int memsys6Size(void *pPrior){ sqlite3_int64 *p; if( pPrior==0 ) return 0; p = (sqlite3_int64*)pPrior; p--; return p[0]; } static void memsys6Free(void *pPrior){ Mem6Chunk *pChunk; void *p = &((sqlite3_int64 *)pPrior)[-1]; mem6Enter(); pChunk = findChunk(p); if( pChunk ){ chunkFree(pChunk, p); if( chunkIsEmpty(pChunk) ){ freeChunk(pChunk); } }else{ free(p); } mem6Leave(); } static void *memsys6Realloc(void *p, int nByte){ void *p2; |
︙ | ︙ | |||
440 441 442 443 444 445 446 | int iFullSz; for(iFullSz=mem6.nMinAlloc; iFullSz<n; iFullSz *= 2); return iFullSz; } static int memsys6Init(void *pCtx){ u8 bMemstat = sqlite3Config.bMemstat; | < < < < < < < < < < < < < | 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 | int iFullSz; for(iFullSz=mem6.nMinAlloc; iFullSz<n; iFullSz *= 2); return iFullSz; } static int memsys6Init(void *pCtx){ u8 bMemstat = sqlite3Config.bMemstat; mem6.nMinAlloc = 16; mem6.pChunk = 0; mem6.nThreshold = sqlite3Config.nSmall; if( mem6.nThreshold<=0 ){ mem6.nThreshold = SMALL_MALLOC_DEFAULT_THRESHOLD; } if( !bMemstat ){ mem6.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); } return SQLITE_OK; } static void memsys6Shutdown(void *pCtx){ memset(&mem6, 0, sizeof(mem6)); } /* ** This routine is the only routine in this file with external ** linkage. It returns a pointer to a static sqlite3_mem_methods ** struct populated with the memsys6 methods. |
︙ | ︙ |