⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 malloc.html

📁 sqlite的帮助文档
💻 HTML
📖 第 1 页 / 共 4 页
字号:
the test suite provides over 99% statement test coverage.  This isstrong evidence that dynamic memory allocation is used correctlyeverywhere within SQLite.</p><a name="config"></a><h2>3.0 Configuration</h2><p>The default memory allocation settings in SQLite are appropriatefor most applications.  However, applications with unusual or particularlystrict requirements may want to adjust the configuration to more closelyalign SQLite to their needs.Both compile-time and start-time configuration options are available.</p><a name="altalloc"></a><h3>3.1 Alternative low-level memory allocators</h3><p>The SQLite source code includes several different memory allocationmodules that can be selected at compile-time, or to a limited extentat start-time.</p><a name="defaultalloc"></a><h4>3.1.1 The default memory allocator</h4><p>By default, SQLite uses the malloc(), realloc(), and free() routinesfrom the standard C library for its memory allocation needs.  These routinesare surrounded by a thin wrapper that also provides a "memsize()" functionthat will return the size of an existing allocation.  The memsize() functionis needed to keep an accurate count of the number of bytes of outstandingmemory; memsize() determines how many bytes to remove from the outstandingcount when an allocation is freed.  The default allocator implementsmemsize() by always allocating 8 extra bytes on each malloc() request andstoring the size of the allocation in that 8-byte header.</p><p>The default memory allocator is recommended for most applications.If you do not have a compelling need to use an alternative memoryallocator, then use the default.</p><a name="memdebug"></a><h4>3.1.2 The debugging memory allocator</h4><p>If SQLite is compiled with the <a href="compile.html#memdebug">SQLITE_MEMDEBUG</a> compile-time option,then a different, heavy wrapper is used around system malloc(), realloc(), and free().The heavy wrapper allocates around 100 bytes of extra spacewith each allocation.  The extra space is used to places sentinel values at both ends of the allocation returned to the SQLite core.  When anallocation is freed,these sentinels are checked to make sure the SQLite core did not overrunthe buffer in either direction.  When the system library is GLIBC, the heavy wrapper also makes use of the GNU backtrace() function to examinethe stack and record the ancestor functions of the malloc() call.  Whenrunning the SQLite test suite, the heavy wrapper also records the name ofthe current test case.  These latter two features are useful fortracking down the source of memory leaks detected by the test suite.</p><p>The heavy wrapper that is used when <a href="compile.html#memdebug">SQLITE_MEMDEBUG</a> is set alsomakes sure each new allocation is filled with nonsense data prior toreturning the allocation to the caller.  And as soon as an allocationis free, it is again filled with nonsense data.  These two actions helpto insure that the SQLite core does not make assumptions about the stateof newly allocated memory and that memory allocations are not used afterthey have been freed.</p><p>The heavy wrapper employed by <a href="compile.html#memdebug">SQLITE_MEMDEBUG</a> is intended for useonly during testing, analysis, and debugging of SQLite.  The heavy wrapperhas a significant performance and memory overhead and probably should notbe used in production.</p><a name="memsys5"></a><h4>3.1.3 Zero-malloc memory allocator</h4><p>When SQLite is compiled with the <a href="compile.html#enable_memsys5">SQLITE_ENABLE_MEMSYS5</a> option, analternative memory allocator that does not use malloc() is included in thebuild.  The SQLite developers refer to this alternative memory allocatoras "memsys5".  Even when it is included in the build, memsys5 is disabled by default.To enable memsys5, the application must invoke the following SQLite interface at start-time:</p><blockquote><pre><a href="c3ref/config.html">sqlite3_config</a>(<a href="c3ref/c_config_getmalloc.html">SQLITE_CONFIG_HEAP</a>, pBuf, szBuf, mnReq);</pre></blockquote><p>In the call above, pBuf is a pointer to a large, contiguous chunkof memory space that SQLite will use to satisfy all of its memoryallocation needs.   pBuf might point to a static array or it mightbe memory obtained from some other application-specific mechanism.szBuf is an integer that is the number of bytes of memory spacepointed to by pBuf.  mnReq is another integer that is theminimum size of an allocation.  Any call to <a href="c3ref/free.html">sqlite3_malloc(N)</a> whereN is less than mnReq will be rounded up to mnReq.  mnReq must bea power of two.  We shall see later that the mnReq parameter isimportant in reducing the value of <b>n</b> and hence the minimum memorysize requirement in the <a href="malloc.html#nofrag">Robson proof</a>.</p><p>The memsys5 allocator is designed for use on embedded systems, though there is nothing to prevent its use on workstations.The szBuf is typically between a few hundred kilobytes up to a fewdozen megabytes, depending on system requirements and memory budget.</p><p>The algorithm used by memsys5 can be called "power-of-two,first-fit".  The sizes of all memory allocation requests are rounded up to a power of two and the request is satisfiedby the first free slot in pBuf that is large enough.  Adjacent freedallocations are coalesced using a buddy system. When used appropriately,this algorithm provides mathematical guarantees against fragmentation andbreakdown, as described further <a href="#nofrag">below</a>.</p><a name="memsysx"></a><h4>3.1.4 Experimental memory allocators</h4><p>The name "memsys5" used for the zero-malloc memory allocator impliesthat there are several additional memory allocators available, and indeedthere are.  The default memory allocator is "memsys1".  The debuggingmemory allocator is "memsys2".  Those have already been covered.</p><p>If SQLite is compiled with <a href="compile.html#enable_memsys3">SQLITE_ENABLE_MEMSYS3</a> than anotherzero-malloc memory allocator, similar to memsys5, is included in thesource tree.  The memsys3 allocator, like memsys5, must be activatedby a call to <a href="c3ref/config.html">sqlite3_config</a>(<a href="c3ref/c_config_getmalloc.html">SQLITE_CONFIG_HEAP</a>,...).  Memsys3uses the memory buffer supplied as its source for all memory allocations.The difference between memsys3 and memsys5 is that memsys3 uses adifferent memory allocation algorithm that seems to work well inpractice, but which does not provide mathematicalguarantees against memory fragmentation and breakdown.  Memsys3 wasa predecessor to memsys5.  The SQLite developers now believe that memsys5 is superior tomemsys3 and that all applications that need a zero-malloc memoryallocator should use memsys5 in preference to memsys3.  Memsys3 isconsidered both experimental and deprecated and will likely be removed from the source tree in a future release of SQLite.</p><p>Code for memsys4 is still in the SQLite source tree (as of this writing - SQLite <a href="releaselog/3_6_1.html">version 3.6.1</a>), but it has not been maintained for several releasecycles and probably does not work.  (Update: memsys4 was removed asof <a href="releaselog/3_6_5.html">version 3.6.5</a>) Memsys4 was an attempt to use mmap()to obtain memory and then use madvise() to release unused pagesback to the operating system so that they could be reused by otherprocesses.  The work on memsys4 has been abandoned and the memsys4 modulewill likely be removed from the source tree in the near future.</p><p>Memsys6 uses system malloc() and free() to obtain the memory it needs.Memsys6 serves as an aggregator.  Memsys6 only calls system malloc() to obtainlarge allocations.  It then subdivides those large allocations to services multiple smaller memory allocation requests coming from the SQLite core.Memsys6 is intended for use in systems wheresystem malloc() is particularly inefficient.  The idea behind memsys6 isto reduce the number of calls to system malloc() by a factor of 10 or more.</p><p>Memsys6 is made available by compiling SQLite with the SQLITE_ENABLE_MEMSYS6compile-time option and then at start-time invoking:</p><blockquote><pre><a href="c3ref/config.html">sqlite3_config</a>(SQLITE_CONFIG_CHUNKALLOC);</pre></blockquote><p>Memsys6 was added in SQLite <a href="releaselog/3_6_1.html">version 3.6.1</a>.It is very experimental.  Its future is uncertain and it may be removedin a subsequent release.  Update:  Memsys6 was removed as of <a href="releaselog/3_6_5.html">version 3.6.5</a>.</p><p>Other experimental memory allocators might be added in future releasesof SQLite.  One many anticipate that these will be called memsys7, memsys8,and so forth.</p><a name="appalloc"></a><h4>3.1.5 Application-defined memory allocators</h4><p>New memory allocators do not have to be part of the SQLite source treenor included in the sqlite3.c <a href="amalgamation.html">amalgamation</a>.  Individual applications cansupply their own memory allocators to SQLite at start-time.</p><p>To cause SQLite to use a new memory allocator, the applicationsimply calls:</p><blockquote><pre><a href="c3ref/config.html">sqlite3_config</a>(<a href="c3ref/c_config_getmalloc.html">SQLITE_CONFIG_MALLOC</a>, pMem);</pre></blockquote><p>In the call above, pMem is a pointer to an <a href="c3ref/mem_methods.html">sqlite3_mem_methods</a> objectthat defines the interface to the application-specific memory allocator.The <a href="c3ref/mem_methods.html">sqlite3_mem_methods</a> object is really just a structure containingpointers to functions to implement the various memory allocation primitives.</p><p>In a multi-threaded application, access to the <a href="c3ref/mem_methods.html">sqlite3_mem_methods</a>is serialized if and only if <a href="c3ref/c_config_getmalloc.html">SQLITE_CONFIG_MEMSTATUS</a> is enabled.If <a href="c3ref/c_config_getmalloc.html">SQLITE_CONFIG_MEMSTATUS</a> is disabled then the methods in<a href="c3ref/mem_methods.html">sqlite3_mem_methods</a> must take care of their own serialization needs.</p><a name="overlayalloc"></a><h4>3.1.6 Memory allocator overlays</h4><p>An application can insert layers or "overlays" in between theSQLite core and the underlying memory allocator.For example, the <a href="#oomtesting">out-of-memory test logic</a>for SQLite uses an overlay that can simulate memory allocationfailures.</p><p>An overlay can be created by using the</p><blockquote><pre><a href="c3ref/config.html">sqlite3_config</a>(<a href="c3ref/c_config_getmalloc.html">SQLITE_CONFIG_GETMALLOC</a>, pOldMem);</pre></blockquote><p>interface to obtain pointers to the existing memory allocator.The existing allocator is saved by the overlay and is used asa fallback to do real memory allocation.  Then the overlay isinserted in place of the existing memory allocator usingthe <a href="c3ref/config.html">sqlite3_config</a>(<a href="c3ref/c_config_getmalloc.html">SQLITE_CONFIG_MALLOC</a>,...) as described<a href="#appalloc">above</a>.<a name="stuballoc"></a><h4>3.1.7 No-op memory allocator stub</h4><p>If SQLite is compiled with the <a href="compile.html#zero_malloc">SQLITE_ZERO_MALLOC</a> option, thenthe <a href="malloc.html#defaultalloc">default memory allocator</a> is omitted and replaced by a stubmemory allocator that never allocates any memory.  Any calls to thestub memory allocator will report back that no memory is available.</p><p>The no-op memory allocator is not useful by itself.  It exists onlyas a placeholder so that SQLite has a memory allocator to link againston systems that may not have malloc(), free(), or realloc() in theirstandard library.An application that is compiled with <a href="compile.html#zero_malloc">SQLITE_ZERO_MALLOC</a> will need touse <a href="c3ref/config.html">sqlite3_config()</a> together with <a href="c3ref/c_config_getmalloc.html">SQLITE_CONFIG_MALLOC</a> or<a href="c3ref/c_config_getmalloc.html">SQLITE_CONFIG_HEAP</a> to specify a new alternative memory allocatorbefore beginning to use SQLite.</p><a name="scratch"></a><h3>3.2 Scratch memory</h3><p>SQLite occasionally needs a large chunk of "scratch" memory toperform some transient calculation.  Scratch memory is used, for example,as temporary storage when rebalancing a B-Tree.  These scratch memoryallocations are typically about 10 kilobytes in size and aretransient - lastingonly for the duration of a single, short-lived function call.</p><p>In older versions of SQLite, the scratch memory was obtained fromthe processor stack.  That works great on workstations with a large stack.But pulling large buffers from the stack caused problems on embedded systems with a small processor stack (typically 4K or 8K).  And so SQLite was modifiedto allocate scratch memory from the heap.</p><p>However, doing occasional large transient allocations from the heap canlead to memory fragmentation in embedded systems.  To work around thisproblem, a separate memory allocation system for scratch memory has beencreated.</p><p>The scratch memory allocator is set up as follows:</p><blockquote><pre><a href="c3ref/config.html">sqlite3_config</a>(<a href="c3ref/c_config_getmalloc.html">SQLITE_CONFIG_SCRATCH</a>, pBuf, sz, N);</pre></blockquote>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -