2 //Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
3 //Copyright (C) 2012-2013 LunarG, Inc.
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_
40 #if (defined(_MSC_VER) && _MSC_VER < 1900 /*vs2015*/) || defined MINGW_HAS_SECURE_API
42 #define snprintf sprintf_s
43 #define safe_vsprintf(buf,max,format,args) vsnprintf_s((buf), (max), (max), (format), (args))
44 #elif defined (solaris)
45 #define safe_vsprintf(buf,max,format,args) vsnprintf((buf), (max), (format), (args))
46 #include <sys/int_types.h>
47 #define UINT_PTR uintptr_t
49 #define safe_vsprintf(buf,max,format,args) vsnprintf((buf), (max), (format), (args))
51 #define UINT_PTR uintptr_t
54 #if defined(__ANDROID__) || _MSC_VER < 1700
58 std::string to_string(const T& val) {
59 std::ostringstream os;
66 #if defined(_MSC_VER) && _MSC_VER < 1700
67 inline long long int strtoll (const char* str, char** endptr, int base)
69 return _strtoi64(str, endptr, base);
71 inline long long int atoll (const char* str)
73 return strtoll(str, NULL, 10);
77 /* windows only pragma */
79 #pragma warning(disable : 4786) // Don't warn about too long identifiers
80 #pragma warning(disable : 4514) // unused inline method
81 #pragma warning(disable : 4201) // nameless union
85 #include <unordered_set>
88 #include <unordered_map>
95 #include "PoolAlloc.h"
98 // Put POOL_ALLOCATOR_NEW_DELETE in base classes to make them use this scheme.
100 #define POOL_ALLOCATOR_NEW_DELETE(A) \
101 void* operator new(size_t s) { return (A).allocate(s); } \
102 void* operator new(size_t, void *_Where) { return (_Where); } \
103 void operator delete(void*) { } \
104 void operator delete(void *, void *) { } \
105 void* operator new[](size_t s) { return (A).allocate(s); } \
106 void* operator new[](size_t, void *_Where) { return (_Where); } \
107 void operator delete[](void*) { } \
108 void operator delete[](void *, void *) { }
113 // Pool version of string.
115 typedef pool_allocator<char> TStringAllocator;
116 typedef std::basic_string <char, std::char_traits<char>, TStringAllocator> TString;
118 } // end namespace glslang
120 // Repackage the std::hash for use by unordered map/set with a TString key.
123 template<> struct hash<glslang::TString> {
124 std::size_t operator()(const glslang::TString& s) const
126 const unsigned _FNV_offset_basis = 2166136261U;
127 const unsigned _FNV_prime = 16777619U;
128 unsigned _Val = _FNV_offset_basis;
129 size_t _Count = s.size();
130 const char* _First = s.c_str();
131 for (size_t _Next = 0; _Next < _Count; ++_Next)
133 _Val ^= (unsigned)_First[_Next];
144 inline TString* NewPoolTString(const char* s)
146 void* memory = GetThreadPoolAllocator().allocate(sizeof(TString));
147 return new(memory) TString(s);
150 template<class T> inline T* NewPoolObject(T)
152 return new(GetThreadPoolAllocator().allocate(sizeof(T))) T;
155 template<class T> inline T* NewPoolObject(T, int instances)
157 return new(GetThreadPoolAllocator().allocate(instances * sizeof(T))) T[instances];
161 // Pool allocator versions of vectors, lists, and maps
163 template <class T> class TVector : public std::vector<T, pool_allocator<T> > {
165 POOL_ALLOCATOR_NEW_DELETE(GetThreadPoolAllocator())
167 typedef typename std::vector<T, pool_allocator<T> >::size_type size_type;
168 TVector() : std::vector<T, pool_allocator<T> >() {}
169 TVector(const pool_allocator<T>& a) : std::vector<T, pool_allocator<T> >(a) {}
170 TVector(size_type i) : std::vector<T, pool_allocator<T> >(i) {}
171 TVector(size_type i, const T& val) : std::vector<T, pool_allocator<T> >(i, val) {}
174 template <class T> class TList : public std::list<T, pool_allocator<T> > {
177 template <class K, class D, class CMP = std::less<K> >
178 class TMap : public std::map<K, D, CMP, pool_allocator<std::pair<K const, D> > > {
181 template <class K, class D, class HASH = std::hash<K>, class PRED = std::equal_to<K> >
182 class TUnorderedMap : public std::unordered_map<K, D, HASH, PRED, pool_allocator<std::pair<K const, D> > > {
186 // Persistent string memory. Should only be used for strings that survive
187 // across compiles/links.
189 typedef std::basic_string<char> TPersistString;
192 // templatized min and max functions.
194 template <class T> T Min(const T a, const T b) { return a < b ? a : b; }
195 template <class T> T Max(const T a, const T b) { return a > b ? a : b; }
198 // Create a TString object from an integer.
200 #if defined _MSC_VER || defined MINGW_HAS_SECURE_API
201 inline const TString String(const int i, const int base = 10)
203 char text[16]; // 32 bit ints are at most 10 digits in base 10
204 _itoa_s(i, text, sizeof(text), base);
208 inline const TString String(const int i, const int /*base*/ = 10)
210 char text[16]; // 32 bit ints are at most 10 digits in base 10
212 // we assume base 10 for all cases
213 snprintf(text, sizeof(text), "%d", i);
220 void init() { name = nullptr; string = 0; line = 0; column = 0; }
221 // Returns the name if it exists. Otherwise, returns the string number.
222 std::string getStringNameOrNum(bool quoteStringName = true) const
225 return quoteStringName ? ("\"" + std::string(name) + "\"") : name;
226 return std::to_string((long long)string);
228 const char* name; // descriptive name for this string
234 typedef TMap<TString, TString> TPragmaTable;
236 const int MaxTokenLength = 1024;
238 template <class T> bool IsPow2(T powerOf2)
243 return (powerOf2 & (powerOf2 - 1)) == 0;
246 // Round number up to a multiple of the given powerOf2, which is not
247 // a power, just a number that must be a power of 2.
248 template <class T> void RoundToPow2(T& number, int powerOf2)
250 assert(IsPow2(powerOf2));
251 number = (number + powerOf2 - 1) & ~(powerOf2 - 1);
254 template <class T> bool IsMultipleOfPow2(T number, int powerOf2)
256 assert(IsPow2(powerOf2));
257 return ! (number & (powerOf2 - 1));
260 } // end namespace glslang
262 #endif // _COMMON_INCLUDED_