| 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 COMPILER_TRANSLATOR_COMMON_H_ |
| 8 | #define COMPILER_TRANSLATOR_COMMON_H_ |
| 9 | |
| 10 | #include <stdio.h> |
| 11 | #include <limits> |
| 12 | #include <map> |
| 13 | #include <sstream> |
| 14 | #include <string> |
| 15 | #include <unordered_map> |
| 16 | #include <vector> |
| 17 | |
| 18 | #include "common/angleutils.h" |
| 19 | #include "common/debug.h" |
| 20 | #include "common/third_party/smhasher/src/PMurHash.h" |
| 21 | #include "compiler/translator/PoolAlloc.h" |
| 22 | |
| 23 | namespace sh |
| 24 | { |
| 25 | |
| 26 | struct TSourceLoc |
| 27 | { |
| 28 | int first_file; |
| 29 | int first_line; |
| 30 | int last_file; |
| 31 | int last_line; |
| 32 | }; |
| 33 | |
| 34 | // |
| 35 | // Put POOL_ALLOCATOR_NEW_DELETE in base classes to make them use this scheme. |
| 36 | // |
| 37 | #define POOL_ALLOCATOR_NEW_DELETE \ |
| 38 | void *operator new(size_t s) { return GetGlobalPoolAllocator()->allocate(s); } \ |
| 39 | void *operator new(size_t, void *_Where) { return (_Where); } \ |
| 40 | void operator delete(void *) {} \ |
| 41 | void operator delete(void *, void *) {} \ |
| 42 | void *operator new[](size_t s) { return GetGlobalPoolAllocator()->allocate(s); } \ |
| 43 | void *operator new[](size_t, void *_Where) { return (_Where); } \ |
| 44 | void operator delete[](void *) {} \ |
| 45 | void operator delete[](void *, void *) {} |
| 46 | |
| 47 | // |
| 48 | // Pool version of string. |
| 49 | // |
| 50 | typedef pool_allocator<char> TStringAllocator; |
| 51 | typedef std::basic_string<char, std::char_traits<char>, TStringAllocator> TString; |
| 52 | typedef std::basic_ostringstream<char, std::char_traits<char>, TStringAllocator> TStringStream; |
| 53 | |
| 54 | // |
| 55 | // Persistent string memory. Should only be used for strings that survive |
| 56 | // across compiles. |
| 57 | // |
| 58 | #define TPersistString std::string |
| 59 | #define TPersistStringStream std::ostringstream |
| 60 | |
| 61 | // |
| 62 | // Pool allocator versions of vectors, lists, and maps |
| 63 | // |
| 64 | template <class T> |
| 65 | class TVector : public std::vector<T, pool_allocator<T>> |
| 66 | { |
| 67 | public: |
| 68 | POOL_ALLOCATOR_NEW_DELETE |
| 69 | |
| 70 | typedef typename std::vector<T, pool_allocator<T>>::size_type size_type; |
| 71 | TVector() : std::vector<T, pool_allocator<T>>() {} |
| 72 | TVector(const pool_allocator<T> &a) : std::vector<T, pool_allocator<T>>(a) {} |
| 73 | TVector(size_type i) : std::vector<T, pool_allocator<T>>(i) {} |
| 74 | }; |
| 75 | |
| 76 | template <class K, class D, class H = std::hash<K>, class CMP = std::equal_to<K>> |
| 77 | class TUnorderedMap : public std::unordered_map<K, D, H, CMP, pool_allocator<std::pair<const K, D>>> |
| 78 | { |
| 79 | public: |
| 80 | POOL_ALLOCATOR_NEW_DELETE |
| 81 | typedef pool_allocator<std::pair<const K, D>> tAllocator; |
| 82 | |
| 83 | TUnorderedMap() : std::unordered_map<K, D, H, CMP, tAllocator>() {} |
| 84 | // use correct two-stage name lookup supported in gcc 3.4 and above |
| 85 | TUnorderedMap(const tAllocator &a) |
| 86 | : std::unordered_map<K, D, H, CMP, tAllocator>( |
| 87 | std::unordered_map<K, D, H, CMP, tAllocator>::key_compare(), |
| 88 | a) |
| 89 | {} |
| 90 | }; |
| 91 | |
| 92 | template <class K, class D, class CMP = std::less<K>> |
| 93 | class TMap : public std::map<K, D, CMP, pool_allocator<std::pair<const K, D>>> |
| 94 | { |
| 95 | public: |
| 96 | POOL_ALLOCATOR_NEW_DELETE |
| 97 | typedef pool_allocator<std::pair<const K, D>> tAllocator; |
| 98 | |
| 99 | TMap() : std::map<K, D, CMP, tAllocator>() {} |
| 100 | // use correct two-stage name lookup supported in gcc 3.4 and above |
| 101 | TMap(const tAllocator &a) |
| 102 | : std::map<K, D, CMP, tAllocator>(std::map<K, D, CMP, tAllocator>::key_compare(), a) |
| 103 | {} |
| 104 | }; |
| 105 | |
| 106 | // Integer to TString conversion |
| 107 | template <typename T> |
| 108 | inline TString str(T i) |
| 109 | { |
| 110 | ASSERT(std::numeric_limits<T>::is_integer); |
| 111 | char buffer[((8 * sizeof(T)) / 3) + 3]; |
| 112 | const char *formatStr = std::numeric_limits<T>::is_signed ? "%d" : "%u" ; |
| 113 | snprintf(buffer, sizeof(buffer), formatStr, i); |
| 114 | return buffer; |
| 115 | } |
| 116 | |
| 117 | // Allocate a char array in the global memory pool. str must be a null terminated string. strLength |
| 118 | // is the length without the null terminator. |
| 119 | inline const char *AllocatePoolCharArray(const char *str, size_t strLength) |
| 120 | { |
| 121 | size_t requiredSize = strLength + 1; |
| 122 | char *buffer = static_cast<char *>(GetGlobalPoolAllocator()->allocate(requiredSize)); |
| 123 | memcpy(buffer, str, requiredSize); |
| 124 | ASSERT(buffer[strLength] == '\0'); |
| 125 | return buffer; |
| 126 | } |
| 127 | |
| 128 | // Initialize a new stream which must be imbued with the classic locale |
| 129 | template <typename T> |
| 130 | T InitializeStream() |
| 131 | { |
| 132 | T stream; |
| 133 | stream.imbue(std::locale::classic()); |
| 134 | return stream; |
| 135 | } |
| 136 | |
| 137 | } // namespace sh |
| 138 | |
| 139 | namespace std |
| 140 | { |
| 141 | template <> |
| 142 | struct hash<sh::TString> |
| 143 | { |
| 144 | size_t operator()(const sh::TString &s) const |
| 145 | { |
| 146 | return angle::PMurHash32(0, s.data(), static_cast<int>(s.length())); |
| 147 | } |
| 148 | }; |
| 149 | } // namespace std |
| 150 | |
| 151 | #endif // COMPILER_TRANSLATOR_COMMON_H_ |
| 152 | |