2
This file is part of kdev-pg
3
Copyright 2005, 2006 Roberto Raggi <roberto@kdevelop.org>
5
Permission to use, copy, modify, distribute, and sell this software and its
6
documentation for any purpose is hereby granted without fee, provided that
7
the above copyright notice appear in all copies and that both that
8
copyright notice and this permission notice appear in supporting
11
The above copyright notice and this permission notice shall be included in
12
all copies or substantial portions of the Software.
14
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
KDEVELOP TEAM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
#ifndef KDEV_PG_ALLOCATOR_H
23
#define KDEV_PG_ALLOCATOR_H
29
#include <QtCore/QtGlobal>
38
typedef _Tp valueType;
40
typedef const _Tp* constPointer;
41
typedef _Tp& reference;
42
typedef const _Tp& constReference;
43
typedef qint64 sizeType;
44
typedef qint64 differenceType;
46
static const sizeType maxBlockCount = sizeType( -1);
55
if (--sReference == 0)
59
for (sizeType index = 0; index < sBlockIndex; ++index)
60
delete[] sStorage[index];
68
pointer address(reference __val)
72
constPointer address(constReference __val) const
77
pointer allocate(sizeType __n, const void* = 0)
79
const sizeType bytes = __n * sizeof(_Tp);
81
if (sCurrentBlock == 0
82
|| sBlockSize < sCurrentIndex + bytes)
86
sStorage = reinterpret_cast<char**>
87
(std::realloc(sStorage, sizeof(char*) * (1 + sBlockIndex)));
89
sCurrentBlock = sStorage[sBlockIndex] = reinterpret_cast<char*>
90
(new char[sBlockSize]);
92
std::memset(sCurrentBlock, 0, sBlockSize);
96
pointer p = reinterpret_cast<pointer>
97
(sCurrentBlock + sCurrentIndex);
99
sCurrentIndex += bytes;
104
void deallocate(pointer __p, sizeType __n)
107
sizeType maxSize() const
109
return sizeType( -1) / sizeof(_Tp);
112
void contruct(pointer __p, constReference __val)
114
new (__p) _Tp(__val);
116
void destruct(pointer __p)
122
template <class _Tp1>
125
typedef Allocator<_Tp1> other;
128
template <class _Tp1>
129
Allocator(const Allocator<_Tp1> &__o)
133
static sizeType sReference;
134
static const sizeType sBlockSize;
135
static sizeType sBlockIndex;
136
static sizeType sCurrentIndex;
137
static char *sCurrentBlock;
138
static char **sStorage;
142
typename Allocator<_Tp>::sizeType
143
Allocator<_Tp>::sReference = 0;
146
const typename Allocator<_Tp>::sizeType
147
Allocator<_Tp>::sBlockSize = 1 << 16; // 64K
150
typename Allocator<_Tp>::sizeType
151
Allocator<_Tp>::sBlockIndex = maxBlockCount;
154
typename Allocator<_Tp>::sizeType
155
Allocator<_Tp>::sCurrentIndex = 0;
159
Allocator<_Tp>::sStorage = 0;
163
Allocator<_Tp>::sCurrentBlock = 0;
167
#endif // KDEV_PG_ALLOCATOR_H