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