Sophie

Sophie

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

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>Boost Pool Interfaces</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>Boost Pool Interfaces</H1>

<P>
<H2>Introduction</H2>

<P>
There are several interfaces provided which allow users great flexibility in how they want to use Pools.  Review the <A HREF="concepts.html">concepts document</A> to get the basic understanding of how Pools work.

<P>
<H2>Terminology and Tradeoffs</H2>

<P>
<H3>Object Usage vs. Singleton Usage</H3>

<P>
<EM>Object Usage</EM> is the method where each Pool is an object that may be created and destroyed.  Destroying a Pool implicitly frees all chunks that have been allocated from it.

<P>
<EM>Singleton Usage</EM> is the method where each Pool is an object with static duration; that is, it will not be destroyed until program exit.  Pool objects with Singleton Usage may be shared; thus, Singleton Usage implies thread-safety as well.  System memory allocated by Pool objects with Singleton Usage may be freed through <SPAN CLASS="code">release_memory</SPAN> or <SPAN CLASS="code">purge_memory</SPAN>.

<P>
<H3>Out-of-Memory Conditions: Exceptions vs. Null Return</H3>

<P>
Some Pool interfaces throw exceptions when out-of-memory; others will return 0.  In general, unless mandated by the Standard, Pool interfaces will always prefer to return 0 instead of throw an exception.

<P>
<H2>The Interfaces</H2>

<P>
<H3>pool</H3>

<P>
The <A HREF="interfaces/pool.html">pool interface</A> is a simple Object Usage interface with Null Return.

<P>
Example:
<PRE CLASS="code">void func()
{
  boost::pool&lt;&gt; p(sizeof(int));
  for (int i = 0; i < 10000; ++i)
  {
    int * const t = p.malloc();
    ... // Do something with t; don't take the time to free() it
  }
} // on function exit, p is destroyed, and all malloc()'ed ints are implicitly freed</PRE>

<P>
<H3>object_pool</H3>

<P>
The <A HREF="interfaces/object_pool.html">object_pool interface</A> is an Object Usage interface with Null Return, but is aware of the type of the object for which it is allocating chunks.  On destruction, any chunks that have been allocated from that object_pool will have their destructors called.

<P>
Example:
<PRE CLASS="code">struct X { ... }; // has destructor with side-effects

void func()
{
  boost::object_pool&lt;X&gt; p;
  for (int i = 0; i < 10000; ++i)
  {
    X * const t = p.malloc();
    ... // Do something with t; don't take the time to free() it
  }
} // on function exit, p is destroyed, and all destructors for the X objects are called</PRE>

<P>
<H3>singleton_pool</H3>

<P>
The <A HREF="interfaces/singleton_pool.html">singleton_pool interface</A> is a Singleton Usage interface with Null Return.  It's just the same as the pool interface but with Singleton Usage instead.

<P>
Example:
<PRE CLASS="code">struct MyPoolTag { };

typedef boost::singleton_pool&lt;MyPoolTag, sizeof(int)&gt; my_pool;
void func()
{
  for (int i = 0; i < 10000; ++i)
  {
    int * const t = my_pool::malloc();
    ... // Do something with t; don't take the time to free() it
  }
  // Explicitly free all malloc()'ed int's
  my_pool::purge_memory();
}</PRE>

<P>
<H3>pool_alloc</H3>

<P>
The <A HREF="interfaces/pool_alloc.html">pool_alloc interface</A> is a Singleton Usage interface with Exceptions.  It is built on the singleton_pool interface, and provides a Standard Allocator-compliant class (for use in containers, etc.).

<P>
Example:
<PRE CLASS="code">void func()
{
  std::vector&lt;int, boost::pool_allocator&lt;int&gt; &gt; v;
  for (int i = 0; i < 10000; ++i)
    v.push_back(13);
} // Exiting the function does NOT free the system memory allocated by the pool allocator
  // You must call
  //  boost::singleton_pool&lt;boost::pool_allocator_tag, sizeof(int)&gt;::release_memory()
  // in order to force that</PRE>

<P>
<H2>Future Directions</H2>

<P>
Another pool interface will be written: a base class for per-class pool allocation.  This &quot;pool_base&quot; interface will be Singleton Usage with Exceptions, and built on the singleton_pool interface.

<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>