~ubuntu-branches/ubuntu/utopic/kdevelop-php/utopic

« back to all changes in this revision

Viewing changes to parser/generated/kdevelop-pg-qt/kdev-pg-allocator.h

  • Committer: Bazaar Package Importer
  • Author(s): Fathi Boudra
  • Date: 2010-01-17 17:10:22 UTC
  • Revision ID: james.westby@ubuntu.com-20100117171022-q2xlgd9ekewo2ijx
Tags: upstream-1.0.0~beta2
ImportĀ upstreamĀ versionĀ 1.0.0~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 /*
 
2
  This file is part of kdev-pg
 
3
  Copyright 2005, 2006 Roberto Raggi <roberto@kdevelop.org>
 
4
 
 
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
 
9
  documentation.
 
10
 
 
11
  The above copyright notice and this permission notice shall be included in
 
12
  all copies or substantial portions of the Software.
 
13
 
 
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.
 
20
*/
 
21
 
 
22
#ifndef KDEV_PG_ALLOCATOR_H
 
23
#define KDEV_PG_ALLOCATOR_H
 
24
 
 
25
#include <memory>
 
26
#include <cstdlib>
 
27
#include <cstring>
 
28
 
 
29
#include <QtCore/QtGlobal>
 
30
 
 
31
namespace KDevPG
 
32
{
 
33
 
 
34
template <class _Tp>
 
35
class Allocator
 
36
{
 
37
public:
 
38
  typedef _Tp valueType;
 
39
  typedef _Tp* pointer;
 
40
  typedef const _Tp* constPointer;
 
41
  typedef _Tp& reference;
 
42
  typedef const _Tp& constReference;
 
43
  typedef qint64 sizeType;
 
44
  typedef qint64 differenceType;
 
45
 
 
46
  static const sizeType maxBlockCount = sizeType( -1);
 
47
 
 
48
  Allocator()
 
49
  {
 
50
    sReference++;
 
51
  }
 
52
 
 
53
  ~Allocator()
 
54
  {
 
55
    if (--sReference == 0)
 
56
      {
 
57
        ++sBlockIndex;
 
58
 
 
59
        for (sizeType index = 0; index < sBlockIndex; ++index)
 
60
          delete[] sStorage[index];
 
61
 
 
62
        --sBlockIndex;
 
63
 
 
64
        std::free(sStorage);
 
65
      }
 
66
  }
 
67
 
 
68
  pointer address(reference __val)
 
69
  {
 
70
    return &__val;
 
71
  }
 
72
  constPointer address(constReference __val) const
 
73
  {
 
74
    return &__val;
 
75
  }
 
76
 
 
77
  pointer allocate(sizeType __n, const void* = 0)
 
78
  {
 
79
    const sizeType bytes = __n * sizeof(_Tp);
 
80
 
 
81
    if (sCurrentBlock == 0
 
82
        || sBlockSize < sCurrentIndex + bytes)
 
83
      {
 
84
        ++sBlockIndex;
 
85
 
 
86
        sStorage = reinterpret_cast<char**>
 
87
          (std::realloc(sStorage, sizeof(char*) * (1 + sBlockIndex)));
 
88
 
 
89
        sCurrentBlock = sStorage[sBlockIndex] = reinterpret_cast<char*>
 
90
          (new char[sBlockSize]);
 
91
 
 
92
        std::memset(sCurrentBlock, 0, sBlockSize);
 
93
        sCurrentIndex = 0;
 
94
      }
 
95
 
 
96
    pointer p = reinterpret_cast<pointer>
 
97
      (sCurrentBlock + sCurrentIndex);
 
98
 
 
99
    sCurrentIndex += bytes;
 
100
 
 
101
    return p;
 
102
  }
 
103
 
 
104
  void deallocate(pointer __p, sizeType __n)
 
105
  {}
 
106
 
 
107
  sizeType maxSize() const
 
108
  {
 
109
    return sizeType( -1) / sizeof(_Tp);
 
110
  }
 
111
 
 
112
  void contruct(pointer __p, constReference __val)
 
113
  {
 
114
    new (__p) _Tp(__val);
 
115
  }
 
116
  void destruct(pointer __p)
 
117
  {
 
118
    __p->~_Tp();
 
119
  }
 
120
 
 
121
private:
 
122
  template <class _Tp1>
 
123
  class Rebind
 
124
  {
 
125
    typedef Allocator<_Tp1> other;
 
126
  };
 
127
 
 
128
  template <class _Tp1>
 
129
  Allocator(const Allocator<_Tp1> &__o)
 
130
  {}
 
131
 
 
132
private:
 
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;
 
139
};
 
140
 
 
141
template <class _Tp>
 
142
typename Allocator<_Tp>::sizeType
 
143
Allocator<_Tp>::sReference = 0;
 
144
 
 
145
template <class _Tp>
 
146
const typename Allocator<_Tp>::sizeType
 
147
Allocator<_Tp>::sBlockSize = 1 << 16; // 64K
 
148
 
 
149
template <class _Tp>
 
150
typename Allocator<_Tp>::sizeType
 
151
Allocator<_Tp>::sBlockIndex = maxBlockCount;
 
152
 
 
153
template <class _Tp>
 
154
typename Allocator<_Tp>::sizeType
 
155
Allocator<_Tp>::sCurrentIndex = 0;
 
156
 
 
157
template <class _Tp>
 
158
char**
 
159
Allocator<_Tp>::sStorage = 0;
 
160
 
 
161
template <class _Tp>
 
162
char*
 
163
Allocator<_Tp>::sCurrentBlock = 0;
 
164
 
 
165
}
 
166
 
 
167
#endif // KDEV_PG_ALLOCATOR_H