merge in KDE changes for cpptoxml/parser: mostly -pedantic and GCC 4.4.0
[lqt.git] / cpptoxml / parser / rxx_allocator.h
blob83b6418345be7f25b516ab908b8a22331f352e3e
1 /****************************************************************************
2 **
3 ** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved.
4 ** Copyright (C) 2002-2005 Roberto Raggi <roberto@kdevelop.org>
5 **
6 ** This file is part of the Qt Script Generator project on Trolltech Labs.
7 **
8 ** This file may be used under the terms of the GNU General Public
9 ** License version 2.0 as published by the Free Software Foundation
10 ** and appearing in the file LICENSE.GPL included in the packaging of
11 ** this file. Please review the following information to ensure GNU
12 ** General Public Licensing requirements will be met:
13 ** http://www.trolltech.com/products/qt/opensource.html
15 ** If you are unsure which license is appropriate for your use, please
16 ** review the following information:
17 ** http://www.trolltech.com/products/qt/licensing.html or contact the
18 ** sales department at sales@trolltech.com.
20 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
21 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 ****************************************************************************/
26 #ifndef RXX_ALLOCATOR_H
27 #define RXX_ALLOCATOR_H
29 #include <cstddef>
30 #include <cstdlib>
31 #include <cstring>
32 #include <memory>
34 template <class _Tp> class rxx_allocator {
35 public:
36 typedef _Tp value_type;
37 typedef _Tp* pointer;
38 typedef const _Tp* const_pointer;
39 typedef _Tp& reference;
40 typedef const _Tp& const_reference;
41 typedef std::size_t size_type;
42 typedef std::ptrdiff_t difference_type;
44 static const size_type max_block_count = size_type(-1);
45 static const size_type _S_block_size = 1 << 16; // 64K
47 rxx_allocator() {
48 _M_block_index = max_block_count;
49 _M_current_index = 0;
50 _M_storage = 0;
51 _M_current_block = 0;
54 ~rxx_allocator() {
55 for (size_type index = 0; index < _M_block_index + 1; ++index)
56 delete[] _M_storage[index];
58 ::free(_M_storage);
61 pointer address(reference __val) { return &__val; }
62 const_pointer address(const_reference __val) const { return &__val; }
64 pointer allocate(size_type __n, const void* = 0) {
65 const size_type bytes = __n * sizeof(_Tp);
67 if (_M_current_block == 0
68 || _S_block_size < _M_current_index + bytes)
70 ++_M_block_index;
72 _M_storage = reinterpret_cast<char**>
73 (std::realloc(_M_storage, sizeof(char*) * (1 + _M_block_index)));
75 _M_current_block = _M_storage[_M_block_index] = reinterpret_cast<char*>
76 (new char[_S_block_size]);
78 #if defined(RXX_ALLOCATOR_INIT_0) // ### make it a policy
79 ::memset(_M_current_block, 0, _S_block_size);
80 #endif
81 _M_current_index = 0;
84 pointer p = reinterpret_cast<pointer>
85 (_M_current_block + _M_current_index);
87 _M_current_index += bytes;
89 return p;
92 void deallocate(pointer __p, size_type __n) {}
94 size_type max_size() const { return size_type(-1) / sizeof(_Tp); }
96 void contruct(pointer __p, const_reference __val) { new (__p) _Tp(__val); }
97 void destruct(pointer __p) { __p->~_Tp(); }
99 private:
100 template <class _Tp1> struct rebind {
101 typedef rxx_allocator<_Tp1> other;
104 template <class _Tp1> rxx_allocator(const rxx_allocator<_Tp1> &__o) {}
106 private:
107 size_type _M_block_index;
108 size_type _M_current_index;
109 char *_M_current_block;
110 char **_M_storage;
113 #endif // RXX_ALLOCATOR_H
115 // kate: space-indent on; indent-width 2; replace-tabs on;