Xalan-C++ API Documentation

The Xalan C++ XSLT Processor Version 1.5

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

XalanArrayAllocator.hpp

Go to the documentation of this file.
00001 /*
00002  * The Apache Software License, Version 1.1
00003  *
00004  *
00005  * Copyright (c) 1999-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 #if !defined(XALANARRAYALLOCATOR_HEADER_GUARD_1357924680)
00058 #define XALANARRAYALLOCATOR_HEADER_GUARD_1357924680
00059 
00060 
00061 
00062 #include <PlatformSupport/PlatformSupportDefinitions.hpp>
00063 
00064 
00065 
00066 #include <cassert>
00067 #include <list>
00068 #include <utility>
00069 #include <vector>
00070 
00071 
00072 
00073 XALAN_CPP_NAMESPACE_BEGIN
00074 
00075 
00076 
00077 template<class Type>
00078 class XALAN_PLATFORMSUPPORT_EXPORT XalanArrayAllocator
00079 {
00080 public:
00081 
00082 #if defined(XALAN_NO_STD_NAMESPACE)
00083     typedef vector<Type>                    VectorType;
00084     typedef typename VectorType::size_type  size_type;
00085     typedef pair<size_type, VectorType>     ListEntryType;
00086     typedef list<ListEntryType>             ListType;
00087 #else
00088     typedef std::vector<Type>                   VectorType;
00089     typedef typename VectorType::size_type      size_type;
00090     typedef std::pair<size_type, VectorType>    ListEntryType;
00091     typedef std::list<ListEntryType>            ListType;
00092 #endif
00093 
00094     typedef Type                            value_type;
00095 
00096     typedef typename ListType::iterator     ListIteratorType;
00097 
00098     // Default size for vector allocation.
00099     enum { eDefaultBlockSize = 500 };
00100 
00106     XalanArrayAllocator(size_type   theBlockSize = eDefaultBlockSize) :
00107         m_list(),
00108         m_blockSize(theBlockSize),
00109         m_lastEntryFound(0)
00110     {
00111     }
00112 
00113     ~XalanArrayAllocator()
00114     {
00115     }
00116 
00120     void
00121     clear()
00122     {
00123         m_list.clear();
00124 
00125         m_lastEntryFound = 0;
00126     }
00127 
00133     void
00134     reset()
00135     {
00136         const ListIteratorType  theEnd = m_list.end();
00137         ListIteratorType        theCurrent = m_list.begin();
00138 
00139         while(theCurrent != theEnd)
00140         {
00141             (*theCurrent).first = (*theCurrent).second.size();
00142 
00143             ++theCurrent;
00144         }
00145 
00146         m_lastEntryFound = &*m_list.begin();
00147     }
00148 
00155     Type*
00156     allocate(size_type  theCount)
00157     {
00158         // Handle the case of theCount being greater than the block size first...
00159         if (theCount >= m_blockSize)
00160         {
00161             return createEntry(theCount, theCount);
00162         }
00163         else
00164         {
00165             ListEntryType*  theEntry =
00166                 findEntry(theCount);
00167 
00168             // Did we find a slot?
00169             if (theEntry == 0)
00170             {
00171                 // Nope, create a new one...
00172                 return createEntry(m_blockSize, theCount);
00173             }
00174             else
00175             {
00176                 // The address we want is that of the first free element in the
00177                 // vector...
00178                 Type* const     thePointer =
00179                     &*theEntry->second.begin() + (theEntry->second.size() - theEntry->first);
00180 
00181                 // Resize the vector to the appropriate size...
00182                 theEntry->first -= theCount;
00183 
00184                 return thePointer;
00185             }
00186         }
00187     }
00188 
00189 private:
00190 
00191     // Utility functions...
00192     Type*
00193     createEntry(
00194             size_type   theBlockSize,
00195             size_type   theCount)
00196     {
00197         assert(theBlockSize >= theCount);
00198 
00199         // Push on a new entry.  The entry has no open space,
00200         // since it's greater than our block size...
00201         m_list.push_back(ListEntryType(0, VectorType()));
00202 
00203         // Get the new entry...
00204         ListEntryType&  theNewEntry = m_list.back();
00205 
00206         // Resize the vector to the appropriate size...
00207         theNewEntry.second.resize(theBlockSize, VectorType::value_type(0));
00208 
00209         // Set the number of free spaces accordingly...
00210         theNewEntry.first = theBlockSize - theCount;
00211 
00212         if (theNewEntry.first != 0)
00213         {
00214             m_lastEntryFound = &theNewEntry;
00215         }
00216 
00217         // Return a pointer to the beginning of the allocated memory...
00218         return &*theNewEntry.second.begin();
00219     }
00220 
00221     ListEntryType*
00222     findEntry(size_type     theCount)
00223     {
00224         // Search for an entry that has some free space.
00225 
00226         if (m_lastEntryFound != 0 && m_lastEntryFound->first >= theCount)
00227         {
00228             return m_lastEntryFound;
00229         }
00230         else
00231         {
00232             const ListIteratorType  theEnd = m_list.end();
00233             ListIteratorType        theCurrent = m_list.begin();
00234 
00235             ListEntryType*  theEntry = 0;
00236 
00237             while(theCurrent != theEnd)
00238             {
00239                 // We're looking for the best fit, so
00240                 // if the free space and the count we're
00241                 // looking for are equal, that's pretty
00242                 // much the best we can do...
00243                 if ((*theCurrent).first == theCount)
00244                 {
00245                     theEntry = &*theCurrent;
00246 
00247                     break;
00248                 }
00249                 else if ((*theCurrent).first >= theCount)
00250                 {
00251                     // If we haven't found anything yet, the first
00252                     // entry we find that's large enough becomes
00253                     // our best fit.
00254                     //
00255                     // Otherwise, we'll assume that a smaller
00256                     // slot is a better fit, though I may be
00257                     // wrong about this...
00258                     if (theEntry == 0 ||
00259                         (*theCurrent).first < theEntry->first)
00260                     {
00261                         // Nope, so this becomes our best-fit so far.
00262                         theEntry = &*theCurrent;
00263                     }
00264 
00265                     ++theCurrent;
00266                 }
00267                 else
00268                 {
00269                     // Won't fit, so just continue...
00270                     ++theCurrent;
00271                 }
00272             }
00273 
00274             m_lastEntryFound = theEntry;
00275 
00276             return theEntry;
00277         }
00278     }
00279 
00280     // Not implemented...
00281     XalanArrayAllocator(const XalanArrayAllocator<Type>&    theSource);
00282 
00283     XalanArrayAllocator<Type>&
00284     operator=(const XalanArrayAllocator<Type>&  theSource);
00285 
00286     bool
00287     operator==(const XalanArrayAllocator<Type>& theRHS) const;
00288 
00289 
00290     // Data members...
00291     ListType            m_list;
00292 
00293     const size_type     m_blockSize;
00294 
00295     ListEntryType*      m_lastEntryFound;
00296 };
00297 
00298 
00299 
00300 XALAN_CPP_NAMESPACE_END
00301 
00302 
00303 
00304 #endif  // !defined(XALANARRAYALLOCATOR_HEADER_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.5
Copyright © 2000, 2001, 2002, 2003 The Apache Software Foundation. All Rights Reserved.