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  

ArenaBlock.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(ARENABLOCK_INCLUDE_GUARD_1357924680)
00059 #define ARENABLOCK_INCLUDE_GUARD_1357924680
00060 
00061 
00062 
00063 #include <algorithm>
00064 #include <cassert>
00065 #include <set>
00066 #include <memory>
00067 
00068 
00069 
00070 #if defined(XALAN_NO_STD_ALLOCATORS) && !defined(XALAN_NO_SELECTIVE_TEMPLATE_INSTANTIATION)
00071 #include <PlatformSupport/XalanAllocator.hpp>
00072 #endif
00073 
00074 
00075 
00076 #if defined(XALAN_NO_SELECTIVE_TEMPLATE_INSTANTIATION)
00077 
00078 template <class Type>
00079 class ArenaBlockAllocator
00080 {
00081 public:
00082 
00083     typedef size_t          size_type;
00084     typedef ptrdiff_t       difference_type;
00085     typedef Type*           pointer;
00086     typedef const Type*     const_pointer;
00087     typedef Type&           reference;
00088     typedef const Type&     const_reference;
00089     typedef Type            value_type;
00090 
00091     ArenaBlockAllocator()
00092     {
00093     }
00094 
00095     ArenaBlockAllocator(const ArenaBlockAllocator<Type>&)
00096     {
00097     };
00098 
00099     ~ArenaBlockAllocator()
00100     {
00101     }
00102 
00103     pointer
00104     allocate(
00105             size_type       size,
00106             const void*     /* hint */ = 0)
00107     {
00108         return (pointer)operator new(size * sizeof(Type));
00109     }
00110 
00111     void
00112     deallocate(
00113                 pointer     p,
00114                 size_type   /* n */)
00115     {
00116         operator delete(p);
00117     }
00118 };
00119 #endif
00120 
00121 
00122 
00123 template<class ObjectType>
00124 class ArenaBlockDestroy
00125 {
00126 public:
00127 
00128     void
00129     operator()(ObjectType&  theObject) const
00130     {
00131         theObject.ObjectType::~ObjectType();
00132     }
00133 };
00134 
00135 
00136 
00137 template<class ObjectType>
00138 class ArenaBlock
00139 {
00140 public:
00141 
00142 #if defined(XALAN_NO_SELECTIVE_TEMPLATE_INSTANTIATION)
00143     typedef ArenaBlockAllocator<ObjectType>     AllocatorType;
00144 #elif defined(XALAN_NO_STD_ALLOCATORS)
00145     typedef XalanAllocator<ObjectType>          AllocatorType;
00146 #else
00147     typedef std::allocator<ObjectType>          AllocatorType;
00148 #endif
00149 
00150     typedef ArenaBlockDestroy<ObjectType>       DestroyFunctionType;
00151 
00152     typedef typename AllocatorType::size_type   size_type;
00153 
00154     /*
00155      * Construct an ArenaBlock of the specified size
00156      * of objects.
00157      *
00158      * @param theBlockSize The size of the block (the number of objects it can contain).
00159      */
00160     ArenaBlock(size_type    theBlockSize) :
00161         m_destroyFunction(DestroyFunctionType()),
00162         m_objectCount(0),
00163         m_blockSize(theBlockSize),
00164         m_objectBlock(0),
00165         m_allocator()
00166     {
00167         assert(theBlockSize > 0);
00168     }
00169 
00170     virtual 
00171     ~ArenaBlock()
00172     {
00173         destroyAll();
00174 
00175         // Release the memory...
00176         m_allocator.deallocate(m_objectBlock, 0);
00177     }
00178 
00179     /*
00180      * Allocate a block.  Once the object is constructed, you must call
00181      * commitAllocation().
00182      *
00183      * @return a pointer to the new block.
00184      */
00185     virtual ObjectType*
00186     allocateBlock()
00187     {
00188         // Any space left?
00189         if (m_objectCount == m_blockSize)
00190         {
00191             return 0;
00192         }
00193         else
00194         {
00195             // If no memory has yet been allocated, then allocate it...
00196             if (m_objectBlock == 0)
00197             {
00198                 m_objectBlock = m_allocator.allocate(m_blockSize, 0);
00199             }
00200             assert(m_objectBlock != 0);
00201 
00202             return m_objectBlock + m_objectCount;
00203         }
00204     }
00205 
00206     /*
00207      * Commit the previous allocation.
00208      *
00209      * @param theBlock the address that was returned by allocateBlock()
00210      */
00211     virtual void
00212 #if defined (NDEBUG)
00213     commitAllocation(ObjectType*    /* theBlock */)
00214 #else
00215     commitAllocation(ObjectType*    theBlock)
00216 #endif
00217     {
00218         assert(theBlock == m_objectBlock + m_objectCount);
00219         assert(m_objectCount < m_blockSize);
00220 
00221         m_objectCount++;
00222     }
00223 
00224     /*
00225      * Find out if there is a block available.
00226      *
00227      * @return true if one is available, false if not.
00228      */
00229     virtual bool
00230     blockAvailable() const
00231     {
00232         return m_objectCount < m_blockSize ? true : false;
00233     }
00234 
00235     /*
00236      * Get the number of objects currently allocated in the
00237      * block.
00238      *
00239      * @return The number of objects allocated.
00240      */
00241     virtual size_type
00242     getCountAllocated() const
00243     {
00244         return m_objectCount;
00245     }
00246 
00247     /*
00248      * Get the block size, that is, the number
00249      * of objects in each block.
00250      *
00251      * @return The size of the block
00252      */
00253     size_type
00254     getBlockSize() const
00255     {
00256         return m_blockSize;
00257     }
00258 
00259     /*
00260      * Determine if this block owns the specified object.  Note
00261      * that even if the object address is within our block, this
00262      * call will return false if no object currently occupies the
00263      * block.  See also ownsBlock().
00264      *
00265      * @param theObject the address of the object.
00266      * @return true if we own the object, false if not.
00267      */
00268     virtual bool
00269     ownsObject(const ObjectType*    theObject) const
00270     {
00271 #if !defined(XALAN_NO_NAMESPACES)
00272         using std::less;
00273 #endif
00274 
00275         // Use less<>, since it's guaranteed to do pointer
00276         // comparisons correctly...
00277         less<const ObjectType*>     functor;
00278 
00279         if (functor(theObject, m_objectBlock) == false &&
00280             functor(theObject, m_objectBlock + m_objectCount) == true)
00281         {
00282             return true;
00283         }
00284         else
00285         {
00286             return false;
00287         }
00288     }
00289 
00290     /*
00291      * Determine if this block owns the specified object block.
00292      * Note that, unlike ownsObject(), there does not need to
00293      * be an object at the address.
00294      *
00295      * @param theObject the address of the object
00296      * @return true if we own the object block, false if not.
00297      */
00298     bool
00299     ownsBlock(const ObjectType*     theObject) const
00300     {
00301 #if !defined(XALAN_NO_NAMESPACES)
00302         using std::less;
00303 #endif
00304 
00305         // Use less<>, since it's guaranteed to do pointer
00306         // comparisons correctly...
00307         less<const ObjectType*>     functor;
00308 
00309         if (functor(theObject, m_objectBlock) == false &&
00310             functor(theObject, m_objectBlock + m_blockSize) == true)
00311         {
00312             return true;
00313         }
00314         else
00315         {
00316             return false;
00317         }
00318     }
00319 
00320     /*
00321      * Destroy all objects in the block.  You can then reuse the
00322      * block.
00323      */
00324     void
00325     destroyAll()
00326     {
00327 #if !defined(XALAN_NO_NAMESPACES)
00328         using std::for_each;
00329 #endif
00330 
00331         // Destroy all existing objects...
00332         for_each(m_objectBlock,
00333                  m_objectBlock + m_objectCount,
00334                  DeleteFunctor(*this, m_destroyFunction));
00335 
00336         m_objectCount = 0;
00337     }
00338 
00339 protected:
00340 
00341     /*
00342      * Determine if the block should be destroyed.  Called by
00343      * an instance of DeleteFunctor, this function is for
00344      * deriving classes that might want to control the destruction
00345      * of things.
00346      *
00347      * @param theObject the address of the object
00348      * @return true if block should be destroyed, false if not.
00349      */
00350     virtual bool
00351     shouldDestroyBlock(const ObjectType*    /* theObject */) const
00352     {
00353         return true;
00354     }
00355 
00356     /*
00357      * Determine the offset into the block for the given address.
00358      * Behavior is undefined if the address is not within our
00359      * block
00360      *
00361      * @param theObject the address of the object
00362      * @return the offset
00363      */
00364     size_type
00365     getBlockOffset(const ObjectType*    theObject) const
00366     {
00367         assert(size_type(theObject - m_objectBlock) < m_blockSize);
00368 
00369         return theObject - m_objectBlock;
00370     }
00371 
00372     /*
00373      * Determine the address within our block of the object
00374      * at the specified offset.
00375      * Behavior is undefined if the offset is greater than the
00376      * block size.
00377      *
00378      * @param theObject the address of the object
00379      * @return the offset
00380      */
00381     ObjectType*
00382     getBlockAddress(size_type   theOffset) const
00383     {
00384         assert(theOffset < m_blockSize);
00385 
00386         return m_objectBlock + theOffset;
00387     }
00388 
00389     struct DeleteFunctor
00390     {
00391         DeleteFunctor(
00392                 const ArenaBlock<ObjectType>&   theArenaBlock,
00393                 const DestroyFunctionType&      theDestroyFunction) :
00394             m_arenaBlock(theArenaBlock),
00395             m_destroyFunction(theDestroyFunction)
00396         {
00397         }
00398 
00399         void
00400         operator()(ObjectType&  theObject) const
00401         {
00402             if (m_arenaBlock.shouldDestroyBlock(&theObject) == true)
00403             {
00404                 m_destroyFunction(theObject);
00405             }
00406         }
00407 
00408     private:
00409 
00410         const ArenaBlock<ObjectType>&   m_arenaBlock;
00411         const DestroyFunctionType&      m_destroyFunction;
00412     };
00413 
00414     friend struct DeleteFunctor;
00415 
00416     const DestroyFunctionType   m_destroyFunction;
00417 
00418 private:
00419 
00420     // Not implemented...
00421     ArenaBlock(const ArenaBlock<ObjectType>&);
00422 
00423     ArenaBlock<ObjectType>&
00424     operator=(const ArenaBlock<ObjectType>&);
00425 
00426     bool
00427     operator==(const ArenaBlock<ObjectType>&) const;
00428 
00429 
00430     // data members...
00431     size_type           m_objectCount;
00432 
00433     const size_type     m_blockSize;
00434 
00435     ObjectType*         m_objectBlock;
00436 
00437     AllocatorType       m_allocator;
00438 };
00439 
00440 
00441 
00442 #endif  // !defined(ARENABLOCK_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.