2 // Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
3 // Copyright (C) 2012-2013 LunarG, Inc.
5 // All rights reserved.
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
11 // Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
14 // Redistributions in binary form must reproduce the above
15 // copyright notice, this list of conditions and the following
16 // disclaimer in the documentation and/or other materials provided
17 // with the distribution.
19 // Neither the name of 3Dlabs Inc. Ltd. nor the names of its
20 // contributors may be used to endorse or promote products derived
21 // from this software without specific prior written permission.
23 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 // POSSIBILITY OF SUCH DAMAGE.
37 #ifndef _COMMON_INCLUDED_
38 #define _COMMON_INCLUDED_
48 #include <unordered_map>
49 #include <unordered_set>
52 #if defined(__ANDROID__) || (defined(_MSC_VER) && _MSC_VER < 1700)
56 std::string to_string(const T& val) {
57 std::ostringstream os;
64 #if (defined(_MSC_VER) && _MSC_VER < 1900 /*vs2015*/) || defined MINGW_HAS_SECURE_API
67 #define snprintf sprintf_s
69 #define safe_vsprintf(buf,max,format,args) vsnprintf_s((buf), (max), (max), (format), (args))
70 #elif defined (solaris)
71 #define safe_vsprintf(buf,max,format,args) vsnprintf((buf), (max), (format), (args))
72 #include <sys/int_types.h>
73 #define UINT_PTR uintptr_t
75 #define safe_vsprintf(buf,max,format,args) vsnprintf((buf), (max), (format), (args))
77 #define UINT_PTR uintptr_t
80 #if defined(_MSC_VER) && _MSC_VER < 1800
82 inline long long int strtoll (const char* str, char** endptr, int base)
84 return _strtoi64(str, endptr, base);
86 inline unsigned long long int strtoull (const char* str, char** endptr, int base)
88 return _strtoui64(str, endptr, base);
90 inline long long int atoll (const char* str)
92 return strtoll(str, NULL, 10);
97 #define strdup _strdup
100 /* windows only pragma */
102 #pragma warning(disable : 4786) // Don't warn about too long identifiers
103 #pragma warning(disable : 4514) // unused inline method
104 #pragma warning(disable : 4201) // nameless union
107 #include "PoolAlloc.h"
110 // Put POOL_ALLOCATOR_NEW_DELETE in base classes to make them use this scheme.
112 #define POOL_ALLOCATOR_NEW_DELETE(A) \
113 void* operator new(size_t s) { return (A).allocate(s); } \
114 void* operator new(size_t, void *_Where) { return (_Where); } \
115 void operator delete(void*) { } \
116 void operator delete(void *, void *) { } \
117 void* operator new[](size_t s) { return (A).allocate(s); } \
118 void* operator new[](size_t, void *_Where) { return (_Where); } \
119 void operator delete[](void*) { } \
120 void operator delete[](void *, void *) { }
125 // Pool version of string.
127 typedef pool_allocator<char> TStringAllocator;
128 typedef std::basic_string <char, std::char_traits<char>, TStringAllocator> TString;
130 } // end namespace glslang
132 // Repackage the std::hash for use by unordered map/set with a TString key.
135 template<> struct hash<glslang::TString> {
136 std::size_t operator()(const glslang::TString& s) const
138 const unsigned _FNV_offset_basis = 2166136261U;
139 const unsigned _FNV_prime = 16777619U;
140 unsigned _Val = _FNV_offset_basis;
141 size_t _Count = s.size();
142 const char* _First = s.c_str();
143 for (size_t _Next = 0; _Next < _Count; ++_Next)
145 _Val ^= (unsigned)_First[_Next];
156 inline TString* NewPoolTString(const char* s)
158 void* memory = GetThreadPoolAllocator().allocate(sizeof(TString));
159 return new(memory) TString(s);
162 template<class T> inline T* NewPoolObject(T*)
164 return new(GetThreadPoolAllocator().allocate(sizeof(T))) T;
167 template<class T> inline T* NewPoolObject(T, int instances)
169 return new(GetThreadPoolAllocator().allocate(instances * sizeof(T))) T[instances];
173 // Pool allocator versions of vectors, lists, and maps
175 template <class T> class TVector : public std::vector<T, pool_allocator<T> > {
177 POOL_ALLOCATOR_NEW_DELETE(GetThreadPoolAllocator())
179 typedef typename std::vector<T, pool_allocator<T> >::size_type size_type;
180 TVector() : std::vector<T, pool_allocator<T> >() {}
181 TVector(const pool_allocator<T>& a) : std::vector<T, pool_allocator<T> >(a) {}
182 TVector(size_type i) : std::vector<T, pool_allocator<T> >(i) {}
183 TVector(size_type i, const T& val) : std::vector<T, pool_allocator<T> >(i, val) {}
186 template <class T> class TList : public std::list<T, pool_allocator<T> > {
189 template <class K, class D, class CMP = std::less<K> >
190 class TMap : public std::map<K, D, CMP, pool_allocator<std::pair<K const, D> > > {
193 template <class K, class D, class HASH = std::hash<K>, class PRED = std::equal_to<K> >
194 class TUnorderedMap : public std::unordered_map<K, D, HASH, PRED, pool_allocator<std::pair<K const, D> > > {
198 // Persistent string memory. Should only be used for strings that survive
199 // across compiles/links.
201 typedef std::basic_string<char> TPersistString;
204 // templatized min and max functions.
206 template <class T> T Min(const T a, const T b) { return a < b ? a : b; }
207 template <class T> T Max(const T a, const T b) { return a > b ? a : b; }
210 // Create a TString object from an integer.
212 #if defined _MSC_VER || defined MINGW_HAS_SECURE_API
213 inline const TString String(const int i, const int base = 10)
215 char text[16]; // 32 bit ints are at most 10 digits in base 10
216 _itoa_s(i, text, sizeof(text), base);
220 inline const TString String(const int i, const int /*base*/ = 10)
222 char text[16]; // 32 bit ints are at most 10 digits in base 10
224 // we assume base 10 for all cases
225 snprintf(text, sizeof(text), "%d", i);
234 name = nullptr; string = 0; line = 0; column = 0;
236 void init(int stringNum) { init(); string = stringNum; }
237 // Returns the name if it exists. Otherwise, returns the string number.
238 std::string getStringNameOrNum(bool quoteStringName = true) const
240 if (name != nullptr) {
241 TString qstr = quoteStringName ? ("\"" + *name + "\"") : *name;
242 std::string ret_str(qstr.c_str());
245 return std::to_string((long long)string);
247 const char* getFilename() const
251 return name->c_str();
253 const char* getFilenameStr() const { return name == nullptr ? "" : name->c_str(); }
254 TString* name; // descriptive name for this string, when a textual name is available, otherwise nullptr
260 class TPragmaTable : public TMap<TString, TString> {
262 POOL_ALLOCATOR_NEW_DELETE(GetThreadPoolAllocator())
265 const int MaxTokenLength = 1024;
267 template <class T> bool IsPow2(T powerOf2)
272 return (powerOf2 & (powerOf2 - 1)) == 0;
275 // Round number up to a multiple of the given powerOf2, which is not
276 // a power, just a number that must be a power of 2.
277 template <class T> void RoundToPow2(T& number, int powerOf2)
279 assert(IsPow2(powerOf2));
280 number = (number + powerOf2 - 1) & ~(powerOf2 - 1);
283 template <class T> bool IsMultipleOfPow2(T number, int powerOf2)
285 assert(IsPow2(powerOf2));
286 return ! (number & (powerOf2 - 1));
289 // Returns log2 of an integer power of 2.
290 // T should be integral.
291 template <class T> int IntLog2(T n)
295 while ((T(1) << result) != n) {
301 } // end namespace glslang
303 #endif // _COMMON_INCLUDED_