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 | |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame^] | 17 | struct TSourceLoc { |
| 18 | int first_file; |
| 19 | int first_line; |
| 20 | int last_file; |
| 21 | int last_line; |
| 22 | }; |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 23 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 24 | // |
| 25 | // Put POOL_ALLOCATOR_NEW_DELETE in base classes to make them use this scheme. |
| 26 | // |
| 27 | #define POOL_ALLOCATOR_NEW_DELETE(A) \ |
| 28 | void* operator new(size_t s) { return (A).allocate(s); } \ |
| 29 | void* operator new(size_t, void *_Where) { return (_Where); } \ |
| 30 | void operator delete(void*) { } \ |
| 31 | void operator delete(void *, void *) { } \ |
| 32 | void* operator new[](size_t s) { return (A).allocate(s); } \ |
| 33 | void* operator new[](size_t, void *_Where) { return (_Where); } \ |
| 34 | void operator delete[](void*) { } \ |
| 35 | void operator delete[](void *, void *) { } |
| 36 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 37 | // |
| 38 | // Pool version of string. |
| 39 | // |
| 40 | typedef pool_allocator<char> TStringAllocator; |
alokp@chromium.org | 4e4facd | 2010-06-02 15:21:22 +0000 | [diff] [blame] | 41 | typedef std::basic_string <char, std::char_traits<char>, TStringAllocator> TString; |
| 42 | typedef std::basic_ostringstream<char, std::char_traits<char>, TStringAllocator> TStringStream; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 43 | inline TString* NewPoolTString(const char* s) |
| 44 | { |
| 45 | void* memory = GlobalPoolAllocator.allocate(sizeof(TString)); |
| 46 | return new(memory) TString(s); |
| 47 | } |
| 48 | |
| 49 | // |
alokp@chromium.org | 4e4facd | 2010-06-02 15:21:22 +0000 | [diff] [blame] | 50 | // Persistent string memory. Should only be used for strings that survive |
alokp@chromium.org | 774d706 | 2010-07-21 18:55:45 +0000 | [diff] [blame] | 51 | // across compiles. |
alokp@chromium.org | 4e4facd | 2010-06-02 15:21:22 +0000 | [diff] [blame] | 52 | // |
| 53 | #define TPersistString std::string |
| 54 | #define TPersistStringStream std::ostringstream |
| 55 | |
| 56 | // |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 57 | // Pool allocator versions of vectors, lists, and maps |
| 58 | // |
| 59 | template <class T> class TVector : public std::vector<T, pool_allocator<T> > { |
| 60 | public: |
| 61 | typedef typename std::vector<T, pool_allocator<T> >::size_type size_type; |
| 62 | TVector() : std::vector<T, pool_allocator<T> >() {} |
| 63 | TVector(const pool_allocator<T>& a) : std::vector<T, pool_allocator<T> >(a) {} |
| 64 | TVector(size_type i): std::vector<T, pool_allocator<T> >(i) {} |
| 65 | }; |
| 66 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 67 | template <class K, class D, class CMP = std::less<K> > |
alokp@chromium.org | 91a01a1 | 2010-05-12 18:39:04 +0000 | [diff] [blame] | 68 | 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] | 69 | public: |
alokp@chromium.org | 91a01a1 | 2010-05-12 18:39:04 +0000 | [diff] [blame] | 70 | typedef pool_allocator<std::pair<const K, D> > tAllocator; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 71 | |
alokp@chromium.org | 91a01a1 | 2010-05-12 18:39:04 +0000 | [diff] [blame] | 72 | TMap() : std::map<K, D, CMP, tAllocator>() {} |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 73 | // 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] | 74 | 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] | 75 | }; |
| 76 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 77 | #endif // _COMMON_INCLUDED_ |