Xalan-C++ API Documentation

The Xalan C++ XSLT Processor Version 1.6

Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

ArenaAllocator.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(ARENAALLOCATOR_INCLUDE_GUARD_1357924680)
00059 #define ARENAALLOCATOR_INCLUDE_GUARD_1357924680
00060 
00061 
00062 
00063 #include <algorithm>
00064 #include <vector>
00065 
00066 
00067 
00068 #include "ArenaBlock.hpp"
00069 
00070 
00071 
00072 XALAN_CPP_NAMESPACE_BEGIN
00073 
00074 
00075 
00076 template<class Type>
00077 class ArenaDeleteFunctor
00078 {
00079 public:
00080 
00081     void
00082     operator()(const Type*  theType) const
00083     {
00084 #if defined(XALAN_CANNOT_DELETE_CONST)
00085         delete (Type*)theType;
00086 #else
00087         delete theType;
00088 #endif
00089     }
00090 };
00091 
00092 
00093 
00094 template<class ObjectType,
00095 #if defined(XALAN_NO_DEFAULT_TEMPLATE_ARGUMENTS)
00096          class ArenaBlockType>
00097 #else
00098          class ArenaBlockType = ArenaBlock<ObjectType> >
00099 #endif
00100 class ArenaAllocator
00101 {
00102 public:
00103 
00104     typedef typename ArenaBlockType::size_type  size_type;
00105 
00106     /*
00107      * Construct an instance that will allocate blocks of the specified size.
00108      *
00109      * @param theBlockSize The block size.
00110      */
00111     ArenaAllocator(size_type    theBlockSize) :
00112         m_blockSize(theBlockSize),
00113         m_blocks()
00114     {
00115     }
00116 
00117     virtual
00118     ~ArenaAllocator()
00119     {
00120         reset();
00121     }
00122 
00123     /*
00124      * Get size of an ArenaBlock, that is, the number
00125      * of objects in each block.
00126      *
00127      * @return The size of the block
00128      */
00129     size_type
00130     getBlockSize() const
00131     {
00132         return m_blockSize;
00133     }
00134 
00135     /*
00136      * Set size of an ArenaBlock, that is, the number
00137      * of objects in each block.  Only affects blocks
00138      * allocated after the call.
00139      *
00140      * @param theSize The size of the block
00141      */
00142     void
00143     setBlockSize(size_type  theSize)
00144     {
00145         m_blockSize = theSize;
00146     }
00147 
00148     /*
00149      * Get the number of ArenaBlocks currently allocated.
00150      *
00151      * @return The number of blocks.
00152      */
00153     size_type
00154     getBlockCount() const
00155     {
00156         return m_blocks.size();
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.
00163      *
00164      * @return A pointer to a block of memory
00165      */
00166     virtual ObjectType*
00167     allocateBlock()
00168     {
00169         if (m_blocks.empty() == true ||
00170             m_blocks.back()->blockAvailable() == false)
00171         {
00172             m_blocks.push_back(new ArenaBlockType(m_blockSize));
00173         }
00174         assert(m_blocks.empty() == false && m_blocks.back() != 0 && m_blocks.back()->blockAvailable() == true);
00175 
00176         return m_blocks.back()->allocateBlock();
00177     }
00178 
00179     /*
00180      * Commits the allocation of the previous
00181      * allocateBlock() call.
00182      *
00183      * @param theObject A pointer to a block of memory
00184      */
00185     virtual void
00186     commitAllocation(ObjectType*    theObject)
00187     {
00188         assert(m_blocks.empty() == false && m_blocks.back()->ownsBlock(theObject) == true);
00189 
00190         m_blocks.back()->commitAllocation(theObject);
00191         assert(m_blocks.back()->ownsObject(theObject) == true);
00192     }
00193 
00194     virtual bool
00195     ownsObject(const ObjectType*    theObject) const
00196     {
00197         bool    fResult = false;
00198 
00199         // Search back for a block that may have allocated the object...
00200         // Note that this-> is required by template lookup rules.
00201         const typename ArenaBlockListType::const_reverse_iterator   theEnd = this->m_blocks.rend();
00202 
00203         typename ArenaBlockListType::const_reverse_iterator i = this->m_blocks.rbegin();
00204 
00205         while(i != theEnd)
00206         {
00207             assert(*i != 0);
00208 
00209             if ((*i)->ownsObject(theObject) == true)
00210             {
00211                 fResult = true;
00212 
00213                 break;
00214             }
00215             else
00216             {
00217                 ++i;
00218             }
00219         }
00220 
00221         return fResult;
00222     }
00223 
00224     virtual void
00225     reset()
00226     {
00227         
00228         XALAN_STD_QUALIFIER for_each(
00229             m_blocks.begin(),
00230             m_blocks.end(),
00231             ArenaDeleteFunctor<ArenaBlockType>());
00232 
00233         m_blocks.clear();
00234     }
00235 
00236 protected:
00237 
00238     // data members...
00239 #if defined(XALAN_NO_STD_NAMESPACE)
00240     typedef vector<ArenaBlockType*>         ArenaBlockListType;
00241 #else
00242     typedef std::vector<ArenaBlockType*>    ArenaBlockListType;
00243 #endif
00244 
00245     size_type           m_blockSize;
00246 
00247     ArenaBlockListType  m_blocks;
00248 
00249 private:
00250 
00251     // Not defined...
00252     ArenaAllocator(const ArenaAllocator<ObjectType, ArenaBlockType>&);
00253 
00254     ArenaAllocator<ObjectType, ArenaBlockType>&
00255     operator=(const ArenaAllocator<ObjectType, ArenaBlockType>&);
00256 };
00257 
00258 
00259 
00260 XALAN_CPP_NAMESPACE_END
00261 
00262 
00263 
00264 #endif  // !defined(ARENAALLOCATOR_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.