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
37namespace XALAN_CPP_NAMESPACE {
38
39
40
41template<class ObjectType,
42#if defined(XALAN_NO_DEFAULT_TEMPLATE_ARGUMENTS)
43 class ArenaBlockType>
44#else
45 class ArenaBlockType = ArenaBlock<ObjectType> >
46#endif
48{
49public:
50
52
54
55 typedef typename ArenaBlockType::size_type size_type;
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,
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 */
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 */
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
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
210protected:
211
212 // data members...
214
216
217private:
218
219 // Not defined...
221
224
225 bool
227};
228
229
230
231}
232
233
234
235#endif // !defined(ARENAALLOCATOR_INCLUDE_GUARD_1357924680)
#define XALAN_CPP_NAMESPACE
Xalan-C++ namespace, including major and minor version.
virtual bool ownsObject(const ObjectType *theObject) const
ArenaAllocator< ObjectType, ArenaBlockType > ThisType
size_type getBlockSize() const
const MemoryManager & getMemoryManager() const
virtual void commitAllocation(ObjectType *theObject)
size_type getBlockCount() const
ArenaAllocator(MemoryManager &theManager, size_type theBlockSize)
virtual ObjectType * allocateBlock()
ArenaBlockType::size_type size_type
ArenaBlockListType m_blocks
XalanList< ArenaBlockType * > ArenaBlockListType
void setBlockSize(size_type theSize)
MemoryManager & getMemoryManager()
size_t size_type
Definition XalanMap.hpp:46
bool operator==(const XalanVector< Type > &theLHS, const XalanVector< Type > &theRHS)