Sophie

Sophie

distrib > Mandriva > 10.0 > i586 > by-pkgid > ef9bad9e14fc2a68cb7c992c11d75f5e > files > 3154

libboost1-devel-1.31.0-1mdk.i586.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<HTML>
<HEAD>
<TITLE>Pool Concepts</TITLE>
<LINK HREF="pool.css" REL="stylesheet" TYPE="text/css">
</HEAD>
<BODY>

<IMG SRC="../../../c++boost.gif" WIDTH=276 HEIGHT=86 ALT="C++ Boost">

<H1 ALIGN=CENTER>Pool Concepts</H1>

<P>
<BLOCKQUOTE>
&quot;Dynamic memory allocation has been a fundamental part of most computer systems since roughly 1960...&quot;<SUP><A HREF="#ref1">1</A></SUP>
</BLOCKQUOTE>

<p>
Everyone uses dynamic memory allocation.  If you have ever called <SPAN CLASS="code">malloc</SPAN> or <SPAN CLASS="code">new</SPAN>, then you have used dynamic memory allocation.  Most programmers have a tendency to treat the heap as a &quot;magic bag&quot;: we ask it for memory, and it magically creates some for us.  Sometimes we run into problems because the heap is <EM>not</EM> magic.

<p>
The heap is limited.  Even on large systems (i.e., not embedded) with huge amounts of virtual memory available, there is a limit.  Everyone is aware of the physical limit, but there is a more subtle, &quot;virtual&quot; limit, that limit at which your program (or the entire system) slows down due to the use of virtual memory.  This virtual limit is much closer to your program than the physical limit, especially if you are running on a multitasking system.  Therefore, when running on a large system, it is considered &quot;nice&quot; to make your program use as few resources as necessary, and release them as soon as possible.  When using an embedded system, programmers usually have no memory to waste.

<P>
The heap is complicated.  It has to satisfy any type of memory request, for any size, and do it <EM>fast</EM>.  The common approaches to memory management have to do with splitting the memory up into portions, and keeping them ordered by size in some sort of a tree or list structure.  Add in other factors, such as locality and estimating lifetime, and heaps quickly become very complicated.  So complicated, in fact, that there is no known &quot;perfect&quot; answer to the problem of how to do dynamic memory allocation.  The diagrams below illustrate how most common memory managers work: for each chunk of memory, it uses part of that memory to maintain its internal tree or list structure.  Even when a chunk is <SPAN CLASS="code">malloc</SPAN>'ed out to a program, the memory manager must &quot;save&quot; some information in it &mdash; usually just its size.  Then, when the block is <SPAN CLASS="code">free</SPAN>'d, the memory manager can easily tell how large it is.

<TABLE CELLSPACING="0" BORDER="3" RULES="none" STYLE="float: left; clear: both;">
<CAPTION><EM>Memory block, not allocated</EM></CAPTION>
<TR><TD STYLE="background-color: red; text-align: center;">Memory not belonging to process</TD></TR>
<TR><TD STYLE="padding: 1em 0em; background-color: silver; text-align: center;">Memory used internally by memory allocator algorithm (usually 8-12 bytes)</TD></TR>
<TR><TD STYLE="padding: 2em 0em; background-color: gray; text-align: center">Unused memory</TD></TR>
<TR><TD STYLE="background-color: red; text-align: center;">Memory not belonging to process</TD></TR>
</TABLE>

<TABLE CELLSPACING="0" BORDER="3" RULES="none" STYLE="float: right; clear: both;">
<CAPTION><EM>Memory block, allocated (used by program)</EM></CAPTION>
<TR><TD STYLE="background-color: red; text-align: center;">Memory not belonging to process</TD></TR>
<TR><TD STYLE="background-color: silver; text-align: center;">Memory used internally by memory allocator algorithm (usually 4 bytes)</TD></TR>
<TR><TD STYLE="padding: 3em 0em; background-color: yellow; text-align: center">Memory usable by program</TD></TR>
<TR><TD STYLE="background-color: red; text-align: center;">Memory not belonging to process</TD></TR>
</TABLE>

<P>
Because of the complication of dynamic memory allocation, it is often inefficient in terms of time and/or space.  Most memory allocation algorithms store some form of information with each memory block, either the block size or some relational information, such as its position in the internal tree or list structure.  It is common for such &quot;header fields&quot; to take up one machine word in a block that is being used by the program.  The obvious problem, then, is when small objects are dynamically allocated.  For example, if <SPAN CLASS="code">int</SPAN>s were dynamically allocated, then automatically the algorithm will reserve space for the header fields as well, and we end up with a 50% waste of memory.  Of course, this is a worst-case scenario.  However, more modern programs are making use of small objects on the heap; and that is making this problem more and more apparent.  Wilson <EM>et. al.</EM> state that an average-case memory overhead is about ten to twenty percent<SUP><A HREF="#ref2">2</A></SUP>.  This memory overhead will grow higher as more programs use more smaller objects.  It is this memory overhead that brings programs closer to the virtual limit.

