Xalan-C++ API Reference  1.12.0
ArenaAllocator.hpp
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #if !defined(ARENAALLOCATOR_INCLUDE_GUARD_1357924680)
20 #define ARENAALLOCATOR_INCLUDE_GUARD_1357924680
21 
22 
23 
24 #include <algorithm>
25 
26 
27 
30 
31 
32 
33 #include "ArenaBlock.hpp"
34 
35 
36 
37 namespace XALAN_CPP_NAMESPACE {
38 
39 
40 
41 template<class ObjectType,
42 #if defined(XALAN_NO_DEFAULT_TEMPLATE_ARGUMENTS)
43  class ArenaBlockType>
44 #else
45  class ArenaBlockType = ArenaBlock<ObjectType> >
46 #endif
48 {
49 public:
50 
52 
54 
56 
57  /*
58  * Construct an instance that will allocate blocks of the specified size.
59  *
60  * @param theBlockSize The block size.
61  */
63  MemoryManager& theManager,
64  size_type theBlockSize) :
65  m_blockSize(theBlockSize),
66  m_blocks(theManager)
67  {
68  }
69 
70  virtual
72  {
73  reset();
74  }
75 
76  MemoryManager&
78  {
79  return m_blocks.getMemoryManager();
80  }
81 
82  const MemoryManager&
84  {
85  return m_blocks.getMemoryManager();
86  }
87 
88  /*
89  * Get size of an ArenaBlock, that is, the number
90  * of objects in each block.
91  *
92  * @return The size of the block
93  */
94  size_type
95  getBlockSize() const
96  {
97  return m_blockSize;
98  }
99 
100  /*
101  * Set size of an ArenaBlock, that is, the number
102  * of objects in each block. Only affects blocks
103  * allocated after the call.
104  *
105  * @param theSize The size of the block
106  */
107  void
109  {
110  m_blockSize = theSize;
111  }
112 
113  /*
114  * Get the number of ArenaBlocks currently allocated.
115  *
116  * @return The number of blocks.
117  */
118  size_type
120  {
121  return (size_type)m_blocks.size();
122  }
123 
124  /*
125  * Allocate a block of the appropriate size for an
126  * object. Call commitAllocation() when after
127  * the object is successfully constructed.
128  *
129  * @return A pointer to a block of memory
130  */
131  virtual ObjectType*
133  {
134  if (m_blocks.empty() == true ||
135  m_blocks.back()->blockAvailable() == false)
136  {
137  m_blocks.push_back(
138  ArenaBlockType::create(
139  getMemoryManager(),
140  m_blockSize));
141  }
142  assert(
143  m_blocks.empty() == false &&
144  m_blocks.back() != 0 &&
145  m_blocks.back()->blockAvailable() == true);
146 
147  return m_blocks.back()->allocateBlock();
148  }
149 
150  /*
151  * Commits the allocation of the previous
152  * allocateBlock() call.
153  *
154  * @param theObject A pointer to a block of memory
155  */
156  virtual void
157  commitAllocation(ObjectType* theObject)
158  {
159  assert(
160  m_blocks.empty() == false &&
161  m_blocks.back()->ownsBlock(theObject) == true);
162 
163  m_blocks.back()->commitAllocation(theObject);
164 
165  assert(m_blocks.back()->ownsObject(theObject) == true);
166  }
167 
168  virtual bool
169  ownsObject(const ObjectType* theObject) const
170  {
171  bool fResult = false;
172 
173  typedef typename ArenaBlockListType::const_reverse_iterator const_reverse_iterator;
174 
175  // Search back for a block that may have allocated the object...
176  const const_reverse_iterator theEnd = this->m_blocks.rend();
177 
178  const_reverse_iterator i = this->m_blocks.rbegin();
179 
180  while(i != theEnd)
181  {
182  assert(*i != 0);
183 
184  if ((*i)->ownsObject(theObject) == true)
185  {
186  fResult = true;
187 
188  break;
189  }
190  else
191  {
192  ++i;
193  }
194  }
195 
196  return fResult;
197  }
198 
199  virtual void
201  {
202  std::for_each(
203  m_blocks.begin(),
204  m_blocks.end(),
205  DeleteFunctor<ArenaBlockType>(m_blocks.getMemoryManager()));
206 
207  m_blocks.clear();
208  }
209 
210 protected:
211 
212  // data members...
214 
216 
217 private:
218 
219  // Not defined...
221 
224 
225  bool
227 };
228 
229 
230 
231 }
232 
233 
234 
235 #endif // !defined(ARENAALLOCATOR_INCLUDE_GUARD_1357924680)
xalanc::ArenaAllocator::size_type
ArenaBlockType::size_type size_type
Definition: ArenaAllocator.hpp:55
XALAN_CPP_NAMESPACE
#define XALAN_CPP_NAMESPACE
Xalan-C++ namespace, including major and minor version.
Definition: XalanVersion.hpp:76
xalanc::ArenaAllocator::getBlockCount
size_type getBlockCount() const
Definition: ArenaAllocator.hpp:119
xalanc::ArenaAllocator::~ArenaAllocator
virtual ~ArenaAllocator()
Definition: ArenaAllocator.hpp:71
XalanList.hpp
xalanc::size_type
size_t size_type
Definition: XalanMap.hpp:46
xalanc::ArenaAllocator::setBlockSize
void setBlockSize(size_type theSize)
Definition: ArenaAllocator.hpp:108
xalanc::ArenaAllocator::m_blocks
ArenaBlockListType m_blocks
Definition: ArenaAllocator.hpp:215
xalanc::ArenaAllocator::ArenaAllocator
ArenaAllocator(MemoryManager &theManager, size_type theBlockSize)
Definition: ArenaAllocator.hpp:62
xalanc::operator==
bool operator==(const XalanVector< Type > &theLHS, const XalanVector< Type > &theRHS)
Definition: XalanVector.hpp:1118
STLHelper.hpp
xalanc::ArenaAllocator::getMemoryManager
MemoryManager & getMemoryManager()
Definition: ArenaAllocator.hpp:77
xalanc::ArenaAllocator
Definition: ArenaAllocator.hpp:47
xalanc::XalanList< ReusableArenaBlock< ObjectType > * >::const_reverse_iterator
const_reverse_iterator_ const_reverse_iterator
Definition: XalanList.hpp:193
xalanc::ArenaAllocator::commitAllocation
virtual void commitAllocation(ObjectType *theObject)
Definition: ArenaAllocator.hpp:157
xalanc::ArenaAllocator::ArenaBlockListType
XalanList< ArenaBlockType * > ArenaBlockListType
Definition: ArenaAllocator.hpp:53
xalanc::ArenaAllocator::ThisType
ArenaAllocator< ObjectType, ArenaBlockType > ThisType
Definition: ArenaAllocator.hpp:51
xalanc::ArenaAllocator::getBlockSize
size_type getBlockSize() const
Definition: ArenaAllocator.hpp:95
xalanc::DeleteFunctor
Functor to delete objects, used in STL iteration algorithms.
Definition: STLHelper.hpp:100
ArenaBlock.hpp
xalanc::ArenaAllocator::reset
virtual void reset()
Definition: ArenaAllocator.hpp:200
xalanc::ArenaAllocator::m_blockSize
size_type m_blockSize
Definition: ArenaAllocator.hpp:213
xalanc::ArenaAllocator::allocateBlock
virtual ObjectType * allocateBlock()
Definition: ArenaAllocator.hpp:132
xalanc::ArenaAllocator::ownsObject
virtual bool ownsObject(const ObjectType *theObject) const
Definition: ArenaAllocator.hpp:169
xalanc::ArenaAllocator::getMemoryManager
const MemoryManager & getMemoryManager() const
Definition: ArenaAllocator.hpp:83
xalanc::XalanList
Xalan implementation of a doubly linked list.
Definition: XalanList.hpp:155