1 /****************************************************************************
3 ** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved.
4 ** Copyright (C) 2002-2005 Roberto Raggi <roberto@kdevelop.org>
6 ** This file is part of the Qt Script Generator project on Trolltech Labs.
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
34 template <class _Tp
> class rxx_allocator
{
36 typedef _Tp value_type
;
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
48 _M_block_index
= max_block_count
;
55 for (size_type index
= 0; index
< _M_block_index
+ 1; ++index
)
56 delete[] _M_storage
[index
];
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
)
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
);
84 pointer p
= reinterpret_cast<pointer
>
85 (_M_current_block
+ _M_current_index
);
87 _M_current_index
+= bytes
;
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(); }
100 template <class _Tp1
> struct rebind
{
101 typedef rxx_allocator
<_Tp1
> other
;
104 template <class _Tp1
> rxx_allocator(const rxx_allocator
<_Tp1
> &__o
) {}
107 size_type _M_block_index
;
108 size_type _M_current_index
;
109 char *_M_current_block
;
113 #endif // RXX_ALLOCATOR_H
115 // kate: space-indent on; indent-width 2; replace-tabs on;