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 <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> |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 14 | |
daniel@transgaming.com | e684229 | 2010-04-20 18:52:50 +0000 | [diff] [blame] | 15 | #include "compiler/PoolAlloc.h" |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 16 | |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 17 | typedef int TSourceLoc; |
alokp@chromium.org | 4e4facd | 2010-06-02 15:21:22 +0000 | [diff] [blame] | 18 | const unsigned int SourceLocLineMask = 0xffff; |
| 19 | const unsigned int SourceLocStringShift = 16; |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 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; |
alokp@chromium.org | 4e4facd | 2010-06-02 15:21:22 +0000 | [diff] [blame] | 38 | typedef std::basic_string <char, std::char_traits<char>, TStringAllocator> TString; |
| 39 | typedef std::basic_ostringstream<char, std::char_traits<char>, TStringAllocator> TStringStream; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 40 | inline TString* NewPoolTString(const char* s) |
| 41 | { |
| 42 | void* memory = GlobalPoolAllocator.allocate(sizeof(TString)); |
| 43 | return new(memory) TString(s); |
| 44 | } |
| 45 | |
| 46 | // |
alokp@chromium.org | 4e4facd | 2010-06-02 15:21:22 +0000 | [diff] [blame] | 47 | // Persistent string memory. Should only be used for strings that survive |
| 48 | // across compiles/links. |
| 49 | // |
| 50 | #define TPersistString std::string |
| 51 | #define TPersistStringStream std::ostringstream |
| 52 | |
| 53 | // |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 54 | // Pool allocator versions of vectors, lists, and maps |
| 55 | // |
| 56 | template <class T> class TVector : public std::vector<T, pool_allocator<T> > { |
| 57 | public: |
| 58 | typedef typename std::vector<T, pool_allocator<T> >::size_type size_type; |
| 59 | TVector() : std::vector<T, pool_allocator<T> >() {} |
| 60 | TVector(const pool_allocator<T>& a) : std::vector<T, pool_allocator<T> >(a) {} |
| 61 | TVector(size_type i): std::vector<T, pool_allocator<T> >(i) {} |
| 62 | }; |
| 63 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 64 | template <class K, class D, class CMP = std::less<K> > |
alokp@chromium.org | 91a01a1 | 2010-05-12 18:39:04 +0000 | [diff] [blame] | 65 | 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] | 66 | public: |
alokp@chromium.org | 91a01a1 | 2010-05-12 18:39:04 +0000 | [diff] [blame] | 67 | typedef pool_allocator<std::pair<const K, D> > tAllocator; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 68 | |
alokp@chromium.org | 91a01a1 | 2010-05-12 18:39:04 +0000 | [diff] [blame] | 69 | TMap() : std::map<K, D, CMP, tAllocator>() {} |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 70 | // 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] | 71 | 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] | 72 | }; |
| 73 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 74 | typedef TMap<TString, TString> TPragmaTable; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 75 | |
| 76 | #endif // _COMMON_INCLUDED_ |