Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 1 | // Copyright 2016 The SwiftShader Authors. All Rights Reserved. |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2 | // |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 6 | // |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 14 | |
| 15 | #ifndef _COMMON_INCLUDED_ |
| 16 | #define _COMMON_INCLUDED_ |
| 17 | |
| 18 | #include <map> |
| 19 | #include <sstream> |
| 20 | #include <string> |
| 21 | #include <vector> |
| 22 | |
Nicolas Capens | cc863da | 2015-01-21 15:50:55 -0500 | [diff] [blame] | 23 | #include "PoolAlloc.h" |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 24 | |
Alexis Hetu | 253fdd1 | 2015-07-07 15:12:46 -0400 | [diff] [blame] | 25 | struct TSourceLoc { |
| 26 | int first_file; |
| 27 | int first_line; |
| 28 | int last_file; |
| 29 | int last_line; |
| 30 | }; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 31 | |
| 32 | // |
| 33 | // Put POOL_ALLOCATOR_NEW_DELETE in base classes to make them use this scheme. |
| 34 | // |
Nicolas Capens | 978ddc5 | 2014-11-11 12:42:08 -0500 | [diff] [blame] | 35 | #define POOL_ALLOCATOR_NEW_DELETE() \ |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 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*) { } \ |
| 39 | void operator delete(void *, void *) { } \ |
| 40 | void* operator new[](size_t s) { return GetGlobalPoolAllocator()->allocate(s); } \ |
| 41 | void* operator new[](size_t, void *_Where) { return (_Where); } \ |
| 42 | void operator delete[](void*) { } \ |
| 43 | void operator delete[](void *, void *) { } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 44 | |
| 45 | // |
| 46 | // Pool version of string. |
| 47 | // |
| 48 | typedef pool_allocator<char> TStringAllocator; |
| 49 | typedef std::basic_string <char, std::char_traits<char>, TStringAllocator> TString; |
| 50 | typedef std::basic_ostringstream<char, std::char_traits<char>, TStringAllocator> TStringStream; |
| 51 | inline TString* NewPoolTString(const char* s) |
| 52 | { |
Nicolas Capens | 978ddc5 | 2014-11-11 12:42:08 -0500 | [diff] [blame] | 53 | void* memory = GetGlobalPoolAllocator()->allocate(sizeof(TString)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 54 | return new(memory) TString(s); |
| 55 | } |
| 56 | |
| 57 | // |
| 58 | // Persistent string memory. Should only be used for strings that survive |
| 59 | // across compiles. |
| 60 | // |
| 61 | #define TPersistString std::string |
| 62 | #define TPersistStringStream std::ostringstream |
| 63 | |
| 64 | // |
| 65 | // Pool allocator versions of vectors, lists, and maps |
| 66 | // |
| 67 | template <class T> class TVector : public std::vector<T, pool_allocator<T> > { |
| 68 | public: |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 69 | typedef typename std::vector<T, pool_allocator<T> >::size_type size_type; |
| 70 | TVector() : std::vector<T, pool_allocator<T> >() {} |
| 71 | TVector(const pool_allocator<T>& a) : std::vector<T, pool_allocator<T> >(a) {} |
| 72 | TVector(size_type i): std::vector<T, pool_allocator<T> >(i) {} |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 73 | }; |
| 74 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 75 | template <class K, class D, class CMP = std::less<K> > |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 76 | class TMap : public std::map<K, D, CMP, pool_allocator<std::pair<const K, D> > > { |
| 77 | public: |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 78 | typedef pool_allocator<std::pair<const K, D> > tAllocator; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 79 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 80 | TMap() : std::map<K, D, CMP, tAllocator>() {} |
| 81 | // use correct two-stage name lookup supported in gcc 3.4 and above |
| 82 | TMap(const tAllocator& a) : std::map<K, D, CMP, tAllocator>(std::map<K, D, CMP, tAllocator>::key_compare(), a) {} |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | #endif // _COMMON_INCLUDED_ |