1 | // Copyright 2016 The SwiftShader Authors. All Rights Reserved. |
2 | // |
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 |
6 | // |
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. |
14 | |
15 | #ifndef _COMMON_INCLUDED_ |
16 | #define _COMMON_INCLUDED_ |
17 | |
18 | #include <map> |
19 | #include <sstream> |
20 | #include <string> |
21 | #include <vector> |
22 | |
23 | #include "PoolAlloc.h" |
24 | |
25 | struct TSourceLoc { |
26 | int first_file; |
27 | int first_line; |
28 | int last_file; |
29 | int last_line; |
30 | }; |
31 | |
32 | // |
33 | // Put POOL_ALLOCATOR_NEW_DELETE in base classes to make them use this scheme. |
34 | // |
35 | #define POOL_ALLOCATOR_NEW_DELETE() \ |
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 *) { } |
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 | { |
53 | void* memory = GetGlobalPoolAllocator()->allocate(sizeof(TString)); |
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: |
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) {} |
73 | }; |
74 | |
75 | template <class K, class D, class CMP = std::less<K> > |
76 | class TMap : public std::map<K, D, CMP, pool_allocator<std::pair<const K, D> > > { |
77 | public: |
78 | typedef pool_allocator<std::pair<const K, D> > tAllocator; |
79 | |
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) {} |
83 | }; |
84 | |
85 | #endif // _COMMON_INCLUDED_ |
86 | |