• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2002 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;
TVector()71     TVector() : std::vector<T, pool_allocator<T>>() {}
TVector(const pool_allocator<T> & a)72     TVector(const pool_allocator<T> &a) : std::vector<T, pool_allocator<T>>(a) {}
TVector(size_type i)73     TVector(size_type i) : std::vector<T, pool_allocator<T>>(i) {}
TVector(std::initializer_list<T> init)74     TVector(std::initializer_list<T> init) : std::vector<T, pool_allocator<T>>(init) {}
75 };
76 
77 template <class K, class D, class H = std::hash<K>, class CMP = std::equal_to<K>>
78 class TUnorderedMap : public std::unordered_map<K, D, H, CMP, pool_allocator<std::pair<const K, D>>>
79 {
80   public:
81     POOL_ALLOCATOR_NEW_DELETE
82     typedef pool_allocator<std::pair<const K, D>> tAllocator;
83 
TUnorderedMap()84     TUnorderedMap() : std::unordered_map<K, D, H, CMP, tAllocator>() {}
85     // use correct two-stage name lookup supported in gcc 3.4 and above
TUnorderedMap(const tAllocator & a)86     TUnorderedMap(const tAllocator &a)
87         : std::unordered_map<K, D, H, CMP, tAllocator>(
88               std::unordered_map<K, D, H, CMP, tAllocator>::key_compare(),
89               a)
90     {}
91 };
92 
93 template <class K, class D, class CMP = std::less<K>>
94 class TMap : public std::map<K, D, CMP, pool_allocator<std::pair<const K, D>>>
95 {
96   public:
97     POOL_ALLOCATOR_NEW_DELETE
98     typedef pool_allocator<std::pair<const K, D>> tAllocator;
99 
TMap()100     TMap() : std::map<K, D, CMP, tAllocator>() {}
101     // use correct two-stage name lookup supported in gcc 3.4 and above
TMap(const tAllocator & a)102     TMap(const tAllocator &a)
103         : std::map<K, D, CMP, tAllocator>(std::map<K, D, CMP, tAllocator>::key_compare(), a)
104     {}
105 };
106 
107 // Integer to TString conversion
108 template <typename T>
str(T i)109 inline TString str(T i)
110 {
111     ASSERT(std::numeric_limits<T>::is_integer);
112     char buffer[((8 * sizeof(T)) / 3) + 3];
113     const char *formatStr = std::numeric_limits<T>::is_signed ? "%d" : "%u";
114     snprintf(buffer, sizeof(buffer), formatStr, i);
115     return buffer;
116 }
117 
118 // Allocate a char array in the global memory pool. str must be a null terminated string. strLength
119 // is the length without the null terminator.
AllocatePoolCharArray(const char * str,size_t strLength)120 inline const char *AllocatePoolCharArray(const char *str, size_t strLength)
121 {
122     size_t requiredSize = strLength + 1;
123     char *buffer        = static_cast<char *>(GetGlobalPoolAllocator()->allocate(requiredSize));
124     memcpy(buffer, str, requiredSize);
125     ASSERT(buffer[strLength] == '\0');
126     return buffer;
127 }
128 
129 // Initialize a new stream which must be imbued with the classic locale
130 template <typename T>
InitializeStream()131 T InitializeStream()
132 {
133     T stream;
134     stream.imbue(std::locale::classic());
135     return stream;
136 }
137 
138 }  // namespace sh
139 
140 namespace std
141 {
142 template <>
143 struct hash<sh::TString>
144 {
145     size_t operator()(const sh::TString &s) const
146     {
147         return angle::PMurHash32(0, s.data(), static_cast<int>(s.length()));
148     }
149 };
150 }  // namespace std
151 
152 #endif  // COMPILER_TRANSLATOR_COMMON_H_
153