daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 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 | |
Geoff Lang | 0a73dd8 | 2014-11-19 16:18:08 -0500 | [diff] [blame] | 7 | #ifndef COMPILER_TRANSLATOR_COMMON_H_ |
| 8 | #define COMPILER_TRANSLATOR_COMMON_H_ |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 9 | |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 10 | #include <map> |
alokp@chromium.org | 4e4facd | 2010-06-02 15:21:22 +0000 | [diff] [blame] | 11 | #include <sstream> |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 12 | #include <string> |
| 13 | #include <vector> |
Jamie Madill | e53c98b | 2014-02-03 11:57:13 -0500 | [diff] [blame] | 14 | #include <limits> |
| 15 | #include <stdio.h> |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 16 | |
Jamie Madill | a656490 | 2015-04-27 14:30:32 +0000 | [diff] [blame] | 17 | #include "common/angleutils.h" |
Olli Etuaho | d57e0db | 2015-04-24 15:05:08 +0300 | [diff] [blame] | 18 | #include "common/debug.h" |
| 19 | #include "compiler/translator/PoolAlloc.h" |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 20 | |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame^] | 21 | namespace sh |
| 22 | { |
| 23 | |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 24 | struct TSourceLoc { |
| 25 | int first_file; |
| 26 | int first_line; |
| 27 | int last_file; |
| 28 | int last_line; |
| 29 | }; |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 30 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 31 | // |
| 32 | // Put POOL_ALLOCATOR_NEW_DELETE in base classes to make them use this scheme. |
| 33 | // |
Alok Priyadarshi | 8156b6b | 2013-09-23 14:56:58 -0400 | [diff] [blame] | 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*) { } \ |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 42 | void operator delete[](void *, void *) { } |
| 43 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 44 | // |
| 45 | // Pool version of string. |
| 46 | // |
| 47 | typedef pool_allocator<char> TStringAllocator; |
alokp@chromium.org | 4e4facd | 2010-06-02 15:21:22 +0000 | [diff] [blame] | 48 | typedef std::basic_string <char, std::char_traits<char>, TStringAllocator> TString; |
| 49 | typedef std::basic_ostringstream<char, std::char_traits<char>, TStringAllocator> TStringStream; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 50 | inline TString* NewPoolTString(const char* s) |
| 51 | { |
Alok Priyadarshi | 8156b6b | 2013-09-23 14:56:58 -0400 | [diff] [blame] | 52 | void* memory = GetGlobalPoolAllocator()->allocate(sizeof(TString)); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 53 | return new(memory) TString(s); |
| 54 | } |
| 55 | |
| 56 | // |
alokp@chromium.org | 4e4facd | 2010-06-02 15:21:22 +0000 | [diff] [blame] | 57 | // Persistent string memory. Should only be used for strings that survive |
alokp@chromium.org | 774d706 | 2010-07-21 18:55:45 +0000 | [diff] [blame] | 58 | // across compiles. |
alokp@chromium.org | 4e4facd | 2010-06-02 15:21:22 +0000 | [diff] [blame] | 59 | // |
| 60 | #define TPersistString std::string |
| 61 | #define TPersistStringStream std::ostringstream |
| 62 | |
| 63 | // |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 64 | // Pool allocator versions of vectors, lists, and maps |
| 65 | // |
Corentin Wallez | 1eabcf4 | 2016-02-02 13:54:00 -0500 | [diff] [blame] | 66 | template <class T> |
| 67 | 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 | TVector() : std::vector<T, pool_allocator<T>>() {} |
| 72 | TVector(const pool_allocator<T> &a) : std::vector<T, pool_allocator<T>>(a) {} |
| 73 | TVector(size_type i) : std::vector<T, pool_allocator<T>>(i) {} |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
Corentin Wallez | 1eabcf4 | 2016-02-02 13:54:00 -0500 | [diff] [blame] | 76 | template <class K, class D, class CMP = std::less<K>> |
| 77 | 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; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 81 | |
alokp@chromium.org | 91a01a1 | 2010-05-12 18:39:04 +0000 | [diff] [blame] | 82 | TMap() : std::map<K, D, CMP, tAllocator>() {} |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 83 | // use correct two-stage name lookup supported in gcc 3.4 and above |
alokp@chromium.org | 91a01a1 | 2010-05-12 18:39:04 +0000 | [diff] [blame] | 84 | TMap(const tAllocator& a) : std::map<K, D, CMP, tAllocator>(std::map<K, D, CMP, tAllocator>::key_compare(), a) {} |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
Jamie Madill | e53c98b | 2014-02-03 11:57:13 -0500 | [diff] [blame] | 87 | // Integer to TString conversion |
| 88 | template <typename T> |
| 89 | inline TString str(T i) |
| 90 | { |
| 91 | ASSERT(std::numeric_limits<T>::is_integer); |
| 92 | char buffer[((8 * sizeof(T)) / 3) + 3]; |
| 93 | const char *formatStr = std::numeric_limits<T>::is_signed ? "%d" : "%u"; |
| 94 | snprintf(buffer, sizeof(buffer), formatStr, i); |
| 95 | return buffer; |
| 96 | } |
| 97 | |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame^] | 98 | } // namespace sh |
| 99 | |
Geoff Lang | 0a73dd8 | 2014-11-19 16:18:08 -0500 | [diff] [blame] | 100 | #endif // COMPILER_TRANSLATOR_COMMON_H_ |