1
|
1 /****************************************************************************
|
|
2 **
|
|
3 ** Copyright (C) 1992-2008 Nokia. All rights reserved.
|
|
4 ** Copyright (C) 2002-2005 Roberto Raggi <roberto@kdevelop.org>
|
|
5 **
|
|
6 ** This file is part of Qt Jambi.
|
|
7 **
|
|
8 ** * Commercial Usage
|
|
9 * Licensees holding valid Qt Commercial licenses may use this file in
|
|
10 * accordance with the Qt Commercial License Agreement provided with the
|
|
11 * Software or, alternatively, in accordance with the terms contained in
|
|
12 * a written agreement between you and Nokia.
|
|
13 *
|
|
14 *
|
|
15 * GNU General Public License Usage
|
|
16 * Alternatively, this file may be used under the terms of the GNU
|
|
17 * General Public License versions 2.0 or 3.0 as published by the Free
|
|
18 * Software Foundation and appearing in the file LICENSE.GPL included in
|
|
19 * the packaging of this file. Please review the following information
|
|
20 * to ensure GNU General Public Licensing requirements will be met:
|
|
21 * http://www.fsf.org/licensing/licenses/info/GPLv2.html and
|
|
22 * http://www.gnu.org/copyleft/gpl.html. In addition, as a special
|
|
23 * exception, Nokia gives you certain additional rights. These rights
|
|
24 * are described in the Nokia Qt GPL Exception version 1.2, included in
|
|
25 * the file GPL_EXCEPTION.txt in this package.
|
|
26 *
|
|
27 * Qt for Windows(R) Licensees
|
|
28 * As a special exception, Nokia, as the sole copyright holder for Qt
|
|
29 * Designer, grants users of the Qt/Eclipse Integration plug-in the
|
|
30 * right for the Qt/Eclipse Integration to link to functionality
|
|
31 * provided by Qt Designer and its related libraries.
|
|
32 *
|
|
33 *
|
|
34 * If you are unsure which license is appropriate for your use, please
|
|
35 * contact the sales department at qt-sales@nokia.com.
|
|
36
|
|
37 **
|
|
38 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
|
39 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|
40 **
|
|
41 ****************************************************************************/
|
|
42
|
|
43
|
|
44 #ifndef RXX_ALLOCATOR_H
|
|
45 #define RXX_ALLOCATOR_H
|
|
46
|
|
47 #include <cstddef>
|
|
48 #include <cstdlib>
|
|
49 #include <cstring>
|
|
50 #include <memory>
|
|
51
|
|
52 template <class _Tp> class rxx_allocator {
|
|
53 public:
|
|
54 typedef _Tp value_type;
|
|
55 typedef _Tp* pointer;
|
|
56 typedef const _Tp* const_pointer;
|
|
57 typedef _Tp& reference;
|
|
58 typedef const _Tp& const_reference;
|
|
59 typedef std::size_t size_type;
|
|
60 typedef std::ptrdiff_t difference_type;
|
|
61
|
|
62 static const size_type max_block_count = size_type(-1);
|
|
63 static const size_type _S_block_size = 1 << 16; // 64K
|
|
64
|
|
65 rxx_allocator() {
|
|
66 _M_block_index = max_block_count;
|
|
67 _M_current_index = 0;
|
|
68 _M_storage = 0;
|
|
69 _M_current_block = 0;
|
|
70 }
|
|
71
|
|
72 ~rxx_allocator() {
|
|
73 for (size_type index = 0; index < _M_block_index + 1; ++index)
|
|
74 delete[] _M_storage[index];
|
|
75
|
|
76 ::free(_M_storage);
|
|
77 }
|
|
78
|
|
79 pointer address(reference __val) { return &__val; }
|
|
80 const_pointer address(const_reference __val) const { return &__val; }
|
|
81
|
|
82 pointer allocate(size_type __n, const void* = 0) {
|
|
83 const size_type bytes = __n * sizeof(_Tp);
|
|
84
|
|
85 if (_M_current_block == 0
|
|
86 || _S_block_size < _M_current_index + bytes)
|
|
87 {
|
|
88 ++_M_block_index;
|
|
89
|
|
90 _M_storage = reinterpret_cast<char**>
|
|
91 (::realloc(_M_storage, sizeof(char*) * (1 + _M_block_index)));
|
|
92
|
|
93 _M_current_block = _M_storage[_M_block_index] = reinterpret_cast<char*>
|
|
94 (new char[_S_block_size]);
|
|
95
|
|
96 #if defined(RXX_ALLOCATOR_INIT_0) // ### make it a policy
|
|
97 ::memset(_M_current_block, 0, _S_block_size);
|
|
98 #endif
|
|
99 _M_current_index = 0;
|
|
100 }
|
|
101
|
|
102 pointer p = reinterpret_cast<pointer>
|
|
103 (_M_current_block + _M_current_index);
|
|
104
|
|
105 _M_current_index += bytes;
|
|
106
|
|
107 return p;
|
|
108 }
|
|
109
|
|
110 void deallocate(pointer __p, size_type __n) {}
|
|
111
|
|
112 size_type max_size() const { return size_type(-1) / sizeof(_Tp); }
|
|
113
|
|
114 void contruct(pointer __p, const_reference __val) { new (__p) _Tp(__val); }
|
|
115 void destruct(pointer __p) { __p->~_Tp(); }
|
|
116
|
|
117 private:
|
|
118 template <class _Tp1> struct rebind {
|
|
119 typedef rxx_allocator<_Tp1> other;
|
|
120 };
|
|
121
|
|
122 template <class _Tp1> rxx_allocator(const rxx_allocator<_Tp1> &__o) {}
|
|
123
|
|
124 private:
|
|
125 size_type _M_block_index;
|
|
126 size_type _M_current_index;
|
|
127 char *_M_current_block;
|
|
128 char **_M_storage;
|
|
129 };
|
|
130
|
|
131 #endif // RXX_ALLOCATOR_H
|
|
132
|
|
133 // kate: space-indent on; indent-width 2; replace-tabs on;
|