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