<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Language" content="en-us"> <meta http-equiv="Content-Type" content="text/html; charset=us-ascii"> <link href="../pool.css" rel="stylesheet" type="text/css"> <title>Pool</title> </head> <body> <img src="../../../../boost.png" width="276" height="86" alt="C++ Boost"> <h1 align="center">User Allocators</h1> <h2>Introduction</h2> <p>Pool objects need to request memory blocks from the system, which the Pool then splits into chunks to allocate to the user. By specifying a <span class="code">UserAllocator</span> template parameter to various Pool interfaces, users can control how those system memory blocks are allocated.</p> <h2>Semantics</h2> <table border align="center" summary=""> <caption> <em>Symbol Table</em> </caption> <tr> <th>Symbol</th> <th>Meaning</th> </tr> <tr> <td class="code">UserAllocator</td> <td>A User Allocator type</td> </tr> <tr> <td class="code">block</td> <td>value of type <span class="code">char *</span></td> </tr> <tr> <td class="code">n</td> <td>value of type <span class= "code">UserAllocator::size_type</span></td> </tr> </table><br> <table border align="center" summary=""> <caption> <em>Typedefs</em> </caption> <tr> <th>Expression</th> <th>Type</th> </tr> <tr> <td class="code">UserAllocator::size_type</td> <td>An unsigned integral type that can represent the size of the largest object to be allocated</td> </tr> <tr> <td class="code">UserAllocator::difference_type</td> <td>A signed integral type that can represent the difference of any two pointers</td> </tr> </table><br> <table border align="center" summary=""> <caption> <em>Allocation and Deallocation</em> </caption> <tr> <th>Expression</th> <th>Return Type</th> <th>Pre-Condition/Notes</th> </tr> <tr> <td class="code">UserAllocator::malloc(n)</td> <td class="code">char *</td> <td>Attempts to allocate <span class="code">n</span> bytes from the system. Returns 0 if out-of-memory.</td> </tr> <tr> <td class="code">UserAllocator::free(block)</td> <td class="code">void</td> <td><span class="code">block</span> must have been previously returned from a call to <span class="code">UserAllocator::malloc</span>.</td> </tr> </table> <h2>Provided Implementations</h2> <p>There are two <span class="code">UserAllocator</span> classes provided. Both of them are in pool.hpp (see <a href="pool.html">pool</a>). The default value for the template parameter <span class= "code">UserAllocator</span> is always <span class= "code">default_user_allocator_new_delete</span>.</p> <h3>Synopsis</h3> <pre class="code"> struct default_user_allocator_new_delete { typedef std::size_t size_type; typedef std::ptrdiff_t difference_type; static char * malloc(const size_type bytes) { return new (std::nothrow) char[bytes]; } static void free(char * const block) { delete [] block; } }; struct default_user_allocator_malloc_free { typedef std::size_t size_type; typedef std::ptrdiff_t difference_type; static char * malloc(const size_type bytes) { return reinterpret_cast<char *>(std::malloc(bytes)); } static void free(char * const block) { std::free(block); } }; </pre> <hr> <p><a href="http://validator.w3.org/check?uri=referer"><img border="0" src= "../../../../doc/images/valid-html401.png" alt="Valid HTML 4.01 Transitional" height="31" width="88"></a></p> <p>Revised <!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %B, %Y" startspan -->05 December, 2006<!--webbot bot="Timestamp" endspan i-checksum="38516" --></p> <p><i>Copyright © 2000, 2001 Stephen Cleary (scleary AT jerviswebb DOT com)</i></p> <p><i>Distributed under the Boost Software License, Version 1.0. (See accompanying file <a href="../../../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or copy at <a href= "http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>)</i></p> </body> </html>