<P>
In larger systems, the memory overhead is not as big of a problem (compared to the amount of time it would take to work around it), and thus is often ignored.  However, there are situations where many allocations and/or deallocations of smaller objects are taking place as part of a time-critical algorithm, and in these situations, the system-supplied memory allocator is often too slow.

<P>
Simple segregated storage addresses both of these issues.  Almost all memory overhead is done away with, and all allocations can take place in a small amount of (amortized) constant time.  However, this is done at the loss of generality; simple segregated storage only can allocate memory chunks of a single size.

<P>
<HR>

<P>
<H1 ALIGN=CENTER>Simple Segregated Storage</H1>

<P>
Simple Segregated Storage is the basic idea behind the Boost Pool library.  Simple Segregated Storage is the simplest, and probably the fastest, memory allocation/deallocation algorithm.  It begins by <EM>partitioning</EM> a memory <EM>block</EM> into fixed-size <EM>chunks</EM>.  Where the block comes from is not important until implementation time.  A <EM>Pool</EM> is some object that uses Simple Segregated Storage in this fashion.  To illustrate:

<TABLE CELLSPACING="0" BORDER="3" RULES="none" ALIGN=CENTER STYLE="clear: both;">
<CAPTION><EM>Memory block, split into chunks</EM></CAPTION>
<TR><TD STYLE="background-color: red; text-align: center;">Memory not belonging to process</TD></TR>
<TR><TD STYLE="padding: 1em 0em; background-color: gray; text-align: center;">Chunk 0</TD></TR>
<TR><TD STYLE="padding: 1em 0em; background-color: gray; text-align: center;">Chunk 1</TD></TR>
<TR><TD STYLE="padding: 1em 0em; background-color: gray; text-align: center;">Chunk 2</TD></TR>
<TR><TD STYLE="padding: 1em 0em; background-color: gray; text-align: center;">Chunk 3</TD></TR>
<TR><TD STYLE="background-color: red; text-align: center;">Memory not belonging to process</TD></TR>
</TABLE>

<P>
Each of the chunks in any given block are <STRONG>always</STRONG> the same size.  This is the fundamental restriction of Simple Segregated Storage: you cannot ask for chunks of different sizes.  For example, you cannot ask a Pool of integers for a character, or a Pool of characters for an integer (assuming that characters and integers are different sizes).

<P>
Simple Segregated Storage works by interleaving a <EM>free list</EM> within the unused chunks.  For example:

<TABLE CELLSPACING="0" BORDER="3" RULES="none" STYLE="float: left; clear: both;">
<CAPTION><EM>Memory block, with no chunks allocated</EM></CAPTION>
<TR><TD STYLE="background-color: red; text-align: center;">Memory not belonging to process</TD></TR>
<TR><TD STYLE="padding: 1em 0em; background-color: gray; text-align: center;">Chunk 0; points to Chunk 1</TD></TR>
<TR><TD STYLE="padding: 1em 0em; background-color: gray; text-align: center;">Chunk 1; points to Chunk 2</TD></TR>
<TR><TD STYLE="padding: 1em 0em; background-color: gray; text-align: center;">Chunk 2; points to Chunk 3</TD></TR>
<TR><TD STYLE="padding: 1em 0em; background-color: gray; text-align: center;">Chunk 3; end-of-list</TD></TR>
<TR><TD STYLE="background-color: red; text-align: center;">Memory not belonging to process</TD></TR>
</TABLE>

<TABLE CELLSPACING="0" BORDER="3" RULES="none" STYLE="float: right; clear: both;">
<CAPTION><EM>Memory block, with two chunks allocated</EM></CAPTION>
<TR><TD STYLE="background-color: red; text-align: center;">Memory not belonging to process</TD></TR>
<TR><TD STYLE="padding: 1em 0em; background-color: gray; text-align: center;">Chunk 0; points to Chunk 2</TD></TR>
<TR><TD STYLE="padding: 1em 0em; background-color: silver; text-align: center;">Chunk 1 (in use by process)</TD></TR>
<TR><TD STYLE="padding: 1em 0em; background-color: gray; text-align: center;">Chunk 2; end-of-list</TD></TR>
<TR><TD STYLE="padding: 1em 0em; background-color: silver; text-align: center;">Chunk 3 (in use by process)</TD></TR>
<TR><TD STYLE="background-color: red; text-align: center;">Memory not belonging to process</TD></TR>
</TABLE>

