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 | |
| 7 | #ifndef _COMMON_INCLUDED_ |
| 8 | #define _COMMON_INCLUDED_ |
| 9 | |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 10 | #include <assert.h> |
| 11 | #include <stdio.h> |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 12 | |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 13 | #include <map> |
| 14 | #include <string> |
| 15 | #include <vector> |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 16 | |
daniel@transgaming.com | e684229 | 2010-04-20 18:52:50 +0000 | [diff] [blame] | 17 | #include "compiler/PoolAlloc.h" |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 18 | |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 19 | typedef int TSourceLoc; |
| 20 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 21 | // |
| 22 | // Put POOL_ALLOCATOR_NEW_DELETE in base classes to make them use this scheme. |
| 23 | // |
| 24 | #define POOL_ALLOCATOR_NEW_DELETE(A) \ |
| 25 | void* operator new(size_t s) { return (A).allocate(s); } \ |
| 26 | void* operator new(size_t, void *_Where) { return (_Where); } \ |
| 27 | void operator delete(void*) { } \ |
| 28 | void operator delete(void *, void *) { } \ |
| 29 | void* operator new[](size_t s) { return (A).allocate(s); } \ |
| 30 | void* operator new[](size_t, void *_Where) { return (_Where); } \ |
| 31 | void operator delete[](void*) { } \ |
| 32 | void operator delete[](void *, void *) { } |
| 33 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 34 | // |
| 35 | // Pool version of string. |
| 36 | // |
| 37 | typedef pool_allocator<char> TStringAllocator; |
| 38 | typedef std::basic_string <char, std::char_traits<char>, TStringAllocator > TString; |
| 39 | inline TString* NewPoolTString(const char* s) |
| 40 | { |
| 41 | void* memory = GlobalPoolAllocator.allocate(sizeof(TString)); |
| 42 | return new(memory) TString(s); |
| 43 | } |
| 44 | |
| 45 | // |
| 46 | // Pool allocator versions of vectors, lists, and maps |
| 47 | // |
| 48 | template <class T> class TVector : public std::vector<T, pool_allocator<T> > { |
| 49 | public: |
| 50 | typedef typename std::vector<T, pool_allocator<T> >::size_type size_type; |
| 51 | TVector() : std::vector<T, pool_allocator<T> >() {} |
| 52 | TVector(const pool_allocator<T>& a) : std::vector<T, pool_allocator<T> >(a) {} |
| 53 | TVector(size_type i): std::vector<T, pool_allocator<T> >(i) {} |
| 54 | }; |
| 55 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 56 | template <class K, class D, class CMP = std::less<K> > |
alokp@chromium.org | 91a01a1 | 2010-05-12 18:39:04 +0000 | [diff] [blame] | 57 | 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] | 58 | public: |
alokp@chromium.org | 91a01a1 | 2010-05-12 18:39:04 +0000 | [diff] [blame] | 59 | typedef pool_allocator<std::pair<const K, D> > tAllocator; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 60 | |
alokp@chromium.org | 91a01a1 | 2010-05-12 18:39:04 +0000 | [diff] [blame] | 61 | TMap() : std::map<K, D, CMP, tAllocator>() {} |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 62 | // 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] | 63 | 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] | 64 | }; |
| 65 | |
| 66 | // |
| 67 | // Persistent string memory. Should only be used for strings that survive |
| 68 | // across compiles/links. |
| 69 | // |
| 70 | typedef std::basic_string<char> TPersistString; |
| 71 | |
| 72 | // |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 73 | // Create a TString object from an integer. |
| 74 | // |
| 75 | inline const TString String(const int i, const int base = 10) |
| 76 | { |
| 77 | char text[16]; // 32 bit ints are at most 10 digits in base 10 |
| 78 | |
| 79 | #ifdef _WIN32 |
| 80 | _itoa(i, text, base); |
| 81 | #else |
| 82 | // we assume base 10 for all cases |
| 83 | sprintf(text, "%d", i); |
| 84 | #endif |
| 85 | |
| 86 | return text; |
| 87 | } |
| 88 | |
| 89 | const unsigned int SourceLocLineMask = 0xffff; |
| 90 | const unsigned int SourceLocStringShift = 16; |
| 91 | |
| 92 | __inline TPersistString FormatSourceLoc(const TSourceLoc loc) |
| 93 | { |
| 94 | char locText[64]; |
| 95 | |
| 96 | int string = loc >> SourceLocStringShift; |
| 97 | int line = loc & SourceLocLineMask; |
| 98 | |
| 99 | if (line) |
| 100 | sprintf(locText, "%d:%d", string, line); |
| 101 | else |
| 102 | sprintf(locText, "%d:? ", string); |
| 103 | |
| 104 | return TPersistString(locText); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | typedef TMap<TString, TString> TPragmaTable; |
| 109 | typedef TMap<TString, TString>::tAllocator TPragmaTableAllocator; |
| 110 | |
| 111 | #endif // _COMMON_INCLUDED_ |