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 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 const ArenaBlockListType::reverse_iterator theEnd = m_blocks.rend(); 00130 00131 ArenaBlockListType::reverse_iterator i = m_blocks.rbegin(); 00132 00133 while(i != theEnd) 00134 { 00135 if ((*i)->ownsObject(theObject) == true) 00136 { 00137 m_lastBlockReferenced = *i; 00138 00139 m_lastBlockReferenced->destroyObject(theObject); 00140 00141 fSucess = true; 00142 00143 break; 00144 } 00145 else 00146 { 00147 ++i; 00148 } 00149 } 00150 } 00151 00152 return fSucess; 00153 } 00154 00155 /* 00156 * Allocate a block of the appropriate size for an 00157 * object. Call commitAllocation() when after 00158 * the object is successfully constructed. You _must_ 00159 * commit an allocation before performing any other 00160 * operation on the allocator. 00161 * 00162 * @return A pointer to a block of memory 00163 */ 00164 virtual ObjectType* 00165 allocateBlock() 00166 { 00167 if (m_lastBlockReferenced == 0 || 00168 m_lastBlockReferenced->blockAvailable() == false) 00169 { 00170 // Search back for a block with some space available... 00171 const ArenaBlockListType::reverse_iterator theEnd = m_blocks.rend(); 00172 00173 ArenaBlockListType::reverse_iterator i = m_blocks.rbegin(); 00174 00175 while(i != theEnd) 00176 { 00177 assert(*i != 0); 00178 00179 if (*i != m_lastBlockReferenced && (*i)->blockAvailable() == true) 00180 { 00181 // Ahh, found one with free space. 00182 m_lastBlockReferenced = *i; 00183 00184 break; 00185 } 00186 else 00187 { 00188 ++i; 00189 } 00190 } 00191 00192 if (i == theEnd) 00193 { 00194 // No blocks have free space available, so create a new block, and 00195 // push it on the list. 00196 m_lastBlockReferenced = new ReusableArenaBlockType(m_blockSize); 00197 00198 m_blocks.push_back(m_lastBlockReferenced); 00199 } 00200 } 00201 assert(m_lastBlockReferenced != 0 && m_lastBlockReferenced->blockAvailable() == true); 00202 00203 return m_lastBlockReferenced->allocateBlock(); 00204 } 00205 00206 /* 00207 * Commits the allocation of the previous 00208 * allocateBlock() call. 00209 * 00210 * @param theObject A pointer to a block of memory 00211 */ 00212 virtual void 00213 commitAllocation(ObjectType* theObject) 00214 { 00215 assert(m_blocks.size() != 0 && m_lastBlockReferenced != 0 && m_lastBlockReferenced->ownsBlock(theObject) == true); 00216 00217 m_lastBlockReferenced->commitAllocation(theObject); 00218 assert(m_lastBlockReferenced->ownsObject(theObject) == true); 00219 } 00220 00221 virtual bool 00222 ownsObject(const ObjectType* theObject) const 00223 { 00224 bool fResult = false; 00225 00226 // If no block has ever been referenced, then we haven't allocated 00227 // any objects. 00228 if (m_lastBlockReferenced != 0) 00229 { 00230 // Check the last referenced block first. 00231 fResult = m_lastBlockReferenced->ownsObject(theObject); 00232 00233 if (fResult == false) 00234 { 00235 // Search back for a block with some space available... 00236 const ArenaBlockListType::const_reverse_iterator theEnd = m_blocks.rend(); 00237 00238 ArenaBlockListType::const_reverse_iterator i = m_blocks.rbegin(); 00239 00240 while(i != theEnd) 00241 { 00242 assert(*i != 0); 00243 00244 if ((*i)->ownsObject(theObject) == true) 00245 { 00246 fResult = true; 00247 00248 break; 00249 } 00250 else 00251 { 00252 ++i; 00253 } 00254 } 00255 } 00256 } 00257 00258 return fResult; 00259 } 00260 00261 private: 00262 00263 // Not defined... 00264 ReusableArenaAllocator(const ReusableArenaAllocator<ObjectType>&); 00265 00266 ReusableArenaAllocator<ObjectType>& 00267 operator=(const ReusableArenaAllocator<ObjectType>&); 00268 00269 // Data members... 00270 ReusableArenaBlockType* m_lastBlockReferenced; 00271 }; 00272 00273 00274 00275 #endif // !defined(REUSABLEARENAALLOCATOR_INCLUDE_GUARD_1357924680)
Doxygen and GraphViz are used to generate this API documentation from the Xalan-C header files.
![]() |
Xalan-C++ XSL Transformer Version 1.0 |
|