Xalan-C++ API Documentation

The Xalan C++ XSLT Processor Version 1.6

Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

ReusableArenaAllocator.hpp

Go to the documentation of this file.
00001 /*
00002  * The Apache Software License, Version 1.1
00003  *
00004  *
00005  * Copyright (c) 2000-2002 The Apache Software Foundation.  All rights 
00006  * reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions
00010  * are met:
00011  *
00012  * 1. Redistributions of source code must retain the above copyright
00013  *    notice, this list of conditions and the following disclaimer. 
00014  *
00015  * 2. Redistributions in binary form must reproduce the above copyright
00016  *    notice, this list of conditions and the following disclaimer in
00017  *    the documentation and/or other materials provided with the
00018  *    distribution.
00019  *
00020  * 3. The end-user documentation included with the redistribution,
00021  *    if any, must include the following acknowledgment:  
00022  *       "This product includes software developed by the
00023  *        Apache Software Foundation (http://www.apache.org/)."
00024  *    Alternately, this acknowledgment may appear in the software itself,
00025  *    if and wherever such third-party acknowledgments normally appear.
00026  *
00027  * 4. The names "Xalan" and "Apache Software Foundation" must
00028  *    not be used to endorse or promote products derived from this
00029  *    software without prior written permission. For written 
00030  *    permission, please contact apache@apache.org.
00031  *
00032  * 5. Products derived from this software may not be called "Apache",
00033  *    nor may "Apache" appear in their name, without prior written
00034  *    permission of the Apache Software Foundation.
00035  *
00036  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
00037  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00038  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00039  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
00040  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00041  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00042  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
00043  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00044  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00045  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00046  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00047  * SUCH DAMAGE.
00048  * ====================================================================
00049  *
00050  * This software consists of voluntary contributions made by many
00051  * individuals on behalf of the Apache Software Foundation and was
00052  * originally based on software copyright (c) 1999, International
00053  * Business Machines, Inc., http://www.ibm.com.  For more
00054  * information on the Apache Software Foundation, please see
00055  * <http://www.apache.org/>.
00056  */
00057 
00058 #if !defined(REUSABLEARENAALLOCATOR_INCLUDE_GUARD_1357924680)
00059 #define REUSABLEARENAALLOCATOR_INCLUDE_GUARD_1357924680
00060 
00061 
00062 
00063 #include <algorithm>
00064 #include <vector>
00065 
00066 
00067 
00068 #include "ReusableArenaBlock.hpp"
00069 #include "ArenaAllocator.hpp"
00070 
00071 
00072 
00073 XALAN_CPP_NAMESPACE_BEGIN
00074 
00075 
00076 
00077 template<class ObjectType>
00078 class ReusableArenaAllocator : public ArenaAllocator<ObjectType,
00079                                                      ReusableArenaBlock<ObjectType> >
00080 {
00081 public:
00082 
00083     typedef ReusableArenaBlock<ObjectType>              ReusableArenaBlockType;
00084 
00085     typedef typename ReusableArenaBlockType::size_type  size_type;
00086 
00087     typedef ArenaAllocator<ObjectType,
00088                            ReusableArenaBlockType>      BaseClassType;
00089 
00090 #if defined (XALAN_NO_STD_NAMESPACE)
00091     typedef vector<ReusableArenaBlockType*>             ArenaBlockListType;
00092 #else
00093     typedef std::vector<ReusableArenaBlockType*>        ArenaBlockListType;
00094 #endif
00095 
00096     /*
00097      * Construct an instance that will allocate blocks of the specified size.
00098      *
00099      * @param theBlockSize The block size.
00100      */
00101     ReusableArenaAllocator(size_type    theBlockSize) :
00102         BaseClassType(theBlockSize),
00103         m_lastBlockReferenced(0)
00104     {
00105     }
00106 
00107     virtual
00108     ~ReusableArenaAllocator()
00109     {
00110     }
00111 
00112     /*
00113      * Destroy the object, and free the block for re-use.
00114      *
00115      * @param theObject the address of the object.
00116      * @return true if the object was deleted, false if not.
00117      */
00118     bool
00119     destroyObject(ObjectType*   theObject)
00120     {
00121         bool    fSuccess = false;
00122 
00123         // Check this, just in case...
00124         if (m_lastBlockReferenced != 0 && m_lastBlockReferenced->ownsObject(theObject) == true)
00125         {
00126             m_lastBlockReferenced->destroyObject(theObject);
00127 
00128             fSuccess = true;
00129         }
00130         else
00131         {
00132             // Note that this-> is required by template lookup rules.
00133             const typename ArenaBlockListType::reverse_iterator theEnd = this->m_blocks.rend();
00134 
00135             typename ArenaBlockListType::reverse_iterator   i = this->m_blocks.rbegin();
00136 
00137             while(i != theEnd)
00138             {
00139                 if ((*i)->ownsObject(theObject) == true)
00140                 {
00141                     m_lastBlockReferenced = *i;
00142 
00143                     m_lastBlockReferenced->destroyObject(theObject);
00144 
00145                     fSuccess = true;
00146 
00147                     break;
00148                 }
00149                 else
00150                 {
00151                     ++i;
00152                 }
00153             }
00154         }
00155 
00156         return fSuccess;
00157     }
00158 
00159     /*
00160      * Allocate a block of the appropriate size for an
00161      * object.  Call commitAllocation() when after
00162      * the object is successfully constructed.  You _must_
00163      * commit an allocation before performing any other
00164      * operation on the allocator.
00165      *
00166      * @return A pointer to a block of memory
00167      */
00168     virtual ObjectType*
00169     allocateBlock()
00170     {
00171         if (m_lastBlockReferenced == 0 ||
00172             m_lastBlockReferenced->blockAvailable() == false)
00173         {
00174             // Search back for a block with some space available...     
00175             const typename ArenaBlockListType::reverse_iterator theEnd = this->m_blocks.rend();
00176             
00177             // Note that this-> is required by template lookup rules.
00178             typename ArenaBlockListType::reverse_iterator   i = this->m_blocks.rbegin();
00179 
00180             while(i != theEnd)
00181             {
00182                 assert(*i != 0);
00183 
00184                 if (*i != m_lastBlockReferenced && (*i)->blockAvailable() == true)
00185                 {
00186                     // Ahh, found one with free space.
00187                     m_lastBlockReferenced = *i;
00188 
00189                     break;
00190                 }
00191                 else
00192                 {
00193                     ++i;
00194                 }
00195             }
00196 
00197             if (i == theEnd)
00198             {
00199                 // No blocks have free space available, so create a new block, and
00200                 // push it on the list.
00201                 // Note that this-> is required by template lookup rules.
00202                 m_lastBlockReferenced = new ReusableArenaBlockType(this->m_blockSize);
00203 
00204                 this->m_blocks.push_back(m_lastBlockReferenced);
00205             }
00206         }
00207         assert(m_lastBlockReferenced != 0 && m_lastBlockReferenced->blockAvailable() == true);
00208 
00209         return m_lastBlockReferenced->allocateBlock();
00210     }
00211 
00212     /*
00213      * Commits the allocation of the previous
00214      * allocateBlock() call.
00215      *
00216      * @param theObject A pointer to a block of memory
00217      */
00218     virtual void
00219     commitAllocation(ObjectType*    theObject)
00220     {
00221         // Note that this-> is required by template lookup rules.
00222         assert(this->m_blocks.empty() == false && m_lastBlockReferenced != 0 && m_lastBlockReferenced->ownsBlock(theObject) == true);
00223 
00224         m_lastBlockReferenced->commitAllocation(theObject);
00225         assert(m_lastBlockReferenced->ownsObject(theObject) == true);
00226     }
00227 
00228     virtual void
00229     reset()
00230     {
00231         m_lastBlockReferenced = 0;
00232 
00233         BaseClassType::reset();
00234     }
00235 
00236     virtual bool
00237     ownsObject(const ObjectType*    theObject) const
00238     {
00239         bool    fResult = false;
00240 
00241         // If no block has ever been referenced, then we haven't allocated
00242         // any objects.
00243         if (m_lastBlockReferenced != 0)
00244         {
00245             // Check the last referenced block first.
00246             fResult = m_lastBlockReferenced->ownsObject(theObject);
00247 
00248             if (fResult == false)
00249             {
00250                 fResult = BaseClassType::ownsObject(theObject);
00251             }
00252         }
00253 
00254         return fResult;
00255     }
00256 
00257 private:
00258 
00259     // Not defined...
00260     ReusableArenaAllocator(const ReusableArenaAllocator<ObjectType>&);
00261 
00262     ReusableArenaAllocator<ObjectType>&
00263     operator=(const ReusableArenaAllocator<ObjectType>&);
00264 
00265     // Data members...
00266     ReusableArenaBlockType*     m_lastBlockReferenced;
00267 };
00268 
00269 
00270 
00271 XALAN_CPP_NAMESPACE_END
00272 
00273 
00274 
00275 #endif  // !defined(REUSABLEARENAALLOCATOR_INCLUDE_GUARD_1357924680)

Interpreting class diagrams

Doxygen and GraphViz are used to generate this API documentation from the Xalan-C header files.

Xalan-C++ XSLT Processor Version 1.6
Copyright © 2000, 2001, 2002, 2003 The Apache Software Foundation. All Rights Reserved.