blob: b6fe5d6b5a782c2461f01a27e7e82ea4e81185be [file] [log] [blame]
Nicolas Capens0bac2852016-05-07 06:09:58 -04001// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
John Bauman89401822014-05-06 15:04:28 -04002//
Nicolas Capens0bac2852016-05-07 06:09:58 -04003// 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 Bauman89401822014-05-06 15:04:28 -04006//
Nicolas Capens0bac2852016-05-07 06:09:58 -04007// 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 Bauman89401822014-05-06 15:04:28 -040014
15#ifndef _COMMON_INCLUDED_
16#define _COMMON_INCLUDED_
17
18#include <map>
19#include <sstream>
20#include <string>
21#include <vector>
22
Nicolas Capenscc863da2015-01-21 15:50:55 -050023#include "PoolAlloc.h"
John Bauman89401822014-05-06 15:04:28 -040024
Alexis Hetu253fdd12015-07-07 15:12:46 -040025struct TSourceLoc {
26 int first_file;
27 int first_line;
28 int last_file;
29 int last_line;
30};
John Bauman89401822014-05-06 15:04:28 -040031
32//
33// Put POOL_ALLOCATOR_NEW_DELETE in base classes to make them use this scheme.
34//
Nicolas Capens978ddc52014-11-11 12:42:08 -050035#define POOL_ALLOCATOR_NEW_DELETE() \
Nicolas Capens0bac2852016-05-07 06:09:58 -040036 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 Bauman89401822014-05-06 15:04:28 -040044
45//
46// Pool version of string.
47//
48typedef pool_allocator<char> TStringAllocator;
49typedef std::basic_string <char, std::char_traits<char>, TStringAllocator> TString;
50typedef std::basic_ostringstream<char, std::char_traits<char>, TStringAllocator> TStringStream;
51inline TString* NewPoolTString(const char* s)
52{
Nicolas Capens978ddc52014-11-11 12:42:08 -050053 void* memory = GetGlobalPoolAllocator()->allocate(sizeof(TString));
John Bauman89401822014-05-06 15:04:28 -040054 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//
67template <class T> class TVector : public std::vector<T, pool_allocator<T> > {
68public:
Nicolas Capens0bac2852016-05-07 06:09:58 -040069 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 Bauman89401822014-05-06 15:04:28 -040073};
74
Nicolas Capens0bac2852016-05-07 06:09:58 -040075template <class K, class D, class CMP = std::less<K> >
John Bauman89401822014-05-06 15:04:28 -040076class TMap : public std::map<K, D, CMP, pool_allocator<std::pair<const K, D> > > {
77public:
Nicolas Capens0bac2852016-05-07 06:09:58 -040078 typedef pool_allocator<std::pair<const K, D> > tAllocator;
John Bauman89401822014-05-06 15:04:28 -040079
Nicolas Capens0bac2852016-05-07 06:09:58 -040080 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 Bauman89401822014-05-06 15:04:28 -040083};
84
85#endif // _COMMON_INCLUDED_