Xalan-C++ API Documentation

The Xalan-C++ XSL Transformer Version 1.0

Main Page   Namespace List   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 #define XALAN_NO_SELECTIVE_TEMPLATE_INSTANTIATION
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 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     ~ArenaBlock()
00171     {
00172         destroyAll();
00173 
00174         // Release the memory...
00175         m_allocator.deallocate(m_objectBlock, 0);
00176     }
00177 
00178     /*
00179      * Allocate a block.  Once the object is constructed, you must call
00180      * commitAllocation().
00181      *
00182      * @return a pointer to the new block.
00183      */
00184     virtual ObjectType*
00185     allocateBlock()
00186     {
00187         // If no memory has yet been allocated, then allocate it...
00188         if (m_objectBlock == 0)
00189         {
00190             m_objectBlock = m_allocator.allocate(m_blockSize, 0);
00191         }
00192         assert(m_objectBlock != 0);
00193 
00194         // Any space left?
00195         if (m_objectCount == m_blockSize)
00196         {
00197             return 0;
00198         }
00199         else
00200         {
00201             return m_objectBlock + m_objectCount;
00202         }
00203     }
00204 
00205     /*
00206      * Commit the previous allocation.
00207      *
00208      * @param theBlock the address that was returned by allocateBlock()
00209      */
00210     virtual void
00211 #if defined (NDEBUG)
00212     commitAllocation(ObjectType*    /* theBlock */)
00213 #else
00214     commitAllocation(ObjectType*    theBlock)
00215 #endif
00216     {
00217         assert(theBlock == m_objectBlock + m_objectCount);
00218         assert(m_objectCount < m_blockSize);
00219 
00220         m_objectCount++;
00221     }
00222 
00223     /*
00224      * Find out if there is a block available.
00225      *
00226      * @return true if one is available, false if not.
00227      */
00228     virtual bool
00229     blockAvailable() const
00230     {
00231         return m_objectCount < m_blockSize ? true : false;
00232     }
00233 
00234     /*
00235      * Get the number of objects currently allocated in the
00236      * block.
00237      *
00238      * @return The number of objects allocated.
00239      */
00240     virtual size_type
00241     getCountAllocated() const
00242     {
00243         return m_objectCount;
00244     }
00245 
00246     /*
00247      * Get the block size, that is, the number
00248      * of objects in each block.
00249      *
00250      * @return The size of the block
00251      */
00252     size_type
00253     getBlockSize() const
00254     {
00255         return m_blockSize;
00256     }
00257 
00258     /*
00259      * Determine if this block owns the specified object.  Note
00260      * that even if the object address is within our block, this
00261      * call will return false if no object currently occupies the
00262      * block.  See also ownsBlock().
00263      *
00264      * @param theObject the address of the object.
00265      * @return true if we own the object, false if not.
00266      */
00267     virtual bool
00268     ownsObject(const ObjectType*    theObject) const
00269     {
00270 #if !defined(XALAN_NO_NAMESPACES)
00271         using std::less;
00272 #endif
00273 
00274         // Use less<>, since it's guaranteed to do pointer
00275         // comparisons correctly...
00276         less<const ObjectType*>     functor;
00277 
00278         if (functor(theObject, m_objectBlock) == false &&
00279             functor(theObject, m_objectBlock + m_objectCount) == true)
00280         {
00281             return true;
00282         }
00283         else
00284         {
00285             return false;
00286         }
00287     }
00288 
00289     /*
00290      * Determine if this block owns the specified object block.
00291      * Note that, unlike ownsObject(), there does not need to
00292      * be an object at the address.
00293      *
00294      * @param theObject the address of the object
00295      * @return true if we own the object block, false if not.
00296      */
00297     bool
00298     ownsBlock(const ObjectType*     theObject) const
00299     {
00300 #if !defined(XALAN_NO_NAMESPACES)
00301         using std::less;
00302 #endif
00303 
00304         // Use less<>, since it's guaranteed to do pointer
00305         // comparisons correctly...
00306         less<const ObjectType*>     functor;
00307 
00308         if (functor(theObject, m_objectBlock) == false &&
00309             functor(theObject, m_objectBlock + m_blockSize) == true)
00310         {
00311             return true;
00312         }
00313         else
00314         {
00315             return false;
00316         }
00317     }
00318 
00319     /*
00320      * Destroy all objects in the block.  You can then reuse the
00321      * block.
00322      */
00323     void
00324     destroyAll()
00325     {
00326 #if !defined(XALAN_NO_NAMESPACES)
00327         using std::for_each;
00328 #endif
00329 
00330         // Destroy all existing objects...
00331         for_each(m_objectBlock,
00332                  m_objectBlock + m_objectCount,
00333                  DeleteFunctor(*this, m_destroyFunction));
00334 
00335         m_objectCount = 0;
00336     }
00337 
00338 protected:
00339 
00340     /*
00341      * Determine if the block should be destroyed.  Called by
00342      * an instance of DeleteFunctor, this function is for
00343      * deriving classes that might want to control the destruction
00344      * of things.
00345      *
00346      * @param theObject the address of the object
00347      * @return true if block should be destroyed, false if not.
00348      */
00349     virtual bool
00350     shouldDestroyBlock(const ObjectType*    /* theObject */) const
00351     {
00352         return true;
00353     }
00354 
00355     /*
00356      * Determine the offset into the block for the given address.
00357      * Behavior is undefined if the address is not within our
00358      * block
00359      *
00360      * @param theObject the address of the object
00361      * @return the offset
00362      */
00363     size_type
00364     getBlockOffset(const ObjectType*    theObject) const
00365     {
00366         assert(theObject - m_objectBlock < m_blockSize);
00367 
00368         return theObject - m_objectBlock;
00369     }
00370 
00371     /*
00372      * Determine the address within our block of the object
00373      * at the specified offset.
00374      * Behavior is undefined if the offset is greater than the
00375      * block size.
00376      *
00377      * @param theObject the address of the object
00378      * @return the offset
00379      */
00380     ObjectType*
00381     getBlockAddress(size_type   theOffset) const
00382     {
00383         assert(theOffset < m_blockSize);
00384 
00385         return m_objectBlock + theOffset;
00386     }
00387 
00388     struct DeleteFunctor
00389     {
00390         DeleteFunctor(
00391                 const ArenaBlock<ObjectType>&   theArenaBlock,
00392                 const DestroyFunctionType&      theDestroyFunction) :
00393             m_arenaBlock(theArenaBlock),
00394             m_destroyFunction(theDestroyFunction)
00395         {
00396         }
00397 
00398         void
00399         operator()(ObjectType&  theObject) const
00400         {
00401             if (m_arenaBlock.shouldDestroyBlock(&theObject) == true)
00402             {
00403                 m_destroyFunction(theObject);
00404             }
00405         }
00406 
00407     private:
00408 
00409         const ArenaBlock<ObjectType>&   m_arenaBlock;
00410         const DestroyFunctionType&      m_destroyFunction;
00411     };
00412 
00413     friend struct DeleteFunctor;
00414 
00415     const DestroyFunctionType       m_destroyFunction;
00416 
00417 private:
00418 
00419     // Not implemented...
00420     ArenaBlock(const ArenaBlock<ObjectType>&);
00421 
00422     ArenaBlock<ObjectType>&
00423     operator=(const ArenaBlock<ObjectType>&);
00424 
00425     bool
00426     operator==(const ArenaBlock<ObjectType>&) const;
00427 
00428 
00429     // data members...
00430     size_type               m_objectCount;
00431 
00432     const size_type         m_blockSize;
00433 
00434     ObjectType*             m_objectBlock;
00435 
00436     AllocatorType           m_allocator;
00437 };
00438 
00439 
00440 
00441 #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.0
Copyright © 2000 The Apache Software Foundation. All Rights Reserved.