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 | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 21 | struct TSourceLoc { |
| 22 | int first_file; |
| 23 | int first_line; |
| 24 | int last_file; |
| 25 | int last_line; |
| 26 | }; |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 27 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 28 | // |
| 29 | // Put POOL_ALLOCATOR_NEW_DELETE in base classes to make them use this scheme. |
| 30 | // |
Alok Priyadarshi | 8156b6b | 2013-09-23 14:56:58 -0400 | [diff] [blame] | 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*) { } \ |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 39 | void operator delete[](void *, void *) { } |
| 40 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 41 | // |
| 42 | // Pool version of string. |
| 43 | // |
| 44 | typedef pool_allocator<char> TStringAllocator; |
alokp@chromium.org | 4e4facd | 2010-06-02 15:21:22 +0000 | [diff] [blame] | 45 | typedef std::basic_string <char, std::char_traits<char>, TStringAllocator> TString; |
| 46 | typedef std::basic_ostringstream<char, std::char_traits<char>, TStringAllocator> TStringStream; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 47 | inline TString* NewPoolTString(const char* s) |
| 48 | { |
Alok Priyadarshi | 8156b6b | 2013-09-23 14:56:58 -0400 | [diff] [blame] | 49 | void* memory = GetGlobalPoolAllocator()->allocate(sizeof(TString)); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 50 | return new(memory) TString(s); |
| 51 | } |
| 52 | |
| 53 | // |
alokp@chromium.org | 4e4facd | 2010-06-02 15:21:22 +0000 | [diff] [blame] | 54 | // Persistent string memory. Should only be used for strings that survive |
alokp@chromium.org | 774d706 | 2010-07-21 18:55:45 +0000 | [diff] [blame] | 55 | // across compiles. |
alokp@chromium.org | 4e4facd | 2010-06-02 15:21:22 +0000 | [diff] [blame] | 56 | // |
| 57 | #define TPersistString std::string |
| 58 | #define TPersistStringStream std::ostringstream |
| 59 | |
| 60 | // |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 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 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 71 | template <class K, class D, class CMP = std::less<K> > |
alokp@chromium.org | 91a01a1 | 2010-05-12 18:39:04 +0000 | [diff] [blame] | 72 | class TMap : public std::map<K, D, CMP, pool_allocator<std::pair<const K, D> > > { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 73 | public: |
alokp@chromium.org | 91a01a1 | 2010-05-12 18:39:04 +0000 | [diff] [blame] | 74 | typedef pool_allocator<std::pair<const K, D> > tAllocator; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 75 | |
alokp@chromium.org | 91a01a1 | 2010-05-12 18:39:04 +0000 | [diff] [blame] | 76 | TMap() : std::map<K, D, CMP, tAllocator>() {} |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 77 | // 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] | 78 | 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] | 79 | }; |
| 80 | |
Jamie Madill | e53c98b | 2014-02-03 11:57:13 -0500 | [diff] [blame] | 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 | |
Geoff Lang | 0a73dd8 | 2014-11-19 16:18:08 -0500 | [diff] [blame] | 92 | #endif // COMPILER_TRANSLATOR_COMMON_H_ |