Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Updates to mem6.c allocator. (CVS 5473) |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
43a4cae2acea33d1a17c0037516e9c27 |
User & Date: | danielk1977 2008-07-25 08:49:00.000 |
Context
2008-07-25
| ||
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) | |
2008-07-24
| ||
23:34 | Reduce the size of the parser allocation. Add additional instrumentation to mem2. speed1*.test uses scratch malloc. (CVS 5472) (check-in: 599a9dea8f user: drh tags: trunk) | |
Changes
Changes to src/main.c.
︙ | ︙ | |||
10 11 12 13 14 15 16 | ** ************************************************************************* ** Main file for the SQLite library. The routines in this file ** implement the programmer interface to the library. Routines in ** other files are for internal use by SQLite and should not be ** accessed by users of the library. ** | | | 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | ** ************************************************************************* ** Main file for the SQLite library. The routines in this file ** implement the programmer interface to the library. Routines in ** other files are for internal use by SQLite and should not be ** accessed by users of the library. ** ** $Id: main.c,v 1.482 2008/07/25 08:49:00 danielk1977 Exp $ */ #include "sqliteInt.h" #include <ctype.h> #ifdef SQLITE_ENABLE_FTS3 # include "fts3.h" #endif |
︙ | ︙ | |||
265 266 267 268 269 270 271 272 273 274 275 276 277 278 | } break; } #endif #if defined(SQLITE_ENABLE_MEMSYS6) case SQLITE_CONFIG_CHUNKALLOC: { sqlite3Config.m = *sqlite3MemGetMemsys6(); break; } #endif default: { rc = SQLITE_ERROR; | > | 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | } break; } #endif #if defined(SQLITE_ENABLE_MEMSYS6) case SQLITE_CONFIG_CHUNKALLOC: { sqlite3Config.nSmall = va_arg(ap, int); sqlite3Config.m = *sqlite3MemGetMemsys6(); break; } #endif default: { rc = SQLITE_ERROR; |
︙ | ︙ |
Changes to src/mem1.c.
︙ | ︙ | |||
13 14 15 16 17 18 19 | ** This file contains low-level memory allocation drivers for when ** SQLite will use the standard C-library malloc/realloc/free interface ** to obtain the memory it needs. ** ** This file contains implementations of the low-level memory allocation ** routines specified in the sqlite3_mem_methods object. ** | | | 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | ** This file contains low-level memory allocation drivers for when ** SQLite will use the standard C-library malloc/realloc/free interface ** to obtain the memory it needs. ** ** This file contains implementations of the low-level memory allocation ** routines specified in the sqlite3_mem_methods object. ** ** $Id: mem1.c,v 1.25 2008/07/25 08:49:00 danielk1977 Exp $ */ #include "sqliteInt.h" /* ** This version of the memory allocator is the default. It is ** used when no other memory allocator is specified using compile-time ** macros. |
︙ | ︙ | |||
116 117 118 119 120 121 122 | /* ** Deinitialize this module. */ static void sqlite3MemShutdown(void *NotUsed){ return; } | | | 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | /* ** Deinitialize this module. */ static void sqlite3MemShutdown(void *NotUsed){ return; } const sqlite3_mem_methods *sqlite3MemGetDefault(void){ static const sqlite3_mem_methods defaultMethods = { sqlite3MemMalloc, sqlite3MemFree, sqlite3MemRealloc, sqlite3MemSize, sqlite3MemRoundup, sqlite3MemInit, |
︙ | ︙ |
Changes to src/mem2.c.
︙ | ︙ | |||
15 16 17 18 19 20 21 | ** to obtain the memory it needs while adding lots of additional debugging ** information to each allocation in order to help detect and fix memory ** leaks and memory usage errors. ** ** This file contains implementations of the low-level memory allocation ** routines specified in the sqlite3_mem_methods object. ** | | | 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | ** to obtain the memory it needs while adding lots of additional debugging ** information to each allocation in order to help detect and fix memory ** leaks and memory usage errors. ** ** This file contains implementations of the low-level memory allocation ** routines specified in the sqlite3_mem_methods object. ** ** $Id: mem2.c,v 1.37 2008/07/25 08:49:00 danielk1977 Exp $ */ #include "sqliteInt.h" /* ** This version of the memory allocator is used only if the ** SQLITE_MEMDEBUG macro is defined */ |
︙ | ︙ | |||
320 321 322 323 324 325 326 | } sqlite3MemFree(pPrior); } return pNew; } | | | 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | } sqlite3MemFree(pPrior); } return pNew; } const sqlite3_mem_methods *sqlite3MemGetDefault(void){ static const sqlite3_mem_methods defaultMethods = { sqlite3MemMalloc, sqlite3MemFree, sqlite3MemRealloc, sqlite3MemSize, sqlite3MemRoundup, sqlite3MemInit, |
︙ | ︙ |
Changes to src/mem6.c.
1 2 3 4 5 6 7 8 9 10 11 12 | /* ** 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. ** ************************************************************************* ** | > > > > > > > > > > > > > > > > > > > > > > | > > | | | > > > > | > > > > > | 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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | /* ** 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 default memory ** allocation system (usually the one found in mem1.c - system malloc). ** ** 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 memory allocation 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.3 2008/07/25 08:49:00 danielk1977 Exp $ */ #ifdef SQLITE_ENABLE_MEMSYS6 #include "sqliteInt.h" /* ** Maximum size of any "small" allocation is ((1<<LOGMAX)*Mem6Chunk.nAtom). ** Mem6Chunk.nAtom is always at least 8, so this is not a practical ** limitation */ #define LOGMAX 30 /* ** Default value for the "small" allocation size threshold. */ #define SMALL_MALLOC_DEFAULT_THRESHOLD 256 /* ** Minimum size for a memory chunk. */ #define MIN_CHUNKSIZE (1<<16) typedef struct Mem6Chunk Mem6Chunk; typedef struct Mem6Link Mem6Link; /* ** A minimum allocation is an instance of the following structure. ** Larger allocations are an array of these structures where the |
︙ | ︙ | |||
274 275 276 277 278 279 280 | } return pChunk; } struct Mem6Global { sqlite3_mem_methods parent; /* Used to allocate chunks */ | < | < < > > > > > > > > > > > > > > > > | 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 344 345 346 347 348 349 350 | } return pChunk; } struct Mem6Global { sqlite3_mem_methods parent; /* Used to allocate chunks */ int nMinAlloc; /* Minimum allowed allocation size */ int nThreshold; /* Allocs larger than this go to parent */ 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 = 0; Mem6Chunk *p; for(p=mem6.pChunk; p; p=p->pNext){ iTotal += mem6.parent.xSize((void *)p); } if( iTotal==0 ){ iTotal = MIN_CHUNKSIZE; } 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 ** a pointer to the owner chunk. If not, return 0. */ static Mem6Chunk *findChunk(u8 *p){ |
︙ | ︙ | |||
319 320 321 322 323 324 325 | } static void *memsys6Malloc(int nByte){ Mem6Chunk *pChunk; void *p = 0; mem6Enter(); | | > | | | 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 | } static void *memsys6Malloc(int nByte){ Mem6Chunk *pChunk; void *p = 0; mem6Enter(); if( nByte>mem6.nThreshold ){ p = mem6.parent.xMalloc(nByte); }else{ for(pChunk=mem6.pChunk; !p && pChunk; pChunk=pChunk->pNext){ p = chunkMalloc(pChunk, nByte); } if( !p ){ int iSize = nextChunkSize(); p = mem6.parent.xMalloc(iSize); if( p ){ pChunk = chunkInit((u8 *)p, iSize, mem6.nMinAlloc); pChunk->pNext = mem6.pChunk; mem6.pChunk = pChunk; p = chunkMalloc(pChunk, nByte); assert(p); } } } |
︙ | ︙ | |||
394 395 396 397 398 399 400 | for(iFullSz=mem6.nMinAlloc; iFullSz<n; iFullSz *= 2); return iFullSz; } static int memsys6Init(void *pCtx){ u8 bMemstat = sqlite3Config.bMemstat; mem6.parent = *sqlite3MemGetDefault(); | < > > > | > > > > | 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 | for(iFullSz=mem6.nMinAlloc; iFullSz<n; iFullSz *= 2); return iFullSz; } static int memsys6Init(void *pCtx){ u8 bMemstat = sqlite3Config.bMemstat; mem6.parent = *sqlite3MemGetDefault(); 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); } /* Initialize the parent allocator. */ #ifdef SQLITE_MEMDEBUG sqlite3Config.bMemstat = 1; #endif mem6.parent.xInit(mem6.parent.pAppData); #ifdef SQLITE_MEMDEBUG sqlite3Config.bMemstat = bMemstat; #endif return SQLITE_OK; } static void memsys6Shutdown(void *pCtx){ if( mem6.parent.xShutdown ){ mem6.parent.xShutdown(mem6.parent.pAppData); } 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. */ |
︙ | ︙ |
Changes to src/sqlite.h.in.
︙ | ︙ | |||
26 27 28 29 30 31 32 | ** on how SQLite interfaces are suppose to operate. ** ** The name of this file under configuration management is "sqlite.h.in". ** The makefile makes some minor changes to this file (such as inserting ** the version number) and changes its name to "sqlite3.h" as ** part of the build process. ** | | | 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | ** on how SQLite interfaces are suppose to operate. ** ** The name of this file under configuration management is "sqlite.h.in". ** The makefile makes some minor changes to this file (such as inserting ** the version number) and changes its name to "sqlite3.h" as ** part of the build process. ** ** @(#) $Id: sqlite.h.in,v 1.379 2008/07/25 08:49:00 danielk1977 Exp $ */ #ifndef _SQLITE3_H_ #define _SQLITE3_H_ #include <stdarg.h> /* Needed for the definition of va_list */ /* ** Make sure we can call this stuff from C++. |
︙ | ︙ | |||
1153 1154 1155 1156 1157 1158 1159 | #define SQLITE_CONFIG_GETMALLOC 5 /* sqlite3_mem_methods* */ #define SQLITE_CONFIG_SCRATCH 6 /* void*, int sz, int N */ #define SQLITE_CONFIG_PAGECACHE 7 /* void*, int sz, int N */ #define SQLITE_CONFIG_HEAP 8 /* void*, int nByte, int min */ #define SQLITE_CONFIG_MEMSTATUS 9 /* boolean */ #define SQLITE_CONFIG_MUTEX 10 /* sqlite3_mutex_methods* */ #define SQLITE_CONFIG_GETMUTEX 11 /* sqlite3_mutex_methods* */ | < | | 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 | #define SQLITE_CONFIG_GETMALLOC 5 /* sqlite3_mem_methods* */ #define SQLITE_CONFIG_SCRATCH 6 /* void*, int sz, int N */ #define SQLITE_CONFIG_PAGECACHE 7 /* void*, int sz, int N */ #define SQLITE_CONFIG_HEAP 8 /* void*, int nByte, int min */ #define SQLITE_CONFIG_MEMSTATUS 9 /* boolean */ #define SQLITE_CONFIG_MUTEX 10 /* sqlite3_mutex_methods* */ #define SQLITE_CONFIG_GETMUTEX 11 /* sqlite3_mutex_methods* */ #define SQLITE_CONFIG_CHUNKALLOC 12 /* int threshold */ /* ** CAPI3REF: Enable Or Disable Extended Result Codes {H12200} <S10700> ** ** The sqlite3_extended_result_codes() routine enables or disables the ** [extended result codes] feature of SQLite. The extended result |
︙ | ︙ |
Changes to src/sqliteInt.h.
1 2 3 4 5 6 7 8 9 10 11 12 13 | /* ** 2001 September 15 ** ** 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. ** ************************************************************************* ** Internal interface definitions for SQLite. ** | | | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | /* ** 2001 September 15 ** ** 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. ** ************************************************************************* ** Internal interface definitions for SQLite. ** ** @(#) $Id: sqliteInt.h,v 1.745 2008/07/25 08:49:00 danielk1977 Exp $ */ #ifndef _SQLITEINT_H_ #define _SQLITEINT_H_ /* ** Include the configuration header output by 'configure' if we're using the ** autoconf-based build |
︙ | ︙ | |||
1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 | int nScratch; /* Number of scratch buffers */ void *pPage; /* Page cache memory */ int szPage; /* Size of each page in pPage[] */ int nPage; /* Number of pages in pPage[] */ int isInit; /* True after initialization has finished */ int isMallocInit; /* True after malloc is initialized */ sqlite3_mutex *pInitMutex; /* Mutex used by sqlite3_initialize() */ }; /* ** Assuming zIn points to the first byte of a UTF-8 character, ** advance zIn to point to the first byte of the next UTF-8 character. */ #define SQLITE_SKIP_UTF8(zIn) { \ | > | 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 | int nScratch; /* Number of scratch buffers */ void *pPage; /* Page cache memory */ int szPage; /* Size of each page in pPage[] */ int nPage; /* Number of pages in pPage[] */ int isInit; /* True after initialization has finished */ int isMallocInit; /* True after malloc is initialized */ sqlite3_mutex *pInitMutex; /* Mutex used by sqlite3_initialize() */ int nSmall; /* alloc size threshold used by mem6.c */ }; /* ** Assuming zIn points to the first byte of a UTF-8 character, ** advance zIn to point to the first byte of the next UTF-8 character. */ #define SQLITE_SKIP_UTF8(zIn) { \ |
︙ | ︙ | |||
1837 1838 1839 1840 1841 1842 1843 | void *sqlite3DbRealloc(sqlite3 *, void *, int); int sqlite3MallocSize(void *); void *sqlite3ScratchMalloc(int); void sqlite3ScratchFree(void*); void *sqlite3PageMalloc(int); void sqlite3PageFree(void*); void sqlite3MemSetDefault(void); | | | 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 | void *sqlite3DbRealloc(sqlite3 *, void *, int); int sqlite3MallocSize(void *); void *sqlite3ScratchMalloc(int); void sqlite3ScratchFree(void*); void *sqlite3PageMalloc(int); void sqlite3PageFree(void*); void sqlite3MemSetDefault(void); const sqlite3_mem_methods *sqlite3MemGetDefault(void); const sqlite3_mem_methods *sqlite3MemGetMemsys5(void); const sqlite3_mem_methods *sqlite3MemGetMemsys3(void); const sqlite3_mem_methods *sqlite3MemGetMemsys6(void); void sqlite3BenignMallocHooks(void (*)(void), void (*)(void)); #ifndef SQLITE_MUTEX_NOOP sqlite3_mutex_methods *sqlite3DefaultMutex(void); |
︙ | ︙ |
Changes to src/test_malloc.c.
︙ | ︙ | |||
9 10 11 12 13 14 15 | ** May you share freely, never taking more than you give. ** ************************************************************************* ** ** This file contains code used to implement test interfaces to the ** memory allocation subsystem. ** | | | 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | ** May you share freely, never taking more than you give. ** ************************************************************************* ** ** This file contains code used to implement test interfaces to the ** memory allocation subsystem. ** ** $Id: test_malloc.c,v 1.40 2008/07/25 08:49:00 danielk1977 Exp $ */ #include "sqliteInt.h" #include "tcl.h" #include <stdlib.h> #include <string.h> #include <assert.h> |
︙ | ︙ | |||
963 964 965 966 967 968 969 | static int test_config_chunkalloc( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] ){ int rc; | > | | > | | 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 | static int test_config_chunkalloc( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] ){ int rc; int nThreshold; if( objc!=2 ){ Tcl_WrongNumArgs(interp, 1, objv, "THRESHOLD"); return TCL_ERROR; } if( Tcl_GetIntFromObj(interp, objv[1], &nThreshold) ) return TCL_ERROR; rc = sqlite3_config(SQLITE_CONFIG_CHUNKALLOC, nThreshold); Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); return TCL_OK; } /* ** Usage: ** |
︙ | ︙ |
Changes to test/permutations.test.
1 2 3 4 5 6 7 8 9 10 11 | # 2008 June 21 # # 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. # #*********************************************************************** # | | | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | # 2008 June 21 # # 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. # #*********************************************************************** # # $Id: permutations.test,v 1.17 2008/07/25 08:49:01 danielk1977 Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl # Argument processing. # set ::testmode [lindex $argv 0] |
︙ | ︙ | |||
443 444 445 446 447 448 449 | Run tests using the allocator in mem6.c. } -exclude { capi3.test capi3c.test } -initialize { catch {db close} sqlite3_reset_auto_extension sqlite3_shutdown | | | 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 | Run tests using the allocator in mem6.c. } -exclude { capi3.test capi3c.test } -initialize { catch {db close} sqlite3_reset_auto_extension sqlite3_shutdown sqlite3_config_chunkalloc 0 install_malloc_faultsim 1 sqlite3_initialize autoinstall_test_functions } -shutdown { catch {db close} sqlite3_reset_auto_extension sqlite3_shutdown |
︙ | ︙ |
Changes to tool/mksqlite3c.tcl.
︙ | ︙ | |||
209 210 211 212 213 214 215 216 217 218 219 220 221 222 | os.c fault.c mem1.c mem2.c mem3.c mem5.c mutex.c mutex_os2.c mutex_unix.c mutex_w32.c malloc.c printf.c random.c | > | 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | os.c fault.c mem1.c mem2.c mem3.c mem5.c mem6.c mutex.c mutex_os2.c mutex_unix.c mutex_w32.c malloc.c printf.c random.c |
︙ | ︙ |