2
//Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
5
//Redistribution and use in source and binary forms, with or without
6
//modification, are permitted provided that the following conditions
9
// Redistributions of source code must retain the above copyright
10
// notice, this list of conditions and the following disclaimer.
12
// Redistributions in binary form must reproduce the above
13
// copyright notice, this list of conditions and the following
14
// disclaimer in the documentation and/or other materials provided
15
// with the distribution.
17
// Neither the name of 3Dlabs Inc. Ltd. nor the names of its
18
// contributors may be used to endorse or promote products derived
19
// from this software without specific prior written permission.
21
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22
//"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23
//LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24
//FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25
//COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26
//INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27
//BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28
//LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29
//CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30
//LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31
//ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
//POSSIBILITY OF SUCH DAMAGE.
35
#ifndef _COMMON_INCLUDED_
36
#define _COMMON_INCLUDED_
40
#elif defined (solaris)
41
#include <sys/int_types.h>
42
#define UINT_PTR uintptr_t
45
#define UINT_PTR uintptr_t
48
/* windows only pragma */
50
#pragma warning(disable : 4786) // Don't warn about too long identifiers
51
#pragma warning(disable : 4514) // unused inline method
52
#pragma warning(disable : 4201) // nameless union
56
// Doing the push and pop below for warnings does not leave the warning state
57
// the way it was. This seems like a defect in the compiler. We would like
58
// to do this, but since it does not work correctly right now, it is turned
61
//??#pragma warning(push, 3)
70
//??#pragma warning(pop)
72
typedef int TSourceLoc;
75
#include "PoolAlloc.h"
78
// Put POOL_ALLOCATOR_NEW_DELETE in base classes to make them use this scheme.
80
#define POOL_ALLOCATOR_NEW_DELETE(A) \
81
void* operator new(size_t s) { return (A).allocate(s); } \
82
void* operator new(size_t, void *_Where) { return (_Where); } \
83
void operator delete(void*) { } \
84
void operator delete(void *, void *) { } \
85
void* operator new[](size_t s) { return (A).allocate(s); } \
86
void* operator new[](size_t, void *_Where) { return (_Where); } \
87
void operator delete[](void*) { } \
88
void operator delete[](void *, void *) { }
92
// The current version of STL that comes with the PSDK (as required for the AMD64 compiler)
93
// has a very old version of the STL which is very out of date. As a result, various additions needed
94
// making to it to get the compilers compiling!
98
// A new version of the Map template class - the operator[] now returns the correct type reference
100
template <class _K, class _Ty, class _Pr = std::less<_K>, class _A = std::allocator<_Ty> >
101
class TBaseMap : public std::map <_K, _Ty, _Pr, _A >
104
_Ty& operator[] (const _K& _Kv)
106
iterator _P = insert(value_type(_Kv, _Ty())).first;
107
return ((*_P).second);
110
explicit TBaseMap(const _Pr& _Pred = _Pr(), const _A& _Al = _A())
111
: std::map<_K, _Ty, _Pr, _A >(_Pred, _Al) {};
117
// A new version of the List template class - the begin function now checks for NULL to eliminate access violations
119
template <class _Ty, class _A = std::allocator<_Ty> >
120
class TBaseList : public std::list <_Ty, _A >
125
return (iterator(_Head == 0 ? 0 : _Acc::_Next(_Head)));
128
const_iterator begin() const
130
return (const_iterator(_Head == 0 ? 0 : _Acc::_Next(_Head)));
134
// These are required - apparently!
136
explicit TBaseList(const _A& _Al = _A())
137
: std::list<_Ty, _A >(_Al) {};
138
explicit TBaseList(size_type _N, const _Ty& _V = _Ty(), const _A& _Al = _A())
139
: std::list<_Ty, _A >(N, _V, _Al) {};
144
// A new version of the set class - this defines the required insert method
146
template<class _K, class _Pr = std::less<_K>, class _A = std::allocator<_K> >
147
class TBaseSet : public std::set <_K, _Pr, _A>
152
// This method wasn't defined
154
template<class _Iter>
155
void insert(_Iter _First, _Iter _Last)
156
{ // insert [_First, _Last)
157
for (; _First != _Last; ++_First)
158
this->insert(*_First);
162
// These methods were not resolved if I declared the previous method??
164
_Pairib insert(const value_type& _X)
166
_Imp::_Pairib _Ans = _Tr.insert(_X);
167
return (_Pairib(_Ans.first, _Ans.second));
170
iterator insert(iterator _P, const value_type& _X)
172
return (_Tr.insert((_Imp::iterator&)_P, _X));
175
void insert(_It _F, _It _L)
177
for (; _F != _L; ++_F)
185
#define TBaseMap std::map
186
#define TBaseList std::list
187
#define TBaseSet std::set
192
// Pool version of string.
194
typedef pool_allocator<char> TStringAllocator;
195
typedef std::basic_string <char, std::char_traits<char>, TStringAllocator > TString;
196
inline TString* NewPoolTString(const char* s)
198
void* memory = GlobalPoolAllocator.allocate(sizeof(TString));
199
return new(memory) TString(s);
203
// Pool allocator versions of vectors, lists, and maps
205
template <class T> class TVector : public std::vector<T, pool_allocator<T> > {
207
typedef typename std::vector<T, pool_allocator<T> >::size_type size_type;
208
TVector() : std::vector<T, pool_allocator<T> >() {}
209
TVector(const pool_allocator<T>& a) : std::vector<T, pool_allocator<T> >(a) {}
210
TVector(size_type i): std::vector<T, pool_allocator<T> >(i) {}
213
template <class T> class TList : public TBaseList <T, pool_allocator<T> > {
215
typedef typename TBaseList<T, pool_allocator<T> >::size_type size_type;
216
TList() : TBaseList<T, pool_allocator<T> >() {}
217
TList(const pool_allocator<T>& a) : TBaseList<T, pool_allocator<T> >(a) {}
218
TList(size_type i): TBaseList<T, pool_allocator<T> >(i) {}
221
// This is called TStlSet, because TSet is taken by an existing compiler class.
222
template <class T, class CMP> class TStlSet : public std::set<T, CMP, pool_allocator<T> > {
223
// No pool allocator versions of constructors in std::set.
227
template <class K, class D, class CMP = std::less<K> > class TMap :
228
public TBaseMap<K, D, CMP, pool_allocator<std::pair<K, D> > > {
230
typedef pool_allocator<std::pair <K, D> > tAllocator;
232
TMap() : TBaseMap<K, D, CMP, tAllocator >() {}
234
TMap(const tAllocator& a) : TBaseMap<K, D, CMP, tAllocator >(key_compare(), a) {}
236
TMap(const tAllocator& a) : TBaseMap<K, D, CMP, tAllocator >() {}
240
// Persistent string memory. Should only be used for strings that survive
241
// across compiles/links.
243
typedef std::basic_string<char> TPersistString;
246
// templatized min and max functions.
248
template <class T> T Min(const T a, const T b) { return a < b ? a : b; }
249
template <class T> T Max(const T a, const T b) { return a > b ? a : b; }
252
// Create a TString object from an integer.
254
inline const TString String(const int i, const int base = 10)
256
char text[16]; // 32 bit ints are at most 10 digits in base 10
261
// we assume base 10 for all cases
262
sprintf(text, "%d", i);
268
const unsigned int SourceLocLineMask = 0xffff;
269
const unsigned int SourceLocStringShift = 16;
271
__inline TPersistString FormatSourceLoc(const TSourceLoc loc)
275
int string = loc >> SourceLocStringShift;
276
int line = loc & SourceLocLineMask;
279
sprintf(locText, "%d:%d", string, line);
281
sprintf(locText, "%d:? ", string);
283
return TPersistString(locText);
285
typedef TMap<TString, TString> TPragmaTable;
286
typedef TMap<TString, TString>::tAllocator TPragmaTableAllocator;
288
#endif // _COMMON_INCLUDED_