Xalan-C++ API Documentation

The Xalan C++ XSL Transformer Version 1.1

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 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 template<class ObjectType>
00074 class ReusableArenaAllocator : public ArenaAllocator<ObjectType,
00075                                                      ReusableArenaBlock<ObjectType> >
00076 {
00077 public:
00078 
00079     typedef ReusableArenaBlock<ObjectType>              ReusableArenaBlockType;
00080 
00081     typedef typename ReusableArenaBlockType::size_type  size_type;
00082 
00083     typedef ArenaAllocator<ObjectType,
00084                            ReusableArenaBlockType>      BaseClassType;
00085 
00086     // $$$ ToDo: This typedef is here because of a bug in gcc.
00087 #if defined (XALAN_NO_NAMESPACES)
00088     typedef vector<ReusableArenaBlockType*>             ArenaBlockListType;
00089 #else
00090     typedef std::vector<ReusableArenaBlockType*>        ArenaBlockListType;
00091 #endif
00092 
00093     /*
00094      * Construct an instance that will allocate blocks of the specified size.
00095      *
00096      * @param theBlockSize The block size.
00097      */
00098     ReusableArenaAllocator(size_type    theBlockSize) :
00099         BaseClassType(theBlockSize),
00100         m_lastBlockReferenced(0)
00101     {
00102     }
00103 
00104     virtual
00105     ~ReusableArenaAllocator()
00106     {
00107     }
00108 
00109     /*
00110      * Destroy the object, and free the block for re-use.
00111      *
00112      * @param theObject the address of the object.
00113      * @return true if the object was deleted, false if not.
00114      */
00115     bool
00116     destroyObject(ObjectType*   theObject)
00117     {
00118         bool    fSucess = false;
00119 
00120         // Check this, just in case...
00121         if (m_lastBlockReferenced != 0 && m_lastBlockReferenced->ownsObject(theObject) == true)
00122         {
00123             m_lastBlockReferenced->destroyObject(theObject);
00124 
00125             fSucess = true;
00126         }
00127         else
00128         {
00129             // Note that this-> is required by template lookup rules.
00130             const typename ArenaBlockListType::reverse_iterator theEnd = this->m_blocks.rend();
00131 
00132             typename ArenaBlockListType::reverse_iterator   i = this->m_blocks.rbegin();
00133 
00134             while(i != theEnd)
00135             {
00136                 if ((*i)->ownsObject(theObject) == true)
00137                 {
00138                     m_lastBlockReferenced = *i;
00139 
00140                     m_lastBlockReferenced->destroyObject(theObject);
00141 
00142                     fSucess = true;
00143 
00144                     break;
00145                 }
00146                 else
00147                 {
00148                     ++i;
00149                 }
00150             }
00151         }
00152 
00153         return fSucess;
00154     }
00155 
00156     /*
00157      * Allocate a block of the appropriate size for an
00158      * object.  Call commitAllocation() when after
00159      * the object is successfully constructed.  You _must_
00160      * commit an allocation before performing any other
00161      * operation on the allocator.
00162      *
00163      * @return A pointer to a block of memory
00164      */
00165     virtual ObjectType*
00166     allocateBlock()
00167     {
00168         if (m_lastBlockReferenced == 0 ||
00169             m_lastBlockReferenced->blockAvailable() == false)
00170         {
00171             // Search back for a block with some space available...     
00172             const typename ArenaBlockListType::reverse_iterator theEnd = this->m_blocks.rend();
00173             
00174             // Note that this-> is required by template lookup rules.
00175             typename ArenaBlockListType::reverse_iterator   i = this->m_blocks.rbegin();
00176 
00177             while(i != theEnd)
00178             {
00179                 assert(*i != 0);
00180 
00181                 if (*i != m_lastBlockReferenced && (*i)->blockAvailable() == true)
00182                 {
00183                     // Ahh, found one with free space.
00184                     m_lastBlockReferenced = *i;
00185 
00186                     break;
00187                 }
00188                 else
00189                 {
00190                     ++i;
00191                 }
00192             }
00193 
00194             if (i == theEnd)
00195             {
00196                 // No blocks have free space available, so create a new block, and
00197                 // push it on the list.
00198                 // Note that this-> is required by template lookup rules.
00199                 m_lastBlockReferenced = new ReusableArenaBlockType(this->m_blockSize);
00200 
00201                 this->m_blocks.push_back(m_lastBlockReferenced);
00202             }
00203         }
00204         assert(m_lastBlockReferenced != 0 && m_lastBlockReferenced->blockAvailable() == true);
00205 
00206         return m_lastBlockReferenced->allocateBlock();
00207     }
00208 
00209     /*
00210      * Commits the allocation of the previous
00211      * allocateBlock() call.
00212      *
00213      * @param theObject A pointer to a block of memory
00214      */
00215     virtual void
00216     commitAllocation(ObjectType*    theObject)
00217     {
00218         // Note that this-> is required by template lookup rules.
00219         assert(this->m_blocks.size() != 0 && m_lastBlockReferenced != 0 && m_lastBlockReferenced->ownsBlock(theObject) == true);
00220 
00221         m_lastBlockReferenced->commitAllocation(theObject);
00222         assert(m_lastBlockReferenced->ownsObject(theObject) == true);
00223     }
00224 
00225     virtual void
00226     reset()
00227     {
00228         m_lastBlockReferenced = 0;
00229 
00230         BaseClassType::reset();
00231     }
00232 
00233     virtual bool
00234     ownsObject(const ObjectType*    theObject) const
00235     {
00236         bool    fResult = false;
00237 
00238         // If no block has ever been referenced, then we haven't allocated
00239         // any objects.
00240         if (m_lastBlockReferenced != 0)
00241         {
00242             // Check the last referenced block first.
00243             fResult = m_lastBlockReferenced->ownsObject(theObject);
00244 
00245             if (fResult == false)
00246             {
00247                 fResult = BaseClassType::ownsObject(theObject);
00248             }
00249         }
00250 
00251         return fResult;
00252     }
00253 
00254 private:
00255 
00256     // Not defined...
00257     ReusableArenaAllocator(const ReusableArenaAllocator<ObjectType>&);
00258 
00259     ReusableArenaAllocator<ObjectType>&
00260     operator=(const ReusableArenaAllocator<ObjectType>&);
00261 
00262     // Data members...
00263     ReusableArenaBlockType*     m_lastBlockReferenced;
00264 };
00265 
00266 
00267 
00268 #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++ XSL Transformer Version 1.1
Copyright © 2000, 2001 The Apache Software Foundation. All Rights Reserved.