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(XALANDOMSTRINGALLOCATOR_INCLUDE_GUARD_12455133)
20 #define XALANDOMSTRINGALLOCATOR_INCLUDE_GUARD_12455133
21 
22 
23 
24 // Base include file.  Must be first.
25 #include <xalanc/PlatformSupport/PlatformSupportDefinitions.hpp>
26 
27 
28 
29 #include <xalanc/PlatformSupport/ArenaAllocator.hpp>
30 
31 
32 
33 #include <xalanc/XalanDOM/XalanDOMString.hpp>
34 
35 
36 
37 namespace XALAN_CPP_NAMESPACE {
38 
39 
40 
41 class XALAN_PLATFORMSUPPORT_EXPORT XalanDOMStringAllocator
42 {
43 public:
44 
45     typedef XalanDOMString                  data_type;
46     typedef data_type::size_type            data_type_size_type;
47 
48 #if defined(XALAN_NO_DEFAULT_TEMPLATE_ARGUMENTS)
49     typedef ArenaBlock<data_type>           ArenaBlockType;
50 
51     typedef ArenaAllocator<data_type,
52                            ArenaBlockType>  ArenaAllocatorType;
53 #else
54     typedef ArenaAllocator<data_type>       ArenaAllocatorType;
55 #endif
56 
57     typedef ArenaAllocatorType::size_type   size_type;
58 
59     enum { eDefaultBlockSize = 32 };
60 
61 
62     /**
63      * Construct an instance that will allocate in blocks of the specified size.
64      *
65      * @param theBlockSize The block size.
66      */
67     XalanDOMStringAllocator(MemoryManager&      theManager, size_type   theBlockCount);
68 
69     ~XalanDOMStringAllocator();
70 
71     /**
72      * Create a XalanDOMString object.
73      *
74      * @return pointer to the new instance
75      */
76     data_type*
77     create();
78 
79     /**
80      * Create a XalanDOMString object.
81      *
82      * @param theString A pointer to a character string
83      * @param theCount The number of characters in the string, or npos if the string is null-terminated.
84      *
85      * @return pointer to the new instance
86      */
87     data_type*
88     create(
89             const char*             theString,
90 #if defined(_MSC_VER) && (_MSC_VER <= 1300)
91             // $$$ ToDo: Some strange bug in MSVC++ complains when using data_type::npos here.
92             data_type_size_type     theCount = data_type_size_type(-1));
93 #else
94             data_type_size_type     theCount = data_type_size_type(data_type::npos));
95 #endif
96 
97     /**
98      * Copy constructor
99      *
100      * @param theSource The source string for the copy
101      * @param theStartPosition The position to start in the source string.
102      * @param theCount The number of characters to copy from the source string.
103      *
104      * @return pointer to the new instance
105      */
106     data_type*
107     create(
108             const data_type&        theSource,
109             data_type_size_type     theStartPosition = 0,
110             data_type_size_type     theCount = data_type_size_type(data_type::npos));
111 
112     /**
113      * Create a XalanDOMString object.
114      *
115      * @param theString A pointer to a wide character string
116      * @param theCount The number of characters in the string, or npos if the string is null-terminated.
117      *
118      * @return pointer to the new instance
119      */
120     data_type*
121     create(
122             const XalanDOMChar*     theString,
123             data_type_size_type     theCount = data_type_size_type(data_type::npos));
124 
125     /**
126      * Create a XalanDOMString object.
127      *
128      * @param theCount the size of the string
129      * @param theChar the character used to initialize the string
130      *
131      * @return pointer to the new instance
132      */
133     data_type*
134     create(
135             data_type_size_type     theCount,
136             XalanDOMChar            theChar);
137 
138     /**
139      * Determine if an object is owned by the allocator...
140      */
141     bool
ownsObject(const data_type * theObject)142     ownsObject(const data_type*     theObject)
143     {
144         return m_allocator.ownsObject(theObject);
145     }
146 
147     /**
148      * Delete all instance objects from allocator.
149      */
150     void
reset()151     reset()
152     {
153         m_allocator.reset();
154     }
155 
156     /**
157      * Get the number of ArenaBlocks currently allocated.
158      *
159      * @return The number of blocks.
160      */
161     size_type
getBlockCount() const162     getBlockCount() const
163     {
164         return m_allocator.getBlockCount();
165     }
166 
167     /**
168      * Get size of an ArenaBlock, that is, the number
169      * of objects in each block.
170      *
171      * @return The size of the block
172      */
173     size_type
getBlockSize() const174     getBlockSize() const
175     {
176         return m_allocator.getBlockSize();
177     }
178 
179     /**
180      * Get a reference to the MemoryManager instance
181      * for this instance.
182      *
183      * @return A reference to the MemoryManager instance.
184      */
185     MemoryManager&
getMemoryManager()186     getMemoryManager()
187     {
188         return m_allocator.getMemoryManager();
189     }
190 
191     /**
192      * Get a reference to the MemoryManager instance
193      * for this instance.
194      *
195      * @return A reference to the MemoryManager instance.
196      */
197     const MemoryManager&
getMemoryManager() const198     getMemoryManager() const
199     {
200         return m_allocator.getMemoryManager();
201     }
202 
203 private:
204 
205     // Not implemented...
206     XalanDOMStringAllocator(const XalanDOMStringAllocator&);
207 
208     XalanDOMStringAllocator&
209     operator=(const XalanDOMStringAllocator&);
210 
211     // Data members...
212     ArenaAllocatorType  m_allocator;
213 };
214 
215 
216 
217 }
218 
219 
220 
221 #endif  // XALANDOMSTRINGALLOCATOR_INCLUDE_GUARD_12455133
222