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(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 template<class Type> 00073 class ArenaDeleteFunctor 00074 { 00075 public: 00076 00077 void 00078 operator()(const Type* theType) const 00079 { 00080 #if defined(XALAN_CANNOT_DELETE_CONST) 00081 delete (Type*)theType; 00082 #else 00083 delete theType; 00084 #endif 00085 } 00086 }; 00087 00088 00089 00090 template<class ObjectType, 00091 #if defined(XALAN_NO_DEFAULT_TEMPLATE_ARGUMENTS) 00092 class ArenaBlockType> 00093 #else 00094 class ArenaBlockType = ArenaBlock<ObjectType> > 00095 #endif 00096 class ArenaAllocator 00097 { 00098 public: 00099 00100 typedef typename ArenaBlockType::size_type size_type; 00101 00102 /* 00103 * Construct an instance that will allocate blocks of the specified size. 00104 * 00105 * @param theBlockSize The block size. 00106 */ 00107 ArenaAllocator(size_type theBlockSize) : 00108 m_blockSize(theBlockSize), 00109 m_blocks() 00110 { 00111 } 00112 00113 virtual 00114 ~ArenaAllocator() 00115 { 00116 reset(); 00117 } 00118 00119 /* 00120 * Get size of an ArenaBlock, that is, the number 00121 * of objects in each block. 00122 * 00123 * @return The size of the block 00124 */ 00125 size_type 00126 getBlockSize() const 00127 { 00128 return m_blockSize; 00129 } 00130 00131 /* 00132 * Set size of an ArenaBlock, that is, the number 00133 * of objects in each block. Only affects blocks 00134 * allocated after the call. 00135 * 00136 * @param theSize The size of the block 00137 */ 00138 void 00139 setBlockSize(size_type theSize) 00140 { 00141 m_blockSize = theSize; 00142 } 00143 00144 /* 00145 * Get the number of ArenaBlocks currently allocated. 00146 * 00147 * @return The number of blocks. 00148 */ 00149 size_type 00150 getBlockCount() const 00151 { 00152 return m_blocks.size(); 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. 00159 * 00160 * @return A pointer to a block of memory 00161 */ 00162 virtual ObjectType* 00163 allocateBlock() 00164 { 00165 if (m_blocks.size() == 0 || 00166 m_blocks.back()->blockAvailable() == false) 00167 { 00168 m_blocks.push_back(new ArenaBlockType(m_blockSize)); 00169 } 00170 assert(m_blocks.size() > 0 && m_blocks.back() != 0 && m_blocks.back()->blockAvailable() == true); 00171 00172 return m_blocks.back()->allocateBlock(); 00173 } 00174 00175 /* 00176 * Commits the allocation of the previous 00177 * allocateBlock() call. 00178 * 00179 * @param theObject A pointer to a block of memory 00180 */ 00181 virtual void 00182 commitAllocation(ObjectType* theObject) 00183 { 00184 assert(m_blocks.size() != 0 && m_blocks.back()->ownsBlock(theObject) == true); 00185 00186 m_blocks.back()->commitAllocation(theObject); 00187 assert(m_blocks.back()->ownsObject(theObject) == true); 00188 } 00189 00190 virtual void 00191 reset() 00192 { 00193 #if !defined(XALAN_NO_NAMESPACES) 00194 using std::for_each; 00195 #endif 00196 00197 for_each(m_blocks.begin(), 00198 m_blocks.end(), 00199 ArenaDeleteFunctor<ArenaBlockType>()); 00200 00201 m_blocks.clear(); 00202 } 00203 00204 protected: 00205 00206 // data members... 00207 #if defined(XALAN_NO_NAMESPACES) 00208 typedef vector<ArenaBlockType*> ArenaBlockListType; 00209 #else 00210 typedef std::vector<ArenaBlockType*> ArenaBlockListType; 00211 #endif 00212 00213 size_type m_blockSize; 00214 00215 ArenaBlockListType m_blocks; 00216 00217 private: 00218 00219 // Not defined... 00220 ArenaAllocator(const ArenaAllocator<ObjectType, ArenaBlockType>&); 00221 00222 ArenaAllocator<ObjectType, ArenaBlockType>& 00223 operator=(const ArenaAllocator<ObjectType, ArenaBlockType>&); 00224 }; 00225 00226 00227 00228 #endif // !defined(ARENAALLOCATOR_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.1 |
|