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 <map>
11 #include <sstream>
12 #include <string>
13 #include <unordered_map>
14 #include <vector>
15 #include <limits>
16 #include <stdio.h>
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;
NewPoolTString(const char * s)53 inline TString *NewPoolTString(const char *s)
54 {
55     void *memory = GetGlobalPoolAllocator()->allocate(sizeof(TString));
56     return new (memory) TString(s);
57 }
58 
59 //
60 // Persistent string memory.  Should only be used for strings that survive
61 // across compiles.
62 //
63 #define TPersistString std::string
64 #define TPersistStringStream std::ostringstream
65 
66 //
67 // Pool allocator versions of vectors, lists, and maps
68 //
69 template <class T>
70 class TVector : public std::vector<T, pool_allocator<T>>
71 {
72   public:
73     POOL_ALLOCATOR_NEW_DELETE();
74 
75     typedef typename std::vector<T, pool_allocator<T>>::size_type size_type;
TVector()76     TVector() : std::vector<T, pool_allocator<T>>() {}
TVector(const pool_allocator<T> & a)77     TVector(const pool_allocator<T> &a) : std::vector<T, pool_allocator<T>>(a) {}
TVector(size_type i)78     TVector(size_type i) : std::vector<T, pool_allocator<T>>(i) {}
79 };
80 
81 template <class K, class D, class H = std::hash<K>, class CMP = std::equal_to<K>>
82 class TUnorderedMap : public std::unordered_map<K, D, H, CMP, pool_allocator<std::pair<const K, D>>>
83 {
84   public:
85     POOL_ALLOCATOR_NEW_DELETE();
86     typedef pool_allocator<std::pair<const K, D>> tAllocator;
87 
TUnorderedMap()88     TUnorderedMap() : std::unordered_map<K, D, H, CMP, tAllocator>() {}
89     // use correct two-stage name lookup supported in gcc 3.4 and above
TUnorderedMap(const tAllocator & a)90     TUnorderedMap(const tAllocator &a)
91         : std::unordered_map<K, D, H, CMP, tAllocator>(
92               std::unordered_map<K, D, H, CMP, tAllocator>::key_compare(),
93               a)
94     {
95     }
96 };
97 
98 template <class K, class D, class CMP = std::less<K>>
99 class TMap : public std::map<K, D, CMP, pool_allocator<std::pair<const K, D>>>
100 {
101   public:
102     POOL_ALLOCATOR_NEW_DELETE();
103     typedef pool_allocator<std::pair<const K, D>> tAllocator;
104 
TMap()105     TMap() : std::map<K, D, CMP, tAllocator>() {}
106     // use correct two-stage name lookup supported in gcc 3.4 and above
TMap(const tAllocator & a)107     TMap(const tAllocator &a)
108         : std::map<K, D, CMP, tAllocator>(std::map<K, D, CMP, tAllocator>::key_compare(), a)
109     {
110     }
111 };
112 
113 // Integer to TString conversion
114 template <typename T>
str(T i)115 inline TString str(T i)
116 {
117     ASSERT(std::numeric_limits<T>::is_integer);
118     char buffer[((8 * sizeof(T)) / 3) + 3];
119     const char *formatStr = std::numeric_limits<T>::is_signed ? "%d" : "%u";
120     snprintf(buffer, sizeof(buffer), formatStr, i);
121     return buffer;
122 }
123 
124 }  // namespace sh
125 
126 namespace std
127 {
128 template <>
129 struct hash<sh::TString>
130 {
131     size_t operator()(const sh::TString &s) const
132     {
133         return angle::PMurHash32(0, s.data(), static_cast<int>(s.length()));
134     }
135 };
136 }  // namespace std
137 
138 #endif  // COMPILER_TRANSLATOR_COMMON_H_
139