blob: 1ab2e911434e2bc36db02b94007fcca1c81a2413 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
2// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7#ifndef _COMMON_INCLUDED_
8#define _COMMON_INCLUDED_
9
10#ifdef _WIN32
11 #include <basetsd.h>
12#elif defined (solaris)
13 #include <sys/int_types.h>
14 #define UINT_PTR uintptr_t
15#else
16 #include <stdint.h>
17 #define UINT_PTR uintptr_t
18#endif
19
20/* windows only pragma */
21#ifdef _MSC_VER
22 #pragma warning(disable : 4786) // Don't warn about too long identifiers
23 #pragma warning(disable : 4514) // unused inline method
24 #pragma warning(disable : 4201) // nameless union
25#endif
26
27//
28// Doing the push and pop below for warnings does not leave the warning state
29// the way it was. This seems like a defect in the compiler. We would like
30// to do this, but since it does not work correctly right now, it is turned
31// off.
32//
33//??#pragma warning(push, 3)
34
35 #include <set>
36 #include <vector>
37 #include <map>
38 #include <list>
39 #include <string>
40 #include <stdio.h>
41
42//??#pragma warning(pop)
43
44typedef int TSourceLoc;
45
46 #include <assert.h>
47
48#include "PoolAlloc.h"
49
50//
51// Put POOL_ALLOCATOR_NEW_DELETE in base classes to make them use this scheme.
52//
53#define POOL_ALLOCATOR_NEW_DELETE(A) \
54 void* operator new(size_t s) { return (A).allocate(s); } \
55 void* operator new(size_t, void *_Where) { return (_Where); } \
56 void operator delete(void*) { } \
57 void operator delete(void *, void *) { } \
58 void* operator new[](size_t s) { return (A).allocate(s); } \
59 void* operator new[](size_t, void *_Where) { return (_Where); } \
60 void operator delete[](void*) { } \
61 void operator delete[](void *, void *) { }
62
63#define TBaseMap std::map
64#define TBaseList std::list
65#define TBaseSet std::set
66
67//
68// Pool version of string.
69//
70typedef pool_allocator<char> TStringAllocator;
71typedef std::basic_string <char, std::char_traits<char>, TStringAllocator > TString;
72inline TString* NewPoolTString(const char* s)
73{
74 void* memory = GlobalPoolAllocator.allocate(sizeof(TString));
75 return new(memory) TString(s);
76}
77
78//
79// Pool allocator versions of vectors, lists, and maps
80//
81template <class T> class TVector : public std::vector<T, pool_allocator<T> > {
82public:
83 typedef typename std::vector<T, pool_allocator<T> >::size_type size_type;
84 TVector() : std::vector<T, pool_allocator<T> >() {}
85 TVector(const pool_allocator<T>& a) : std::vector<T, pool_allocator<T> >(a) {}
86 TVector(size_type i): std::vector<T, pool_allocator<T> >(i) {}
87};
88
89template <class T> class TList : public TBaseList <T, pool_allocator<T> > {
90public:
91 typedef typename TBaseList<T, pool_allocator<T> >::size_type size_type;
92 TList() : TBaseList<T, pool_allocator<T> >() {}
93 TList(const pool_allocator<T>& a) : TBaseList<T, pool_allocator<T> >(a) {}
94 TList(size_type i): TBaseList<T, pool_allocator<T> >(i) {}
95};
96
97// This is called TStlSet, because TSet is taken by an existing compiler class.
98template <class T, class CMP> class TStlSet : public std::set<T, CMP, pool_allocator<T> > {
99 // No pool allocator versions of constructors in std::set.
100};
101
102
103template <class K, class D, class CMP = std::less<K> >
104class TMap : public TBaseMap<K, D, CMP, pool_allocator<std::pair<K, D> > > {
105public:
106 typedef pool_allocator<std::pair <K, D> > tAllocator;
107
108 TMap() : TBaseMap<K, D, CMP, tAllocator >() {}
109 // use correct two-stage name lookup supported in gcc 3.4 and above
110 TMap(const tAllocator& a) : TBaseMap<K, D, CMP, tAllocator>(TBaseMap<K, D, CMP, tAllocator >::key_compare(), a) {}
111};
112
113//
114// Persistent string memory. Should only be used for strings that survive
115// across compiles/links.
116//
117typedef std::basic_string<char> TPersistString;
118
119//
120// templatized min and max functions.
121//
122template <class T> T Min(const T a, const T b) { return a < b ? a : b; }
123template <class T> T Max(const T a, const T b) { return a > b ? a : b; }
124
125//
126// Create a TString object from an integer.
127//
128inline const TString String(const int i, const int base = 10)
129{
130 char text[16]; // 32 bit ints are at most 10 digits in base 10
131
132 #ifdef _WIN32
133 _itoa(i, text, base);
134 #else
135 // we assume base 10 for all cases
136 sprintf(text, "%d", i);
137 #endif
138
139 return text;
140}
141
142const unsigned int SourceLocLineMask = 0xffff;
143const unsigned int SourceLocStringShift = 16;
144
145__inline TPersistString FormatSourceLoc(const TSourceLoc loc)
146{
147 char locText[64];
148
149 int string = loc >> SourceLocStringShift;
150 int line = loc & SourceLocLineMask;
151
152 if (line)
153 sprintf(locText, "%d:%d", string, line);
154 else
155 sprintf(locText, "%d:? ", string);
156
157 return TPersistString(locText);
158}
159
160
161typedef TMap<TString, TString> TPragmaTable;
162typedef TMap<TString, TString>::tAllocator TPragmaTableAllocator;
163
164#endif // _COMMON_INCLUDED_