Line data Source code
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 <vector>
14 : #include <limits>
15 : #include <stdio.h>
16 :
17 : #include "common/angleutils.h"
18 : #include "common/debug.h"
19 : #include "compiler/translator/PoolAlloc.h"
20 :
21 : namespace sh
22 : {
23 :
24 : struct TSourceLoc {
25 : int first_file;
26 : int first_line;
27 : int last_file;
28 : int last_line;
29 : };
30 :
31 : //
32 : // Put POOL_ALLOCATOR_NEW_DELETE in base classes to make them use this scheme.
33 : //
34 : #define POOL_ALLOCATOR_NEW_DELETE() \
35 : void* operator new(size_t s) { return GetGlobalPoolAllocator()->allocate(s); } \
36 : void* operator new(size_t, void *_Where) { return (_Where); } \
37 : void operator delete(void*) { } \
38 : void operator delete(void *, void *) { } \
39 : void* operator new[](size_t s) { return GetGlobalPoolAllocator()->allocate(s); } \
40 : void* operator new[](size_t, void *_Where) { return (_Where); } \
41 : void operator delete[](void*) { } \
42 : void operator delete[](void *, void *) { }
43 :
44 : //
45 : // Pool version of string.
46 : //
47 : typedef pool_allocator<char> TStringAllocator;
48 : typedef std::basic_string <char, std::char_traits<char>, TStringAllocator> TString;
49 : typedef std::basic_ostringstream<char, std::char_traits<char>, TStringAllocator> TStringStream;
50 0 : inline TString* NewPoolTString(const char* s)
51 : {
52 0 : void* memory = GetGlobalPoolAllocator()->allocate(sizeof(TString));
53 0 : return new(memory) TString(s);
54 : }
55 :
56 : //
57 : // Persistent string memory. Should only be used for strings that survive
58 : // across compiles.
59 : //
60 : #define TPersistString std::string
61 : #define TPersistStringStream std::ostringstream
62 :
63 : //
64 : // Pool allocator versions of vectors, lists, and maps
65 : //
66 : template <class T>
67 0 : class TVector : public std::vector<T, pool_allocator<T>>
68 : {
69 : public:
70 : typedef typename std::vector<T, pool_allocator<T>>::size_type size_type;
71 0 : TVector() : std::vector<T, pool_allocator<T>>() {}
72 : TVector(const pool_allocator<T> &a) : std::vector<T, pool_allocator<T>>(a) {}
73 0 : TVector(size_type i) : std::vector<T, pool_allocator<T>>(i) {}
74 : };
75 :
76 : template <class K, class D, class CMP = std::less<K>>
77 0 : class TMap : public std::map<K, D, CMP, pool_allocator<std::pair<const K, D>>>
78 : {
79 : public:
80 : typedef pool_allocator<std::pair<const K, D>> tAllocator;
81 :
82 0 : TMap() : std::map<K, D, CMP, tAllocator>() {}
83 : // use correct two-stage name lookup supported in gcc 3.4 and above
84 : TMap(const tAllocator& a) : std::map<K, D, CMP, tAllocator>(std::map<K, D, CMP, tAllocator>::key_compare(), a) {}
85 : };
86 :
87 : // Integer to TString conversion
88 : template <typename T>
89 0 : inline TString str(T i)
90 : {
91 : ASSERT(std::numeric_limits<T>::is_integer);
92 : char buffer[((8 * sizeof(T)) / 3) + 3];
93 0 : const char *formatStr = std::numeric_limits<T>::is_signed ? "%d" : "%u";
94 0 : snprintf(buffer, sizeof(buffer), formatStr, i);
95 0 : return buffer;
96 : }
97 :
98 : } // namespace sh
99 :
100 : #endif // COMPILER_TRANSLATOR_COMMON_H_
|