• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
10 #include <map>
11 #include <sstream>
12 #include <string>
13 #include <vector>
14 
15 #include "compiler/PoolAlloc.h"
16 
17 // We need two pieces of information to report errors/warnings - string and
18 // line number. We encode these into a single int so that it can be easily
19 // incremented/decremented by lexer. The right SOURCE_LOC_LINE_SIZE bits store
20 // line number while the rest store the string number. Since the shaders are
21 // usually small, we should not run out of memory. SOURCE_LOC_LINE_SIZE
22 // can be increased to alleviate this issue.
23 typedef int TSourceLoc;
24 const unsigned int SOURCE_LOC_LINE_SIZE = 16;  // in bits.
25 const unsigned int SOURCE_LOC_LINE_MASK = (1 << SOURCE_LOC_LINE_SIZE) - 1;
26 
EncodeSourceLoc(int string,int line)27 inline TSourceLoc EncodeSourceLoc(int string, int line) {
28     return (string << SOURCE_LOC_LINE_SIZE) | (line & SOURCE_LOC_LINE_MASK);
29 }
30 
DecodeSourceLoc(TSourceLoc loc,int * string,int * line)31 inline void DecodeSourceLoc(TSourceLoc loc, int* string, int* line) {
32     if (string) *string = loc >> SOURCE_LOC_LINE_SIZE;
33     if (line) *line = loc & SOURCE_LOC_LINE_MASK;
34 }
35 
36 //
37 // Put POOL_ALLOCATOR_NEW_DELETE in base classes to make them use this scheme.
38 //
39 #define POOL_ALLOCATOR_NEW_DELETE(A)                                  \
40     void* operator new(size_t s) { return (A).allocate(s); }          \
41     void* operator new(size_t, void *_Where) { return (_Where);	}     \
42     void operator delete(void*) { }                                   \
43     void operator delete(void *, void *) { }                          \
44     void* operator new[](size_t s) { return (A).allocate(s); }        \
45     void* operator new[](size_t, void *_Where) { return (_Where);	} \
46     void operator delete[](void*) { }                                 \
47     void operator delete[](void *, void *) { }
48 
49 //
50 // Pool version of string.
51 //
52 typedef pool_allocator<char> TStringAllocator;
53 typedef std::basic_string <char, std::char_traits<char>, TStringAllocator> TString;
54 typedef std::basic_ostringstream<char, std::char_traits<char>, TStringAllocator> TStringStream;
NewPoolTString(const char * s)55 inline TString* NewPoolTString(const char* s)
56 {
57 	void* memory = GlobalPoolAllocator.allocate(sizeof(TString));
58 	return new(memory) TString(s);
59 }
60 
61 //
62 // Persistent string memory.  Should only be used for strings that survive
63 // across compiles.
64 //
65 #define TPersistString std::string
66 #define TPersistStringStream std::ostringstream
67 
68 //
69 // Pool allocator versions of vectors, lists, and maps
70 //
71 template <class T> class TVector : public std::vector<T, pool_allocator<T> > {
72 public:
73     typedef typename std::vector<T, pool_allocator<T> >::size_type size_type;
TVector()74     TVector() : std::vector<T, pool_allocator<T> >() {}
TVector(const pool_allocator<T> & a)75     TVector(const pool_allocator<T>& a) : std::vector<T, pool_allocator<T> >(a) {}
TVector(size_type i)76     TVector(size_type i): std::vector<T, pool_allocator<T> >(i) {}
77 };
78 
79 template <class K, class D, class CMP = std::less<K> >
80 class TMap : public std::map<K, D, CMP, pool_allocator<std::pair<const K, D> > > {
81 public:
82     typedef pool_allocator<std::pair<const K, D> > tAllocator;
83 
TMap()84     TMap() : std::map<K, D, CMP, tAllocator>() {}
85     // use correct two-stage name lookup supported in gcc 3.4 and above
TMap(const tAllocator & a)86     TMap(const tAllocator& a) : std::map<K, D, CMP, tAllocator>(std::map<K, D, CMP, tAllocator>::key_compare(), a) {}
87 };
88 
89 #endif // _COMMON_INCLUDED_
90