daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | // |
| 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 | |
| 44 | typedef int TSourceLoc; |
| 45 | |
| 46 | #include <assert.h> |
| 47 | |
daniel@transgaming.com | e684229 | 2010-04-20 18:52:50 +0000 | [diff] [blame] | 48 | #include "compiler/PoolAlloc.h" |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 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 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 63 | // |
| 64 | // Pool version of string. |
| 65 | // |
| 66 | typedef pool_allocator<char> TStringAllocator; |
| 67 | typedef std::basic_string <char, std::char_traits<char>, TStringAllocator > TString; |
| 68 | inline TString* NewPoolTString(const char* s) |
| 69 | { |
| 70 | void* memory = GlobalPoolAllocator.allocate(sizeof(TString)); |
| 71 | return new(memory) TString(s); |
| 72 | } |
| 73 | |
| 74 | // |
| 75 | // Pool allocator versions of vectors, lists, and maps |
| 76 | // |
| 77 | template <class T> class TVector : public std::vector<T, pool_allocator<T> > { |
| 78 | public: |
| 79 | typedef typename std::vector<T, pool_allocator<T> >::size_type size_type; |
| 80 | TVector() : std::vector<T, pool_allocator<T> >() {} |
| 81 | TVector(const pool_allocator<T>& a) : std::vector<T, pool_allocator<T> >(a) {} |
| 82 | TVector(size_type i): std::vector<T, pool_allocator<T> >(i) {} |
| 83 | }; |
| 84 | |
alokp@chromium.org | 91a01a1 | 2010-05-12 18:39:04 +0000 | [diff] [blame^] | 85 | template <class T> class TList : public std::list<T, pool_allocator<T> > { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 86 | public: |
alokp@chromium.org | 91a01a1 | 2010-05-12 18:39:04 +0000 | [diff] [blame^] | 87 | typedef typename std::list<T, pool_allocator<T> >::size_type size_type; |
| 88 | TList() : std::list<T, pool_allocator<T> >() {} |
| 89 | TList(const pool_allocator<T>& a) : std::list<T, pool_allocator<T> >(a) {} |
| 90 | TList(size_type i): std::list<T, pool_allocator<T> >(i) {} |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | // This is called TStlSet, because TSet is taken by an existing compiler class. |
| 94 | template <class T, class CMP> class TStlSet : public std::set<T, CMP, pool_allocator<T> > { |
| 95 | // No pool allocator versions of constructors in std::set. |
| 96 | }; |
| 97 | |
| 98 | |
| 99 | template <class K, class D, class CMP = std::less<K> > |
alokp@chromium.org | 91a01a1 | 2010-05-12 18:39:04 +0000 | [diff] [blame^] | 100 | class TMap : public std::map<K, D, CMP, pool_allocator<std::pair<const K, D> > > { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 101 | public: |
alokp@chromium.org | 91a01a1 | 2010-05-12 18:39:04 +0000 | [diff] [blame^] | 102 | typedef pool_allocator<std::pair<const K, D> > tAllocator; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 103 | |
alokp@chromium.org | 91a01a1 | 2010-05-12 18:39:04 +0000 | [diff] [blame^] | 104 | TMap() : std::map<K, D, CMP, tAllocator>() {} |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 105 | // use correct two-stage name lookup supported in gcc 3.4 and above |
alokp@chromium.org | 91a01a1 | 2010-05-12 18:39:04 +0000 | [diff] [blame^] | 106 | TMap(const tAllocator& a) : std::map<K, D, CMP, tAllocator>(std::map<K, D, CMP, tAllocator>::key_compare(), a) {} |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | // |
| 110 | // Persistent string memory. Should only be used for strings that survive |
| 111 | // across compiles/links. |
| 112 | // |
| 113 | typedef std::basic_string<char> TPersistString; |
| 114 | |
| 115 | // |
| 116 | // templatized min and max functions. |
| 117 | // |
| 118 | template <class T> T Min(const T a, const T b) { return a < b ? a : b; } |
| 119 | template <class T> T Max(const T a, const T b) { return a > b ? a : b; } |
| 120 | |
| 121 | // |
| 122 | // Create a TString object from an integer. |
| 123 | // |
| 124 | inline const TString String(const int i, const int base = 10) |
| 125 | { |
| 126 | char text[16]; // 32 bit ints are at most 10 digits in base 10 |
| 127 | |
| 128 | #ifdef _WIN32 |
| 129 | _itoa(i, text, base); |
| 130 | #else |
| 131 | // we assume base 10 for all cases |
| 132 | sprintf(text, "%d", i); |
| 133 | #endif |
| 134 | |
| 135 | return text; |
| 136 | } |
| 137 | |
| 138 | const unsigned int SourceLocLineMask = 0xffff; |
| 139 | const unsigned int SourceLocStringShift = 16; |
| 140 | |
| 141 | __inline TPersistString FormatSourceLoc(const TSourceLoc loc) |
| 142 | { |
| 143 | char locText[64]; |
| 144 | |
| 145 | int string = loc >> SourceLocStringShift; |
| 146 | int line = loc & SourceLocLineMask; |
| 147 | |
| 148 | if (line) |
| 149 | sprintf(locText, "%d:%d", string, line); |
| 150 | else |
| 151 | sprintf(locText, "%d:? ", string); |
| 152 | |
| 153 | return TPersistString(locText); |
| 154 | } |
| 155 | |
| 156 | |
| 157 | typedef TMap<TString, TString> TPragmaTable; |
| 158 | typedef TMap<TString, TString>::tAllocator TPragmaTableAllocator; |
| 159 | |
| 160 | #endif // _COMMON_INCLUDED_ |