00001 /* 00002 * The Apache Software License, Version 1.1 00003 * 00004 * 00005 * Copyright (c) 2000-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 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 <xalanc/PlatformSupport/XalanAllocator.hpp> 00072 #endif 00073 00074 00075 00076 XALAN_CPP_NAMESPACE_BEGIN 00077 00078 00079 00080 #if defined(XALAN_NO_SELECTIVE_TEMPLATE_INSTANTIATION) 00081 00082 template <class Type> 00083 class ArenaBlockAllocator 00084 { 00085 public: 00086 00087 typedef size_t size_type; 00088 typedef ptrdiff_t difference_type; 00089 typedef Type* pointer; 00090 typedef const Type* const_pointer; 00091 typedef Type& reference; 00092 typedef const Type& const_reference; 00093 typedef Type value_type; 00094 00095 ArenaBlockAllocator() 00096 { 00097 } 00098 00099 ArenaBlockAllocator(const ArenaBlockAllocator<Type>&) 00100 { 00101 }; 00102 00103 ~ArenaBlockAllocator() 00104 { 00105 } 00106 00107 pointer 00108 allocate( 00109 size_type size, 00110 const void* /* hint */ = 0) 00111 { 00112 return (pointer)operator new(size * sizeof(Type)); 00113 } 00114 00115 void 00116 deallocate( 00117 pointer p, 00118 size_type /* n */) 00119 { 00120 operator delete(p); 00121 } 00122 }; 00123 #endif 00124 00125 00126 00127 template<class ObjectType> 00128 class ArenaBlockDestroy 00129 { 00130 public: 00131 00132 void 00133 operator()(ObjectType& theObject) const 00134 { 00135 #if defined(XALAN_EXPLICIT_SCOPE_IN_TEMPLATE_BUG) 00136 theObject.~ObjectType(); 00137 #else 00138 theObject.ObjectType::~ObjectType(); 00139 #endif 00140 } 00141 }; 00142 00143 00144 00145 template<class ObjectType> 00146 class ArenaBlock 00147 { 00148 public: 00149 00150 #if defined(XALAN_NO_SELECTIVE_TEMPLATE_INSTANTIATION) 00151 typedef ArenaBlockAllocator<ObjectType> AllocatorType; 00152 #elif defined(XALAN_NO_STD_ALLOCATORS) 00153 typedef XalanAllocator<ObjectType> AllocatorType; 00154 #else 00155 typedef std::allocator<ObjectType> AllocatorType; 00156 #endif 00157 00158 typedef ArenaBlockDestroy<ObjectType> DestroyFunctionType; 00159 00160 typedef typename AllocatorType::size_type size_type; 00161 00162 /* 00163 * Construct an ArenaBlock of the specified size 00164 * of objects. 00165 * 00166 * @param theBlockSize The size of the block (the number of objects it can contain). 00167 */ 00168 ArenaBlock(size_type theBlockSize) : 00169 m_destroyFunction(DestroyFunctionType()), 00170 m_objectCount(0), 00171 m_blockSize(theBlockSize), 00172 m_objectBlock(0), 00173 m_allocator() 00174 { 00175 assert(theBlockSize > 0); 00176 } 00177 00178 virtual 00179 ~ArenaBlock() 00180 { 00181 destroyAll(); 00182 00183 // Release the memory... 00184 m_allocator.deallocate(m_objectBlock, m_blockSize); 00185 } 00186 00187 /* 00188 * Allocate a block. Once the object is constructed, you must call 00189 * commitAllocation(). 00190 * 00191 * @return a pointer to the new block. 00192 */ 00193 virtual ObjectType* 00194 allocateBlock() 00195 { 00196 // Any space left? 00197 if (m_objectCount == m_blockSize) 00198 { 00199 return 0; 00200 } 00201 else 00202 { 00203 // If no memory has yet been allocated, then allocate it... 00204 if (m_objectBlock == 0) 00205 { 00206 #if defined(XALAN_NEW_STD_ALLOCATOR) 00207 m_objectBlock = m_allocator.allocate(m_blockSize); 00208 #else 00209 m_objectBlock = m_allocator.allocate(m_blockSize, 0); 00210 #endif 00211 } 00212 assert(m_objectBlock != 0); 00213 00214 return m_objectBlock + m_objectCount; 00215 } 00216 } 00217 00218 /* 00219 * Commit the previous allocation. 00220 * 00221 * @param theBlock the address that was returned by allocateBlock() 00222 */ 00223 virtual void 00224 #if defined (NDEBUG) 00225 commitAllocation(ObjectType* /* theBlock */) 00226 #else 00227 commitAllocation(ObjectType* theBlock) 00228 #endif 00229 { 00230 assert(theBlock == m_objectBlock + m_objectCount); 00231 assert(m_objectCount < m_blockSize); 00232 00233 m_objectCount++; 00234 } 00235 00236 /* 00237 * Find out if there is a block available. 00238 * 00239 * @return true if one is available, false if not. 00240 */ 00241 virtual bool 00242 blockAvailable() const 00243 { 00244 return m_objectCount < m_blockSize ? true : false; 00245 } 00246 00247 /* 00248 * Get the number of objects currently allocated in the 00249 * block. 00250 * 00251 * @return The number of objects allocated. 00252 */ 00253 virtual size_type 00254 getCountAllocated() const 00255 { 00256 return m_objectCount; 00257 } 00258 00259 /* 00260 * Get the block size, that is, the number 00261 * of objects in each block. 00262 * 00263 * @return The size of the block 00264 */ 00265 size_type 00266 getBlockSize() const 00267 { 00268 return m_blockSize; 00269 } 00270 00271 /* 00272 * Determine if this block owns the specified object. Note 00273 * that even if the object address is within our block, this 00274 * call will return false if no object currently occupies the 00275 * block. See also ownsBlock(). 00276 * 00277 * @param theObject the address of the object. 00278 * @return true if we own the object, false if not. 00279 */ 00280 virtual bool 00281 ownsObject(const ObjectType* theObject) const 00282 { 00283 // Use less<>, since it's guaranteed to do pointer 00284 // comparisons correctly... 00285 XALAN_STD_QUALIFIER less<const ObjectType*> functor; 00286 00287 if (functor(theObject, m_objectBlock) == false && 00288 functor(theObject, m_objectBlock + m_objectCount) == true) 00289 { 00290 return true; 00291 } 00292 else 00293 { 00294 return false; 00295 } 00296 } 00297 00298 /* 00299 * Determine if this block owns the specified object block. 00300 * Note that, unlike ownsObject(), there does not need to 00301 * be an object at the address. 00302 * 00303 * @param theObject the address of the object 00304 * @return true if we own the object block, false if not. 00305 */ 00306 bool 00307 ownsBlock(const ObjectType* theObject) const 00308 { 00309 // Use less<>, since it's guaranteed to do pointer 00310 // comparisons correctly... 00311 XALAN_STD_QUALIFIER less<const ObjectType*> functor; 00312 00313 if (functor(theObject, m_objectBlock) == false && 00314 functor(theObject, m_objectBlock + m_blockSize) == true) 00315 { 00316 return true; 00317 } 00318 else 00319 { 00320 return false; 00321 } 00322 } 00323 00324 /* 00325 * Destroy all objects in the block. You can then reuse the 00326 * block. 00327 */ 00328 void 00329 destroyAll() 00330 { 00331 // Destroy all existing objects... 00332 XALAN_STD_QUALIFIER 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 XALAN_CPP_NAMESPACE_END 00443 00444 00445 00446 #endif // !defined(ARENABLOCK_INCLUDE_GUARD_1357924680)
Doxygen and GraphViz are used to generate this API documentation from the Xalan-C header files.
![]() |
Xalan-C++ XSLT Processor Version 1.6 |
|