Xalan-C++ API Reference  1.12.0
ArenaBlockBase.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(ARENABLOCKBASE_INCLUDE_GUARD_1357924680)
20 #define ARENABLOCKBASE_INCLUDE_GUARD_1357924680
21 
22 
23 #include <cassert>
24 #include <functional>
25 //#include <memory>
26 
28 
29 
30 #if !defined(XALAN_NO_SELECTIVE_TEMPLATE_INSTANTIATION)
32 #endif
33 
34 
35 namespace XALAN_CPP_NAMESPACE {
36 
37 
38 #if defined(XALAN_NO_SELECTIVE_TEMPLATE_INSTANTIATION)
39 
40 template <class Type>
41 class ArenaBlockAllocator
42 {
43 public:
44 
45  typedef typename T size_type;
46  typedef ptrdiff_t difference_type;
47  typedef Type* pointer;
48  typedef const Type* const_pointer;
49  typedef Type& reference;
50  typedef const Type& const_reference;
51  typedef Type value_type;
52 
53  ArenaBlockAllocator(MemoryManager& theManager) :
54  m_memoryManager(theManager)
55  {
56  }
57 
58  ~ArenaBlockAllocator()
59  {
60  }
61 
62  MemoryManager&
63  getMemoryManager()
64  {
65  return m_memoryManager;
66  }
67 
68  pointer
69  allocate(
70  size_type size,
71  const void* /* hint */ = 0)
72  {
73  return (pointer)m_memoryManager.allocate(size * sizeof(Type));
74  }
75 
76  void
77  deallocate(
78  pointer p,
79  size_type /* n */)
80  {
81  if(p != 0)
82  {
83  m_memoryManager.deallocate(p);
84  }
85  }
86 
87 private:
88 
89  // not defined
90  ArenaBlockAllocator(const ArenaBlockAllocator<Type>&);
91 
92  ArenaBlockAllocator<Type>&
93  operator=(const ArenaBlockAllocator<Type>&);
94 
95  MemoryManager& m_memoryManager;
96 };
97 #endif
98 
99 
100 
101 template<class ObjectType,
102 #if defined(XALAN_NO_DEFAULT_TEMPLATE_ARGUMENTS)
103  class SizeType>
104 #else
105  class SizeType = size_t>
106 #endif
108 {
109 public:
110 
112 
113 #if defined(XALAN_NO_SELECTIVE_TEMPLATE_INSTANTIATION)
114  typedef ArenaBlockAllocator<ObjectType> AllocatorType;
115 #else
117 #endif
118 
119  typedef SizeType size_type;
120 
121  MemoryManager&
123  {
124  return m_allocator.getMemoryManager();
125  }
126 
127  /*
128  * Find out if there is a block available.
129  *
130  * @return true if one is available, false if not.
131  */
132  bool
134  {
135  return m_objectCount < m_blockSize ? true : false;
136  }
137 
138  /*
139  * Find out if there are any block is allocated
140  *
141  * @return true if one is available, false if not.
142  */
143  bool
144  isEmpty() const
145  {
146  return m_objectCount == 0 ? true : false;
147  }
148 
149  /*
150  * Get the number of objects currently allocated in the
151  * block.
152  *
153  * @return The number of objects allocated.
154  */
155  size_type
157  {
158  return m_objectCount;
159  }
160 
161  /*
162  * Get the block size, that is, the number
163  * of objects in each block.
164  *
165  * @return The size of the block
166  */
167  size_type
168  getBlockSize() const
169  {
170  return m_blockSize;
171  }
172 
173  /*
174  * Determine if this block owns the specified object block.
175  * Note that, unlike ownsObject(), there does not need to
176  * be an object at the address.
177  *
178  * @param theObject the address of the object
179  * @return true if we own the object block, false if not.
180  */
181  bool
182  ownsBlock(const ObjectType* theObject) const
183  {
184  return isInBorders(theObject, m_blockSize);
185  }
186 
187 protected:
188 
190  MemoryManager& theManager,
191  size_type theBlockSize) :
192  m_allocator(theManager),
193  m_objectCount(0),
194  m_blockSize(theBlockSize),
195  m_objectBlock(m_allocator.allocate(m_blockSize))
196  {
197  assert(theBlockSize > 0);
198 
199  assert(m_objectBlock != 0);
200  }
201 
203  {
204  // Release the memory...
205  m_allocator.deallocate(m_objectBlock, m_blockSize);
206 
207  }
208 
209  /*
210  * Determine if this block is located between beginning of the array
211  * and the "rightBorder" array member (not included)
212  * @param theObject the address of the object
213  * rightBorder the right
214  * @return true if we own the object block, false if not.
215  */
216  bool
218  const ObjectType* theObject,
219  size_type rightBoundary) const
220  {
221  if ( rightBoundary > m_blockSize )
222  {
223  rightBoundary = m_blockSize;
224  }
225 
226  // Use less<>, since it's guaranteed to do pointer
227  // comparisons correctly...
228  std::less<const ObjectType*> functor;
229 
230  if (functor(theObject, m_objectBlock) == false &&
231  functor(theObject, m_objectBlock + rightBoundary) == true)
232  {
233  return true;
234  }
235  else
236  {
237  return false;
238  }
239  }
240 
241  /*
242  * Determine the offset into the block for the given address.
243  * Behavior is undefined if the address is not within our
244  * block
245  *
246  * @param theObject the address of the object
247  * @return the offset
248  */
249  size_type
250  getBlockOffset(const ObjectType* theObject) const
251  {
252  assert(size_type( (theObject - m_objectBlock) / sizeof(ObjectType) ) < m_blockSize);
253 
254  return theObject - m_objectBlock;
255  }
256 
257  /*
258  * Determine the address within our block of the object
259  * at the specified offset.
260  * Behavior is undefined if the offset is greater than the
261  * block size.
262  *
263  * @param theObject the address of the object
264  * @return the offset
265  */
266  ObjectType*
267  getBlockAddress(size_type theOffset) const
268  {
269  assert(theOffset < m_blockSize);
270 
271  return m_objectBlock + theOffset;
272  }
273 
274  // data members...
276 
278 
280 
281  ObjectType* m_objectBlock;
282 
283 private:
284 
285  // Not implemented...
286  ArenaBlockBase(const ThisType&);
287 
288  ThisType&
289  operator=(const ThisType&);
290 
291  bool
292  operator==(const ThisType&) const;
293 };
294 
295 }
296 
297 
298 
299 #endif // !defined(ARENABLOCKBASE_INCLUDE_GUARD_1357924680)
xalanc::ArenaBlockBase::m_objectCount
size_type m_objectCount
Definition: ArenaBlockBase.hpp:277
xalanc::ArenaBlockBase::m_allocator
AllocatorType m_allocator
Definition: ArenaBlockBase.hpp:275
XalanAllocator.hpp
XALAN_CPP_NAMESPACE
#define XALAN_CPP_NAMESPACE
Xalan-C++ namespace, including major and minor version.
Definition: XalanVersion.hpp:76
xalanc::ArenaBlockBase::isEmpty
bool isEmpty() const
Definition: ArenaBlockBase.hpp:144
xalanc::XalanAllocator< string_type >
xalanc::ArenaBlockBase::ownsBlock
bool ownsBlock(const ObjectType *theObject) const
Definition: ArenaBlockBase.hpp:182
xalanc::size_type
size_t size_type
Definition: XalanMap.hpp:46
xalanc::ArenaBlockBase::isInBorders
bool isInBorders(const ObjectType *theObject, size_type rightBoundary) const
Definition: ArenaBlockBase.hpp:217
xalanc::ArenaBlockBase::AllocatorType
XalanAllocator< ObjectType > AllocatorType
Definition: ArenaBlockBase.hpp:116
xalanc::operator==
bool operator==(const XalanVector< Type > &theLHS, const XalanVector< Type > &theRHS)
Definition: XalanVector.hpp:1118
xalanc::ArenaBlockBase::getMemoryManager
MemoryManager & getMemoryManager()
Definition: ArenaBlockBase.hpp:122
XalanMemoryManagement.hpp
xalanc::ArenaBlockBase
Definition: ArenaBlockBase.hpp:107
xalanc::ArenaBlockBase::m_objectBlock
ObjectType * m_objectBlock
Definition: ArenaBlockBase.hpp:281
xalanc::ArenaBlockBase::getBlockSize
size_type getBlockSize() const
Definition: ArenaBlockBase.hpp:168
xalanc::ArenaBlockBase::~ArenaBlockBase
~ArenaBlockBase()
Definition: ArenaBlockBase.hpp:202
xalanc::ArenaBlockBase::getCountAllocated
size_type getCountAllocated() const
Definition: ArenaBlockBase.hpp:156
xalanc::ArenaBlockBase::ArenaBlockBase
ArenaBlockBase(MemoryManager &theManager, size_type theBlockSize)
Definition: ArenaBlockBase.hpp:189
xalanc::ArenaBlockBase::getBlockAddress
ObjectType * getBlockAddress(size_type theOffset) const
Definition: ArenaBlockBase.hpp:267
xalanc::ArenaBlockBase::getBlockOffset
size_type getBlockOffset(const ObjectType *theObject) const
Definition: ArenaBlockBase.hpp:250
xalanc::ArenaBlockBase::size_type
SizeType size_type
Definition: ArenaBlockBase.hpp:119
xalanc::ArenaBlockBase::ThisType
ArenaBlockBase< ObjectType, SizeType > ThisType
Definition: ArenaBlockBase.hpp:111
xalanc::ArenaBlockBase::m_blockSize
const size_type m_blockSize
Definition: ArenaBlockBase.hpp:279
xalanc::ArenaBlockBase::blockAvailable
bool blockAvailable() const
Definition: ArenaBlockBase.hpp:133