<P>
By interleaving the free list inside the chunks, each Simple Segregated Storage only has the overhead of a single pointer (the pointer to the first element in the list).  It has <EM>no</EM> memory overhead for chunks that are in use by the process.

<P>
Simple Segregated Storage is also extremely fast.  In the simplest case, memory allocation is merely removing the first chunk from the free list, a O(1) operation.  In the case where the free list is empty, another block may have to be acquired and partitioned, which would result in an amortized O(1) time.  Memory deallocation may be as simple as adding that chunk to the front of the free list, a O(1) operation.  However, more complicated uses of Simple Segregated Storage may require a sorted free list, which makes deallocation O(N).

<P>
Simple Segregated Storage gives faster execution and less memory overhead than a system-supplied allocator, but at the loss of generality.  A good place to use a Pool is in situations where many (noncontiguous) small objects may be allocated on the heap, or if allocation and deallocation of the same-sized objects happens repeatedly.

<P>
<HR>

<P>
<H2>References</H2>

<P>
<OL>
<LI><A NAME="ref1">Doug Lea, <EM>A Memory Allocator</EM>.</A>  Available on the web at <A HREF="http://gee.cs.oswego.edu/dl/html/malloc.html">http://gee.cs.oswego.edu/dl/html/malloc.html</A></LI>
<LI><A NAME="ref2">Paul R. Wilson, Mark S. Johnstone, Michael Neely, and David Boles, &quot;Dynamic Storage Allocation: A Survey and Critical Review&quot; in <EM>International Workshop on Memory Management</EM>, September 1995, pg. 28, 36.</A>  Available on the web at <A HREF="ftp://ftp.cs.utexas.edu/pub/garbage/allocsrv.ps">ftp://ftp.cs.utexas.edu/pub/garbage/allocsrv.ps</A></LI>
</OL>

<P>
<H2>Other Implementations</H2>

<P>
Pool allocators are found in many programming languages, and in many variations.  The beginnings of many implementations may be found in common programming literature; some of these are given below.  Note that none of these are complete implementations of a Pool; most of these leave some aspects of a Pool as a user exercise.  However, in each case, even though some aspects are missing, these examples use the same underlying concept of a Simple Segregated Storage described in this document.

<P>
<UL>

<LI>
&quot;The C++ Programming Language&quot;, 3rd ed., by Bjarne Stroustrup, Section 19.4.2.  Missing aspects:
 <OL>
 <LI>Not portable</LI>
 <LI>Cannot handle allocations of arbitrary numbers of objects (this was left as an exercise)</LI>
 <LI>Not thread-safe</LI>
 <LI>Suffers from the static initialization problem</LI>
 </OL>
</LI>

<LI>
&quot;MicroC/OS-II: The Real-Time Kernel&quot;, by Jean J. Labrosse, Chapter 7 and Appendix B.04.  This is an example of the Simple Segregated Storage scheme at work in the internals of an actual OS.  Missing aspects:
 <OL>
 <LI>Not portable (though this is OK, since it's part of its own OS)</LI>
 <LI>Cannot handle allocations of arbitrary numbers of blocks (which is also OK, since this feature is not needed)</LI>
 <LI>Requires non-intuitive user code to create and destroy the Pool</LI>
 </OL>
</LI>

<LI>
&quot;Efficient C++: Performance Programming Techniques&quot;, by Dov Bulka and David Mayhew, Chapters 6 and 7.  This is a good example of iteratively developing a Pool solution; however, their premise (that the system-supplied allocation mechanism is hopelessly inefficient) is flawed on every system I've tested on.  Run their timings on your system before you accept their conclusions.  Missing aspects:
 <OL>
 <LI>Requires non-intuitive user code to create and destroy the Pool</LI>
 </OL>
</LI>

<LI>
&quot;Advanced C++: Programming Styles and Idioms&quot;, by James O. Coplien, Section 3.6.  This has examples of both static and dynamic pooling.  Missing aspects:
 <OL>
 <LI>Not thread-safe</LI>
 <LI>The static pooling example is not portable</LI>
 </OL>
</LI>

</UL>

<P>
<HR>

<P>
Copyright &copy; 2000, 2001 Stephen Cleary (scleary AT jerviswebb DOT com)

<P>
This file can be redistributed and/or modified under the terms found in <A HREF="copyright.html">copyright.html</A>

<P>
This software and its documentation is provided &quot;as is&quot; without express or implied warranty, and with no claim as to its suitability for any purpose.

</BODY>
</HTML>